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