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