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