chiark / gitweb /
dbus: expose monotonic timestamps on the bus
[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 #include <unistd.h>
24
25 #include "dbus.h"
26 #include "log.h"
27 #include "dbus-manager.h"
28 #include "strv.h"
29 #include "bus-errors.h"
30 #include "build.h"
31
32 #define BUS_MANAGER_INTERFACE_BEGIN                                     \
33         " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
34
35 #define BUS_MANAGER_INTERFACE_METHODS                                   \
36         "  <method name=\"GetUnit\">\n"                                 \
37         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
38         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
39         "  </method>\n"                                                 \
40         "  <method name=\"GetUnitByPID\">\n"                            \
41         "   <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n"          \
42         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
43         "  </method>\n"                                                 \
44         "  <method name=\"LoadUnit\">\n"                                \
45         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
46         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
47         "  </method>\n"                                                 \
48         "  <method name=\"StartUnit\">\n"                               \
49         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
50         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
51         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
52         "  </method>\n"                                                 \
53         "  <method name=\"StartUnitReplace\">\n"                        \
54         "   <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n"     \
55         "   <arg name=\"new_unit\" 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=\"StopUnit\">\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=\"ReloadUnit\">\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=\"RestartUnit\">\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=\"TryRestartUnit\">\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=\"ReloadOrRestartUnit\">\n"                     \
80         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
81         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
82         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
83         "  </method>\n"                                                 \
84         "  <method name=\"ReloadOrTryRestartUnit\">\n"                  \
85         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
86         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
87         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
88         "  </method>\n"                                                 \
89         "  <method name=\"KillUnit\">\n"                                \
90         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
91         "   <arg name=\"who\" type=\"s\" direction=\"in\"/>\n"          \
92         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
93         "   <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n"       \
94         "  </method>\n"                                                 \
95         "  <method name=\"ResetFailedUnit\">\n"                         \
96         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
97         "  </method>\n"                                                 \
98         "  <method name=\"GetJob\">\n"                                  \
99         "   <arg name=\"id\" type=\"u\" direction=\"in\"/>\n"           \
100         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
101         "  </method>\n"                                                 \
102         "  <method name=\"ClearJobs\"/>\n"                              \
103         "  <method name=\"ResetFailed\"/>\n"                            \
104         "  <method name=\"ListUnits\">\n"                               \
105         "   <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
106         "  </method>\n"                                                 \
107         "  <method name=\"ListJobs\">\n"                                \
108         "   <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
109         "  </method>\n"                                                 \
110         "  <method name=\"Subscribe\"/>\n"                              \
111         "  <method name=\"Unsubscribe\"/>\n"                            \
112         "  <method name=\"Dump\"/>\n"                                   \
113         "  <method name=\"CreateSnapshot\">\n"                          \
114         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
115         "   <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n"      \
116         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
117         "  </method>\n"                                                 \
118         "  <method name=\"Reload\"/>\n"                                 \
119         "  <method name=\"Reexecute\"/>\n"                              \
120         "  <method name=\"Exit\"/>\n"                                   \
121         "  <method name=\"Reboot\"/>\n"                                 \
122         "  <method name=\"PowerOff\"/>\n"                               \
123         "  <method name=\"Halt\"/>\n"                                   \
124         "  <method name=\"KExec\"/>\n"                                  \
125         "  <method name=\"SetEnvironment\">\n"                          \
126         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
127         "  </method>\n"                                                 \
128         "  <method name=\"UnsetEnvironment\">\n"                        \
129         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
130         "  </method>\n"
131
132 #define BUS_MANAGER_INTERFACE_SIGNALS                                   \
133         "  <signal name=\"UnitNew\">\n"                                 \
134         "   <arg name=\"id\" type=\"s\"/>\n"                            \
135         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
136         "  </signal>\n"                                                 \
137         "  <signal name=\"UnitRemoved\">\n"                             \
138         "   <arg name=\"id\" type=\"s\"/>\n"                            \
139         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
140         "  </signal>\n"                                                 \
141         "  <signal name=\"JobNew\">\n"                                  \
142         "   <arg name=\"id\" type=\"u\"/>\n"                            \
143         "   <arg name=\"job\" type=\"o\"/>\n"                           \
144         "  </signal>\n"                                                 \
145         "  <signal name=\"JobRemoved\">\n"                              \
146         "   <arg name=\"id\" type=\"u\"/>\n"                            \
147         "   <arg name=\"job\" type=\"o\"/>\n"                           \
148         "   <arg name=\"result\" type=\"s\"/>\n"                        \
149         "  </signal>"
150
151 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL                        \
152         "  <property name=\"Version\" type=\"s\" access=\"read\"/>\n"   \
153         "  <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
154         "  <property name=\"Features\" type=\"s\" access=\"read\"/>\n"  \
155         "  <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n"   \
156         "  <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
157         "  <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
158         "  <property name=\"InitRDTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
159         "  <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
160         "  <property name=\"StartupTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
161         "  <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
162         "  <property name=\"FinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
163         "  <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n"  \
164         "  <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
165         "  <property name=\"NNames\" type=\"u\" access=\"read\"/>\n"    \
166         "  <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n"     \
167         "  <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
168         "  <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
169         "  <property name=\"Progress\" type=\"d\" access=\"read\"/>\n"  \
170         "  <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
171         "  <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
172         "  <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
173         "  <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
174         "  <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
175         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
176         "  <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
177         "  <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n"  \
178         "  <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
179         "  <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
180         "  <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
181
182 #ifdef HAVE_SYSV_COMPAT
183 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV                           \
184         "  <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
185         "  <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
186         "  <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
187 #else
188 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
189 #endif
190
191 #define BUS_MANAGER_INTERFACE_END                                       \
192         " </interface>\n"
193
194 #define BUS_MANAGER_INTERFACE                                           \
195         BUS_MANAGER_INTERFACE_BEGIN                                     \
196         BUS_MANAGER_INTERFACE_METHODS                                   \
197         BUS_MANAGER_INTERFACE_SIGNALS                                   \
198         BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL                        \
199         BUS_MANAGER_INTERFACE_PROPERTIES_SYSV                           \
200         BUS_MANAGER_INTERFACE_END
201
202 #define INTROSPECTION_BEGIN                                             \
203         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
204         "<node>\n"                                                      \
205         BUS_MANAGER_INTERFACE                                           \
206         BUS_PROPERTIES_INTERFACE                                        \
207         BUS_PEER_INTERFACE                                              \
208         BUS_INTROSPECTABLE_INTERFACE
209
210 #define INTROSPECTION_END                                               \
211         "</node>\n"
212
213 #define INTERFACES_LIST                              \
214         BUS_GENERIC_INTERFACES_LIST                  \
215         "org.freedesktop.systemd1.Manager\0"
216
217 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
218
219 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
220 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
221
222 static int bus_manager_append_tainted(Manager *m, DBusMessageIter *i, const char *property, void *data) {
223         const char *t;
224         char buf[LINE_MAX] = "", *e = buf, *p = NULL;
225
226         assert(m);
227         assert(i);
228         assert(property);
229
230         if (m->taint_usr)
231                 e = stpcpy(e, "usr-separate-fs ");
232
233         if (readlink_malloc("/etc/mtab", &p) < 0)
234                 e = stpcpy(e, "etc-mtab-not-symlink ");
235         else
236                 free(p);
237
238         if (access("/proc/cgroups", F_OK) < 0)
239                 e = stpcpy(e, "cgroups-missing ");
240
241         t = strstrip(buf);
242
243         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
244                 return -ENOMEM;
245
246         return 0;
247 }
248
249 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
250         const char *t;
251
252         assert(m);
253         assert(i);
254         assert(property);
255
256         t = log_target_to_string(log_get_target());
257
258         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
259                 return -ENOMEM;
260
261         return 0;
262 }
263
264 static int bus_manager_set_log_target(Manager *m, DBusMessageIter *i, const char *property) {
265         const char *t;
266
267         assert(m);
268         assert(i);
269         assert(property);
270
271         dbus_message_iter_get_basic(i, &t);
272
273         return log_set_target_from_string(t);
274 }
275
276 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
277         const char *t;
278
279         assert(m);
280         assert(i);
281         assert(property);
282
283         t = log_level_to_string(log_get_max_level());
284
285         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
286                 return -ENOMEM;
287
288         return 0;
289 }
290
291 static int bus_manager_set_log_level(Manager *m, DBusMessageIter *i, const char *property) {
292         const char *t;
293
294         assert(m);
295         assert(i);
296         assert(property);
297
298         dbus_message_iter_get_basic(i, &t);
299
300         return log_set_max_level_from_string(t);
301 }
302
303 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
304         uint32_t u;
305
306         assert(m);
307         assert(i);
308         assert(property);
309
310         u = hashmap_size(m->units);
311
312         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
313                 return -ENOMEM;
314
315         return 0;
316 }
317
318 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
319         uint32_t u;
320
321         assert(m);
322         assert(i);
323         assert(property);
324
325         u = hashmap_size(m->jobs);
326
327         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
328                 return -ENOMEM;
329
330         return 0;
331 }
332
333 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
334         double d;
335
336         assert(m);
337         assert(i);
338         assert(property);
339
340         if (dual_timestamp_is_set(&m->finish_timestamp))
341                 d = 1.0;
342         else
343                 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
344
345         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
346                 return -ENOMEM;
347
348         return 0;
349 }
350
351 static const char *message_get_sender_with_fallback(DBusMessage *m) {
352         const char *s;
353
354         assert(m);
355
356         if ((s = dbus_message_get_sender(m)))
357                 return s;
358
359         /* When the message came in from a direct connection the
360          * message will have no sender. We fix that here. */
361
362         return ":no-sender";
363 }
364
365 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
366         Manager *m = data;
367
368         const BusProperty properties[] = {
369                 { "org.freedesktop.systemd1.Manager", "Version",       bus_property_append_string,    "s",  PACKAGE_STRING     },
370                 { "org.freedesktop.systemd1.Manager", "Distribution",  bus_property_append_string,    "s",  DISTRIBUTION       },
371                 { "org.freedesktop.systemd1.Manager", "Features",      bus_property_append_string,    "s",  SYSTEMD_FEATURES   },
372                 { "org.freedesktop.systemd1.Manager", "RunningAs",     bus_manager_append_running_as, "s",  &m->running_as     },
373                 { "org.freedesktop.systemd1.Manager", "Tainted",       bus_manager_append_tainted,    "s",  m                  },
374                 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64,  "t",  &m->initrd_timestamp.realtime },
375                 { "org.freedesktop.systemd1.Manager", "InitRDTimestampMonotonic", bus_property_append_uint64, "t", &m->initrd_timestamp.monotonic },
376                 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t",  &m->startup_timestamp.realtime },
377                 { "org.freedesktop.systemd1.Manager", "StartupTimestampMonotonic", bus_property_append_uint64, "t", &m->startup_timestamp.monotonic },
378                 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64,  "t",  &m->finish_timestamp.realtime },
379                 { "org.freedesktop.systemd1.Manager", "FinishTimestampMonotonic", bus_property_append_uint64, "t",&m->finish_timestamp.monotonic },
380                 { "org.freedesktop.systemd1.Manager", "LogLevel",      bus_manager_append_log_level,  "s",  NULL,               bus_manager_set_log_level},
381                 { "org.freedesktop.systemd1.Manager", "LogTarget",     bus_manager_append_log_target, "s",  NULL,               bus_manager_set_log_target},
382                 { "org.freedesktop.systemd1.Manager", "NNames",        bus_manager_append_n_names,    "u",  NULL               },
383                 { "org.freedesktop.systemd1.Manager", "NJobs",         bus_manager_append_n_jobs,     "u",  NULL               },
384                 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32,    "u",  &m->n_installed_jobs },
385                 { "org.freedesktop.systemd1.Manager", "NFailedJobs",   bus_property_append_uint32,    "u",  &m->n_failed_jobs  },
386                 { "org.freedesktop.systemd1.Manager", "Progress",      bus_manager_append_progress,   "d",  NULL               },
387                 { "org.freedesktop.systemd1.Manager", "Environment",   bus_property_append_strv,      "as", m->environment     },
388                 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn",  bus_property_append_bool,      "b",  &m->confirm_spawn  },
389                 { "org.freedesktop.systemd1.Manager", "ShowStatus",    bus_property_append_bool,      "b",  &m->show_status    },
390                 { "org.freedesktop.systemd1.Manager", "UnitPath",      bus_property_append_strv,      "as", m->lookup_paths.unit_path },
391                 { "org.freedesktop.systemd1.Manager", "NotifySocket",  bus_property_append_string,    "s",  m->notify_socket   },
392                 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
393                 { "org.freedesktop.systemd1.Manager", "MountAuto",     bus_property_append_bool,      "b",  &m->mount_auto     },
394                 { "org.freedesktop.systemd1.Manager", "SwapAuto",      bus_property_append_bool,      "b",  &m->swap_auto      },
395                 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
396                 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output },
397                 { "org.freedesktop.systemd1.Manager", "DefaultStandardError",  bus_manager_append_exec_output, "s", &m->default_std_error  },
398 #ifdef HAVE_SYSV_COMPAT
399                 { "org.freedesktop.systemd1.Manager", "SysVConsole",   bus_property_append_bool,      "b",  &m->sysv_console   },
400                 { "org.freedesktop.systemd1.Manager", "SysVInitPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvinit_path },
401                 { "org.freedesktop.systemd1.Manager", "SysVRcndPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvrcnd_path },
402 #endif
403                 { NULL, NULL, NULL, NULL, NULL }
404         };
405
406         int r;
407         DBusError error;
408         DBusMessage *reply = NULL;
409         char * path = NULL;
410         JobType job_type = _JOB_TYPE_INVALID;
411         bool reload_if_possible = false;
412
413         assert(connection);
414         assert(message);
415         assert(m);
416
417         dbus_error_init(&error);
418
419         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
420                 const char *name;
421                 Unit *u;
422
423                 if (!dbus_message_get_args(
424                                     message,
425                                     &error,
426                                     DBUS_TYPE_STRING, &name,
427                                     DBUS_TYPE_INVALID))
428                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
429
430                 if (!(u = manager_get_unit(m, name))) {
431                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
432                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
433                 }
434
435                 if (!(reply = dbus_message_new_method_return(message)))
436                         goto oom;
437
438                 if (!(path = unit_dbus_path(u)))
439                         goto oom;
440
441                 if (!dbus_message_append_args(
442                                     reply,
443                                     DBUS_TYPE_OBJECT_PATH, &path,
444                                     DBUS_TYPE_INVALID))
445                         goto oom;
446         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
447                 Unit *u;
448                 uint32_t pid;
449
450                 if (!dbus_message_get_args(
451                                     message,
452                                     &error,
453                                     DBUS_TYPE_UINT32, &pid,
454                                     DBUS_TYPE_INVALID))
455                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
456
457                 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
458                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
459                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
460                 }
461
462                 if (!(reply = dbus_message_new_method_return(message)))
463                         goto oom;
464
465                 if (!(path = unit_dbus_path(u)))
466                         goto oom;
467
468                 if (!dbus_message_append_args(
469                                     reply,
470                                     DBUS_TYPE_OBJECT_PATH, &path,
471                                     DBUS_TYPE_INVALID))
472                         goto oom;
473         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
474                 const char *name;
475                 Unit *u;
476
477                 if (!dbus_message_get_args(
478                                     message,
479                                     &error,
480                                     DBUS_TYPE_STRING, &name,
481                                     DBUS_TYPE_INVALID))
482                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
483
484                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
485                         return bus_send_error_reply(m, connection, message, &error, r);
486
487                 if (!(reply = dbus_message_new_method_return(message)))
488                         goto oom;
489
490                 if (!(path = unit_dbus_path(u)))
491                         goto oom;
492
493                 if (!dbus_message_append_args(
494                                     reply,
495                                     DBUS_TYPE_OBJECT_PATH, &path,
496                                     DBUS_TYPE_INVALID))
497                         goto oom;
498
499         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
500                 job_type = JOB_START;
501         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
502                 job_type = JOB_START;
503         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
504                 job_type = JOB_STOP;
505         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
506                 job_type = JOB_RELOAD;
507         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
508                 job_type = JOB_RESTART;
509         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
510                 job_type = JOB_TRY_RESTART;
511         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
512                 reload_if_possible = true;
513                 job_type = JOB_RESTART;
514         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
515                 reload_if_possible = true;
516                 job_type = JOB_TRY_RESTART;
517         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
518                 const char *name, *swho, *smode;
519                 int32_t signo;
520                 Unit *u;
521                 KillMode mode;
522                 KillWho who;
523
524                 if (!dbus_message_get_args(
525                                     message,
526                                     &error,
527                                     DBUS_TYPE_STRING, &name,
528                                     DBUS_TYPE_STRING, &swho,
529                                     DBUS_TYPE_STRING, &smode,
530                                     DBUS_TYPE_INT32, &signo,
531                                     DBUS_TYPE_INVALID))
532                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
533
534                 if ((mode = kill_mode_from_string(smode)) < 0 ||
535                     (who = kill_who_from_string(swho)) < 0 ||
536                     signo <= 0 ||
537                     signo >= _NSIG)
538                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
539
540                 if (!(u = manager_get_unit(m, name))) {
541                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
542                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
543                 }
544
545                 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
546                         return bus_send_error_reply(m, connection, message, &error, r);
547
548                 if (!(reply = dbus_message_new_method_return(message)))
549                         goto oom;
550
551         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
552                 uint32_t id;
553                 Job *j;
554
555                 if (!dbus_message_get_args(
556                                     message,
557                                     &error,
558                                     DBUS_TYPE_UINT32, &id,
559                                     DBUS_TYPE_INVALID))
560                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
561
562                 if (!(j = manager_get_job(m, id))) {
563                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
564                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
565                 }
566
567                 if (!(reply = dbus_message_new_method_return(message)))
568                         goto oom;
569
570                 if (!(path = job_dbus_path(j)))
571                         goto oom;
572
573                 if (!dbus_message_append_args(
574                                     reply,
575                                     DBUS_TYPE_OBJECT_PATH, &path,
576                                     DBUS_TYPE_INVALID))
577                         goto oom;
578
579         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
580
581                 manager_clear_jobs(m);
582
583                 if (!(reply = dbus_message_new_method_return(message)))
584                         goto oom;
585
586         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
587
588                 manager_reset_failed(m);
589
590                 if (!(reply = dbus_message_new_method_return(message)))
591                         goto oom;
592
593         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
594                 const char *name;
595                 Unit *u;
596
597                 if (!dbus_message_get_args(
598                                     message,
599                                     &error,
600                                     DBUS_TYPE_STRING, &name,
601                                     DBUS_TYPE_INVALID))
602                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
603
604                 if (!(u = manager_get_unit(m, name))) {
605                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
606                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
607                 }
608
609                 unit_reset_failed(u);
610
611                 if (!(reply = dbus_message_new_method_return(message)))
612                         goto oom;
613
614         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
615                 DBusMessageIter iter, sub;
616                 Iterator i;
617                 Unit *u;
618                 const char *k;
619
620                 if (!(reply = dbus_message_new_method_return(message)))
621                         goto oom;
622
623                 dbus_message_iter_init_append(reply, &iter);
624
625                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
626                         goto oom;
627
628                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
629                         char *u_path, *j_path;
630                         const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
631                         DBusMessageIter sub2;
632                         uint32_t job_id;
633                         Unit *f;
634
635                         if (k != u->meta.id)
636                                 continue;
637
638                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
639                                 goto oom;
640
641                         description = unit_description(u);
642                         load_state = unit_load_state_to_string(u->meta.load_state);
643                         active_state = unit_active_state_to_string(unit_active_state(u));
644                         sub_state = unit_sub_state_to_string(u);
645
646                         f = unit_following(u);
647                         following = f ? f->meta.id : "";
648
649                         if (!(u_path = unit_dbus_path(u)))
650                                 goto oom;
651
652                         if (u->meta.job) {
653                                 job_id = (uint32_t) u->meta.job->id;
654
655                                 if (!(j_path = job_dbus_path(u->meta.job))) {
656                                         free(u_path);
657                                         goto oom;
658                                 }
659
660                                 sjob_type = job_type_to_string(u->meta.job->type);
661                         } else {
662                                 job_id = 0;
663                                 j_path = u_path;
664                                 sjob_type = "";
665                         }
666
667                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
668                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
669                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
670                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
671                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
672                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
673                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
674                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
675                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
676                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
677                                 free(u_path);
678                                 if (u->meta.job)
679                                         free(j_path);
680                                 goto oom;
681                         }
682
683                         free(u_path);
684                         if (u->meta.job)
685                                 free(j_path);
686
687                         if (!dbus_message_iter_close_container(&sub, &sub2))
688                                 goto oom;
689                 }
690
691                 if (!dbus_message_iter_close_container(&iter, &sub))
692                         goto oom;
693
694         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
695                 DBusMessageIter iter, sub;
696                 Iterator i;
697                 Job *j;
698
699                 if (!(reply = dbus_message_new_method_return(message)))
700                         goto oom;
701
702                 dbus_message_iter_init_append(reply, &iter);
703
704                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
705                         goto oom;
706
707                 HASHMAP_FOREACH(j, m->jobs, i) {
708                         char *u_path, *j_path;
709                         const char *state, *type;
710                         uint32_t id;
711                         DBusMessageIter sub2;
712
713                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
714                                 goto oom;
715
716                         id = (uint32_t) j->id;
717                         state = job_state_to_string(j->state);
718                         type = job_type_to_string(j->type);
719
720                         if (!(j_path = job_dbus_path(j)))
721                                 goto oom;
722
723                         if (!(u_path = unit_dbus_path(j->unit))) {
724                                 free(j_path);
725                                 goto oom;
726                         }
727
728                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
729                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
730                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
731                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
732                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
733                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
734                                 free(j_path);
735                                 free(u_path);
736                                 goto oom;
737                         }
738
739                         free(j_path);
740                         free(u_path);
741
742                         if (!dbus_message_iter_close_container(&sub, &sub2))
743                                 goto oom;
744                 }
745
746                 if (!dbus_message_iter_close_container(&iter, &sub))
747                         goto oom;
748
749         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
750                 char *client;
751                 Set *s;
752
753                 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
754                         if (!(s = set_new(string_hash_func, string_compare_func)))
755                                 goto oom;
756
757                         if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
758                                 set_free(s);
759                                 goto oom;
760                         }
761                 }
762
763                 if (!(client = strdup(message_get_sender_with_fallback(message))))
764                         goto oom;
765
766                 if ((r = set_put(s, client)) < 0) {
767                         free(client);
768                         return bus_send_error_reply(m, connection, message, NULL, r);
769                 }
770
771                 if (!(reply = dbus_message_new_method_return(message)))
772                         goto oom;
773
774         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
775                 char *client;
776
777                 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
778                         dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
779                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
780                 }
781
782                 free(client);
783
784                 if (!(reply = dbus_message_new_method_return(message)))
785                         goto oom;
786
787         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
788                 FILE *f;
789                 char *dump = NULL;
790                 size_t size;
791
792                 if (!(reply = dbus_message_new_method_return(message)))
793                         goto oom;
794
795                 if (!(f = open_memstream(&dump, &size)))
796                         goto oom;
797
798                 manager_dump_units(m, f, NULL);
799                 manager_dump_jobs(m, f, NULL);
800
801                 if (ferror(f)) {
802                         fclose(f);
803                         free(dump);
804                         goto oom;
805                 }
806
807                 fclose(f);
808
809                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
810                         free(dump);
811                         goto oom;
812                 }
813
814                 free(dump);
815         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
816                 const char *name;
817                 dbus_bool_t cleanup;
818                 Snapshot *s;
819
820                 if (!dbus_message_get_args(
821                                     message,
822                                     &error,
823                                     DBUS_TYPE_STRING, &name,
824                                     DBUS_TYPE_BOOLEAN, &cleanup,
825                                     DBUS_TYPE_INVALID))
826                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
827
828                 if (name && name[0] == 0)
829                         name = NULL;
830
831                 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
832                         return bus_send_error_reply(m, connection, message, &error, r);
833
834                 if (!(reply = dbus_message_new_method_return(message)))
835                         goto oom;
836
837                 if (!(path = unit_dbus_path(UNIT(s))))
838                         goto oom;
839
840                 if (!dbus_message_append_args(
841                                     reply,
842                                     DBUS_TYPE_OBJECT_PATH, &path,
843                                     DBUS_TYPE_INVALID))
844                         goto oom;
845
846         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
847                 char *introspection = NULL;
848                 FILE *f;
849                 Iterator i;
850                 Unit *u;
851                 Job *j;
852                 const char *k;
853                 size_t size;
854
855                 if (!(reply = dbus_message_new_method_return(message)))
856                         goto oom;
857
858                 /* We roll our own introspection code here, instead of
859                  * relying on bus_default_message_handler() because we
860                  * need to generate our introspection string
861                  * dynamically. */
862
863                 if (!(f = open_memstream(&introspection, &size)))
864                         goto oom;
865
866                 fputs(INTROSPECTION_BEGIN, f);
867
868                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
869                         char *p;
870
871                         if (k != u->meta.id)
872                                 continue;
873
874                         if (!(p = bus_path_escape(k))) {
875                                 fclose(f);
876                                 free(introspection);
877                                 goto oom;
878                         }
879
880                         fprintf(f, "<node name=\"unit/%s\"/>", p);
881                         free(p);
882                 }
883
884                 HASHMAP_FOREACH(j, m->jobs, i)
885                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
886
887                 fputs(INTROSPECTION_END, f);
888
889                 if (ferror(f)) {
890                         fclose(f);
891                         free(introspection);
892                         goto oom;
893                 }
894
895                 fclose(f);
896
897                 if (!introspection)
898                         goto oom;
899
900                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
901                         free(introspection);
902                         goto oom;
903                 }
904
905                 free(introspection);
906
907         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
908
909                 assert(!m->queued_message);
910
911                 /* Instead of sending the reply back right away, we
912                  * just remember that we need to and then send it
913                  * after the reload is finished. That way the caller
914                  * knows when the reload finished. */
915
916                 if (!(m->queued_message = dbus_message_new_method_return(message)))
917                         goto oom;
918
919                 m->queued_message_connection = connection;
920                 m->exit_code = MANAGER_RELOAD;
921
922         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
923
924                 if (!(reply = dbus_message_new_method_return(message)))
925                         goto oom;
926
927                 m->exit_code = MANAGER_REEXECUTE;
928
929         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
930
931                 if (m->running_as == MANAGER_SYSTEM) {
932                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
933                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
934                 }
935
936                 if (!(reply = dbus_message_new_method_return(message)))
937                         goto oom;
938
939                 m->exit_code = MANAGER_EXIT;
940
941         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
942
943                 if (m->running_as != MANAGER_SYSTEM) {
944                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
945                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
946                 }
947
948                 if (!(reply = dbus_message_new_method_return(message)))
949                         goto oom;
950
951                 m->exit_code = MANAGER_REBOOT;
952
953         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
954
955                 if (m->running_as != MANAGER_SYSTEM) {
956                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
957                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
958                 }
959
960                 if (!(reply = dbus_message_new_method_return(message)))
961                         goto oom;
962
963                 m->exit_code = MANAGER_POWEROFF;
964
965         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
966
967                 if (m->running_as != MANAGER_SYSTEM) {
968                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
969                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
970                 }
971
972                 if (!(reply = dbus_message_new_method_return(message)))
973                         goto oom;
974
975                 m->exit_code = MANAGER_HALT;
976
977         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
978
979                 if (m->running_as != MANAGER_SYSTEM) {
980                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
981                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
982                 }
983
984                 if (!(reply = dbus_message_new_method_return(message)))
985                         goto oom;
986
987                 m->exit_code = MANAGER_KEXEC;
988
989         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
990                 char **l = NULL, **e = NULL;
991
992                 if ((r = bus_parse_strv(message, &l)) < 0) {
993                         if (r == -ENOMEM)
994                                 goto oom;
995
996                         return bus_send_error_reply(m, connection, message, NULL, r);
997                 }
998
999                 e = strv_env_merge(2, m->environment, l);
1000                 strv_free(l);
1001
1002                 if (!e)
1003                         goto oom;
1004
1005                 if (!(reply = dbus_message_new_method_return(message))) {
1006                         strv_free(e);
1007                         goto oom;
1008                 }
1009
1010                 strv_free(m->environment);
1011                 m->environment = e;
1012
1013         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1014                 char **l = NULL, **e = NULL;
1015
1016                 if ((r = bus_parse_strv(message, &l)) < 0) {
1017                         if (r == -ENOMEM)
1018                                 goto oom;
1019
1020                         return bus_send_error_reply(m, connection, message, NULL, r);
1021                 }
1022
1023                 e = strv_env_delete(m->environment, 1, l);
1024                 strv_free(l);
1025
1026                 if (!e)
1027                         goto oom;
1028
1029                 if (!(reply = dbus_message_new_method_return(message))) {
1030                         strv_free(e);
1031                         goto oom;
1032                 }
1033
1034                 strv_free(m->environment);
1035                 m->environment = e;
1036
1037         } else
1038                 return bus_default_message_handler(m, connection, message, NULL, INTERFACES_LIST, properties);
1039
1040         if (job_type != _JOB_TYPE_INVALID) {
1041                 const char *name, *smode, *old_name = NULL;
1042                 JobMode mode;
1043                 Job *j;
1044                 Unit *u;
1045                 bool b;
1046
1047                 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1048                         b = dbus_message_get_args(
1049                                         message,
1050                                         &error,
1051                                         DBUS_TYPE_STRING, &old_name,
1052                                         DBUS_TYPE_STRING, &name,
1053                                         DBUS_TYPE_STRING, &smode,
1054                                         DBUS_TYPE_INVALID);
1055                 else
1056                         b = dbus_message_get_args(
1057                                         message,
1058                                         &error,
1059                                         DBUS_TYPE_STRING, &name,
1060                                         DBUS_TYPE_STRING, &smode,
1061                                         DBUS_TYPE_INVALID);
1062
1063                 if (!b)
1064                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1065
1066                 if (old_name)
1067                         if (!(u = manager_get_unit(m, old_name)) ||
1068                             !u->meta.job ||
1069                             u->meta.job->type != JOB_START) {
1070                                 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1071                                 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
1072                         }
1073
1074
1075                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1076                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1077                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1078                 }
1079
1080                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1081                         return bus_send_error_reply(m, connection, message, &error, r);
1082
1083                 if (reload_if_possible && unit_can_reload(u)) {
1084                         if (job_type == JOB_RESTART)
1085                                 job_type = JOB_RELOAD_OR_START;
1086                         else if (job_type == JOB_TRY_RESTART)
1087                                 job_type = JOB_RELOAD;
1088                 }
1089
1090                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1091                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1092                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1093                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1094                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1095                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
1096                 }
1097
1098                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1099                         return bus_send_error_reply(m, connection, message, &error, r);
1100
1101                 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1102                         goto oom;
1103
1104                 j->bus = connection;
1105
1106                 if (!(reply = dbus_message_new_method_return(message)))
1107                         goto oom;
1108
1109                 if (!(path = job_dbus_path(j)))
1110                         goto oom;
1111
1112                 if (!dbus_message_append_args(
1113                                     reply,
1114                                     DBUS_TYPE_OBJECT_PATH, &path,
1115                                     DBUS_TYPE_INVALID))
1116                         goto oom;
1117         }
1118
1119         if (reply) {
1120                 if (!dbus_connection_send(connection, reply, NULL))
1121                         goto oom;
1122
1123                 dbus_message_unref(reply);
1124         }
1125
1126         free(path);
1127
1128         return DBUS_HANDLER_RESULT_HANDLED;
1129
1130 oom:
1131         free(path);
1132
1133         if (reply)
1134                 dbus_message_unref(reply);
1135
1136         dbus_error_free(&error);
1137
1138         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1139 }
1140
1141 const DBusObjectPathVTable bus_manager_vtable = {
1142         .message_function = bus_manager_message_handler
1143 };