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