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