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