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