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