1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
26 #include "dbus-manager.h"
28 #include "bus-errors.h"
30 #define BUS_MANAGER_INTERFACE_BEGIN \
31 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
33 #define BUS_MANAGER_INTERFACE_METHODS \
34 " <method name=\"GetUnit\">\n" \
35 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
36 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
38 " <method name=\"GetUnitByPID\">\n" \
39 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
40 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
42 " <method name=\"LoadUnit\">\n" \
43 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
44 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
46 " <method name=\"StartUnit\">\n" \
47 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
48 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
51 " <method name=\"StartUnitReplace\">\n" \
52 " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
53 " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \
54 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
57 " <method name=\"StopUnit\">\n" \
58 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
59 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
60 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
62 " <method name=\"ReloadUnit\">\n" \
63 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
65 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
67 " <method name=\"RestartUnit\">\n" \
68 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
69 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
70 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
72 " <method name=\"TryRestartUnit\">\n" \
73 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
74 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
75 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
77 " <method name=\"ReloadOrRestartUnit\">\n" \
78 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
79 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
80 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
82 " <method name=\"ReloadOrTryRestartUnit\">\n" \
83 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
84 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
85 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
87 " <method name=\"KillUnit\">\n" \
88 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
89 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
90 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
91 " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
93 " <method name=\"ResetFailedUnit\">\n" \
94 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
96 " <method name=\"GetJob\">\n" \
97 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
98 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
100 " <method name=\"ClearJobs\"/>\n" \
101 " <method name=\"ResetFailed\"/>\n" \
102 " <method name=\"ListUnits\">\n" \
103 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
105 " <method name=\"ListJobs\">\n" \
106 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
108 " <method name=\"Subscribe\"/>\n" \
109 " <method name=\"Unsubscribe\"/>\n" \
110 " <method name=\"Dump\"/>\n" \
111 " <method name=\"CreateSnapshot\">\n" \
112 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
113 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
114 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
116 " <method name=\"Reload\"/>\n" \
117 " <method name=\"Reexecute\"/>\n" \
118 " <method name=\"Exit\"/>\n" \
119 " <method name=\"Reboot\"/>\n" \
120 " <method name=\"PowerOff\"/>\n" \
121 " <method name=\"Halt\"/>\n" \
122 " <method name=\"KExec\"/>\n" \
123 " <method name=\"SetEnvironment\">\n" \
124 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
126 " <method name=\"UnsetEnvironment\">\n" \
127 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
130 #define BUS_MANAGER_INTERFACE_SIGNALS \
131 " <signal name=\"UnitNew\">\n" \
132 " <arg name=\"id\" type=\"s\"/>\n" \
133 " <arg name=\"unit\" type=\"o\"/>\n" \
135 " <signal name=\"UnitRemoved\">\n" \
136 " <arg name=\"id\" type=\"s\"/>\n" \
137 " <arg name=\"unit\" type=\"o\"/>\n" \
139 " <signal name=\"JobNew\">\n" \
140 " <arg name=\"id\" type=\"u\"/>\n" \
141 " <arg name=\"job\" type=\"o\"/>\n" \
143 " <signal name=\"JobRemoved\">\n" \
144 " <arg name=\"id\" type=\"u\"/>\n" \
145 " <arg name=\"job\" type=\"o\"/>\n" \
146 " <arg name=\"success\" type=\"b\"/>\n" \
150 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
151 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
152 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
153 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
154 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
155 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
156 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
157 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
158 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
159 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
160 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
161 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
162 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
163 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
164 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
165 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
166 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
167 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
168 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
169 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
170 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n"
172 #ifdef HAVE_SYSV_COMPAT
173 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
174 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
175 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
176 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
178 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
181 #define BUS_MANAGER_INTERFACE_END \
184 #define BUS_MANAGER_INTERFACE \
185 BUS_MANAGER_INTERFACE_BEGIN \
186 BUS_MANAGER_INTERFACE_METHODS \
187 BUS_MANAGER_INTERFACE_SIGNALS \
188 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
189 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
190 BUS_MANAGER_INTERFACE_END
192 #define INTROSPECTION_BEGIN \
193 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
195 BUS_MANAGER_INTERFACE \
196 BUS_PROPERTIES_INTERFACE \
198 BUS_INTROSPECTABLE_INTERFACE
200 #define INTROSPECTION_END \
203 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
205 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
207 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
214 t = log_target_to_string(log_get_target());
216 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
222 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
229 t = log_level_to_string(log_get_max_level());
231 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
237 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
244 u = hashmap_size(m->units);
246 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
252 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
259 u = hashmap_size(m->jobs);
261 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
267 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
274 if (dual_timestamp_is_set(&m->finish_timestamp))
277 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
279 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
285 static const char *message_get_sender_with_fallback(DBusMessage *m) {
290 if ((s = dbus_message_get_sender(m)))
293 /* When the message came in from a direct connection the
294 * message will have no sender. We fix that here. */
299 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
302 const BusProperty properties[] = {
303 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
304 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
305 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime },
306 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
307 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
308 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
309 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
310 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
311 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
312 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
313 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
314 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
315 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
316 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
317 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
318 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
319 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
320 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
321 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
322 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
323 #ifdef HAVE_SYSV_COMPAT
324 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
325 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
326 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
328 { NULL, NULL, NULL, NULL, NULL }
333 DBusMessage *reply = NULL;
335 JobType job_type = _JOB_TYPE_INVALID;
336 bool reload_if_possible = false;
342 dbus_error_init(&error);
344 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
348 if (!dbus_message_get_args(
351 DBUS_TYPE_STRING, &name,
353 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
355 if (!(u = manager_get_unit(m, name))) {
356 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
357 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
360 if (!(reply = dbus_message_new_method_return(message)))
363 if (!(path = unit_dbus_path(u)))
366 if (!dbus_message_append_args(
368 DBUS_TYPE_OBJECT_PATH, &path,
371 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
375 if (!dbus_message_get_args(
378 DBUS_TYPE_UINT32, &pid,
380 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
382 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
383 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
384 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
387 if (!(reply = dbus_message_new_method_return(message)))
390 if (!(path = unit_dbus_path(u)))
393 if (!dbus_message_append_args(
395 DBUS_TYPE_OBJECT_PATH, &path,
398 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
402 if (!dbus_message_get_args(
405 DBUS_TYPE_STRING, &name,
407 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
409 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
410 return bus_send_error_reply(m, connection, message, &error, r);
412 if (!(reply = dbus_message_new_method_return(message)))
415 if (!(path = unit_dbus_path(u)))
418 if (!dbus_message_append_args(
420 DBUS_TYPE_OBJECT_PATH, &path,
424 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
425 job_type = JOB_START;
426 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
427 job_type = JOB_START;
428 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
430 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
431 job_type = JOB_RELOAD;
432 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
433 job_type = JOB_RESTART;
434 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
435 job_type = JOB_TRY_RESTART;
436 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
437 reload_if_possible = true;
438 job_type = JOB_RESTART;
439 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
440 reload_if_possible = true;
441 job_type = JOB_TRY_RESTART;
442 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
443 const char *name, *swho, *smode;
449 if (!dbus_message_get_args(
452 DBUS_TYPE_STRING, &name,
453 DBUS_TYPE_STRING, &swho,
454 DBUS_TYPE_STRING, &smode,
455 DBUS_TYPE_INT32, &signo,
457 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
459 if ((mode = kill_mode_from_string(smode)) < 0 ||
460 (who = kill_who_from_string(swho)) < 0 ||
463 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
465 if (!(u = manager_get_unit(m, name))) {
466 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
467 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
470 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
471 return bus_send_error_reply(m, connection, message, &error, r);
473 if (!(reply = dbus_message_new_method_return(message)))
476 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
480 if (!dbus_message_get_args(
483 DBUS_TYPE_UINT32, &id,
485 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
487 if (!(j = manager_get_job(m, id))) {
488 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
489 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
492 if (!(reply = dbus_message_new_method_return(message)))
495 if (!(path = job_dbus_path(j)))
498 if (!dbus_message_append_args(
500 DBUS_TYPE_OBJECT_PATH, &path,
504 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
506 manager_clear_jobs(m);
508 if (!(reply = dbus_message_new_method_return(message)))
511 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
513 manager_reset_failed(m);
515 if (!(reply = dbus_message_new_method_return(message)))
518 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
522 if (!dbus_message_get_args(
525 DBUS_TYPE_STRING, &name,
527 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
529 if (!(u = manager_get_unit(m, name))) {
530 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
531 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
534 unit_reset_failed(u);
536 if (!(reply = dbus_message_new_method_return(message)))
539 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
540 DBusMessageIter iter, sub;
545 if (!(reply = dbus_message_new_method_return(message)))
548 dbus_message_iter_init_append(reply, &iter);
550 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
553 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
554 char *u_path, *j_path;
555 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
556 DBusMessageIter sub2;
563 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
566 description = unit_description(u);
567 load_state = unit_load_state_to_string(u->meta.load_state);
568 active_state = unit_active_state_to_string(unit_active_state(u));
569 sub_state = unit_sub_state_to_string(u);
571 f = unit_following(u);
572 following = f ? f->meta.id : "";
574 if (!(u_path = unit_dbus_path(u)))
578 job_id = (uint32_t) u->meta.job->id;
580 if (!(j_path = job_dbus_path(u->meta.job))) {
585 sjob_type = job_type_to_string(u->meta.job->type);
592 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
593 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
594 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
595 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
596 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
597 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
598 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
599 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
600 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
601 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
612 if (!dbus_message_iter_close_container(&sub, &sub2))
616 if (!dbus_message_iter_close_container(&iter, &sub))
619 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
620 DBusMessageIter iter, sub;
624 if (!(reply = dbus_message_new_method_return(message)))
627 dbus_message_iter_init_append(reply, &iter);
629 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
632 HASHMAP_FOREACH(j, m->jobs, i) {
633 char *u_path, *j_path;
634 const char *state, *type;
636 DBusMessageIter sub2;
638 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
641 id = (uint32_t) j->id;
642 state = job_state_to_string(j->state);
643 type = job_type_to_string(j->type);
645 if (!(j_path = job_dbus_path(j)))
648 if (!(u_path = unit_dbus_path(j->unit))) {
653 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
654 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
655 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
656 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
657 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
658 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
667 if (!dbus_message_iter_close_container(&sub, &sub2))
671 if (!dbus_message_iter_close_container(&iter, &sub))
674 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
678 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
679 if (!(s = set_new(string_hash_func, string_compare_func)))
682 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
688 if (!(client = strdup(message_get_sender_with_fallback(message))))
691 if ((r = set_put(s, client)) < 0) {
693 return bus_send_error_reply(m, connection, message, NULL, r);
696 if (!(reply = dbus_message_new_method_return(message)))
699 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
702 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
703 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
704 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
709 if (!(reply = dbus_message_new_method_return(message)))
712 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
717 if (!(reply = dbus_message_new_method_return(message)))
720 if (!(f = open_memstream(&dump, &size)))
723 manager_dump_units(m, f, NULL);
724 manager_dump_jobs(m, f, NULL);
734 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
740 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
745 if (!dbus_message_get_args(
748 DBUS_TYPE_STRING, &name,
749 DBUS_TYPE_BOOLEAN, &cleanup,
751 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
753 if (name && name[0] == 0)
756 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
757 return bus_send_error_reply(m, connection, message, &error, r);
759 if (!(reply = dbus_message_new_method_return(message)))
762 if (!(path = unit_dbus_path(UNIT(s))))
765 if (!dbus_message_append_args(
767 DBUS_TYPE_OBJECT_PATH, &path,
771 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
772 char *introspection = NULL;
780 if (!(reply = dbus_message_new_method_return(message)))
783 /* We roll our own introspection code here, instead of
784 * relying on bus_default_message_handler() because we
785 * need to generate our introspection string
788 if (!(f = open_memstream(&introspection, &size)))
791 fputs(INTROSPECTION_BEGIN, f);
793 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
799 if (!(p = bus_path_escape(k))) {
805 fprintf(f, "<node name=\"unit/%s\"/>", p);
809 HASHMAP_FOREACH(j, m->jobs, i)
810 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
812 fputs(INTROSPECTION_END, f);
825 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
832 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
834 assert(!m->queued_message);
836 /* Instead of sending the reply back right away, we
837 * just remember that we need to and then send it
838 * after the reload is finished. That way the caller
839 * knows when the reload finished. */
841 if (!(m->queued_message = dbus_message_new_method_return(message)))
844 m->queued_message_connection = connection;
845 m->exit_code = MANAGER_RELOAD;
847 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
849 if (!(reply = dbus_message_new_method_return(message)))
852 m->exit_code = MANAGER_REEXECUTE;
854 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
856 if (m->running_as == MANAGER_SYSTEM) {
857 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
858 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
861 if (!(reply = dbus_message_new_method_return(message)))
864 m->exit_code = MANAGER_EXIT;
866 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
868 if (m->running_as != MANAGER_SYSTEM) {
869 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
870 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
873 if (!(reply = dbus_message_new_method_return(message)))
876 m->exit_code = MANAGER_REBOOT;
878 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
880 if (m->running_as != MANAGER_SYSTEM) {
881 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
882 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
885 if (!(reply = dbus_message_new_method_return(message)))
888 m->exit_code = MANAGER_POWEROFF;
890 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
892 if (m->running_as != MANAGER_SYSTEM) {
893 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
894 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
897 if (!(reply = dbus_message_new_method_return(message)))
900 m->exit_code = MANAGER_HALT;
902 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
904 if (m->running_as != MANAGER_SYSTEM) {
905 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
906 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
909 if (!(reply = dbus_message_new_method_return(message)))
912 m->exit_code = MANAGER_KEXEC;
914 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
915 char **l = NULL, **e = NULL;
917 if ((r = bus_parse_strv(message, &l)) < 0) {
921 return bus_send_error_reply(m, connection, message, NULL, r);
924 e = strv_env_merge(2, m->environment, l);
930 if (!(reply = dbus_message_new_method_return(message))) {
935 strv_free(m->environment);
938 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
939 char **l = NULL, **e = NULL;
941 if ((r = bus_parse_strv(message, &l)) < 0) {
945 return bus_send_error_reply(m, connection, message, NULL, r);
948 e = strv_env_delete(m->environment, 1, l);
954 if (!(reply = dbus_message_new_method_return(message)))
957 strv_free(m->environment);
961 return bus_default_message_handler(m, connection, message, NULL, properties);
963 if (job_type != _JOB_TYPE_INVALID) {
964 const char *name, *smode, *old_name = NULL;
970 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
971 b = dbus_message_get_args(
974 DBUS_TYPE_STRING, &old_name,
975 DBUS_TYPE_STRING, &name,
976 DBUS_TYPE_STRING, &smode,
979 b = dbus_message_get_args(
982 DBUS_TYPE_STRING, &name,
983 DBUS_TYPE_STRING, &smode,
987 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
990 if (!(u = manager_get_unit(m, old_name)) ||
992 u->meta.job->type != JOB_START) {
993 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
994 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
998 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
999 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1000 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1003 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1004 return bus_send_error_reply(m, connection, message, &error, r);
1006 if (reload_if_possible && unit_can_reload(u)) {
1007 if (job_type == JOB_RESTART)
1008 job_type = JOB_RELOAD_OR_START;
1009 else if (job_type == JOB_TRY_RESTART)
1010 job_type = JOB_RELOAD;
1013 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1014 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1015 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1016 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1017 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1018 return bus_send_error_reply(m, connection, message, &error, -EPERM);
1021 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1022 return bus_send_error_reply(m, connection, message, &error, r);
1024 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1027 j->bus = connection;
1029 if (!(reply = dbus_message_new_method_return(message)))
1032 if (!(path = job_dbus_path(j)))
1035 if (!dbus_message_append_args(
1037 DBUS_TYPE_OBJECT_PATH, &path,
1045 if (!dbus_connection_send(connection, reply, NULL))
1048 dbus_message_unref(reply);
1051 return DBUS_HANDLER_RESULT_HANDLED;
1057 dbus_message_unref(reply);
1059 dbus_error_free(&error);
1061 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1064 const DBusObjectPathVTable bus_manager_vtable = {
1065 .message_function = bus_manager_message_handler