chiark / gitweb /
device: set recursive_stop=true by default
[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
24 #include "dbus.h"
25 #include "log.h"
26 #include "dbus-manager.h"
27 #include "strv.h"
28 #include "bus-errors.h"
29
30 #define BUS_MANAGER_INTERFACE_BEGIN                                     \
31         " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
32
33 #define BUS_MANAGER_INTERFACE_METHODS                                   \
34         "  <method name=\"GetUnit\">\n"                                 \
35         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
36         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
37         "  </method>\n"                                                 \
38         "  <method name=\"GetUnitByPID\">\n"                            \
39         "   <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n"          \
40         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
41         "  </method>\n"                                                 \
42         "  <method name=\"LoadUnit\">\n"                                \
43         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
44         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
45         "  </method>\n"                                                 \
46         "  <method name=\"StartUnit\">\n"                               \
47         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
48         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
49         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
50         "  </method>\n"                                                 \
51         "  <method name=\"StartUnitReplace\">\n"                        \
52         "   <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n"     \
53         "   <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n"     \
54         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
55         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
56         "  </method>\n"                                                 \
57         "  <method name=\"StopUnit\">\n"                                \
58         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
59         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
60         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
61         "  </method>\n"                                                 \
62         "  <method name=\"ReloadUnit\">\n"                              \
63         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
64         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
65         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
66         "  </method>\n"                                                 \
67         "  <method name=\"RestartUnit\">\n"                             \
68         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
69         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
70         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
71         "  </method>\n"                                                 \
72         "  <method name=\"TryRestartUnit\">\n"                          \
73         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
74         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
75         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
76         "  </method>\n"                                                 \
77         "  <method name=\"ReloadOrRestartUnit\">\n"                     \
78         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
79         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
80         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
81         "  </method>\n"                                                 \
82         "  <method name=\"ReloadOrTryRestartUnit\">\n"                  \
83         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
84         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
85         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
86         "  </method>\n"                                                 \
87         "  <method name=\"KillUnit\">\n"                                \
88         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
89         "   <arg name=\"who\" type=\"s\" direction=\"in\"/>\n"          \
90         "   <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n"         \
91         "   <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n"       \
92         "  </method>\n"                                                 \
93         "  <method name=\"ResetFailedUnit\">\n"                         \
94         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
95         "  </method>\n"                                                 \
96         "  <method name=\"GetJob\">\n"                                  \
97         "   <arg name=\"id\" type=\"u\" direction=\"in\"/>\n"           \
98         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
99         "  </method>\n"                                                 \
100         "  <method name=\"ClearJobs\"/>\n"                              \
101         "  <method name=\"ResetFailed\"/>\n"                            \
102         "  <method name=\"ListUnits\">\n"                               \
103         "   <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
104         "  </method>\n"                                                 \
105         "  <method name=\"ListJobs\">\n"                                \
106         "   <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
107         "  </method>\n"                                                 \
108         "  <method name=\"Subscribe\"/>\n"                              \
109         "  <method name=\"Unsubscribe\"/>\n"                            \
110         "  <method name=\"Dump\"/>\n"                                   \
111         "  <method name=\"CreateSnapshot\">\n"                          \
112         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
113         "   <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n"      \
114         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
115         "  </method>\n"                                                 \
116         "  <method name=\"Reload\"/>\n"                                 \
117         "  <method name=\"Reexecute\"/>\n"                              \
118         "  <method name=\"Exit\"/>\n"                                   \
119         "  <method name=\"Reboot\"/>\n"                                 \
120         "  <method name=\"PowerOff\"/>\n"                               \
121         "  <method name=\"Halt\"/>\n"                                   \
122         "  <method name=\"KExec\"/>\n"                                  \
123         "  <method name=\"SetEnvironment\">\n"                          \
124         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
125         "  </method>\n"                                                 \
126         "  <method name=\"UnsetEnvironment\">\n"                        \
127         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
128         "  </method>\n"
129
130 #define BUS_MANAGER_INTERFACE_SIGNALS                                   \
131         "  <signal name=\"UnitNew\">\n"                                 \
132         "   <arg name=\"id\" type=\"s\"/>\n"                            \
133         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
134         "  </signal>\n"                                                 \
135         "  <signal name=\"UnitRemoved\">\n"                             \
136         "   <arg name=\"id\" type=\"s\"/>\n"                            \
137         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
138         "  </signal>\n"                                                 \
139         "  <signal name=\"JobNew\">\n"                                  \
140         "   <arg name=\"id\" type=\"u\"/>\n"                            \
141         "   <arg name=\"job\" type=\"o\"/>\n"                           \
142         "  </signal>\n"                                                 \
143         "  <signal name=\"JobRemoved\">\n"                              \
144         "   <arg name=\"id\" type=\"u\"/>\n"                            \
145         "   <arg name=\"job\" type=\"o\"/>\n"                           \
146         "   <arg name=\"success\" type=\"b\"/>\n"                       \
147         "  </signal>"
148
149
150 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL                        \
151         "  <property name=\"Version\" type=\"s\" access=\"read\"/>\n"   \
152         "  <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
153         "  <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
154         "  <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n"  \
155         "  <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
156         "  <property name=\"NNames\" type=\"u\" access=\"read\"/>\n"    \
157         "  <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n"     \
158         "  <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
159         "  <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
160         "  <property name=\"Progress\" type=\"d\" access=\"read\"/>\n"  \
161         "  <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
162         "  <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
163         "  <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
164         "  <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
165         "  <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
166         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
167         "  <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
168         "  <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n"
169
170 #ifdef HAVE_SYSV_COMPAT
171 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV                           \
172         "  <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
173         "  <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
174         "  <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
175 #else
176 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
177 #endif
178
179 #define BUS_MANAGER_INTERFACE_END                                       \
180         " </interface>\n"
181
182 #define BUS_MANAGER_INTERFACE                                           \
183         BUS_MANAGER_INTERFACE_BEGIN                                     \
184         BUS_MANAGER_INTERFACE_METHODS                                   \
185         BUS_MANAGER_INTERFACE_SIGNALS                                   \
186         BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL                        \
187         BUS_MANAGER_INTERFACE_PROPERTIES_SYSV                           \
188         BUS_MANAGER_INTERFACE_END
189
190 #define INTROSPECTION_BEGIN                                             \
191         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
192         "<node>\n"                                                      \
193         BUS_MANAGER_INTERFACE                                           \
194         BUS_PROPERTIES_INTERFACE                                        \
195         BUS_PEER_INTERFACE                                              \
196         BUS_INTROSPECTABLE_INTERFACE
197
198 #define INTROSPECTION_END                                               \
199         "</node>\n"
200
201 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
202
203 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
204
205 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
206         const char *t;
207
208         assert(m);
209         assert(i);
210         assert(property);
211
212         t = log_target_to_string(log_get_target());
213
214         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
215                 return -ENOMEM;
216
217         return 0;
218 }
219
220 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
221         const char *t;
222
223         assert(m);
224         assert(i);
225         assert(property);
226
227         t = log_level_to_string(log_get_max_level());
228
229         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
230                 return -ENOMEM;
231
232         return 0;
233 }
234
235 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
236         uint32_t u;
237
238         assert(m);
239         assert(i);
240         assert(property);
241
242         u = hashmap_size(m->units);
243
244         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
245                 return -ENOMEM;
246
247         return 0;
248 }
249
250 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
251         uint32_t u;
252
253         assert(m);
254         assert(i);
255         assert(property);
256
257         u = hashmap_size(m->jobs);
258
259         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
260                 return -ENOMEM;
261
262         return 0;
263 }
264
265 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
266         double d;
267
268         assert(m);
269         assert(i);
270         assert(property);
271
272         if (dual_timestamp_is_set(&m->finish_timestamp))
273                 d = 1.0;
274         else
275                 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
276
277         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
278                 return -ENOMEM;
279
280         return 0;
281 }
282
283 static const char *message_get_sender_with_fallback(DBusMessage *m) {
284         const char *s;
285
286         assert(m);
287
288         if ((s = dbus_message_get_sender(m)))
289                 return s;
290
291         /* When the message came in from a direct connection the
292          * message will have no sender. We fix that here. */
293
294         return ":no-sender";
295 }
296
297 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
298         Manager *m = data;
299
300         const BusProperty properties[] = {
301                 { "org.freedesktop.systemd1.Manager", "Version",       bus_property_append_string,    "s",  PACKAGE_STRING     },
302                 { "org.freedesktop.systemd1.Manager", "RunningAs",     bus_manager_append_running_as, "s",  &m->running_as     },
303                 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t",  &m->startup_timestamp.realtime },
304                 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64,  "t",  &m->finish_timestamp.realtime },
305                 { "org.freedesktop.systemd1.Manager", "LogLevel",      bus_manager_append_log_level,  "s",  NULL               },
306                 { "org.freedesktop.systemd1.Manager", "LogTarget",     bus_manager_append_log_target, "s",  NULL               },
307                 { "org.freedesktop.systemd1.Manager", "NNames",        bus_manager_append_n_names,    "u",  NULL               },
308                 { "org.freedesktop.systemd1.Manager", "NJobs",         bus_manager_append_n_jobs,     "u",  NULL               },
309                 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32,    "u",  &m->n_installed_jobs },
310                 { "org.freedesktop.systemd1.Manager", "NFailedJobs",   bus_property_append_uint32,    "u",  &m->n_failed_jobs  },
311                 { "org.freedesktop.systemd1.Manager", "Progress",      bus_manager_append_progress,   "d",  NULL               },
312                 { "org.freedesktop.systemd1.Manager", "Environment",   bus_property_append_strv,      "as", m->environment     },
313                 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn",  bus_property_append_bool,      "b",  &m->confirm_spawn  },
314                 { "org.freedesktop.systemd1.Manager", "ShowStatus",    bus_property_append_bool,      "b",  &m->show_status    },
315                 { "org.freedesktop.systemd1.Manager", "UnitPath",      bus_property_append_strv,      "as", m->lookup_paths.unit_path },
316                 { "org.freedesktop.systemd1.Manager", "NotifySocket",  bus_property_append_string,    "s",  m->notify_socket   },
317                 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
318                 { "org.freedesktop.systemd1.Manager", "MountAuto",     bus_property_append_bool,      "b",  &m->mount_auto     },
319                 { "org.freedesktop.systemd1.Manager", "SwapAuto",      bus_property_append_bool,      "b",  &m->swap_auto     },
320 #ifdef HAVE_SYSV_COMPAT
321                 { "org.freedesktop.systemd1.Manager", "SysVConsole",   bus_property_append_bool,      "b",  &m->sysv_console   },
322                 { "org.freedesktop.systemd1.Manager", "SysVInitPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvinit_path },
323                 { "org.freedesktop.systemd1.Manager", "SysVRcndPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvrcnd_path },
324 #endif
325                 { NULL, NULL, NULL, NULL, NULL }
326         };
327
328         int r;
329         DBusError error;
330         DBusMessage *reply = NULL;
331         char * path = NULL;
332         JobType job_type = _JOB_TYPE_INVALID;
333         bool reload_if_possible = false;
334
335         assert(connection);
336         assert(message);
337         assert(m);
338
339         dbus_error_init(&error);
340
341         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
342                 const char *name;
343                 Unit *u;
344
345                 if (!dbus_message_get_args(
346                                     message,
347                                     &error,
348                                     DBUS_TYPE_STRING, &name,
349                                     DBUS_TYPE_INVALID))
350                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
351
352                 if (!(u = manager_get_unit(m, name))) {
353                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
354                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
355                 }
356
357                 if (!(reply = dbus_message_new_method_return(message)))
358                         goto oom;
359
360                 if (!(path = unit_dbus_path(u)))
361                         goto oom;
362
363                 if (!dbus_message_append_args(
364                                     reply,
365                                     DBUS_TYPE_OBJECT_PATH, &path,
366                                     DBUS_TYPE_INVALID))
367                         goto oom;
368         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
369                 Unit *u;
370                 uint32_t pid;
371
372                 if (!dbus_message_get_args(
373                                     message,
374                                     &error,
375                                     DBUS_TYPE_UINT32, &pid,
376                                     DBUS_TYPE_INVALID))
377                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
378
379                 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
380                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
381                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
382                 }
383
384                 if (!(reply = dbus_message_new_method_return(message)))
385                         goto oom;
386
387                 if (!(path = unit_dbus_path(u)))
388                         goto oom;
389
390                 if (!dbus_message_append_args(
391                                     reply,
392                                     DBUS_TYPE_OBJECT_PATH, &path,
393                                     DBUS_TYPE_INVALID))
394                         goto oom;
395         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
396                 const char *name;
397                 Unit *u;
398
399                 if (!dbus_message_get_args(
400                                     message,
401                                     &error,
402                                     DBUS_TYPE_STRING, &name,
403                                     DBUS_TYPE_INVALID))
404                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
405
406                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
407                         return bus_send_error_reply(m, connection, message, &error, r);
408
409                 if (!(reply = dbus_message_new_method_return(message)))
410                         goto oom;
411
412                 if (!(path = unit_dbus_path(u)))
413                         goto oom;
414
415                 if (!dbus_message_append_args(
416                                     reply,
417                                     DBUS_TYPE_OBJECT_PATH, &path,
418                                     DBUS_TYPE_INVALID))
419                         goto oom;
420
421         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
422                 job_type = JOB_START;
423         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
424                 job_type = JOB_START;
425         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
426                 job_type = JOB_STOP;
427         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
428                 job_type = JOB_RELOAD;
429         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
430                 job_type = JOB_RESTART;
431         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
432                 job_type = JOB_TRY_RESTART;
433         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
434                 reload_if_possible = true;
435                 job_type = JOB_RESTART;
436         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
437                 reload_if_possible = true;
438                 job_type = JOB_TRY_RESTART;
439         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
440                 const char *name, *swho, *smode;
441                 int32_t signo;
442                 Unit *u;
443                 KillMode mode;
444                 KillWho who;
445
446                 if (!dbus_message_get_args(
447                                     message,
448                                     &error,
449                                     DBUS_TYPE_STRING, &name,
450                                     DBUS_TYPE_STRING, &swho,
451                                     DBUS_TYPE_STRING, &smode,
452                                     DBUS_TYPE_INT32, &signo,
453                                     DBUS_TYPE_INVALID))
454                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
455
456                 if ((mode = kill_mode_from_string(smode)) < 0 ||
457                     (who = kill_who_from_string(swho)) < 0 ||
458                     signo <= 0 ||
459                     signo >= _NSIG)
460                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
461
462                 if (!(u = manager_get_unit(m, name))) {
463                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
464                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
465                 }
466
467                 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
468                         return bus_send_error_reply(m, connection, message, &error, r);
469
470                 if (!(reply = dbus_message_new_method_return(message)))
471                         goto oom;
472
473         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
474                 uint32_t id;
475                 Job *j;
476
477                 if (!dbus_message_get_args(
478                                     message,
479                                     &error,
480                                     DBUS_TYPE_UINT32, &id,
481                                     DBUS_TYPE_INVALID))
482                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
483
484                 if (!(j = manager_get_job(m, id))) {
485                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
486                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
487                 }
488
489                 if (!(reply = dbus_message_new_method_return(message)))
490                         goto oom;
491
492                 if (!(path = job_dbus_path(j)))
493                         goto oom;
494
495                 if (!dbus_message_append_args(
496                                     reply,
497                                     DBUS_TYPE_OBJECT_PATH, &path,
498                                     DBUS_TYPE_INVALID))
499                         goto oom;
500
501         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
502
503                 manager_clear_jobs(m);
504
505                 if (!(reply = dbus_message_new_method_return(message)))
506                         goto oom;
507
508         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
509
510                 manager_reset_failed(m);
511
512                 if (!(reply = dbus_message_new_method_return(message)))
513                         goto oom;
514
515         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
516                 const char *name;
517                 Unit *u;
518
519                 if (!dbus_message_get_args(
520                                     message,
521                                     &error,
522                                     DBUS_TYPE_STRING, &name,
523                                     DBUS_TYPE_INVALID))
524                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
525
526                 if (!(u = manager_get_unit(m, name))) {
527                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
528                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
529                 }
530
531                 unit_reset_failed(u);
532
533                 if (!(reply = dbus_message_new_method_return(message)))
534                         goto oom;
535
536         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
537                 DBusMessageIter iter, sub;
538                 Iterator i;
539                 Unit *u;
540                 const char *k;
541
542                 if (!(reply = dbus_message_new_method_return(message)))
543                         goto oom;
544
545                 dbus_message_iter_init_append(reply, &iter);
546
547                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
548                         goto oom;
549
550                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
551                         char *u_path, *j_path;
552                         const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
553                         DBusMessageIter sub2;
554                         uint32_t job_id;
555                         Unit *f;
556
557                         if (k != u->meta.id)
558                                 continue;
559
560                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
561                                 goto oom;
562
563                         description = unit_description(u);
564                         load_state = unit_load_state_to_string(u->meta.load_state);
565                         active_state = unit_active_state_to_string(unit_active_state(u));
566                         sub_state = unit_sub_state_to_string(u);
567
568                         f = unit_following(u);
569                         following = f ? f->meta.id : "";
570
571                         if (!(u_path = unit_dbus_path(u)))
572                                 goto oom;
573
574                         if (u->meta.job) {
575                                 job_id = (uint32_t) u->meta.job->id;
576
577                                 if (!(j_path = job_dbus_path(u->meta.job))) {
578                                         free(u_path);
579                                         goto oom;
580                                 }
581
582                                 sjob_type = job_type_to_string(u->meta.job->type);
583                         } else {
584                                 job_id = 0;
585                                 j_path = u_path;
586                                 sjob_type = "";
587                         }
588
589                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
590                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
591                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
592                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
593                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
594                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
595                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
596                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
597                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
598                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
599                                 free(u_path);
600                                 if (u->meta.job)
601                                         free(j_path);
602                                 goto oom;
603                         }
604
605                         free(u_path);
606                         if (u->meta.job)
607                                 free(j_path);
608
609                         if (!dbus_message_iter_close_container(&sub, &sub2))
610                                 goto oom;
611                 }
612
613                 if (!dbus_message_iter_close_container(&iter, &sub))
614                         goto oom;
615
616         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
617                 DBusMessageIter iter, sub;
618                 Iterator i;
619                 Job *j;
620
621                 if (!(reply = dbus_message_new_method_return(message)))
622                         goto oom;
623
624                 dbus_message_iter_init_append(reply, &iter);
625
626                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
627                         goto oom;
628
629                 HASHMAP_FOREACH(j, m->jobs, i) {
630                         char *u_path, *j_path;
631                         const char *state, *type;
632                         uint32_t id;
633                         DBusMessageIter sub2;
634
635                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
636                                 goto oom;
637
638                         id = (uint32_t) j->id;
639                         state = job_state_to_string(j->state);
640                         type = job_type_to_string(j->type);
641
642                         if (!(j_path = job_dbus_path(j)))
643                                 goto oom;
644
645                         if (!(u_path = unit_dbus_path(j->unit))) {
646                                 free(j_path);
647                                 goto oom;
648                         }
649
650                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
651                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
652                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
653                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
654                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
655                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
656                                 free(j_path);
657                                 free(u_path);
658                                 goto oom;
659                         }
660
661                         free(j_path);
662                         free(u_path);
663
664                         if (!dbus_message_iter_close_container(&sub, &sub2))
665                                 goto oom;
666                 }
667
668                 if (!dbus_message_iter_close_container(&iter, &sub))
669                         goto oom;
670
671         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
672                 char *client;
673                 Set *s;
674
675                 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
676                         if (!(s = set_new(string_hash_func, string_compare_func)))
677                                 goto oom;
678
679                         if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
680                                 set_free(s);
681                                 goto oom;
682                         }
683                 }
684
685                 if (!(client = strdup(message_get_sender_with_fallback(message))))
686                         goto oom;
687
688                 if ((r = set_put(s, client)) < 0) {
689                         free(client);
690                         return bus_send_error_reply(m, connection, message, NULL, r);
691                 }
692
693                 if (!(reply = dbus_message_new_method_return(message)))
694                         goto oom;
695
696         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
697                 char *client;
698
699                 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
700                         dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
701                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
702                 }
703
704                 free(client);
705
706                 if (!(reply = dbus_message_new_method_return(message)))
707                         goto oom;
708
709         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
710                 FILE *f;
711                 char *dump = NULL;
712                 size_t size;
713
714                 if (!(reply = dbus_message_new_method_return(message)))
715                         goto oom;
716
717                 if (!(f = open_memstream(&dump, &size)))
718                         goto oom;
719
720                 manager_dump_units(m, f, NULL);
721                 manager_dump_jobs(m, f, NULL);
722
723                 if (ferror(f)) {
724                         fclose(f);
725                         free(dump);
726                         goto oom;
727                 }
728
729                 fclose(f);
730
731                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
732                         free(dump);
733                         goto oom;
734                 }
735
736                 free(dump);
737         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
738                 const char *name;
739                 dbus_bool_t cleanup;
740                 Snapshot *s;
741
742                 if (!dbus_message_get_args(
743                                     message,
744                                     &error,
745                                     DBUS_TYPE_STRING, &name,
746                                     DBUS_TYPE_BOOLEAN, &cleanup,
747                                     DBUS_TYPE_INVALID))
748                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
749
750                 if (name && name[0] == 0)
751                         name = NULL;
752
753                 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
754                         return bus_send_error_reply(m, connection, message, &error, r);
755
756                 if (!(reply = dbus_message_new_method_return(message)))
757                         goto oom;
758
759                 if (!(path = unit_dbus_path(UNIT(s))))
760                         goto oom;
761
762                 if (!dbus_message_append_args(
763                                     reply,
764                                     DBUS_TYPE_OBJECT_PATH, &path,
765                                     DBUS_TYPE_INVALID))
766                         goto oom;
767
768         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
769                 char *introspection = NULL;
770                 FILE *f;
771                 Iterator i;
772                 Unit *u;
773                 Job *j;
774                 const char *k;
775                 size_t size;
776
777                 if (!(reply = dbus_message_new_method_return(message)))
778                         goto oom;
779
780                 /* We roll our own introspection code here, instead of
781                  * relying on bus_default_message_handler() because we
782                  * need to generate our introspection string
783                  * dynamically. */
784
785                 if (!(f = open_memstream(&introspection, &size)))
786                         goto oom;
787
788                 fputs(INTROSPECTION_BEGIN, f);
789
790                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
791                         char *p;
792
793                         if (k != u->meta.id)
794                                 continue;
795
796                         if (!(p = bus_path_escape(k))) {
797                                 fclose(f);
798                                 free(introspection);
799                                 goto oom;
800                         }
801
802                         fprintf(f, "<node name=\"unit/%s\"/>", p);
803                         free(p);
804                 }
805
806                 HASHMAP_FOREACH(j, m->jobs, i)
807                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
808
809                 fputs(INTROSPECTION_END, f);
810
811                 if (ferror(f)) {
812                         fclose(f);
813                         free(introspection);
814                         goto oom;
815                 }
816
817                 fclose(f);
818
819                 if (!introspection)
820                         goto oom;
821
822                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
823                         free(introspection);
824                         goto oom;
825                 }
826
827                 free(introspection);
828
829         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
830
831                 assert(!m->queued_message);
832
833                 /* Instead of sending the reply back right away, we
834                  * just remember that we need to and then send it
835                  * after the reload is finished. That way the caller
836                  * knows when the reload finished. */
837
838                 if (!(m->queued_message = dbus_message_new_method_return(message)))
839                         goto oom;
840
841                 m->queued_message_connection = connection;
842                 m->exit_code = MANAGER_RELOAD;
843
844         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
845
846                 if (!(reply = dbus_message_new_method_return(message)))
847                         goto oom;
848
849                 m->exit_code = MANAGER_REEXECUTE;
850
851         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
852
853                 if (m->running_as == MANAGER_SYSTEM) {
854                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
855                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
856                 }
857
858                 if (!(reply = dbus_message_new_method_return(message)))
859                         goto oom;
860
861                 m->exit_code = MANAGER_EXIT;
862
863         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
864
865                 if (m->running_as != MANAGER_SYSTEM) {
866                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
867                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
868                 }
869
870                 if (!(reply = dbus_message_new_method_return(message)))
871                         goto oom;
872
873                 m->exit_code = MANAGER_REBOOT;
874
875         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
876
877                 if (m->running_as != MANAGER_SYSTEM) {
878                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
879                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
880                 }
881
882                 if (!(reply = dbus_message_new_method_return(message)))
883                         goto oom;
884
885                 m->exit_code = MANAGER_POWEROFF;
886
887         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
888
889                 if (m->running_as != MANAGER_SYSTEM) {
890                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
891                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
892                 }
893
894                 if (!(reply = dbus_message_new_method_return(message)))
895                         goto oom;
896
897                 m->exit_code = MANAGER_HALT;
898
899         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
900
901                 if (m->running_as != MANAGER_SYSTEM) {
902                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
903                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
904                 }
905
906                 if (!(reply = dbus_message_new_method_return(message)))
907                         goto oom;
908
909                 m->exit_code = MANAGER_KEXEC;
910
911         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
912                 char **l = NULL, **e = NULL;
913
914                 if ((r = bus_parse_strv(message, &l)) < 0) {
915                         if (r == -ENOMEM)
916                                 goto oom;
917
918                         return bus_send_error_reply(m, connection, message, NULL, r);
919                 }
920
921                 e = strv_env_merge(2, m->environment, l);
922                 strv_free(l);
923
924                 if (!e)
925                         goto oom;
926
927                 if (!(reply = dbus_message_new_method_return(message))) {
928                         strv_free(e);
929                         goto oom;
930                 }
931
932                 strv_free(m->environment);
933                 m->environment = e;
934
935         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
936                 char **l = NULL, **e = NULL;
937
938                 if ((r = bus_parse_strv(message, &l)) < 0) {
939                         if (r == -ENOMEM)
940                                 goto oom;
941
942                         return bus_send_error_reply(m, connection, message, NULL, r);
943                 }
944
945                 e = strv_env_delete(m->environment, 1, l);
946                 strv_free(l);
947
948                 if (!e)
949                         goto oom;
950
951                 if (!(reply = dbus_message_new_method_return(message)))
952                         goto oom;
953
954                 strv_free(m->environment);
955                 m->environment = e;
956
957         } else
958                 return bus_default_message_handler(m, connection, message, NULL, properties);
959
960         if (job_type != _JOB_TYPE_INVALID) {
961                 const char *name, *smode, *old_name = NULL;
962                 JobMode mode;
963                 Job *j;
964                 Unit *u;
965                 bool b;
966
967                 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
968                         b = dbus_message_get_args(
969                                         message,
970                                         &error,
971                                         DBUS_TYPE_STRING, &old_name,
972                                         DBUS_TYPE_STRING, &name,
973                                         DBUS_TYPE_STRING, &smode,
974                                         DBUS_TYPE_INVALID);
975                 else
976                         b = dbus_message_get_args(
977                                         message,
978                                         &error,
979                                         DBUS_TYPE_STRING, &name,
980                                         DBUS_TYPE_STRING, &smode,
981                                         DBUS_TYPE_INVALID);
982
983                 if (!b)
984                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
985
986                 if (old_name)
987                         if (!(u = manager_get_unit(m, old_name)) ||
988                             !u->meta.job ||
989                             u->meta.job->type != JOB_START) {
990                                 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
991                                 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
992                         }
993
994
995                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
996                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
997                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
998                 }
999
1000                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1001                         return bus_send_error_reply(m, connection, message, &error, r);
1002
1003                 if (reload_if_possible && unit_can_reload(u)) {
1004                         if (job_type == JOB_RESTART)
1005                                 job_type = JOB_RELOAD_OR_START;
1006                         else if (job_type == JOB_TRY_RESTART)
1007                                 job_type = JOB_RELOAD;
1008                 }
1009
1010                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1011                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1012                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1013                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1014                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1015                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
1016                 }
1017
1018                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1019                         return bus_send_error_reply(m, connection, message, &error, r);
1020
1021                 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1022                         goto oom;
1023
1024                 j->bus = connection;
1025
1026                 if (!(reply = dbus_message_new_method_return(message)))
1027                         goto oom;
1028
1029                 if (!(path = job_dbus_path(j)))
1030                         goto oom;
1031
1032                 if (!dbus_message_append_args(
1033                                     reply,
1034                                     DBUS_TYPE_OBJECT_PATH, &path,
1035                                     DBUS_TYPE_INVALID))
1036                         goto oom;
1037         }
1038
1039         free(path);
1040
1041         if (reply) {
1042                 if (!dbus_connection_send(connection, reply, NULL))
1043                         goto oom;
1044
1045                 dbus_message_unref(reply);
1046         }
1047
1048         return DBUS_HANDLER_RESULT_HANDLED;
1049
1050 oom:
1051         free(path);
1052
1053         if (reply)
1054                 dbus_message_unref(reply);
1055
1056         dbus_error_free(&error);
1057
1058         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1059 }
1060
1061 const DBusObjectPathVTable bus_manager_vtable = {
1062         .message_function = bus_manager_message_handler
1063 };