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