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