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