chiark / gitweb /
dbus: complete coverage of service units
[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
456                 if (!(client = strdup(dbus_message_get_sender(message))))
457                         goto oom;
458
459                 r = set_put(m->subscribed, client);
460
461                 if (r < 0)
462                         return bus_send_error_reply(m, connection, message, NULL, r);
463
464                 if (!(reply = dbus_message_new_method_return(message)))
465                         goto oom;
466
467         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
468                 char *client;
469
470                 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
471                         return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
472
473                 free(client);
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", "Dump")) {
479                 FILE *f;
480                 char *dump = NULL;
481                 size_t size;
482
483                 if (!(reply = dbus_message_new_method_return(message)))
484                         goto oom;
485
486                 if (!(f = open_memstream(&dump, &size)))
487                         goto oom;
488
489                 manager_dump_units(m, f, NULL);
490                 manager_dump_jobs(m, f, NULL);
491
492                 if (ferror(f)) {
493                         fclose(f);
494                         free(dump);
495                         goto oom;
496                 }
497
498                 fclose(f);
499
500                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
501                         free(dump);
502                         goto oom;
503                 }
504
505                 free(dump);
506         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
507                 const char *name;
508                 dbus_bool_t cleanup;
509                 Snapshot *s;
510
511                 if (!dbus_message_get_args(
512                                     message,
513                                     &error,
514                                     DBUS_TYPE_STRING, &name,
515                                     DBUS_TYPE_BOOLEAN, &cleanup,
516                                     DBUS_TYPE_INVALID))
517                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
518
519                 if (name && name[0] == 0)
520                         name = NULL;
521
522                 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
523                         return bus_send_error_reply(m, connection, message, NULL, r);
524
525                 if (!(reply = dbus_message_new_method_return(message)))
526                         goto oom;
527
528                 if (!(path = unit_dbus_path(UNIT(s))))
529                         goto oom;
530
531                 if (!dbus_message_append_args(
532                                     reply,
533                                     DBUS_TYPE_OBJECT_PATH, &path,
534                                     DBUS_TYPE_INVALID))
535                         goto oom;
536
537         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
538                 char *introspection = NULL;
539                 FILE *f;
540                 Iterator i;
541                 Unit *u;
542                 Job *j;
543                 const char *k;
544                 size_t size;
545
546                 if (!(reply = dbus_message_new_method_return(message)))
547                         goto oom;
548
549                 /* We roll our own introspection code here, instead of
550                  * relying on bus_default_message_handler() because we
551                  * need to generate our introspection string
552                  * dynamically. */
553
554                 if (!(f = open_memstream(&introspection, &size)))
555                         goto oom;
556
557                 fputs(INTROSPECTION_BEGIN, f);
558
559                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
560                         char *p;
561
562                         if (k != u->meta.id)
563                                 continue;
564
565                         if (!(p = bus_path_escape(k))) {
566                                 fclose(f);
567                                 free(introspection);
568                                 goto oom;
569                         }
570
571                         fprintf(f, "<node name=\"unit/%s\"/>", p);
572                         free(p);
573                 }
574
575                 HASHMAP_FOREACH(j, m->jobs, i)
576                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
577
578                 fputs(INTROSPECTION_END, f);
579
580                 if (ferror(f)) {
581                         fclose(f);
582                         free(introspection);
583                         goto oom;
584                 }
585
586                 fclose(f);
587
588                 if (!introspection)
589                         goto oom;
590
591                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
592                         free(introspection);
593                         goto oom;
594                 }
595
596                 free(introspection);
597
598         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
599
600                 assert(!m->queued_message);
601
602                 /* Instead of sending the reply back right away, we
603                  * just remember that we need to and then send it
604                  * after the reload is finished. That way the caller
605                  * knows when the reload finished. */
606
607                 if (!(m->queued_message = dbus_message_new_method_return(message)))
608                         goto oom;
609
610                 m->exit_code = MANAGER_RELOAD;
611
612         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
613
614                 if (!(reply = dbus_message_new_method_return(message)))
615                         goto oom;
616
617                 m->exit_code = MANAGER_REEXECUTE;
618
619         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
620
621                 if (m->running_as == MANAGER_SYSTEM)
622                         return bus_send_error_reply(m, connection, message, NULL, -ENOTSUP);
623
624                 if (!(reply = dbus_message_new_method_return(message)))
625                         goto oom;
626
627                 m->exit_code = MANAGER_EXIT;
628
629         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
630                 char **l = NULL, **e = NULL;
631
632                 if ((r = bus_parse_strv(message, &l)) < 0) {
633                         if (r == -ENOMEM)
634                                 goto oom;
635
636                         return bus_send_error_reply(m, connection, message, NULL, r);
637                 }
638
639                 e = strv_env_merge(2, m->environment, l);
640                 strv_free(l);
641
642                 if (!e)
643                         goto oom;
644
645                 if (!(reply = dbus_message_new_method_return(message))) {
646                         strv_free(e);
647                         goto oom;
648                 }
649
650                 strv_free(m->environment);
651                 m->environment = e;
652
653         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
654                 char **l = NULL, **e = NULL;
655
656                 if ((r = bus_parse_strv(message, &l)) < 0) {
657                         if (r == -ENOMEM)
658                                 goto oom;
659
660                         return bus_send_error_reply(m, connection, message, NULL, r);
661                 }
662
663                 e = strv_env_delete(m->environment, 1, l);
664                 strv_free(l);
665
666                 if (!e)
667                         goto oom;
668
669                 if (!(reply = dbus_message_new_method_return(message)))
670                         goto oom;
671
672                 strv_free(m->environment);
673                 m->environment = e;
674
675         } else
676                 return bus_default_message_handler(m, connection, message, NULL, properties);
677
678
679         if (job_type != _JOB_TYPE_INVALID) {
680                 const char *name, *smode;
681                 JobMode mode;
682                 Job *j;
683                 Unit *u;
684
685                 if (!dbus_message_get_args(
686                                     message,
687                                     &error,
688                                     DBUS_TYPE_STRING, &name,
689                                     DBUS_TYPE_STRING, &smode,
690                                     DBUS_TYPE_INVALID))
691                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
692
693                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
694                         return bus_send_error_reply(m, connection, message, NULL, -EINVAL);
695
696                 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
697                         return bus_send_error_reply(m, connection, message, NULL, r);
698
699                 if (job_type == JOB_START && u->meta.only_by_dependency)
700                         return bus_send_error_reply(m, connection, message, NULL, -EPERM);
701
702                 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
703                         return bus_send_error_reply(m, connection, message, NULL, r);
704
705                 if (!(reply = dbus_message_new_method_return(message)))
706                         goto oom;
707
708                 if (!(path = job_dbus_path(j)))
709                         goto oom;
710
711                 if (!dbus_message_append_args(
712                                     reply,
713                                     DBUS_TYPE_OBJECT_PATH, &path,
714                                     DBUS_TYPE_INVALID))
715                         goto oom;
716         }
717
718         free(path);
719
720         if (reply) {
721                 if (!dbus_connection_send(connection, reply, NULL))
722                         goto oom;
723
724                 dbus_message_unref(reply);
725         }
726
727         return DBUS_HANDLER_RESULT_HANDLED;
728
729 oom:
730         free(path);
731
732         if (reply)
733                 dbus_message_unref(reply);
734
735         dbus_error_free(&error);
736
737         return DBUS_HANDLER_RESULT_NEED_MEMORY;
738 }
739
740 const DBusObjectPathVTable bus_manager_vtable = {
741         .message_function = bus_manager_message_handler
742 };