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