chiark / gitweb /
sd-login: build test code again
[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 #include "install.h"
33
34 #define BUS_MANAGER_INTERFACE_BEGIN                                     \
35         " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
36
37 #define BUS_MANAGER_INTERFACE_METHODS                                   \
38         "  <method name=\"GetUnit\">\n"                                 \
39         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
40         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
41         "  </method>\n"                                                 \
42         "  <method name=\"GetUnitByPID\">\n"                            \
43         "   <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n"          \
44         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
45         "  </method>\n"                                                 \
46         "  <method name=\"LoadUnit\">\n"                                \
47         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
48         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
49         "  </method>\n"                                                 \
50         "  <method name=\"StartUnit\">\n"                               \
51         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
52         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
53         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
54         "  </method>\n"                                                 \
55         "  <method name=\"StartUnitReplace\">\n"                        \
56         "   <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n"     \
57         "   <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n"     \
58         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
59         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
60         "  </method>\n"                                                 \
61         "  <method name=\"StopUnit\">\n"                                \
62         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
63         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
64         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
65         "  </method>\n"                                                 \
66         "  <method name=\"ReloadUnit\">\n"                              \
67         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
68         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
69         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
70         "  </method>\n"                                                 \
71         "  <method name=\"RestartUnit\">\n"                             \
72         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
73         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
74         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
75         "  </method>\n"                                                 \
76         "  <method name=\"TryRestartUnit\">\n"                          \
77         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
78         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
79         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
80         "  </method>\n"                                                 \
81         "  <method name=\"ReloadOrRestartUnit\">\n"                     \
82         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
83         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
84         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
85         "  </method>\n"                                                 \
86         "  <method name=\"ReloadOrTryRestartUnit\">\n"                  \
87         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
88         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
89         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
90         "  </method>\n"                                                 \
91         "  <method name=\"KillUnit\">\n"                                \
92         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
93         "   <arg name=\"who\" type=\"s\" direction=\"in\"/>\n"          \
94         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
95         "   <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n"       \
96         "  </method>\n"                                                 \
97         "  <method name=\"ResetFailedUnit\">\n"                         \
98         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
99         "  </method>\n"                                                 \
100         "  <method name=\"GetJob\">\n"                                  \
101         "   <arg name=\"id\" type=\"u\" direction=\"in\"/>\n"           \
102         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
103         "  </method>\n"                                                 \
104         "  <method name=\"ClearJobs\"/>\n"                              \
105         "  <method name=\"ResetFailed\"/>\n"                            \
106         "  <method name=\"ListUnits\">\n"                               \
107         "   <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
108         "  </method>\n"                                                 \
109         "  <method name=\"ListJobs\">\n"                                \
110         "   <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
111         "  </method>\n"                                                 \
112         "  <method name=\"Subscribe\"/>\n"                              \
113         "  <method name=\"Unsubscribe\"/>\n"                            \
114         "  <method name=\"Dump\"/>\n"                                   \
115         "  <method name=\"CreateSnapshot\">\n"                          \
116         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
117         "   <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n"      \
118         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
119         "  </method>\n"                                                 \
120         "  <method name=\"Reload\"/>\n"                                 \
121         "  <method name=\"Reexecute\"/>\n"                              \
122         "  <method name=\"Exit\"/>\n"                                   \
123         "  <method name=\"Reboot\"/>\n"                                 \
124         "  <method name=\"PowerOff\"/>\n"                               \
125         "  <method name=\"Halt\"/>\n"                                   \
126         "  <method name=\"KExec\"/>\n"                                  \
127         "  <method name=\"SetEnvironment\">\n"                          \
128         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
129         "  </method>\n"                                                 \
130         "  <method name=\"UnsetEnvironment\">\n"                        \
131         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
132         "  </method>\n"                                                 \
133         "  <method name=\"UnsetAndSetEnvironment\">\n"                  \
134         "   <arg name=\"unset\" type=\"as\" direction=\"in\"/>\n"       \
135         "   <arg name=\"set\" type=\"as\" direction=\"in\"/>\n"         \
136         "  </method>\n"                                                 \
137         "  <method name=\"ListUnitFiles\">\n"                            \
138         "   <arg name=\"changes\" type=\"a(ss)\" direction=\"out\"/>\n" \
139         "  </method>\n"                                                 \
140         "  <method name=\"GetUnitFileState\">\n"                        \
141         "   <arg name=\"file\" type=\"s\" direction=\"in\"/>\n"         \
142         "   <arg name=\"state\" type=\"s\" direction=\"out\"/>\n"       \
143         "  </method>\n"                                                 \
144         "  <method name=\"EnableUnitFiles\">\n"                         \
145         "   <arg name=\"files\" type=\"as\" direction=\"in\"/>\n"       \
146         "   <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n"      \
147         "   <arg name=\"force\" type=\"b\" direction=\"in\"/>\n"        \
148         "   <arg name=\"carries_install_info\" type=\"b\" directrion=\"out\"/>\n" \
149         "   <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
150         "  </method>\n"                                                 \
151         "  <method name=\"DisableUnitFiles\">\n"                        \
152         "   <arg name=\"files\" type=\"as\" direction=\"in\"/>\n"       \
153         "   <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n"      \
154         "   <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
155         "  </method>\n"                                                 \
156         "  <method name=\"ReenableUnitFiles\">\n"                       \
157         "   <arg name=\"files\" type=\"as\" direction=\"in\"/>\n"       \
158         "   <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n"      \
159         "   <arg name=\"force\" type=\"b\" direction=\"in\"/>\n"        \
160         "   <arg name=\"carries_install_info\" type=\"b\" directrion=\"out\"/>\n" \
161         "   <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
162         "  </method>\n"                                                 \
163         "  <method name=\"LinkUnitFiles\">\n"                           \
164         "   <arg name=\"files\" type=\"as\" direction=\"in\"/>\n"       \
165         "   <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n"      \
166         "   <arg name=\"force\" type=\"b\" direction=\"in\"/>\n"        \
167         "   <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
168         "  </method>\n"                                                 \
169         "  <method name=\"PresetUnitFiles\">\n"                         \
170         "   <arg name=\"files\" type=\"as\" direction=\"in\"/>\n"       \
171         "   <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n"      \
172         "   <arg name=\"force\" type=\"b\" direction=\"in\"/>\n"        \
173         "   <arg name=\"carries_install_info\" type=\"b\" directrion=\"out\"/>\n" \
174         "   <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
175         "  </method>\n"                                                 \
176         "  <method name=\"MaskUnitFiles\">\n"                           \
177         "   <arg name=\"files\" type=\"as\" direction=\"in\"/>\n"       \
178         "   <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n"      \
179         "   <arg name=\"force\" type=\"b\" direction=\"in\"/>\n"        \
180         "   <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
181         "  </method>\n"                                                 \
182         "  <method name=\"UnmaskUnitFiles\">\n"                         \
183         "   <arg name=\"files\" type=\"as\" direction=\"in\"/>\n"       \
184         "   <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n"      \
185         "   <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
186         "  </method>\n"
187
188 #define BUS_MANAGER_INTERFACE_SIGNALS                                   \
189         "  <signal name=\"UnitNew\">\n"                                 \
190         "   <arg name=\"id\" type=\"s\"/>\n"                            \
191         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
192         "  </signal>\n"                                                 \
193         "  <signal name=\"UnitRemoved\">\n"                             \
194         "   <arg name=\"id\" type=\"s\"/>\n"                            \
195         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
196         "  </signal>\n"                                                 \
197         "  <signal name=\"JobNew\">\n"                                  \
198         "   <arg name=\"id\" type=\"u\"/>\n"                            \
199         "   <arg name=\"job\" type=\"o\"/>\n"                           \
200         "  </signal>\n"                                                 \
201         "  <signal name=\"JobRemoved\">\n"                              \
202         "   <arg name=\"id\" type=\"u\"/>\n"                            \
203         "   <arg name=\"job\" type=\"o\"/>\n"                           \
204         "   <arg name=\"result\" type=\"s\"/>\n"                        \
205         "  </signal>"                                                   \
206         "  <signal name=\"StartupFinished\">\n"                         \
207         "   <arg name=\"kernel\" type=\"t\"/>\n"                        \
208         "   <arg name=\"initrd\" type=\"t\"/>\n"                        \
209         "   <arg name=\"userspace\" type=\"t\"/>\n"                     \
210         "   <arg name=\"total\" type=\"t\"/>\n"                         \
211         "  </signal>"                                                   \
212         "  <signal name=\"UnitFilesChanged\"/>\n"
213
214 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL                        \
215         "  <property name=\"Version\" type=\"s\" access=\"read\"/>\n"   \
216         "  <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
217         "  <property name=\"Features\" type=\"s\" access=\"read\"/>\n"  \
218         "  <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n"   \
219         "  <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
220         "  <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
221         "  <property name=\"InitRDTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
222         "  <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
223         "  <property name=\"StartupTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
224         "  <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
225         "  <property name=\"FinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
226         "  <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n"  \
227         "  <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
228         "  <property name=\"NNames\" type=\"u\" access=\"read\"/>\n"    \
229         "  <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n"     \
230         "  <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
231         "  <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
232         "  <property name=\"Progress\" type=\"d\" access=\"read\"/>\n"  \
233         "  <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
234         "  <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
235         "  <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
236         "  <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
237         "  <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
238         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
239         "  <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
240         "  <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n"  \
241         "  <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
242         "  <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
243         "  <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
244
245 #ifdef HAVE_SYSV_COMPAT
246 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV                           \
247         "  <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
248         "  <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
249         "  <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
250 #else
251 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
252 #endif
253
254 #define BUS_MANAGER_INTERFACE_END                                       \
255         " </interface>\n"
256
257 #define BUS_MANAGER_INTERFACE                                           \
258         BUS_MANAGER_INTERFACE_BEGIN                                     \
259         BUS_MANAGER_INTERFACE_METHODS                                   \
260         BUS_MANAGER_INTERFACE_SIGNALS                                   \
261         BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL                        \
262         BUS_MANAGER_INTERFACE_PROPERTIES_SYSV                           \
263         BUS_MANAGER_INTERFACE_END
264
265 #define INTROSPECTION_BEGIN                                             \
266         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
267         "<node>\n"                                                      \
268         BUS_MANAGER_INTERFACE                                           \
269         BUS_PROPERTIES_INTERFACE                                        \
270         BUS_PEER_INTERFACE                                              \
271         BUS_INTROSPECTABLE_INTERFACE
272
273 #define INTROSPECTION_END                                               \
274         "</node>\n"
275
276 #define INTERFACES_LIST                              \
277         BUS_GENERIC_INTERFACES_LIST                  \
278         "org.freedesktop.systemd1.Manager\0"
279
280 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
281
282 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
283 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
284
285 static int bus_manager_append_tainted(DBusMessageIter *i, const char *property, void *data) {
286         const char *t;
287         Manager *m = data;
288         char buf[LINE_MAX] = "", *e = buf, *p = NULL;
289
290         assert(i);
291         assert(property);
292         assert(m);
293
294         if (m->taint_usr)
295                 e = stpcpy(e, "usr-separate-fs ");
296
297         if (readlink_malloc("/etc/mtab", &p) < 0)
298                 e = stpcpy(e, "etc-mtab-not-symlink ");
299         else
300                 free(p);
301
302         if (access("/proc/cgroups", F_OK) < 0)
303                 e = stpcpy(e, "cgroups-missing ");
304
305         t = strstrip(buf);
306
307         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
308                 return -ENOMEM;
309
310         return 0;
311 }
312
313 static int bus_manager_append_log_target(DBusMessageIter *i, const char *property, void *data) {
314         const char *t;
315
316         assert(i);
317         assert(property);
318
319         t = log_target_to_string(log_get_target());
320
321         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
322                 return -ENOMEM;
323
324         return 0;
325 }
326
327 static int bus_manager_set_log_target(DBusMessageIter *i, const char *property) {
328         const char *t;
329
330         assert(i);
331         assert(property);
332
333         dbus_message_iter_get_basic(i, &t);
334
335         return log_set_target_from_string(t);
336 }
337
338 static int bus_manager_append_log_level(DBusMessageIter *i, const char *property, void *data) {
339         const char *t;
340
341         assert(i);
342         assert(property);
343
344         t = log_level_to_string(log_get_max_level());
345
346         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
347                 return -ENOMEM;
348
349         return 0;
350 }
351
352 static int bus_manager_set_log_level(DBusMessageIter *i, const char *property) {
353         const char *t;
354
355         assert(i);
356         assert(property);
357
358         dbus_message_iter_get_basic(i, &t);
359
360         return log_set_max_level_from_string(t);
361 }
362
363 static int bus_manager_append_n_names(DBusMessageIter *i, const char *property, void *data) {
364         Manager *m = data;
365         uint32_t u;
366
367         assert(i);
368         assert(property);
369         assert(m);
370
371         u = hashmap_size(m->units);
372
373         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
374                 return -ENOMEM;
375
376         return 0;
377 }
378
379 static int bus_manager_append_n_jobs(DBusMessageIter *i, const char *property, void *data) {
380         Manager *m = data;
381         uint32_t u;
382
383         assert(i);
384         assert(property);
385         assert(m);
386
387         u = hashmap_size(m->jobs);
388
389         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
390                 return -ENOMEM;
391
392         return 0;
393 }
394
395 static int bus_manager_append_progress(DBusMessageIter *i, const char *property, void *data) {
396         double d;
397         Manager *m = data;
398
399         assert(i);
400         assert(property);
401         assert(m);
402
403         if (dual_timestamp_is_set(&m->finish_timestamp))
404                 d = 1.0;
405         else
406                 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
407
408         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
409                 return -ENOMEM;
410
411         return 0;
412 }
413
414 static const char *message_get_sender_with_fallback(DBusMessage *m) {
415         const char *s;
416
417         assert(m);
418
419         if ((s = dbus_message_get_sender(m)))
420                 return s;
421
422         /* When the message came in from a direct connection the
423          * message will have no sender. We fix that here. */
424
425         return ":no-sender";
426 }
427
428 static DBusMessage *message_from_file_changes(
429                 DBusMessage *m,
430                 UnitFileChange *changes,
431                 unsigned n_changes,
432                 int carries_install_info) {
433
434         DBusMessageIter iter, sub, sub2;
435         DBusMessage *reply;
436         unsigned i;
437
438         reply = dbus_message_new_method_return(m);
439         if (!reply)
440                 return NULL;
441
442         dbus_message_iter_init_append(reply, &iter);
443
444         if (carries_install_info >= 0) {
445                 dbus_bool_t b;
446
447                 b = carries_install_info;
448                 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b))
449                         goto oom;
450         }
451
452         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sss)", &sub))
453                 goto oom;
454
455         for (i = 0; i < n_changes; i++) {
456                 const char *type, *path, *source;
457
458                 type = unit_file_change_type_to_string(changes[i].type);
459                 path = strempty(changes[i].path);
460                 source = strempty(changes[i].source);
461
462                 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
463                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
464                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &path) ||
465                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &source) ||
466                     !dbus_message_iter_close_container(&sub, &sub2))
467                         goto oom;
468         }
469
470         if (!dbus_message_iter_close_container(&iter, &sub))
471                 goto oom;
472
473         return reply;
474
475 oom:
476         dbus_message_unref(reply);
477         return NULL;
478 }
479
480 static int bus_manager_send_unit_files_changed(Manager *m) {
481         DBusMessage *s;
482         int r;
483
484         s = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitFilesChanged");
485         if (!s)
486                 return -ENOMEM;
487
488         r = bus_broadcast(m, s);
489         dbus_message_unref(s);
490
491         return r;
492 }
493
494 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
495         Manager *m = data;
496
497         const BusProperty properties[] = {
498                 { "org.freedesktop.systemd1.Manager", "Version",       bus_property_append_string,    "s",  PACKAGE_STRING     },
499                 { "org.freedesktop.systemd1.Manager", "Distribution",  bus_property_append_string,    "s",  DISTRIBUTION       },
500                 { "org.freedesktop.systemd1.Manager", "Features",      bus_property_append_string,    "s",  SYSTEMD_FEATURES   },
501                 { "org.freedesktop.systemd1.Manager", "RunningAs",     bus_manager_append_running_as, "s",  &m->running_as     },
502                 { "org.freedesktop.systemd1.Manager", "Tainted",       bus_manager_append_tainted,    "s",  m                  },
503                 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64,  "t",  &m->initrd_timestamp.realtime },
504                 { "org.freedesktop.systemd1.Manager", "InitRDTimestampMonotonic", bus_property_append_uint64, "t", &m->initrd_timestamp.monotonic },
505                 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t",  &m->startup_timestamp.realtime },
506                 { "org.freedesktop.systemd1.Manager", "StartupTimestampMonotonic", bus_property_append_uint64, "t", &m->startup_timestamp.monotonic },
507                 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64,  "t",  &m->finish_timestamp.realtime },
508                 { "org.freedesktop.systemd1.Manager", "FinishTimestampMonotonic", bus_property_append_uint64, "t",&m->finish_timestamp.monotonic },
509                 { "org.freedesktop.systemd1.Manager", "LogLevel",      bus_manager_append_log_level,  "s",  m, bus_manager_set_log_level },
510                 { "org.freedesktop.systemd1.Manager", "LogTarget",     bus_manager_append_log_target, "s",  m, bus_manager_set_log_target },
511                 { "org.freedesktop.systemd1.Manager", "NNames",        bus_manager_append_n_names,    "u",  m                  },
512                 { "org.freedesktop.systemd1.Manager", "NJobs",         bus_manager_append_n_jobs,     "u",  m                  },
513                 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32,    "u",  &m->n_installed_jobs },
514                 { "org.freedesktop.systemd1.Manager", "NFailedJobs",   bus_property_append_uint32,    "u",  &m->n_failed_jobs  },
515                 { "org.freedesktop.systemd1.Manager", "Progress",      bus_manager_append_progress,   "d",  m                  },
516                 { "org.freedesktop.systemd1.Manager", "Environment",   bus_property_append_strv,      "as", m->environment     },
517                 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn",  bus_property_append_bool,      "b",  &m->confirm_spawn  },
518                 { "org.freedesktop.systemd1.Manager", "ShowStatus",    bus_property_append_bool,      "b",  &m->show_status    },
519                 { "org.freedesktop.systemd1.Manager", "UnitPath",      bus_property_append_strv,      "as", m->lookup_paths.unit_path },
520                 { "org.freedesktop.systemd1.Manager", "NotifySocket",  bus_property_append_string,    "s",  m->notify_socket   },
521                 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
522                 { "org.freedesktop.systemd1.Manager", "MountAuto",     bus_property_append_bool,      "b",  &m->mount_auto     },
523                 { "org.freedesktop.systemd1.Manager", "SwapAuto",      bus_property_append_bool,      "b",  &m->swap_auto      },
524                 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
525                 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output },
526                 { "org.freedesktop.systemd1.Manager", "DefaultStandardError",  bus_manager_append_exec_output, "s", &m->default_std_error  },
527 #ifdef HAVE_SYSV_COMPAT
528                 { "org.freedesktop.systemd1.Manager", "SysVConsole",   bus_property_append_bool,      "b",  &m->sysv_console   },
529                 { "org.freedesktop.systemd1.Manager", "SysVInitPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvinit_path },
530                 { "org.freedesktop.systemd1.Manager", "SysVRcndPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvrcnd_path },
531 #endif
532                 { NULL, NULL, NULL, NULL, NULL }
533         };
534
535         int r;
536         DBusError error;
537         DBusMessage *reply = NULL;
538         char * path = NULL;
539         JobType job_type = _JOB_TYPE_INVALID;
540         bool reload_if_possible = false;
541         const char *member;
542
543         assert(connection);
544         assert(message);
545         assert(m);
546
547         dbus_error_init(&error);
548
549         member = dbus_message_get_member(message);
550
551         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
552                 const char *name;
553                 Unit *u;
554
555                 if (!dbus_message_get_args(
556                                     message,
557                                     &error,
558                                     DBUS_TYPE_STRING, &name,
559                                     DBUS_TYPE_INVALID))
560                         return bus_send_error_reply(connection, message, &error, -EINVAL);
561
562                 if (!(u = manager_get_unit(m, name))) {
563                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
564                         return bus_send_error_reply(connection, message, &error, -ENOENT);
565                 }
566
567                 if (!(reply = dbus_message_new_method_return(message)))
568                         goto oom;
569
570                 if (!(path = unit_dbus_path(u)))
571                         goto oom;
572
573                 if (!dbus_message_append_args(
574                                     reply,
575                                     DBUS_TYPE_OBJECT_PATH, &path,
576                                     DBUS_TYPE_INVALID))
577                         goto oom;
578         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
579                 Unit *u;
580                 uint32_t pid;
581
582                 if (!dbus_message_get_args(
583                                     message,
584                                     &error,
585                                     DBUS_TYPE_UINT32, &pid,
586                                     DBUS_TYPE_INVALID))
587                         return bus_send_error_reply(connection, message, &error, -EINVAL);
588
589                 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
590                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
591                         return bus_send_error_reply(connection, message, &error, -ENOENT);
592                 }
593
594                 if (!(reply = dbus_message_new_method_return(message)))
595                         goto oom;
596
597                 if (!(path = unit_dbus_path(u)))
598                         goto oom;
599
600                 if (!dbus_message_append_args(
601                                     reply,
602                                     DBUS_TYPE_OBJECT_PATH, &path,
603                                     DBUS_TYPE_INVALID))
604                         goto oom;
605         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
606                 const char *name;
607                 Unit *u;
608
609                 if (!dbus_message_get_args(
610                                     message,
611                                     &error,
612                                     DBUS_TYPE_STRING, &name,
613                                     DBUS_TYPE_INVALID))
614                         return bus_send_error_reply(connection, message, &error, -EINVAL);
615
616                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
617                         return bus_send_error_reply(connection, message, &error, r);
618
619                 if (!(reply = dbus_message_new_method_return(message)))
620                         goto oom;
621
622                 if (!(path = unit_dbus_path(u)))
623                         goto oom;
624
625                 if (!dbus_message_append_args(
626                                     reply,
627                                     DBUS_TYPE_OBJECT_PATH, &path,
628                                     DBUS_TYPE_INVALID))
629                         goto oom;
630
631         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
632                 job_type = JOB_START;
633         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
634                 job_type = JOB_START;
635         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
636                 job_type = JOB_STOP;
637         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
638                 job_type = JOB_RELOAD;
639         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
640                 job_type = JOB_RESTART;
641         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
642                 job_type = JOB_TRY_RESTART;
643         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
644                 reload_if_possible = true;
645                 job_type = JOB_RESTART;
646         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
647                 reload_if_possible = true;
648                 job_type = JOB_TRY_RESTART;
649         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
650                 const char *name, *swho, *smode;
651                 int32_t signo;
652                 Unit *u;
653                 KillMode mode;
654                 KillWho who;
655
656                 if (!dbus_message_get_args(
657                                     message,
658                                     &error,
659                                     DBUS_TYPE_STRING, &name,
660                                     DBUS_TYPE_STRING, &swho,
661                                     DBUS_TYPE_STRING, &smode,
662                                     DBUS_TYPE_INT32, &signo,
663                                     DBUS_TYPE_INVALID))
664                         return bus_send_error_reply(connection, message, &error, -EINVAL);
665
666                 if (isempty(swho))
667                         who = KILL_ALL;
668                 else {
669                         who = kill_who_from_string(swho);
670                         if (who < 0)
671                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
672                 }
673
674                 if (isempty(smode))
675                         mode = KILL_CONTROL_GROUP;
676                 else {
677                         mode = kill_mode_from_string(smode);
678                         if (mode < 0)
679                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
680                 }
681
682                 if (signo <= 0 || signo >= _NSIG)
683                         return bus_send_error_reply(connection, message, &error, -EINVAL);
684
685                 if (!(u = manager_get_unit(m, name))) {
686                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
687                         return bus_send_error_reply(connection, message, &error, -ENOENT);
688                 }
689
690                 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
691                         return bus_send_error_reply(connection, message, &error, r);
692
693                 if (!(reply = dbus_message_new_method_return(message)))
694                         goto oom;
695
696         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
697                 uint32_t id;
698                 Job *j;
699
700                 if (!dbus_message_get_args(
701                                     message,
702                                     &error,
703                                     DBUS_TYPE_UINT32, &id,
704                                     DBUS_TYPE_INVALID))
705                         return bus_send_error_reply(connection, message, &error, -EINVAL);
706
707                 if (!(j = manager_get_job(m, id))) {
708                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
709                         return bus_send_error_reply(connection, message, &error, -ENOENT);
710                 }
711
712                 if (!(reply = dbus_message_new_method_return(message)))
713                         goto oom;
714
715                 if (!(path = job_dbus_path(j)))
716                         goto oom;
717
718                 if (!dbus_message_append_args(
719                                     reply,
720                                     DBUS_TYPE_OBJECT_PATH, &path,
721                                     DBUS_TYPE_INVALID))
722                         goto oom;
723
724         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
725
726                 manager_clear_jobs(m);
727
728                 if (!(reply = dbus_message_new_method_return(message)))
729                         goto oom;
730
731         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
732
733                 manager_reset_failed(m);
734
735                 if (!(reply = dbus_message_new_method_return(message)))
736                         goto oom;
737
738         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
739                 const char *name;
740                 Unit *u;
741
742                 if (!dbus_message_get_args(
743                                     message,
744                                     &error,
745                                     DBUS_TYPE_STRING, &name,
746                                     DBUS_TYPE_INVALID))
747                         return bus_send_error_reply(connection, message, &error, -EINVAL);
748
749                 if (!(u = manager_get_unit(m, name))) {
750                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
751                         return bus_send_error_reply(connection, message, &error, -ENOENT);
752                 }
753
754                 unit_reset_failed(u);
755
756                 if (!(reply = dbus_message_new_method_return(message)))
757                         goto oom;
758
759         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
760                 DBusMessageIter iter, sub;
761                 Iterator i;
762                 Unit *u;
763                 const char *k;
764
765                 if (!(reply = dbus_message_new_method_return(message)))
766                         goto oom;
767
768                 dbus_message_iter_init_append(reply, &iter);
769
770                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
771                         goto oom;
772
773                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
774                         char *u_path, *j_path;
775                         const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
776                         DBusMessageIter sub2;
777                         uint32_t job_id;
778                         Unit *f;
779
780                         if (k != u->meta.id)
781                                 continue;
782
783                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
784                                 goto oom;
785
786                         description = unit_description(u);
787                         load_state = unit_load_state_to_string(u->meta.load_state);
788                         active_state = unit_active_state_to_string(unit_active_state(u));
789                         sub_state = unit_sub_state_to_string(u);
790
791                         f = unit_following(u);
792                         following = f ? f->meta.id : "";
793
794                         if (!(u_path = unit_dbus_path(u)))
795                                 goto oom;
796
797                         if (u->meta.job) {
798                                 job_id = (uint32_t) u->meta.job->id;
799
800                                 if (!(j_path = job_dbus_path(u->meta.job))) {
801                                         free(u_path);
802                                         goto oom;
803                                 }
804
805                                 sjob_type = job_type_to_string(u->meta.job->type);
806                         } else {
807                                 job_id = 0;
808                                 j_path = u_path;
809                                 sjob_type = "";
810                         }
811
812                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
813                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
814                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
815                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
816                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
817                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
818                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
819                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
820                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
821                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
822                                 free(u_path);
823                                 if (u->meta.job)
824                                         free(j_path);
825                                 goto oom;
826                         }
827
828                         free(u_path);
829                         if (u->meta.job)
830                                 free(j_path);
831
832                         if (!dbus_message_iter_close_container(&sub, &sub2))
833                                 goto oom;
834                 }
835
836                 if (!dbus_message_iter_close_container(&iter, &sub))
837                         goto oom;
838
839         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
840                 DBusMessageIter iter, sub;
841                 Iterator i;
842                 Job *j;
843
844                 if (!(reply = dbus_message_new_method_return(message)))
845                         goto oom;
846
847                 dbus_message_iter_init_append(reply, &iter);
848
849                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
850                         goto oom;
851
852                 HASHMAP_FOREACH(j, m->jobs, i) {
853                         char *u_path, *j_path;
854                         const char *state, *type;
855                         uint32_t id;
856                         DBusMessageIter sub2;
857
858                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
859                                 goto oom;
860
861                         id = (uint32_t) j->id;
862                         state = job_state_to_string(j->state);
863                         type = job_type_to_string(j->type);
864
865                         if (!(j_path = job_dbus_path(j)))
866                                 goto oom;
867
868                         if (!(u_path = unit_dbus_path(j->unit))) {
869                                 free(j_path);
870                                 goto oom;
871                         }
872
873                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
874                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
875                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
876                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
877                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
878                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
879                                 free(j_path);
880                                 free(u_path);
881                                 goto oom;
882                         }
883
884                         free(j_path);
885                         free(u_path);
886
887                         if (!dbus_message_iter_close_container(&sub, &sub2))
888                                 goto oom;
889                 }
890
891                 if (!dbus_message_iter_close_container(&iter, &sub))
892                         goto oom;
893
894         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
895                 char *client;
896                 Set *s;
897
898                 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
899                         if (!(s = set_new(string_hash_func, string_compare_func)))
900                                 goto oom;
901
902                         if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
903                                 set_free(s);
904                                 goto oom;
905                         }
906                 }
907
908                 if (!(client = strdup(message_get_sender_with_fallback(message))))
909                         goto oom;
910
911                 if ((r = set_put(s, client)) < 0) {
912                         free(client);
913                         return bus_send_error_reply(connection, message, NULL, r);
914                 }
915
916                 if (!(reply = dbus_message_new_method_return(message)))
917                         goto oom;
918
919         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
920                 char *client;
921
922                 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
923                         dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
924                         return bus_send_error_reply(connection, message, &error, -ENOENT);
925                 }
926
927                 free(client);
928
929                 if (!(reply = dbus_message_new_method_return(message)))
930                         goto oom;
931
932         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
933                 FILE *f;
934                 char *dump = NULL;
935                 size_t size;
936
937                 if (!(reply = dbus_message_new_method_return(message)))
938                         goto oom;
939
940                 if (!(f = open_memstream(&dump, &size)))
941                         goto oom;
942
943                 manager_dump_units(m, f, NULL);
944                 manager_dump_jobs(m, f, NULL);
945
946                 if (ferror(f)) {
947                         fclose(f);
948                         free(dump);
949                         goto oom;
950                 }
951
952                 fclose(f);
953
954                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
955                         free(dump);
956                         goto oom;
957                 }
958
959                 free(dump);
960         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
961                 const char *name;
962                 dbus_bool_t cleanup;
963                 Snapshot *s;
964
965                 if (!dbus_message_get_args(
966                                     message,
967                                     &error,
968                                     DBUS_TYPE_STRING, &name,
969                                     DBUS_TYPE_BOOLEAN, &cleanup,
970                                     DBUS_TYPE_INVALID))
971                         return bus_send_error_reply(connection, message, &error, -EINVAL);
972
973                 if (name && name[0] == 0)
974                         name = NULL;
975
976                 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
977                         return bus_send_error_reply(connection, message, &error, r);
978
979                 if (!(reply = dbus_message_new_method_return(message)))
980                         goto oom;
981
982                 if (!(path = unit_dbus_path(UNIT(s))))
983                         goto oom;
984
985                 if (!dbus_message_append_args(
986                                     reply,
987                                     DBUS_TYPE_OBJECT_PATH, &path,
988                                     DBUS_TYPE_INVALID))
989                         goto oom;
990
991         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
992                 char *introspection = NULL;
993                 FILE *f;
994                 Iterator i;
995                 Unit *u;
996                 Job *j;
997                 const char *k;
998                 size_t size;
999
1000                 if (!(reply = dbus_message_new_method_return(message)))
1001                         goto oom;
1002
1003                 /* We roll our own introspection code here, instead of
1004                  * relying on bus_default_message_handler() because we
1005                  * need to generate our introspection string
1006                  * dynamically. */
1007
1008                 if (!(f = open_memstream(&introspection, &size)))
1009                         goto oom;
1010
1011                 fputs(INTROSPECTION_BEGIN, f);
1012
1013                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1014                         char *p;
1015
1016                         if (k != u->meta.id)
1017                                 continue;
1018
1019                         if (!(p = bus_path_escape(k))) {
1020                                 fclose(f);
1021                                 free(introspection);
1022                                 goto oom;
1023                         }
1024
1025                         fprintf(f, "<node name=\"unit/%s\"/>", p);
1026                         free(p);
1027                 }
1028
1029                 HASHMAP_FOREACH(j, m->jobs, i)
1030                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
1031
1032                 fputs(INTROSPECTION_END, f);
1033
1034                 if (ferror(f)) {
1035                         fclose(f);
1036                         free(introspection);
1037                         goto oom;
1038                 }
1039
1040                 fclose(f);
1041
1042                 if (!introspection)
1043                         goto oom;
1044
1045                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1046                         free(introspection);
1047                         goto oom;
1048                 }
1049
1050                 free(introspection);
1051
1052         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
1053
1054                 assert(!m->queued_message);
1055
1056                 /* Instead of sending the reply back right away, we
1057                  * just remember that we need to and then send it
1058                  * after the reload is finished. That way the caller
1059                  * knows when the reload finished. */
1060
1061                 if (!(m->queued_message = dbus_message_new_method_return(message)))
1062                         goto oom;
1063
1064                 m->queued_message_connection = connection;
1065                 m->exit_code = MANAGER_RELOAD;
1066
1067         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
1068
1069                 /* We don't send a reply back here, the client should
1070                  * just wait for us disconnecting. */
1071
1072                 m->exit_code = MANAGER_REEXECUTE;
1073
1074         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
1075
1076                 if (m->running_as == MANAGER_SYSTEM) {
1077                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
1078                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1079                 }
1080
1081                 if (!(reply = dbus_message_new_method_return(message)))
1082                         goto oom;
1083
1084                 m->exit_code = MANAGER_EXIT;
1085
1086         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
1087
1088                 if (m->running_as != MANAGER_SYSTEM) {
1089                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
1090                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1091                 }
1092
1093                 if (!(reply = dbus_message_new_method_return(message)))
1094                         goto oom;
1095
1096                 m->exit_code = MANAGER_REBOOT;
1097
1098         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
1099
1100                 if (m->running_as != MANAGER_SYSTEM) {
1101                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
1102                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1103                 }
1104
1105                 if (!(reply = dbus_message_new_method_return(message)))
1106                         goto oom;
1107
1108                 m->exit_code = MANAGER_POWEROFF;
1109
1110         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
1111
1112                 if (m->running_as != MANAGER_SYSTEM) {
1113                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
1114                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1115                 }
1116
1117                 if (!(reply = dbus_message_new_method_return(message)))
1118                         goto oom;
1119
1120                 m->exit_code = MANAGER_HALT;
1121
1122         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
1123
1124                 if (m->running_as != MANAGER_SYSTEM) {
1125                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
1126                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1127                 }
1128
1129                 if (!(reply = dbus_message_new_method_return(message)))
1130                         goto oom;
1131
1132                 m->exit_code = MANAGER_KEXEC;
1133
1134         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
1135                 char **l = NULL, **e = NULL;
1136
1137                 if ((r = bus_parse_strv(message, &l)) < 0) {
1138                         if (r == -ENOMEM)
1139                                 goto oom;
1140
1141                         return bus_send_error_reply(connection, message, NULL, r);
1142                 }
1143
1144                 e = strv_env_merge(2, m->environment, l);
1145                 strv_free(l);
1146
1147                 if (!e)
1148                         goto oom;
1149
1150                 if (!(reply = dbus_message_new_method_return(message))) {
1151                         strv_free(e);
1152                         goto oom;
1153                 }
1154
1155                 strv_free(m->environment);
1156                 m->environment = e;
1157
1158         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1159                 char **l = NULL, **e = NULL;
1160
1161                 if ((r = bus_parse_strv(message, &l)) < 0) {
1162                         if (r == -ENOMEM)
1163                                 goto oom;
1164
1165                         return bus_send_error_reply(connection, message, NULL, r);
1166                 }
1167
1168                 e = strv_env_delete(m->environment, 1, l);
1169                 strv_free(l);
1170
1171                 if (!e)
1172                         goto oom;
1173
1174                 if (!(reply = dbus_message_new_method_return(message))) {
1175                         strv_free(e);
1176                         goto oom;
1177                 }
1178
1179                 strv_free(m->environment);
1180                 m->environment = e;
1181
1182         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment")) {
1183                 char **l_set = NULL, **l_unset = NULL, **e = NULL, **f = NULL;
1184                 DBusMessageIter iter;
1185
1186                 if (!dbus_message_iter_init(message, &iter))
1187                         goto oom;
1188
1189                 r = bus_parse_strv_iter(&iter, &l_unset);
1190                 if (r < 0) {
1191                         if (r == -ENOMEM)
1192                                 goto oom;
1193
1194                         return bus_send_error_reply(connection, message, NULL, r);
1195                 }
1196
1197                 if (!dbus_message_iter_next(&iter)) {
1198                         strv_free(l_unset);
1199                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1200                 }
1201
1202                 r = bus_parse_strv_iter(&iter, &l_set);
1203                 if (r < 0) {
1204                         strv_free(l_unset);
1205                         if (r == -ENOMEM)
1206                                 goto oom;
1207
1208                         return bus_send_error_reply(connection, message, NULL, r);
1209                 }
1210
1211                 e = strv_env_delete(m->environment, 1, l_unset);
1212                 strv_free(l_unset);
1213
1214                 if (!e) {
1215                         strv_free(l_set);
1216                         goto oom;
1217                 }
1218
1219                 f = strv_env_merge(2, e, l_set);
1220                 strv_free(l_set);
1221                 strv_free(e);
1222
1223                 if (!f)
1224                         goto oom;
1225
1226                 if (!(reply = dbus_message_new_method_return(message))) {
1227                         strv_free(f);
1228                         goto oom;
1229                 }
1230
1231                 strv_free(m->environment);
1232                 m->environment = f;
1233         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnitFiles")) {
1234                 DBusMessageIter iter, sub, sub2;
1235                 Hashmap *h;
1236                 Iterator i;
1237                 UnitFileList *item;
1238
1239                 reply = dbus_message_new_method_return(message);
1240                 if (!reply)
1241                         goto oom;
1242
1243                 h = hashmap_new(string_hash_func, string_compare_func);
1244                 if (!h)
1245                         goto oom;
1246
1247                 r = unit_file_get_list(m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, h);
1248                 if (r < 0) {
1249                         unit_file_list_free(h);
1250                         dbus_message_unref(reply);
1251                         return bus_send_error_reply(connection, message, NULL, r);
1252                 }
1253
1254                 dbus_message_iter_init_append(reply, &iter);
1255
1256                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ss)", &sub)) {
1257                         unit_file_list_free(h);
1258                         goto oom;
1259                 }
1260
1261                 HASHMAP_FOREACH(item, h, i) {
1262                         const char *state;
1263
1264                         state = unit_file_state_to_string(item->state);
1265                         assert(state);
1266
1267                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
1268                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &item->path) ||
1269                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
1270                             !dbus_message_iter_close_container(&sub, &sub2)) {
1271                                 unit_file_list_free(h);
1272                                 goto oom;
1273                         }
1274                 }
1275
1276                 unit_file_list_free(h);
1277
1278                 if (!dbus_message_iter_close_container(&iter, &sub))
1279                         goto oom;
1280
1281         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitFileState")) {
1282                 const char *name;
1283                 UnitFileState state;
1284                 const char *s;
1285
1286                 if (!dbus_message_get_args(
1287                                     message,
1288                                     &error,
1289                                     DBUS_TYPE_STRING, &name,
1290                                     DBUS_TYPE_INVALID))
1291                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1292
1293                 state = unit_file_get_state(m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, name);
1294                 if (state < 0)
1295                         return bus_send_error_reply(connection, message, NULL, state);
1296
1297                 s = unit_file_state_to_string(state);
1298                 assert(s);
1299
1300                 reply = dbus_message_new_method_return(message);
1301                 if (!reply)
1302                         goto oom;
1303
1304                 if (!dbus_message_append_args(
1305                                     reply,
1306                                     DBUS_TYPE_STRING, &s,
1307                                     DBUS_TYPE_INVALID))
1308                         goto oom;
1309         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "EnableUnitFiles") ||
1310                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReenableUnitFiles") ||
1311                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LinkUnitFiles") ||
1312                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PresetUnitFiles") ||
1313                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "MaskUnitFiles")) {
1314
1315                 char **l = NULL;
1316                 DBusMessageIter iter;
1317                 UnitFileScope scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1318                 UnitFileChange *changes = NULL;
1319                 unsigned n_changes = 0;
1320                 dbus_bool_t runtime, force;
1321                 bool carries_install_info = -1;
1322
1323                 if (!dbus_message_iter_init(message, &iter))
1324                         goto oom;
1325
1326                 r = bus_parse_strv_iter(&iter, &l);
1327                 if (r < 0) {
1328                         if (r == -ENOMEM)
1329                                 goto oom;
1330
1331                         return bus_send_error_reply(connection, message, NULL, r);
1332                 }
1333
1334                 if (!dbus_message_iter_next(&iter) ||
1335                     bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, true) < 0 ||
1336                     bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &force, false) < 0) {
1337                         strv_free(l);
1338                         return bus_send_error_reply(connection, message, NULL, -EIO);
1339                 }
1340
1341                 if (streq(member, "EnableUnitFiles")) {
1342                         r = unit_file_enable(scope, runtime, NULL, l, force, &changes, &n_changes);
1343                         carries_install_info = r;
1344                 } else if (streq(member, "ReenableUnitFiles")) {
1345                         r = unit_file_reenable(scope, runtime, NULL, l, force, &changes, &n_changes);
1346                         carries_install_info = r;
1347                 } else if (streq(member, "LinkUnitFiles"))
1348                         r = unit_file_link(scope, runtime, NULL, l, force, &changes, &n_changes);
1349                 else if (streq(member, "PresetUnitFiles")) {
1350                         r = unit_file_preset(scope, runtime, NULL, l, force, &changes, &n_changes);
1351                         carries_install_info = r;
1352                 } else if (streq(member, "MaskUnitFiles"))
1353                         r = unit_file_mask(scope, runtime, NULL, l, force, &changes, &n_changes);
1354                 else
1355                         assert_not_reached("Uh? Wrong method");
1356
1357                 strv_free(l);
1358                 bus_manager_send_unit_files_changed(m);
1359
1360                 if (r < 0) {
1361                         unit_file_changes_free(changes, n_changes);
1362                         return bus_send_error_reply(connection, message, NULL, r);
1363                 }
1364
1365                 reply = message_from_file_changes(message, changes, n_changes, carries_install_info);
1366                 unit_file_changes_free(changes, n_changes);
1367
1368                 if (!reply)
1369                         goto oom;
1370
1371         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "DisableUnitFiles") ||
1372                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnmaskUnitFiles")) {
1373
1374                 char **l = NULL;
1375                 DBusMessageIter iter;
1376                 UnitFileScope scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1377                 UnitFileChange *changes = NULL;
1378                 unsigned n_changes = 0;
1379                 dbus_bool_t runtime;
1380
1381                 if (!dbus_message_iter_init(message, &iter))
1382                         goto oom;
1383
1384                 r = bus_parse_strv_iter(&iter, &l);
1385                 if (r < 0) {
1386                         if (r == -ENOMEM)
1387                                 goto oom;
1388
1389                         return bus_send_error_reply(connection, message, NULL, r);
1390                 }
1391
1392                 if (!dbus_message_iter_next(&iter) ||
1393                     bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, false) < 0) {
1394                         strv_free(l);
1395                         return bus_send_error_reply(connection, message, NULL, -EIO);
1396                 }
1397
1398                 if (streq(member, "DisableUnitFiles"))
1399                         r = unit_file_disable(scope, runtime, NULL, l, &changes, &n_changes);
1400                 else if (streq(member, "UnmaskUnitFiles"))
1401                         r = unit_file_unmask(scope, runtime, NULL, l, &changes, &n_changes);
1402                 else
1403                         assert_not_reached("Uh? Wrong method");
1404
1405                 strv_free(l);
1406                 bus_manager_send_unit_files_changed(m);
1407
1408                 if (r < 0) {
1409                         unit_file_changes_free(changes, n_changes);
1410                         return bus_send_error_reply(connection, message, NULL, r);
1411                 }
1412
1413                 reply = message_from_file_changes(message, changes, n_changes, -1);
1414                 unit_file_changes_free(changes, n_changes);
1415
1416                 if (!reply)
1417                         goto oom;
1418
1419         } else
1420                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1421
1422         if (job_type != _JOB_TYPE_INVALID) {
1423                 const char *name, *smode, *old_name = NULL;
1424                 JobMode mode;
1425                 Job *j;
1426                 Unit *u;
1427                 bool b;
1428
1429                 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1430                         b = dbus_message_get_args(
1431                                         message,
1432                                         &error,
1433                                         DBUS_TYPE_STRING, &old_name,
1434                                         DBUS_TYPE_STRING, &name,
1435                                         DBUS_TYPE_STRING, &smode,
1436                                         DBUS_TYPE_INVALID);
1437                 else
1438                         b = dbus_message_get_args(
1439                                         message,
1440                                         &error,
1441                                         DBUS_TYPE_STRING, &name,
1442                                         DBUS_TYPE_STRING, &smode,
1443                                         DBUS_TYPE_INVALID);
1444
1445                 if (!b)
1446                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1447
1448                 if (old_name)
1449                         if (!(u = manager_get_unit(m, old_name)) ||
1450                             !u->meta.job ||
1451                             u->meta.job->type != JOB_START) {
1452                                 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1453                                 return bus_send_error_reply(connection, message, &error, -ENOENT);
1454                         }
1455
1456
1457                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1458                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1459                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1460                 }
1461
1462                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1463                         return bus_send_error_reply(connection, message, &error, r);
1464
1465                 if (reload_if_possible && unit_can_reload(u)) {
1466                         if (job_type == JOB_RESTART)
1467                                 job_type = JOB_RELOAD_OR_START;
1468                         else if (job_type == JOB_TRY_RESTART)
1469                                 job_type = JOB_RELOAD;
1470                 }
1471
1472                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1473                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1474                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1475                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1476                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1477                         return bus_send_error_reply(connection, message, &error, -EPERM);
1478                 }
1479
1480                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1481                         return bus_send_error_reply(connection, message, &error, r);
1482
1483                 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1484                         goto oom;
1485
1486                 j->bus = connection;
1487
1488                 if (!(reply = dbus_message_new_method_return(message)))
1489                         goto oom;
1490
1491                 if (!(path = job_dbus_path(j)))
1492                         goto oom;
1493
1494                 if (!dbus_message_append_args(
1495                                     reply,
1496                                     DBUS_TYPE_OBJECT_PATH, &path,
1497                                     DBUS_TYPE_INVALID))
1498                         goto oom;
1499         }
1500
1501         if (reply) {
1502                 if (!dbus_connection_send(connection, reply, NULL))
1503                         goto oom;
1504
1505                 dbus_message_unref(reply);
1506         }
1507
1508         free(path);
1509
1510         return DBUS_HANDLER_RESULT_HANDLED;
1511
1512 oom:
1513         free(path);
1514
1515         if (reply)
1516                 dbus_message_unref(reply);
1517
1518         dbus_error_free(&error);
1519
1520         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1521 }
1522
1523 const DBusObjectPathVTable bus_manager_vtable = {
1524         .message_function = bus_manager_message_handler
1525 };