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