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