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