chiark / gitweb /
Remove a stray empty line
[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         assert(changes);
439
440         reply = dbus_message_new_method_return(m);
441         if (!reply)
442                 return NULL;
443
444         dbus_message_iter_init_append(reply, &iter);
445
446         if (carries_install_info >= 0) {
447                 dbus_bool_t b;
448
449                 b = carries_install_info;
450                 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b))
451                         goto oom;
452         }
453
454         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sss)", &sub))
455                 goto oom;
456
457         for (i = 0; i < n_changes; i++) {
458                 const char *type, *path, *source;
459
460                 type = unit_file_change_type_to_string(changes[i].type);
461                 path = strempty(changes[i].path);
462                 source = strempty(changes[i].source);
463
464                 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
465                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
466                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &path) ||
467                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &source) ||
468                     !dbus_message_iter_close_container(&sub, &sub2))
469                         goto oom;
470         }
471
472         if (!dbus_message_iter_close_container(&iter, &sub))
473                 goto oom;
474
475         return reply;
476
477 oom:
478         dbus_message_unref(reply);
479         return NULL;
480 }
481
482 static int bus_manager_send_unit_files_changed(Manager *m) {
483         DBusMessage *s;
484         int r;
485
486         s = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitFilesChanged");
487         if (!s)
488                 return -ENOMEM;
489
490         r = bus_broadcast(m, s);
491         dbus_message_unref(s);
492
493         return r;
494 }
495
496 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
497         Manager *m = data;
498
499         const BusProperty properties[] = {
500                 { "org.freedesktop.systemd1.Manager", "Version",       bus_property_append_string,    "s",  PACKAGE_STRING     },
501                 { "org.freedesktop.systemd1.Manager", "Distribution",  bus_property_append_string,    "s",  DISTRIBUTION       },
502                 { "org.freedesktop.systemd1.Manager", "Features",      bus_property_append_string,    "s",  SYSTEMD_FEATURES   },
503                 { "org.freedesktop.systemd1.Manager", "RunningAs",     bus_manager_append_running_as, "s",  &m->running_as     },
504                 { "org.freedesktop.systemd1.Manager", "Tainted",       bus_manager_append_tainted,    "s",  m                  },
505                 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64,  "t",  &m->initrd_timestamp.realtime },
506                 { "org.freedesktop.systemd1.Manager", "InitRDTimestampMonotonic", bus_property_append_uint64, "t", &m->initrd_timestamp.monotonic },
507                 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t",  &m->startup_timestamp.realtime },
508                 { "org.freedesktop.systemd1.Manager", "StartupTimestampMonotonic", bus_property_append_uint64, "t", &m->startup_timestamp.monotonic },
509                 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64,  "t",  &m->finish_timestamp.realtime },
510                 { "org.freedesktop.systemd1.Manager", "FinishTimestampMonotonic", bus_property_append_uint64, "t",&m->finish_timestamp.monotonic },
511                 { "org.freedesktop.systemd1.Manager", "LogLevel",      bus_manager_append_log_level,  "s",  m, bus_manager_set_log_level },
512                 { "org.freedesktop.systemd1.Manager", "LogTarget",     bus_manager_append_log_target, "s",  m, bus_manager_set_log_target },
513                 { "org.freedesktop.systemd1.Manager", "NNames",        bus_manager_append_n_names,    "u",  m                  },
514                 { "org.freedesktop.systemd1.Manager", "NJobs",         bus_manager_append_n_jobs,     "u",  m                  },
515                 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32,    "u",  &m->n_installed_jobs },
516                 { "org.freedesktop.systemd1.Manager", "NFailedJobs",   bus_property_append_uint32,    "u",  &m->n_failed_jobs  },
517                 { "org.freedesktop.systemd1.Manager", "Progress",      bus_manager_append_progress,   "d",  m                  },
518                 { "org.freedesktop.systemd1.Manager", "Environment",   bus_property_append_strv,      "as", m->environment     },
519                 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn",  bus_property_append_bool,      "b",  &m->confirm_spawn  },
520                 { "org.freedesktop.systemd1.Manager", "ShowStatus",    bus_property_append_bool,      "b",  &m->show_status    },
521                 { "org.freedesktop.systemd1.Manager", "UnitPath",      bus_property_append_strv,      "as", m->lookup_paths.unit_path },
522                 { "org.freedesktop.systemd1.Manager", "NotifySocket",  bus_property_append_string,    "s",  m->notify_socket   },
523                 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
524                 { "org.freedesktop.systemd1.Manager", "MountAuto",     bus_property_append_bool,      "b",  &m->mount_auto     },
525                 { "org.freedesktop.systemd1.Manager", "SwapAuto",      bus_property_append_bool,      "b",  &m->swap_auto      },
526                 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
527                 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output },
528                 { "org.freedesktop.systemd1.Manager", "DefaultStandardError",  bus_manager_append_exec_output, "s", &m->default_std_error  },
529 #ifdef HAVE_SYSV_COMPAT
530                 { "org.freedesktop.systemd1.Manager", "SysVConsole",   bus_property_append_bool,      "b",  &m->sysv_console   },
531                 { "org.freedesktop.systemd1.Manager", "SysVInitPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvinit_path },
532                 { "org.freedesktop.systemd1.Manager", "SysVRcndPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvrcnd_path },
533 #endif
534                 { NULL, NULL, NULL, NULL, NULL }
535         };
536
537         int r;
538         DBusError error;
539         DBusMessage *reply = NULL;
540         char * path = NULL;
541         JobType job_type = _JOB_TYPE_INVALID;
542         bool reload_if_possible = false;
543         const char *member;
544
545         assert(connection);
546         assert(message);
547         assert(m);
548
549         dbus_error_init(&error);
550
551         member = dbus_message_get_member(message);
552
553         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
554                 const char *name;
555                 Unit *u;
556
557                 if (!dbus_message_get_args(
558                                     message,
559                                     &error,
560                                     DBUS_TYPE_STRING, &name,
561                                     DBUS_TYPE_INVALID))
562                         return bus_send_error_reply(connection, message, &error, -EINVAL);
563
564                 if (!(u = manager_get_unit(m, name))) {
565                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
566                         return bus_send_error_reply(connection, message, &error, -ENOENT);
567                 }
568
569                 if (!(reply = dbus_message_new_method_return(message)))
570                         goto oom;
571
572                 if (!(path = unit_dbus_path(u)))
573                         goto oom;
574
575                 if (!dbus_message_append_args(
576                                     reply,
577                                     DBUS_TYPE_OBJECT_PATH, &path,
578                                     DBUS_TYPE_INVALID))
579                         goto oom;
580         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
581                 Unit *u;
582                 uint32_t pid;
583
584                 if (!dbus_message_get_args(
585                                     message,
586                                     &error,
587                                     DBUS_TYPE_UINT32, &pid,
588                                     DBUS_TYPE_INVALID))
589                         return bus_send_error_reply(connection, message, &error, -EINVAL);
590
591                 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
592                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
593                         return bus_send_error_reply(connection, message, &error, -ENOENT);
594                 }
595
596                 if (!(reply = dbus_message_new_method_return(message)))
597                         goto oom;
598
599                 if (!(path = unit_dbus_path(u)))
600                         goto oom;
601
602                 if (!dbus_message_append_args(
603                                     reply,
604                                     DBUS_TYPE_OBJECT_PATH, &path,
605                                     DBUS_TYPE_INVALID))
606                         goto oom;
607         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
608                 const char *name;
609                 Unit *u;
610
611                 if (!dbus_message_get_args(
612                                     message,
613                                     &error,
614                                     DBUS_TYPE_STRING, &name,
615                                     DBUS_TYPE_INVALID))
616                         return bus_send_error_reply(connection, message, &error, -EINVAL);
617
618                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
619                         return bus_send_error_reply(connection, message, &error, r);
620
621                 if (!(reply = dbus_message_new_method_return(message)))
622                         goto oom;
623
624                 if (!(path = unit_dbus_path(u)))
625                         goto oom;
626
627                 if (!dbus_message_append_args(
628                                     reply,
629                                     DBUS_TYPE_OBJECT_PATH, &path,
630                                     DBUS_TYPE_INVALID))
631                         goto oom;
632
633         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
634                 job_type = JOB_START;
635         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
636                 job_type = JOB_START;
637         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
638                 job_type = JOB_STOP;
639         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
640                 job_type = JOB_RELOAD;
641         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
642                 job_type = JOB_RESTART;
643         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
644                 job_type = JOB_TRY_RESTART;
645         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
646                 reload_if_possible = true;
647                 job_type = JOB_RESTART;
648         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
649                 reload_if_possible = true;
650                 job_type = JOB_TRY_RESTART;
651         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
652                 const char *name, *swho, *smode;
653                 int32_t signo;
654                 Unit *u;
655                 KillMode mode;
656                 KillWho who;
657
658                 if (!dbus_message_get_args(
659                                     message,
660                                     &error,
661                                     DBUS_TYPE_STRING, &name,
662                                     DBUS_TYPE_STRING, &swho,
663                                     DBUS_TYPE_STRING, &smode,
664                                     DBUS_TYPE_INT32, &signo,
665                                     DBUS_TYPE_INVALID))
666                         return bus_send_error_reply(connection, message, &error, -EINVAL);
667
668                 if (isempty(swho))
669                         who = KILL_ALL;
670                 else {
671                         who = kill_who_from_string(swho);
672                         if (who < 0)
673                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
674                 }
675
676                 if (isempty(smode))
677                         mode = KILL_CONTROL_GROUP;
678                 else {
679                         mode = kill_mode_from_string(smode);
680                         if (mode < 0)
681                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
682                 }
683
684                 if (signo <= 0 || signo >= _NSIG)
685                         return bus_send_error_reply(connection, message, &error, -EINVAL);
686
687                 if (!(u = manager_get_unit(m, name))) {
688                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
689                         return bus_send_error_reply(connection, message, &error, -ENOENT);
690                 }
691
692                 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
693                         return bus_send_error_reply(connection, message, &error, r);
694
695                 if (!(reply = dbus_message_new_method_return(message)))
696                         goto oom;
697
698         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
699                 uint32_t id;
700                 Job *j;
701
702                 if (!dbus_message_get_args(
703                                     message,
704                                     &error,
705                                     DBUS_TYPE_UINT32, &id,
706                                     DBUS_TYPE_INVALID))
707                         return bus_send_error_reply(connection, message, &error, -EINVAL);
708
709                 if (!(j = manager_get_job(m, id))) {
710                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
711                         return bus_send_error_reply(connection, message, &error, -ENOENT);
712                 }
713
714                 if (!(reply = dbus_message_new_method_return(message)))
715                         goto oom;
716
717                 if (!(path = job_dbus_path(j)))
718                         goto oom;
719
720                 if (!dbus_message_append_args(
721                                     reply,
722                                     DBUS_TYPE_OBJECT_PATH, &path,
723                                     DBUS_TYPE_INVALID))
724                         goto oom;
725
726         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
727
728                 manager_clear_jobs(m);
729
730                 if (!(reply = dbus_message_new_method_return(message)))
731                         goto oom;
732
733         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
734
735                 manager_reset_failed(m);
736
737                 if (!(reply = dbus_message_new_method_return(message)))
738                         goto oom;
739
740         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
741                 const char *name;
742                 Unit *u;
743
744                 if (!dbus_message_get_args(
745                                     message,
746                                     &error,
747                                     DBUS_TYPE_STRING, &name,
748                                     DBUS_TYPE_INVALID))
749                         return bus_send_error_reply(connection, message, &error, -EINVAL);
750
751                 if (!(u = manager_get_unit(m, name))) {
752                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
753                         return bus_send_error_reply(connection, message, &error, -ENOENT);
754                 }
755
756                 unit_reset_failed(u);
757
758                 if (!(reply = dbus_message_new_method_return(message)))
759                         goto oom;
760
761         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
762                 DBusMessageIter iter, sub;
763                 Iterator i;
764                 Unit *u;
765                 const char *k;
766
767                 if (!(reply = dbus_message_new_method_return(message)))
768                         goto oom;
769
770                 dbus_message_iter_init_append(reply, &iter);
771
772                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
773                         goto oom;
774
775                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
776                         char *u_path, *j_path;
777                         const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
778                         DBusMessageIter sub2;
779                         uint32_t job_id;
780                         Unit *f;
781
782                         if (k != u->meta.id)
783                                 continue;
784
785                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
786                                 goto oom;
787
788                         description = unit_description(u);
789                         load_state = unit_load_state_to_string(u->meta.load_state);
790                         active_state = unit_active_state_to_string(unit_active_state(u));
791                         sub_state = unit_sub_state_to_string(u);
792
793                         f = unit_following(u);
794                         following = f ? f->meta.id : "";
795
796                         if (!(u_path = unit_dbus_path(u)))
797                                 goto oom;
798
799                         if (u->meta.job) {
800                                 job_id = (uint32_t) u->meta.job->id;
801
802                                 if (!(j_path = job_dbus_path(u->meta.job))) {
803                                         free(u_path);
804                                         goto oom;
805                                 }
806
807                                 sjob_type = job_type_to_string(u->meta.job->type);
808                         } else {
809                                 job_id = 0;
810                                 j_path = u_path;
811                                 sjob_type = "";
812                         }
813
814                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
815                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
816                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
817                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
818                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
819                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
820                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
821                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
822                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
823                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
824                                 free(u_path);
825                                 if (u->meta.job)
826                                         free(j_path);
827                                 goto oom;
828                         }
829
830                         free(u_path);
831                         if (u->meta.job)
832                                 free(j_path);
833
834                         if (!dbus_message_iter_close_container(&sub, &sub2))
835                                 goto oom;
836                 }
837
838                 if (!dbus_message_iter_close_container(&iter, &sub))
839                         goto oom;
840
841         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
842                 DBusMessageIter iter, sub;
843                 Iterator i;
844                 Job *j;
845
846                 if (!(reply = dbus_message_new_method_return(message)))
847                         goto oom;
848
849                 dbus_message_iter_init_append(reply, &iter);
850
851                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
852                         goto oom;
853
854                 HASHMAP_FOREACH(j, m->jobs, i) {
855                         char *u_path, *j_path;
856                         const char *state, *type;
857                         uint32_t id;
858                         DBusMessageIter sub2;
859
860                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
861                                 goto oom;
862
863                         id = (uint32_t) j->id;
864                         state = job_state_to_string(j->state);
865                         type = job_type_to_string(j->type);
866
867                         if (!(j_path = job_dbus_path(j)))
868                                 goto oom;
869
870                         if (!(u_path = unit_dbus_path(j->unit))) {
871                                 free(j_path);
872                                 goto oom;
873                         }
874
875                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
876                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
877                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
878                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
879                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
880                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
881                                 free(j_path);
882                                 free(u_path);
883                                 goto oom;
884                         }
885
886                         free(j_path);
887                         free(u_path);
888
889                         if (!dbus_message_iter_close_container(&sub, &sub2))
890                                 goto oom;
891                 }
892
893                 if (!dbus_message_iter_close_container(&iter, &sub))
894                         goto oom;
895
896         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
897                 char *client;
898                 Set *s;
899
900                 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
901                         if (!(s = set_new(string_hash_func, string_compare_func)))
902                                 goto oom;
903
904                         if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
905                                 set_free(s);
906                                 goto oom;
907                         }
908                 }
909
910                 if (!(client = strdup(message_get_sender_with_fallback(message))))
911                         goto oom;
912
913                 if ((r = set_put(s, client)) < 0) {
914                         free(client);
915                         return bus_send_error_reply(connection, message, NULL, r);
916                 }
917
918                 if (!(reply = dbus_message_new_method_return(message)))
919                         goto oom;
920
921         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
922                 char *client;
923
924                 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
925                         dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
926                         return bus_send_error_reply(connection, message, &error, -ENOENT);
927                 }
928
929                 free(client);
930
931                 if (!(reply = dbus_message_new_method_return(message)))
932                         goto oom;
933
934         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
935                 FILE *f;
936                 char *dump = NULL;
937                 size_t size;
938
939                 if (!(reply = dbus_message_new_method_return(message)))
940                         goto oom;
941
942                 if (!(f = open_memstream(&dump, &size)))
943                         goto oom;
944
945                 manager_dump_units(m, f, NULL);
946                 manager_dump_jobs(m, f, NULL);
947
948                 if (ferror(f)) {
949                         fclose(f);
950                         free(dump);
951                         goto oom;
952                 }
953
954                 fclose(f);
955
956                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
957                         free(dump);
958                         goto oom;
959                 }
960
961                 free(dump);
962         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
963                 const char *name;
964                 dbus_bool_t cleanup;
965                 Snapshot *s;
966
967                 if (!dbus_message_get_args(
968                                     message,
969                                     &error,
970                                     DBUS_TYPE_STRING, &name,
971                                     DBUS_TYPE_BOOLEAN, &cleanup,
972                                     DBUS_TYPE_INVALID))
973                         return bus_send_error_reply(connection, message, &error, -EINVAL);
974
975                 if (name && name[0] == 0)
976                         name = NULL;
977
978                 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
979                         return bus_send_error_reply(connection, message, &error, r);
980
981                 if (!(reply = dbus_message_new_method_return(message)))
982                         goto oom;
983
984                 if (!(path = unit_dbus_path(UNIT(s))))
985                         goto oom;
986
987                 if (!dbus_message_append_args(
988                                     reply,
989                                     DBUS_TYPE_OBJECT_PATH, &path,
990                                     DBUS_TYPE_INVALID))
991                         goto oom;
992
993         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
994                 char *introspection = NULL;
995                 FILE *f;
996                 Iterator i;
997                 Unit *u;
998                 Job *j;
999                 const char *k;
1000                 size_t size;
1001
1002                 if (!(reply = dbus_message_new_method_return(message)))
1003                         goto oom;
1004
1005                 /* We roll our own introspection code here, instead of
1006                  * relying on bus_default_message_handler() because we
1007                  * need to generate our introspection string
1008                  * dynamically. */
1009
1010                 if (!(f = open_memstream(&introspection, &size)))
1011                         goto oom;
1012
1013                 fputs(INTROSPECTION_BEGIN, f);
1014
1015                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1016                         char *p;
1017
1018                         if (k != u->meta.id)
1019                                 continue;
1020
1021                         if (!(p = bus_path_escape(k))) {
1022                                 fclose(f);
1023                                 free(introspection);
1024                                 goto oom;
1025                         }
1026
1027                         fprintf(f, "<node name=\"unit/%s\"/>", p);
1028                         free(p);
1029                 }
1030
1031                 HASHMAP_FOREACH(j, m->jobs, i)
1032                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
1033
1034                 fputs(INTROSPECTION_END, f);
1035
1036                 if (ferror(f)) {
1037                         fclose(f);
1038                         free(introspection);
1039                         goto oom;
1040                 }
1041
1042                 fclose(f);
1043
1044                 if (!introspection)
1045                         goto oom;
1046
1047                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1048                         free(introspection);
1049                         goto oom;
1050                 }
1051
1052                 free(introspection);
1053
1054         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
1055
1056                 assert(!m->queued_message);
1057
1058                 /* Instead of sending the reply back right away, we
1059                  * just remember that we need to and then send it
1060                  * after the reload is finished. That way the caller
1061                  * knows when the reload finished. */
1062
1063                 if (!(m->queued_message = dbus_message_new_method_return(message)))
1064                         goto oom;
1065
1066                 m->queued_message_connection = connection;
1067                 m->exit_code = MANAGER_RELOAD;
1068
1069         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
1070
1071                 /* We don't send a reply back here, the client should
1072                  * just wait for us disconnecting. */
1073
1074                 m->exit_code = MANAGER_REEXECUTE;
1075
1076         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
1077
1078                 if (m->running_as == MANAGER_SYSTEM) {
1079                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
1080                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1081                 }
1082
1083                 if (!(reply = dbus_message_new_method_return(message)))
1084                         goto oom;
1085
1086                 m->exit_code = MANAGER_EXIT;
1087
1088         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
1089
1090                 if (m->running_as != MANAGER_SYSTEM) {
1091                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
1092                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1093                 }
1094
1095                 if (!(reply = dbus_message_new_method_return(message)))
1096                         goto oom;
1097
1098                 m->exit_code = MANAGER_REBOOT;
1099
1100         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
1101
1102                 if (m->running_as != MANAGER_SYSTEM) {
1103                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
1104                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1105                 }
1106
1107                 if (!(reply = dbus_message_new_method_return(message)))
1108                         goto oom;
1109
1110                 m->exit_code = MANAGER_POWEROFF;
1111
1112         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
1113
1114                 if (m->running_as != MANAGER_SYSTEM) {
1115                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
1116                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1117                 }
1118
1119                 if (!(reply = dbus_message_new_method_return(message)))
1120                         goto oom;
1121
1122                 m->exit_code = MANAGER_HALT;
1123
1124         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
1125
1126                 if (m->running_as != MANAGER_SYSTEM) {
1127                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
1128                         return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1129                 }
1130
1131                 if (!(reply = dbus_message_new_method_return(message)))
1132                         goto oom;
1133
1134                 m->exit_code = MANAGER_KEXEC;
1135
1136         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
1137                 char **l = NULL, **e = NULL;
1138
1139                 if ((r = bus_parse_strv(message, &l)) < 0) {
1140                         if (r == -ENOMEM)
1141                                 goto oom;
1142
1143                         return bus_send_error_reply(connection, message, NULL, r);
1144                 }
1145
1146                 e = strv_env_merge(2, m->environment, l);
1147                 strv_free(l);
1148
1149                 if (!e)
1150                         goto oom;
1151
1152                 if (!(reply = dbus_message_new_method_return(message))) {
1153                         strv_free(e);
1154                         goto oom;
1155                 }
1156
1157                 strv_free(m->environment);
1158                 m->environment = e;
1159
1160         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1161                 char **l = NULL, **e = NULL;
1162
1163                 if ((r = bus_parse_strv(message, &l)) < 0) {
1164                         if (r == -ENOMEM)
1165                                 goto oom;
1166
1167                         return bus_send_error_reply(connection, message, NULL, r);
1168                 }
1169
1170                 e = strv_env_delete(m->environment, 1, l);
1171                 strv_free(l);
1172
1173                 if (!e)
1174                         goto oom;
1175
1176                 if (!(reply = dbus_message_new_method_return(message))) {
1177                         strv_free(e);
1178                         goto oom;
1179                 }
1180
1181                 strv_free(m->environment);
1182                 m->environment = e;
1183
1184         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment")) {
1185                 char **l_set = NULL, **l_unset = NULL, **e = NULL, **f = NULL;
1186                 DBusMessageIter iter;
1187
1188                 if (!dbus_message_iter_init(message, &iter))
1189                         goto oom;
1190
1191                 r = bus_parse_strv_iter(&iter, &l_unset);
1192                 if (r < 0) {
1193                         if (r == -ENOMEM)
1194                                 goto oom;
1195
1196                         return bus_send_error_reply(connection, message, NULL, r);
1197                 }
1198
1199                 if (!dbus_message_iter_next(&iter)) {
1200                         strv_free(l_unset);
1201                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1202                 }
1203
1204                 r = bus_parse_strv_iter(&iter, &l_set);
1205                 if (r < 0) {
1206                         strv_free(l_unset);
1207                         if (r == -ENOMEM)
1208                                 goto oom;
1209
1210                         return bus_send_error_reply(connection, message, NULL, r);
1211                 }
1212
1213                 e = strv_env_delete(m->environment, 1, l_unset);
1214                 strv_free(l_unset);
1215
1216                 if (!e) {
1217                         strv_free(l_set);
1218                         goto oom;
1219                 }
1220
1221                 f = strv_env_merge(2, e, l_set);
1222                 strv_free(l_set);
1223                 strv_free(e);
1224
1225                 if (!f)
1226                         goto oom;
1227
1228                 if (!(reply = dbus_message_new_method_return(message))) {
1229                         strv_free(f);
1230                         goto oom;
1231                 }
1232
1233                 strv_free(m->environment);
1234                 m->environment = f;
1235         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnitFiles")) {
1236                 DBusMessageIter iter, sub, sub2;
1237                 Hashmap *h;
1238                 Iterator i;
1239                 UnitFileList *item;
1240
1241                 reply = dbus_message_new_method_return(message);
1242                 if (!reply)
1243                         goto oom;
1244
1245                 h = hashmap_new(string_hash_func, string_compare_func);
1246                 if (!h)
1247                         goto oom;
1248
1249                 r = unit_file_get_list(m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, h);
1250                 if (r < 0) {
1251                         unit_file_list_free(h);
1252                         dbus_message_unref(reply);
1253                         return bus_send_error_reply(connection, message, NULL, r);
1254                 }
1255
1256                 dbus_message_iter_init_append(reply, &iter);
1257
1258                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ss)", &sub)) {
1259                         unit_file_list_free(h);
1260                         goto oom;
1261                 }
1262
1263                 HASHMAP_FOREACH(item, h, i) {
1264                         const char *state;
1265
1266                         state = unit_file_state_to_string(item->state);
1267                         assert(state);
1268
1269                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
1270                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &item->path) ||
1271                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
1272                             !dbus_message_iter_close_container(&sub, &sub2)) {
1273                                 unit_file_list_free(h);
1274                                 goto oom;
1275                         }
1276                 }
1277
1278                 unit_file_list_free(h);
1279
1280                 if (!dbus_message_iter_close_container(&iter, &sub))
1281                         goto oom;
1282
1283         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitFileState")) {
1284                 const char *name;
1285                 UnitFileState state;
1286                 const char *s;
1287
1288                 if (!dbus_message_get_args(
1289                                     message,
1290                                     &error,
1291                                     DBUS_TYPE_STRING, &name,
1292                                     DBUS_TYPE_INVALID))
1293                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1294
1295                 state = unit_file_get_state(m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, name);
1296                 if (state < 0)
1297                         return bus_send_error_reply(connection, message, NULL, state);
1298
1299                 s = unit_file_state_to_string(state);
1300                 assert(s);
1301
1302                 reply = dbus_message_new_method_return(message);
1303                 if (!reply)
1304                         goto oom;
1305
1306                 if (!dbus_message_append_args(
1307                                     reply,
1308                                     DBUS_TYPE_STRING, &s,
1309                                     DBUS_TYPE_INVALID))
1310                         goto oom;
1311         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "EnableUnitFiles") ||
1312                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReenableUnitFiles") ||
1313                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LinkUnitFiles") ||
1314                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PresetUnitFiles") ||
1315                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "MaskUnitFiles")) {
1316
1317                 char **l = NULL;
1318                 DBusMessageIter iter;
1319                 UnitFileScope scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1320                 UnitFileChange *changes = NULL;
1321                 unsigned n_changes = 0;
1322                 dbus_bool_t runtime, force;
1323                 bool carries_install_info = -1;
1324
1325                 if (!dbus_message_iter_init(message, &iter))
1326                         goto oom;
1327
1328                 r = bus_parse_strv_iter(&iter, &l);
1329                 if (r < 0) {
1330                         if (r == -ENOMEM)
1331                                 goto oom;
1332
1333                         return bus_send_error_reply(connection, message, NULL, r);
1334                 }
1335
1336                 if (!dbus_message_iter_next(&iter) ||
1337                     bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, true) < 0 ||
1338                     bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &force, false) < 0) {
1339                         strv_free(l);
1340                         return bus_send_error_reply(connection, message, NULL, -EIO);
1341                 }
1342
1343                 if (streq(member, "EnableUnitFiles")) {
1344                         r = unit_file_enable(scope, runtime, NULL, l, force, &changes, &n_changes);
1345                         carries_install_info = r;
1346                 } else if (streq(member, "ReenableUnitFiles")) {
1347                         r = unit_file_reenable(scope, runtime, NULL, l, force, &changes, &n_changes);
1348                         carries_install_info = r;
1349                 } else if (streq(member, "LinkUnitFiles"))
1350                         r = unit_file_link(scope, runtime, NULL, l, force, &changes, &n_changes);
1351                 else if (streq(member, "PresetUnitFiles")) {
1352                         r = unit_file_preset(scope, runtime, NULL, l, force, &changes, &n_changes);
1353                         carries_install_info = r;
1354                 } else if (streq(member, "MaskUnitFiles"))
1355                         r = unit_file_mask(scope, runtime, NULL, l, force, &changes, &n_changes);
1356                 else
1357                         assert_not_reached("Uh? Wrong method");
1358
1359                 strv_free(l);
1360                 bus_manager_send_unit_files_changed(m);
1361
1362                 if (r < 0) {
1363                         unit_file_changes_free(changes, n_changes);
1364                         return bus_send_error_reply(connection, message, NULL, r);
1365                 }
1366
1367                 reply = message_from_file_changes(message, changes, n_changes, carries_install_info);
1368                 unit_file_changes_free(changes, n_changes);
1369
1370                 if (!reply)
1371                         goto oom;
1372
1373         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "DisableUnitFiles") ||
1374                    dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnmaskUnitFiles")) {
1375
1376                 char **l = NULL;
1377                 DBusMessageIter iter;
1378                 UnitFileScope scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1379                 UnitFileChange *changes = NULL;
1380                 unsigned n_changes = 0;
1381                 dbus_bool_t runtime;
1382
1383                 if (!dbus_message_iter_init(message, &iter))
1384                         goto oom;
1385
1386                 r = bus_parse_strv_iter(&iter, &l);
1387                 if (r < 0) {
1388                         if (r == -ENOMEM)
1389                                 goto oom;
1390
1391                         return bus_send_error_reply(connection, message, NULL, r);
1392                 }
1393
1394                 if (!dbus_message_iter_next(&iter) ||
1395                     bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, false) < 0) {
1396                         strv_free(l);
1397                         return bus_send_error_reply(connection, message, NULL, -EIO);
1398                 }
1399
1400                 if (streq(member, "DisableUnitFiles"))
1401                         r = unit_file_disable(scope, runtime, NULL, l, &changes, &n_changes);
1402                 else if (streq(member, "UnmaskUnitFiles"))
1403                         r = unit_file_unmask(scope, runtime, NULL, l, &changes, &n_changes);
1404                 else
1405                         assert_not_reached("Uh? Wrong method");
1406
1407                 strv_free(l);
1408                 bus_manager_send_unit_files_changed(m);
1409
1410                 if (r < 0) {
1411                         unit_file_changes_free(changes, n_changes);
1412                         return bus_send_error_reply(connection, message, NULL, r);
1413                 }
1414
1415                 reply = message_from_file_changes(message, changes, n_changes, -1);
1416                 unit_file_changes_free(changes, n_changes);
1417
1418                 if (!reply)
1419                         goto oom;
1420
1421         } else
1422                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1423
1424         if (job_type != _JOB_TYPE_INVALID) {
1425                 const char *name, *smode, *old_name = NULL;
1426                 JobMode mode;
1427                 Job *j;
1428                 Unit *u;
1429                 bool b;
1430
1431                 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1432                         b = dbus_message_get_args(
1433                                         message,
1434                                         &error,
1435                                         DBUS_TYPE_STRING, &old_name,
1436                                         DBUS_TYPE_STRING, &name,
1437                                         DBUS_TYPE_STRING, &smode,
1438                                         DBUS_TYPE_INVALID);
1439                 else
1440                         b = dbus_message_get_args(
1441                                         message,
1442                                         &error,
1443                                         DBUS_TYPE_STRING, &name,
1444                                         DBUS_TYPE_STRING, &smode,
1445                                         DBUS_TYPE_INVALID);
1446
1447                 if (!b)
1448                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1449
1450                 if (old_name)
1451                         if (!(u = manager_get_unit(m, old_name)) ||
1452                             !u->meta.job ||
1453                             u->meta.job->type != JOB_START) {
1454                                 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1455                                 return bus_send_error_reply(connection, message, &error, -ENOENT);
1456                         }
1457
1458
1459                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1460                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1461                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1462                 }
1463
1464                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1465                         return bus_send_error_reply(connection, message, &error, r);
1466
1467                 if (reload_if_possible && unit_can_reload(u)) {
1468                         if (job_type == JOB_RESTART)
1469                                 job_type = JOB_RELOAD_OR_START;
1470                         else if (job_type == JOB_TRY_RESTART)
1471                                 job_type = JOB_RELOAD;
1472                 }
1473
1474                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1475                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1476                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1477                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1478                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1479                         return bus_send_error_reply(connection, message, &error, -EPERM);
1480                 }
1481
1482                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1483                         return bus_send_error_reply(connection, message, &error, r);
1484
1485                 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1486                         goto oom;
1487
1488                 j->bus = connection;
1489
1490                 if (!(reply = dbus_message_new_method_return(message)))
1491                         goto oom;
1492
1493                 if (!(path = job_dbus_path(j)))
1494                         goto oom;
1495
1496                 if (!dbus_message_append_args(
1497                                     reply,
1498                                     DBUS_TYPE_OBJECT_PATH, &path,
1499                                     DBUS_TYPE_INVALID))
1500                         goto oom;
1501         }
1502
1503         if (reply) {
1504                 if (!dbus_connection_send(connection, reply, NULL))
1505                         goto oom;
1506
1507                 dbus_message_unref(reply);
1508         }
1509
1510         free(path);
1511
1512         return DBUS_HANDLER_RESULT_HANDLED;
1513
1514 oom:
1515         free(path);
1516
1517         if (reply)
1518                 dbus_message_unref(reply);
1519
1520         dbus_error_free(&error);
1521
1522         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1523 }
1524
1525 const DBusObjectPathVTable bus_manager_vtable = {
1526         .message_function = bus_manager_message_handler
1527 };