chiark / gitweb /
shutdown: untabify
[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 (isempty(swho))
546                         who = KILL_ALL;
547                 else {
548                         who = kill_who_from_string(swho);
549                         if (who < 0)
550                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
551                 }
552
553                 if (isempty(smode))
554                         mode = KILL_CONTROL_GROUP;
555                 else {
556                         mode = kill_mode_from_string(smode);
557                         if (mode < 0)
558                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
559                 }
560
561                 if (signo <= 0 || signo >= _NSIG)
562                         return bus_send_error_reply(connection, message, &error, -EINVAL);
563
564                 if (!(u = manager_get_unit(m, name))) {
565                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
566                         return bus_send_error_reply(connection, message, &error, -ENOENT);
567                 }
568
569                 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
570                         return bus_send_error_reply(connection, message, &error, r);
571
572                 if (!(reply = dbus_message_new_method_return(message)))
573                         goto oom;
574
575         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
576                 uint32_t id;
577                 Job *j;
578
579                 if (!dbus_message_get_args(
580                                     message,
581                                     &error,
582                                     DBUS_TYPE_UINT32, &id,
583                                     DBUS_TYPE_INVALID))
584                         return bus_send_error_reply(connection, message, &error, -EINVAL);
585
586                 if (!(j = manager_get_job(m, id))) {
587                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
588                         return bus_send_error_reply(connection, message, &error, -ENOENT);
589                 }
590
591                 if (!(reply = dbus_message_new_method_return(message)))
592                         goto oom;
593
594                 if (!(path = job_dbus_path(j)))
595                         goto oom;
596
597                 if (!dbus_message_append_args(
598                                     reply,
599                                     DBUS_TYPE_OBJECT_PATH, &path,
600                                     DBUS_TYPE_INVALID))
601                         goto oom;
602
603         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
604
605                 manager_clear_jobs(m);
606
607                 if (!(reply = dbus_message_new_method_return(message)))
608                         goto oom;
609
610         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
611
612                 manager_reset_failed(m);
613
614                 if (!(reply = dbus_message_new_method_return(message)))
615                         goto oom;
616
617         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
618                 const char *name;
619                 Unit *u;
620
621                 if (!dbus_message_get_args(
622                                     message,
623                                     &error,
624                                     DBUS_TYPE_STRING, &name,
625                                     DBUS_TYPE_INVALID))
626                         return bus_send_error_reply(connection, message, &error, -EINVAL);
627
628                 if (!(u = manager_get_unit(m, name))) {
629                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
630                         return bus_send_error_reply(connection, message, &error, -ENOENT);
631                 }
632
633                 unit_reset_failed(u);
634
635                 if (!(reply = dbus_message_new_method_return(message)))
636                         goto oom;
637
638         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
639                 DBusMessageIter iter, sub;
640                 Iterator i;
641                 Unit *u;
642                 const char *k;
643
644                 if (!(reply = dbus_message_new_method_return(message)))
645                         goto oom;
646
647                 dbus_message_iter_init_append(reply, &iter);
648
649                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
650                         goto oom;
651
652                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
653                         char *u_path, *j_path;
654                         const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
655                         DBusMessageIter sub2;
656                         uint32_t job_id;
657                         Unit *f;
658
659                         if (k != u->meta.id)
660                                 continue;
661
662                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
663                                 goto oom;
664
665                         description = unit_description(u);
666                         load_state = unit_load_state_to_string(u->meta.load_state);
667                         active_state = unit_active_state_to_string(unit_active_state(u));
668                         sub_state = unit_sub_state_to_string(u);
669
670                         f = unit_following(u);
671                         following = f ? f->meta.id : "";
672
673                         if (!(u_path = unit_dbus_path(u)))
674                                 goto oom;
675
676                         if (u->meta.job) {
677                                 job_id = (uint32_t) u->meta.job->id;
678
679                                 if (!(j_path = job_dbus_path(u->meta.job))) {
680                                         free(u_path);
681                                         goto oom;
682                                 }
683
684                                 sjob_type = job_type_to_string(u->meta.job->type);
685                         } else {
686                                 job_id = 0;
687                                 j_path = u_path;
688                                 sjob_type = "";
689                         }
690
691                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
692                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
693                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
694                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
695                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
696                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
697                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
698                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
699                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
700                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
701                                 free(u_path);
702                                 if (u->meta.job)
703                                         free(j_path);
704                                 goto oom;
705                         }
706
707                         free(u_path);
708                         if (u->meta.job)
709                                 free(j_path);
710
711                         if (!dbus_message_iter_close_container(&sub, &sub2))
712                                 goto oom;
713                 }
714
715                 if (!dbus_message_iter_close_container(&iter, &sub))
716                         goto oom;
717
718         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
719                 DBusMessageIter iter, sub;
720                 Iterator i;
721                 Job *j;
722
723                 if (!(reply = dbus_message_new_method_return(message)))
724                         goto oom;
725
726                 dbus_message_iter_init_append(reply, &iter);
727
728                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
729                         goto oom;
730
731                 HASHMAP_FOREACH(j, m->jobs, i) {
732                         char *u_path, *j_path;
733                         const char *state, *type;
734                         uint32_t id;
735                         DBusMessageIter sub2;
736
737                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
738                                 goto oom;
739
740                         id = (uint32_t) j->id;
741                         state = job_state_to_string(j->state);
742                         type = job_type_to_string(j->type);
743
744                         if (!(j_path = job_dbus_path(j)))
745                                 goto oom;
746
747                         if (!(u_path = unit_dbus_path(j->unit))) {
748                                 free(j_path);
749                                 goto oom;
750                         }
751
752                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
753                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
754                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
755                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
756                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
757                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
758                                 free(j_path);
759                                 free(u_path);
760                                 goto oom;
761                         }
762
763                         free(j_path);
764                         free(u_path);
765
766                         if (!dbus_message_iter_close_container(&sub, &sub2))
767                                 goto oom;
768                 }
769
770                 if (!dbus_message_iter_close_container(&iter, &sub))
771                         goto oom;
772
773         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
774                 char *client;
775                 Set *s;
776
777                 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
778                         if (!(s = set_new(string_hash_func, string_compare_func)))
779                                 goto oom;
780
781                         if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
782                                 set_free(s);
783                                 goto oom;
784                         }
785                 }
786
787                 if (!(client = strdup(message_get_sender_with_fallback(message))))
788                         goto oom;
789
790                 if ((r = set_put(s, client)) < 0) {
791                         free(client);
792                         return bus_send_error_reply(connection, message, NULL, r);
793                 }
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", "Unsubscribe")) {
799                 char *client;
800
801                 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
802                         dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
803                         return bus_send_error_reply(connection, message, &error, -ENOENT);
804                 }
805
806                 free(client);
807
808                 if (!(reply = dbus_message_new_method_return(message)))
809                         goto oom;
810
811         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
812                 FILE *f;
813                 char *dump = NULL;
814                 size_t size;
815
816                 if (!(reply = dbus_message_new_method_return(message)))
817                         goto oom;
818
819                 if (!(f = open_memstream(&dump, &size)))
820                         goto oom;
821
822                 manager_dump_units(m, f, NULL);
823                 manager_dump_jobs(m, f, NULL);
824
825                 if (ferror(f)) {
826                         fclose(f);
827                         free(dump);
828                         goto oom;
829                 }
830
831                 fclose(f);
832
833                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
834                         free(dump);
835                         goto oom;
836                 }
837
838                 free(dump);
839         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
840                 const char *name;
841                 dbus_bool_t cleanup;
842                 Snapshot *s;
843
844                 if (!dbus_message_get_args(
845                                     message,
846                                     &error,
847                                     DBUS_TYPE_STRING, &name,
848                                     DBUS_TYPE_BOOLEAN, &cleanup,
849                                     DBUS_TYPE_INVALID))
850                         return bus_send_error_reply(connection, message, &error, -EINVAL);
851
852                 if (name && name[0] == 0)
853                         name = NULL;
854
855                 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
856                         return bus_send_error_reply(connection, message, &error, r);
857
858                 if (!(reply = dbus_message_new_method_return(message)))
859                         goto oom;
860
861                 if (!(path = unit_dbus_path(UNIT(s))))
862                         goto oom;
863
864                 if (!dbus_message_append_args(
865                                     reply,
866                                     DBUS_TYPE_OBJECT_PATH, &path,
867                                     DBUS_TYPE_INVALID))
868                         goto oom;
869
870         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
871                 char *introspection = NULL;
872                 FILE *f;
873                 Iterator i;
874                 Unit *u;
875                 Job *j;
876                 const char *k;
877                 size_t size;
878
879                 if (!(reply = dbus_message_new_method_return(message)))
880                         goto oom;
881
882                 /* We roll our own introspection code here, instead of
883                  * relying on bus_default_message_handler() because we
884                  * need to generate our introspection string
885                  * dynamically. */
886
887                 if (!(f = open_memstream(&introspection, &size)))
888                         goto oom;
889
890                 fputs(INTROSPECTION_BEGIN, f);
891
892                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
893                         char *p;
894
895                         if (k != u->meta.id)
896                                 continue;
897
898                         if (!(p = bus_path_escape(k))) {
899                                 fclose(f);
900                                 free(introspection);
901                                 goto oom;
902                         }
903
904                         fprintf(f, "<node name=\"unit/%s\"/>", p);
905                         free(p);
906                 }
907
908                 HASHMAP_FOREACH(j, m->jobs, i)
909                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
910
911                 fputs(INTROSPECTION_END, f);
912
913                 if (ferror(f)) {
914                         fclose(f);
915                         free(introspection);
916                         goto oom;
917                 }
918
919                 fclose(f);
920
921                 if (!introspection)
922                         goto oom;
923
924                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
925                         free(introspection);
926                         goto oom;
927                 }
928
929                 free(introspection);
930
931         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
932
933                 assert(!m->queued_message);
934
935                 /* Instead of sending the reply back right away, we
936                  * just remember that we need to and then send it
937                  * after the reload is finished. That way the caller
938                  * knows when the reload finished. */
939
940                 if (!(m->queued_message = dbus_message_new_method_return(message)))
941                         goto oom;
942
943                 m->queued_message_connection = connection;
944                 m->exit_code = MANAGER_RELOAD;
945
946         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
947
948                 /* We don't send a reply back here, the client should
949                  * just wait for us disconnecting. */
950
951                 m->exit_code = MANAGER_REEXECUTE;
952
953         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
954
955                 if (m->running_as == MANAGER_SYSTEM) {
956                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
957                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
958                 }
959
960                 if (!(reply = dbus_message_new_method_return(message)))
961                         goto oom;
962
963                 m->exit_code = MANAGER_EXIT;
964
965         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
966
967                 if (m->running_as != MANAGER_SYSTEM) {
968                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
969                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
970                 }
971
972                 if (!(reply = dbus_message_new_method_return(message)))
973                         goto oom;
974
975                 m->exit_code = MANAGER_REBOOT;
976
977         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
978
979                 if (m->running_as != MANAGER_SYSTEM) {
980                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
981                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
982                 }
983
984                 if (!(reply = dbus_message_new_method_return(message)))
985                         goto oom;
986
987                 m->exit_code = MANAGER_POWEROFF;
988
989         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
990
991                 if (m->running_as != MANAGER_SYSTEM) {
992                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
993                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
994                 }
995
996                 if (!(reply = dbus_message_new_method_return(message)))
997                         goto oom;
998
999                 m->exit_code = MANAGER_HALT;
1000
1001         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
1002
1003                 if (m->running_as != MANAGER_SYSTEM) {
1004                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
1005                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1006                 }
1007
1008                 if (!(reply = dbus_message_new_method_return(message)))
1009                         goto oom;
1010
1011                 m->exit_code = MANAGER_KEXEC;
1012
1013         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
1014                 char **l = NULL, **e = NULL;
1015
1016                 if ((r = bus_parse_strv(message, &l)) < 0) {
1017                         if (r == -ENOMEM)
1018                                 goto oom;
1019
1020                         return bus_send_error_reply(connection, message, NULL, r);
1021                 }
1022
1023                 e = strv_env_merge(2, m->environment, l);
1024                 strv_free(l);
1025
1026                 if (!e)
1027                         goto oom;
1028
1029                 if (!(reply = dbus_message_new_method_return(message))) {
1030                         strv_free(e);
1031                         goto oom;
1032                 }
1033
1034                 strv_free(m->environment);
1035                 m->environment = e;
1036
1037         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1038                 char **l = NULL, **e = NULL;
1039
1040                 if ((r = bus_parse_strv(message, &l)) < 0) {
1041                         if (r == -ENOMEM)
1042                                 goto oom;
1043
1044                         return bus_send_error_reply(connection, message, NULL, r);
1045                 }
1046
1047                 e = strv_env_delete(m->environment, 1, l);
1048                 strv_free(l);
1049
1050                 if (!e)
1051                         goto oom;
1052
1053                 if (!(reply = dbus_message_new_method_return(message))) {
1054                         strv_free(e);
1055                         goto oom;
1056                 }
1057
1058                 strv_free(m->environment);
1059                 m->environment = e;
1060
1061         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment")) {
1062                 char **l_set = NULL, **l_unset = NULL, **e = NULL, **f = NULL;
1063                 DBusMessageIter iter;
1064
1065                 if (!dbus_message_iter_init(message, &iter))
1066                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1067
1068                 r = bus_parse_strv_iter(&iter, &l_unset);
1069                 if (r < 0) {
1070                         if (r == -ENOMEM)
1071                                 goto oom;
1072
1073                         return bus_send_error_reply(connection, message, NULL, r);
1074                 }
1075
1076                 if (!dbus_message_iter_next(&iter)) {
1077                         strv_free(l_unset);
1078                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1079                 }
1080
1081                 r = bus_parse_strv_iter(&iter, &l_set);
1082                 if (r < 0) {
1083                         strv_free(l_unset);
1084                         if (r == -ENOMEM)
1085                                 goto oom;
1086
1087                         return bus_send_error_reply(connection, message, NULL, r);
1088                 }
1089
1090                 e = strv_env_delete(m->environment, 1, l_unset);
1091                 strv_free(l_unset);
1092
1093                 if (!e) {
1094                         strv_free(l_set);
1095                         goto oom;
1096                 }
1097
1098                 f = strv_env_merge(2, e, l_set);
1099                 strv_free(l_set);
1100                 strv_free(e);
1101
1102                 if (!f)
1103                         goto oom;
1104
1105                 if (!(reply = dbus_message_new_method_return(message))) {
1106                         strv_free(f);
1107                         goto oom;
1108                 }
1109
1110                 strv_free(m->environment);
1111                 m->environment = f;
1112
1113         } else
1114                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1115
1116         if (job_type != _JOB_TYPE_INVALID) {
1117                 const char *name, *smode, *old_name = NULL;
1118                 JobMode mode;
1119                 Job *j;
1120                 Unit *u;
1121                 bool b;
1122
1123                 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1124                         b = dbus_message_get_args(
1125                                         message,
1126                                         &error,
1127                                         DBUS_TYPE_STRING, &old_name,
1128                                         DBUS_TYPE_STRING, &name,
1129                                         DBUS_TYPE_STRING, &smode,
1130                                         DBUS_TYPE_INVALID);
1131                 else
1132                         b = dbus_message_get_args(
1133                                         message,
1134                                         &error,
1135                                         DBUS_TYPE_STRING, &name,
1136                                         DBUS_TYPE_STRING, &smode,
1137                                         DBUS_TYPE_INVALID);
1138
1139                 if (!b)
1140                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1141
1142                 if (old_name)
1143                         if (!(u = manager_get_unit(m, old_name)) ||
1144                             !u->meta.job ||
1145                             u->meta.job->type != JOB_START) {
1146                                 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1147                                 return bus_send_error_reply(connection, message, &error, -ENOENT);
1148                         }
1149
1150
1151                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1152                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1153                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1154                 }
1155
1156                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1157                         return bus_send_error_reply(connection, message, &error, r);
1158
1159                 if (reload_if_possible && unit_can_reload(u)) {
1160                         if (job_type == JOB_RESTART)
1161                                 job_type = JOB_RELOAD_OR_START;
1162                         else if (job_type == JOB_TRY_RESTART)
1163                                 job_type = JOB_RELOAD;
1164                 }
1165
1166                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1167                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1168                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1169                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1170                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1171                         return bus_send_error_reply(connection, message, &error, -EPERM);
1172                 }
1173
1174                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1175                         return bus_send_error_reply(connection, message, &error, r);
1176
1177                 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1178                         goto oom;
1179
1180                 j->bus = connection;
1181
1182                 if (!(reply = dbus_message_new_method_return(message)))
1183                         goto oom;
1184
1185                 if (!(path = job_dbus_path(j)))
1186                         goto oom;
1187
1188                 if (!dbus_message_append_args(
1189                                     reply,
1190                                     DBUS_TYPE_OBJECT_PATH, &path,
1191                                     DBUS_TYPE_INVALID))
1192                         goto oom;
1193         }
1194
1195         if (reply) {
1196                 if (!dbus_connection_send(connection, reply, NULL))
1197                         goto oom;
1198
1199                 dbus_message_unref(reply);
1200         }
1201
1202         free(path);
1203
1204         return DBUS_HANDLER_RESULT_HANDLED;
1205
1206 oom:
1207         free(path);
1208
1209         if (reply)
1210                 dbus_message_unref(reply);
1211
1212         dbus_error_free(&error);
1213
1214         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1215 }
1216
1217 const DBusObjectPathVTable bus_manager_vtable = {
1218         .message_function = bus_manager_message_handler
1219 };