chiark / gitweb /
systemctl: when calling 'status' accept a PID
[elogind.git] / src / dbus-manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "dbus.h"
25 #include "log.h"
26 #include "dbus-manager.h"
27 #include "strv.h"
28 #include "bus-errors.h"
29
30 #define BUS_MANAGER_INTERFACE                                           \
31         " <interface name=\"org.freedesktop.systemd1.Manager\">\n"      \
32         "  <method name=\"GetUnit\">\n"                                 \
33         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
34         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
35         "  </method>\n"                                                 \
36         "  <method name=\"GetUnitByPID\">\n"                            \
37         "   <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n"         \
38         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
39         "  </method>\n"                                                 \
40         "  <method name=\"LoadUnit\">\n"                                \
41         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
42         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
43         "  </method>\n"                                                 \
44         "  <method name=\"StartUnit\">\n"                               \
45         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
46         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
47         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
48         "  </method>\n"                                                 \
49         "  <method name=\"StopUnit\">\n"                                \
50         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
51         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
52         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
53         "  </method>\n"                                                 \
54         "  <method name=\"ReloadUnit\">\n"                              \
55         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
56         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
57         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
58         "  </method>\n"                                                 \
59         "  <method name=\"RestartUnit\">\n"                             \
60         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
61         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
62         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
63         "  </method>\n"                                                 \
64         "  <method name=\"TryRestartUnit\">\n"                          \
65         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
66         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
67         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
68         "  </method>\n"                                                 \
69         "  <method name=\"ReloadOrRestartUnit\">\n"                     \
70         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
71         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
72         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
73         "  </method>\n"                                                 \
74         "  <method name=\"ReloadOrTryRestartUnit\">\n"                  \
75         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
76         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
77         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
78         "  </method>\n"                                                 \
79         "  <method name=\"ResetMaintenanceUnit\">\n"                    \
80         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
81         "  </method>\n"                                                 \
82         "  <method name=\"GetJob\">\n"                                  \
83         "   <arg name=\"id\" type=\"u\" direction=\"in\"/>\n"           \
84         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
85         "  </method>\n"                                                 \
86         "  <method name=\"ClearJobs\"/>\n"                              \
87         "  <method name=\"ResetMaintenance\"/>\n"                       \
88         "  <method name=\"ListUnits\">\n"                               \
89         "   <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
90         "  </method>\n"                                                 \
91         "  <method name=\"ListJobs\">\n"                                \
92         "   <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
93         "  </method>\n"                                                 \
94         "  <method name=\"Subscribe\"/>\n"                              \
95         "  <method name=\"Unsubscribe\"/>\n"                            \
96         "  <method name=\"Dump\"/>\n"                                   \
97         "  <method name=\"CreateSnapshot\">\n"                          \
98         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
99         "   <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>\n"      \
100         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
101         "  </method>\n"                                                 \
102         "  <method name=\"Reload\"/>\n"                                 \
103         "  <method name=\"Reexecute\"/>\n"                              \
104         "  <method name=\"Exit\"/>\n"                                   \
105         "  <method name=\"SetEnvironment\">\n"                          \
106         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
107         "  </method>\n"                                                 \
108         "  <method name=\"UnsetEnvironment\">\n"                        \
109         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
110         "  </method>\n"                                                 \
111         "  <signal name=\"UnitNew\">\n"                                 \
112         "   <arg name=\"id\" type=\"s\"/>\n"                            \
113         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
114         "  </signal>\n"                                                 \
115         "  <signal name=\"UnitRemoved\">\n"                             \
116         "   <arg name=\"id\" type=\"s\"/>\n"                            \
117         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
118         "  </signal>\n"                                                 \
119         "  <signal name=\"JobNew\">\n"                                  \
120         "   <arg name=\"id\" type=\"u\"/>\n"                            \
121         "   <arg name=\"job\" type=\"o\"/>\n"                           \
122         "  </signal>\n"                                                 \
123         "  <signal name=\"JobRemoved\">\n"                              \
124         "   <arg name=\"id\" type=\"u\"/>\n"                            \
125         "   <arg name=\"job\" type=\"o\"/>\n"                           \
126         "   <arg name=\"success\" type=\"b\"/>\n"                       \
127         "  </signal>"                                                   \
128         "  <property name=\"Version\" type=\"s\" access=\"read\"/>\n"   \
129         "  <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
130         "  <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
131         "  <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n"  \
132         "  <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
133         "  <property name=\"NNames\" type=\"u\" access=\"read\"/>\n"    \
134         "  <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n"     \
135         "  <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
136         "  <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
137         "  <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
138         "  <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
139         "  <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
140         "  <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
141         "  <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \
142         "  <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
143         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
144         " </interface>\n"
145
146 #define INTROSPECTION_BEGIN                                             \
147         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
148         "<node>\n"                                                      \
149         BUS_MANAGER_INTERFACE                                           \
150         BUS_PROPERTIES_INTERFACE                                        \
151         BUS_INTROSPECTABLE_INTERFACE
152
153 #define INTROSPECTION_END                                               \
154         "</node>\n"
155
156 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
157
158 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
159
160 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
161         const char *t;
162
163         assert(m);
164         assert(i);
165         assert(property);
166
167         t = log_target_to_string(log_get_target());
168
169         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
170                 return -ENOMEM;
171
172         return 0;
173 }
174
175 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
176         const char *t;
177
178         assert(m);
179         assert(i);
180         assert(property);
181
182         t = log_level_to_string(log_get_max_level());
183
184         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
185                 return -ENOMEM;
186
187         return 0;
188 }
189
190 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
191         uint32_t u;
192
193         assert(m);
194         assert(i);
195         assert(property);
196
197         u = hashmap_size(m->units);
198
199         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
200                 return -ENOMEM;
201
202         return 0;
203 }
204
205 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
206         uint32_t u;
207
208         assert(m);
209         assert(i);
210         assert(property);
211
212         u = hashmap_size(m->jobs);
213
214         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
215                 return -ENOMEM;
216
217         return 0;
218 }
219
220 static const char *message_get_sender_with_fallback(DBusMessage *m) {
221         const char *s;
222
223         assert(m);
224
225         if ((s = dbus_message_get_sender(m)))
226                 return s;
227
228         /* When the message came in from a direct connection the
229          * message will have no sender. We fix that here. */
230
231         return ":no-sender";
232 }
233
234 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
235         Manager *m = data;
236
237         const BusProperty properties[] = {
238                 { "org.freedesktop.systemd1.Manager", "Version",       bus_property_append_string,    "s",  PACKAGE_STRING     },
239                 { "org.freedesktop.systemd1.Manager", "RunningAs",     bus_manager_append_running_as, "s",  &m->running_as     },
240                 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t",  &m->startup_timestamp.realtime },
241                 { "org.freedesktop.systemd1.Manager", "LogLevel",      bus_manager_append_log_level,  "s",  NULL               },
242                 { "org.freedesktop.systemd1.Manager", "LogTarget",     bus_manager_append_log_target, "s",  NULL               },
243                 { "org.freedesktop.systemd1.Manager", "NNames",        bus_manager_append_n_names,    "u",  NULL               },
244                 { "org.freedesktop.systemd1.Manager", "NJobs",         bus_manager_append_n_jobs,     "u",  NULL               },
245                 { "org.freedesktop.systemd1.Manager", "Environment",   bus_property_append_strv,      "as", m->environment     },
246                 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn",  bus_property_append_bool,      "b",  &m->confirm_spawn  },
247                 { "org.freedesktop.systemd1.Manager", "ShowStatus",    bus_property_append_bool,      "b",  &m->show_status    },
248                 { "org.freedesktop.systemd1.Manager", "SysVConsole",   bus_property_append_bool,      "b",  &m->sysv_console   },
249                 { "org.freedesktop.systemd1.Manager", "UnitPath",      bus_property_append_strv,      "as", m->lookup_paths.unit_path },
250                 { "org.freedesktop.systemd1.Manager", "SysVInitPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvinit_path },
251                 { "org.freedesktop.systemd1.Manager", "SysVRcndPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvrcnd_path },
252                 { "org.freedesktop.systemd1.Manager", "NotifySocket",  bus_property_append_string,    "s",  m->notify_socket   },
253                 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
254                 { NULL, NULL, NULL, NULL, NULL }
255         };
256
257         int r;
258         DBusError error;
259         DBusMessage *reply = NULL;
260         char * path = NULL;
261         JobType job_type = _JOB_TYPE_INVALID;
262         bool reload_if_possible = false;
263
264         assert(connection);
265         assert(message);
266         assert(m);
267
268         dbus_error_init(&error);
269
270         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
271                 const char *name;
272                 Unit *u;
273
274                 if (!dbus_message_get_args(
275                                     message,
276                                     &error,
277                                     DBUS_TYPE_STRING, &name,
278                                     DBUS_TYPE_INVALID))
279                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
280
281                 if (!(u = manager_get_unit(m, name))) {
282                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
283                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
284                 }
285
286                 if (!(reply = dbus_message_new_method_return(message)))
287                         goto oom;
288
289                 if (!(path = unit_dbus_path(u)))
290                         goto oom;
291
292                 if (!dbus_message_append_args(
293                                     reply,
294                                     DBUS_TYPE_OBJECT_PATH, &path,
295                                     DBUS_TYPE_INVALID))
296                         goto oom;
297         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
298                 const char *name;
299                 Unit *u;
300                 uint32_t pid;
301
302                 if (!dbus_message_get_args(
303                                     message,
304                                     &error,
305                                     DBUS_TYPE_UINT32, &pid,
306                                     DBUS_TYPE_INVALID))
307                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
308
309                 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
310                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
311                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
312                 }
313
314                 if (!(reply = dbus_message_new_method_return(message)))
315                         goto oom;
316
317                 if (!(path = unit_dbus_path(u)))
318                         goto oom;
319
320                 if (!dbus_message_append_args(
321                                     reply,
322                                     DBUS_TYPE_OBJECT_PATH, &path,
323                                     DBUS_TYPE_INVALID))
324                         goto oom;
325         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
326                 const char *name;
327                 Unit *u;
328
329                 if (!dbus_message_get_args(
330                                     message,
331                                     &error,
332                                     DBUS_TYPE_STRING, &name,
333                                     DBUS_TYPE_INVALID))
334                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
335
336                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
337                         return bus_send_error_reply(m, connection, message, &error, r);
338
339                 if (!(reply = dbus_message_new_method_return(message)))
340                         goto oom;
341
342                 if (!(path = unit_dbus_path(u)))
343                         goto oom;
344
345                 if (!dbus_message_append_args(
346                                     reply,
347                                     DBUS_TYPE_OBJECT_PATH, &path,
348                                     DBUS_TYPE_INVALID))
349                         goto oom;
350
351         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
352                 job_type = JOB_START;
353         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
354                 job_type = JOB_STOP;
355         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
356                 job_type = JOB_RELOAD;
357         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
358                 job_type = JOB_RESTART;
359         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
360                 job_type = JOB_TRY_RESTART;
361         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
362                 reload_if_possible = true;
363                 job_type = JOB_RESTART;
364         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
365                 reload_if_possible = true;
366                 job_type = JOB_TRY_RESTART;
367         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
368                 uint32_t id;
369                 Job *j;
370
371                 if (!dbus_message_get_args(
372                                     message,
373                                     &error,
374                                     DBUS_TYPE_UINT32, &id,
375                                     DBUS_TYPE_INVALID))
376                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
377
378                 if (!(j = manager_get_job(m, id))) {
379                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
380                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
381                 }
382
383                 if (!(reply = dbus_message_new_method_return(message)))
384                         goto oom;
385
386                 if (!(path = job_dbus_path(j)))
387                         goto oom;
388
389                 if (!dbus_message_append_args(
390                                     reply,
391                                     DBUS_TYPE_OBJECT_PATH, &path,
392                                     DBUS_TYPE_INVALID))
393                         goto oom;
394
395         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
396
397                 manager_clear_jobs(m);
398
399                 if (!(reply = dbus_message_new_method_return(message)))
400                         goto oom;
401
402         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetMaintenance")) {
403
404                 manager_reset_maintenance(m);
405
406                 if (!(reply = dbus_message_new_method_return(message)))
407                         goto oom;
408
409         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetMaintenanceUnit")) {
410                 const char *name;
411                 Unit *u;
412
413                 if (!dbus_message_get_args(
414                                     message,
415                                     &error,
416                                     DBUS_TYPE_STRING, &name,
417                                     DBUS_TYPE_INVALID))
418                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
419
420                 if (!(u = manager_get_unit(m, name))) {
421                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
422                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
423                 }
424
425                 unit_reset_maintenance(u);
426
427                 if (!(reply = dbus_message_new_method_return(message)))
428                         goto oom;
429
430         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
431                 DBusMessageIter iter, sub;
432                 Iterator i;
433                 Unit *u;
434                 const char *k;
435
436                 if (!(reply = dbus_message_new_method_return(message)))
437                         goto oom;
438
439                 dbus_message_iter_init_append(reply, &iter);
440
441                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
442                         goto oom;
443
444                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
445                         char *u_path, *j_path;
446                         const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
447                         DBusMessageIter sub2;
448                         uint32_t job_id;
449                         Unit *f;
450
451                         if (k != u->meta.id)
452                                 continue;
453
454                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
455                                 goto oom;
456
457                         description = unit_description(u);
458                         load_state = unit_load_state_to_string(u->meta.load_state);
459                         active_state = unit_active_state_to_string(unit_active_state(u));
460                         sub_state = unit_sub_state_to_string(u);
461
462                         f = unit_following(u);
463                         following = f ? f->meta.id : "";
464
465                         if (!(u_path = unit_dbus_path(u)))
466                                 goto oom;
467
468                         if (u->meta.job) {
469                                 job_id = (uint32_t) u->meta.job->id;
470
471                                 if (!(j_path = job_dbus_path(u->meta.job))) {
472                                         free(u_path);
473                                         goto oom;
474                                 }
475
476                                 sjob_type = job_type_to_string(u->meta.job->type);
477                         } else {
478                                 job_id = 0;
479                                 j_path = u_path;
480                                 sjob_type = "";
481                         }
482
483                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
484                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
485                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
486                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
487                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
488                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
489                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
490                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
491                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
492                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
493                                 free(u_path);
494                                 if (u->meta.job)
495                                         free(j_path);
496                                 goto oom;
497                         }
498
499                         free(u_path);
500                         if (u->meta.job)
501                                 free(j_path);
502
503                         if (!dbus_message_iter_close_container(&sub, &sub2))
504                                 goto oom;
505                 }
506
507                 if (!dbus_message_iter_close_container(&iter, &sub))
508                         goto oom;
509
510         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
511                 DBusMessageIter iter, sub;
512                 Iterator i;
513                 Job *j;
514
515                 if (!(reply = dbus_message_new_method_return(message)))
516                         goto oom;
517
518                 dbus_message_iter_init_append(reply, &iter);
519
520                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
521                         goto oom;
522
523                 HASHMAP_FOREACH(j, m->jobs, i) {
524                         char *u_path, *j_path;
525                         const char *state, *type;
526                         uint32_t id;
527                         DBusMessageIter sub2;
528
529                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
530                                 goto oom;
531
532                         id = (uint32_t) j->id;
533                         state = job_state_to_string(j->state);
534                         type = job_type_to_string(j->type);
535
536                         if (!(j_path = job_dbus_path(j)))
537                                 goto oom;
538
539                         if (!(u_path = unit_dbus_path(j->unit))) {
540                                 free(j_path);
541                                 goto oom;
542                         }
543
544                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
545                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
546                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
547                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
548                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
549                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
550                                 free(j_path);
551                                 free(u_path);
552                                 goto oom;
553                         }
554
555                         free(j_path);
556                         free(u_path);
557
558                         if (!dbus_message_iter_close_container(&sub, &sub2))
559                                 goto oom;
560                 }
561
562                 if (!dbus_message_iter_close_container(&iter, &sub))
563                         goto oom;
564
565         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
566                 char *client;
567                 Set *s;
568
569                 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
570                         if (!(s = set_new(string_hash_func, string_compare_func)))
571                                 goto oom;
572
573                         if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
574                                 set_free(s);
575                                 goto oom;
576                         }
577                 }
578
579                 if (!(client = strdup(message_get_sender_with_fallback(message))))
580                         goto oom;
581
582                 if ((r = set_put(s, client)) < 0) {
583                         free(client);
584                         return bus_send_error_reply(m, connection, message, NULL, r);
585                 }
586
587                 if (!(reply = dbus_message_new_method_return(message)))
588                         goto oom;
589
590         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
591                 char *client;
592
593                 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
594                         dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
595                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
596                 }
597
598                 free(client);
599
600                 if (!(reply = dbus_message_new_method_return(message)))
601                         goto oom;
602
603         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
604                 FILE *f;
605                 char *dump = NULL;
606                 size_t size;
607
608                 if (!(reply = dbus_message_new_method_return(message)))
609                         goto oom;
610
611                 if (!(f = open_memstream(&dump, &size)))
612                         goto oom;
613
614                 manager_dump_units(m, f, NULL);
615                 manager_dump_jobs(m, f, NULL);
616
617                 if (ferror(f)) {
618                         fclose(f);
619                         free(dump);
620                         goto oom;
621                 }
622
623                 fclose(f);
624
625                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
626                         free(dump);
627                         goto oom;
628                 }
629
630                 free(dump);
631         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
632                 const char *name;
633                 dbus_bool_t cleanup;
634                 Snapshot *s;
635
636                 if (!dbus_message_get_args(
637                                     message,
638                                     &error,
639                                     DBUS_TYPE_STRING, &name,
640                                     DBUS_TYPE_BOOLEAN, &cleanup,
641                                     DBUS_TYPE_INVALID))
642                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
643
644                 if (name && name[0] == 0)
645                         name = NULL;
646
647                 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
648                         return bus_send_error_reply(m, connection, message, &error, r);
649
650                 if (!(reply = dbus_message_new_method_return(message)))
651                         goto oom;
652
653                 if (!(path = unit_dbus_path(UNIT(s))))
654                         goto oom;
655
656                 if (!dbus_message_append_args(
657                                     reply,
658                                     DBUS_TYPE_OBJECT_PATH, &path,
659                                     DBUS_TYPE_INVALID))
660                         goto oom;
661
662         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
663                 char *introspection = NULL;
664                 FILE *f;
665                 Iterator i;
666                 Unit *u;
667                 Job *j;
668                 const char *k;
669                 size_t size;
670
671                 if (!(reply = dbus_message_new_method_return(message)))
672                         goto oom;
673
674                 /* We roll our own introspection code here, instead of
675                  * relying on bus_default_message_handler() because we
676                  * need to generate our introspection string
677                  * dynamically. */
678
679                 if (!(f = open_memstream(&introspection, &size)))
680                         goto oom;
681
682                 fputs(INTROSPECTION_BEGIN, f);
683
684                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
685                         char *p;
686
687                         if (k != u->meta.id)
688                                 continue;
689
690                         if (!(p = bus_path_escape(k))) {
691                                 fclose(f);
692                                 free(introspection);
693                                 goto oom;
694                         }
695
696                         fprintf(f, "<node name=\"unit/%s\"/>", p);
697                         free(p);
698                 }
699
700                 HASHMAP_FOREACH(j, m->jobs, i)
701                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
702
703                 fputs(INTROSPECTION_END, f);
704
705                 if (ferror(f)) {
706                         fclose(f);
707                         free(introspection);
708                         goto oom;
709                 }
710
711                 fclose(f);
712
713                 if (!introspection)
714                         goto oom;
715
716                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
717                         free(introspection);
718                         goto oom;
719                 }
720
721                 free(introspection);
722
723         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
724
725                 assert(!m->queued_message);
726
727                 /* Instead of sending the reply back right away, we
728                  * just remember that we need to and then send it
729                  * after the reload is finished. That way the caller
730                  * knows when the reload finished. */
731
732                 if (!(m->queued_message = dbus_message_new_method_return(message)))
733                         goto oom;
734
735                 m->queued_message_connection = connection;
736                 m->exit_code = MANAGER_RELOAD;
737
738         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
739
740                 if (!(reply = dbus_message_new_method_return(message)))
741                         goto oom;
742
743                 m->exit_code = MANAGER_REEXECUTE;
744
745         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
746
747                 if (m->running_as == MANAGER_SYSTEM) {
748                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
749                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
750                 }
751
752                 if (!(reply = dbus_message_new_method_return(message)))
753                         goto oom;
754
755                 m->exit_code = MANAGER_EXIT;
756
757         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
758                 char **l = NULL, **e = NULL;
759
760                 if ((r = bus_parse_strv(message, &l)) < 0) {
761                         if (r == -ENOMEM)
762                                 goto oom;
763
764                         return bus_send_error_reply(m, connection, message, NULL, r);
765                 }
766
767                 e = strv_env_merge(2, m->environment, l);
768                 strv_free(l);
769
770                 if (!e)
771                         goto oom;
772
773                 if (!(reply = dbus_message_new_method_return(message))) {
774                         strv_free(e);
775                         goto oom;
776                 }
777
778                 strv_free(m->environment);
779                 m->environment = e;
780
781         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
782                 char **l = NULL, **e = NULL;
783
784                 if ((r = bus_parse_strv(message, &l)) < 0) {
785                         if (r == -ENOMEM)
786                                 goto oom;
787
788                         return bus_send_error_reply(m, connection, message, NULL, r);
789                 }
790
791                 e = strv_env_delete(m->environment, 1, l);
792                 strv_free(l);
793
794                 if (!e)
795                         goto oom;
796
797                 if (!(reply = dbus_message_new_method_return(message)))
798                         goto oom;
799
800                 strv_free(m->environment);
801                 m->environment = e;
802
803         } else
804                 return bus_default_message_handler(m, connection, message, NULL, properties);
805
806         if (job_type != _JOB_TYPE_INVALID) {
807                 const char *name, *smode;
808                 JobMode mode;
809                 Job *j;
810                 Unit *u;
811
812                 if (!dbus_message_get_args(
813                                     message,
814                                     &error,
815                                     DBUS_TYPE_STRING, &name,
816                                     DBUS_TYPE_STRING, &smode,
817                                     DBUS_TYPE_INVALID))
818                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
819
820                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
821                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
822                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
823                 }
824
825                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
826                         return bus_send_error_reply(m, connection, message, &error, r);
827
828                 if (reload_if_possible && unit_can_reload(u)) {
829                         if (job_type == JOB_RESTART)
830                                 job_type = JOB_RELOAD_OR_START;
831                         else if (job_type == JOB_TRY_RESTART)
832                                 job_type = JOB_RELOAD;
833                 }
834
835                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
836                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
837                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
838                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
839                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
840                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
841                 }
842
843                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
844                         return bus_send_error_reply(m, connection, message, &error, r);
845
846                 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
847                         goto oom;
848
849                 j->bus = connection;
850
851                 if (!(reply = dbus_message_new_method_return(message)))
852                         goto oom;
853
854                 if (!(path = job_dbus_path(j)))
855                         goto oom;
856
857                 if (!dbus_message_append_args(
858                                     reply,
859                                     DBUS_TYPE_OBJECT_PATH, &path,
860                                     DBUS_TYPE_INVALID))
861                         goto oom;
862         }
863
864         free(path);
865
866         if (reply) {
867                 if (!dbus_connection_send(connection, reply, NULL))
868                         goto oom;
869
870                 dbus_message_unref(reply);
871         }
872
873         return DBUS_HANDLER_RESULT_HANDLED;
874
875 oom:
876         free(path);
877
878         if (reply)
879                 dbus_message_unref(reply);
880
881         dbus_error_free(&error);
882
883         return DBUS_HANDLER_RESULT_NEED_MEMORY;
884 }
885
886 const DBusObjectPathVTable bus_manager_vtable = {
887         .message_function = bus_manager_message_handler
888 };