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