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