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=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
154 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
155 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
156 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
157 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
158 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
159 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
160 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
161 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
162 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
163 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
164 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
165 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
166 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
167 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
168 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n"
170 #ifdef HAVE_SYSV_COMPAT
171 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
172 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
173 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
174 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
176 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
179 #define BUS_MANAGER_INTERFACE_END \
182 #define BUS_MANAGER_INTERFACE \
183 BUS_MANAGER_INTERFACE_BEGIN \
184 BUS_MANAGER_INTERFACE_METHODS \
185 BUS_MANAGER_INTERFACE_SIGNALS \
186 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
187 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
188 BUS_MANAGER_INTERFACE_END
190 #define INTROSPECTION_BEGIN \
191 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
193 BUS_MANAGER_INTERFACE \
194 BUS_PROPERTIES_INTERFACE \
196 BUS_INTROSPECTABLE_INTERFACE
198 #define INTROSPECTION_END \
201 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
203 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
205 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
212 t = log_target_to_string(log_get_target());
214 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
220 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
227 t = log_level_to_string(log_get_max_level());
229 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
235 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
242 u = hashmap_size(m->units);
244 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
250 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
257 u = hashmap_size(m->jobs);
259 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
265 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
272 if (dual_timestamp_is_set(&m->finish_timestamp))
275 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
277 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
283 static const char *message_get_sender_with_fallback(DBusMessage *m) {
288 if ((s = dbus_message_get_sender(m)))
291 /* When the message came in from a direct connection the
292 * message will have no sender. We fix that here. */
297 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
300 const BusProperty properties[] = {
301 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
302 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
303 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
304 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
305 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
306 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
307 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
308 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
309 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
310 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
311 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
312 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
313 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
314 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
315 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
316 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
317 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
318 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
319 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
320 #ifdef HAVE_SYSV_COMPAT
321 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
322 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
323 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
325 { NULL, NULL, NULL, NULL, NULL }
330 DBusMessage *reply = NULL;
332 JobType job_type = _JOB_TYPE_INVALID;
333 bool reload_if_possible = false;
339 dbus_error_init(&error);
341 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
345 if (!dbus_message_get_args(
348 DBUS_TYPE_STRING, &name,
350 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
352 if (!(u = manager_get_unit(m, name))) {
353 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
354 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
357 if (!(reply = dbus_message_new_method_return(message)))
360 if (!(path = unit_dbus_path(u)))
363 if (!dbus_message_append_args(
365 DBUS_TYPE_OBJECT_PATH, &path,
368 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
372 if (!dbus_message_get_args(
375 DBUS_TYPE_UINT32, &pid,
377 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
379 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
380 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
381 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
384 if (!(reply = dbus_message_new_method_return(message)))
387 if (!(path = unit_dbus_path(u)))
390 if (!dbus_message_append_args(
392 DBUS_TYPE_OBJECT_PATH, &path,
395 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
399 if (!dbus_message_get_args(
402 DBUS_TYPE_STRING, &name,
404 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
406 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
407 return bus_send_error_reply(m, connection, message, &error, r);
409 if (!(reply = dbus_message_new_method_return(message)))
412 if (!(path = unit_dbus_path(u)))
415 if (!dbus_message_append_args(
417 DBUS_TYPE_OBJECT_PATH, &path,
421 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
422 job_type = JOB_START;
423 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
424 job_type = JOB_START;
425 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
427 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
428 job_type = JOB_RELOAD;
429 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
430 job_type = JOB_RESTART;
431 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
432 job_type = JOB_TRY_RESTART;
433 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
434 reload_if_possible = true;
435 job_type = JOB_RESTART;
436 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
437 reload_if_possible = true;
438 job_type = JOB_TRY_RESTART;
439 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
440 const char *name, *swho, *smode;
446 if (!dbus_message_get_args(
449 DBUS_TYPE_STRING, &name,
450 DBUS_TYPE_STRING, &swho,
451 DBUS_TYPE_STRING, &smode,
452 DBUS_TYPE_INT32, &signo,
454 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
456 if ((mode = kill_mode_from_string(smode)) < 0 ||
457 (who = kill_who_from_string(swho)) < 0 ||
460 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
462 if (!(u = manager_get_unit(m, name))) {
463 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
464 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
467 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
468 return bus_send_error_reply(m, connection, message, &error, r);
470 if (!(reply = dbus_message_new_method_return(message)))
473 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
477 if (!dbus_message_get_args(
480 DBUS_TYPE_UINT32, &id,
482 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
484 if (!(j = manager_get_job(m, id))) {
485 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
486 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
489 if (!(reply = dbus_message_new_method_return(message)))
492 if (!(path = job_dbus_path(j)))
495 if (!dbus_message_append_args(
497 DBUS_TYPE_OBJECT_PATH, &path,
501 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
503 manager_clear_jobs(m);
505 if (!(reply = dbus_message_new_method_return(message)))
508 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
510 manager_reset_failed(m);
512 if (!(reply = dbus_message_new_method_return(message)))
515 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
519 if (!dbus_message_get_args(
522 DBUS_TYPE_STRING, &name,
524 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
526 if (!(u = manager_get_unit(m, name))) {
527 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
528 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
531 unit_reset_failed(u);
533 if (!(reply = dbus_message_new_method_return(message)))
536 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
537 DBusMessageIter iter, sub;
542 if (!(reply = dbus_message_new_method_return(message)))
545 dbus_message_iter_init_append(reply, &iter);
547 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
550 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
551 char *u_path, *j_path;
552 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
553 DBusMessageIter sub2;
560 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
563 description = unit_description(u);
564 load_state = unit_load_state_to_string(u->meta.load_state);
565 active_state = unit_active_state_to_string(unit_active_state(u));
566 sub_state = unit_sub_state_to_string(u);
568 f = unit_following(u);
569 following = f ? f->meta.id : "";
571 if (!(u_path = unit_dbus_path(u)))
575 job_id = (uint32_t) u->meta.job->id;
577 if (!(j_path = job_dbus_path(u->meta.job))) {
582 sjob_type = job_type_to_string(u->meta.job->type);
589 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
590 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
591 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
592 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
593 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
594 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
595 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
596 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
597 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
598 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
609 if (!dbus_message_iter_close_container(&sub, &sub2))
613 if (!dbus_message_iter_close_container(&iter, &sub))
616 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
617 DBusMessageIter iter, sub;
621 if (!(reply = dbus_message_new_method_return(message)))
624 dbus_message_iter_init_append(reply, &iter);
626 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
629 HASHMAP_FOREACH(j, m->jobs, i) {
630 char *u_path, *j_path;
631 const char *state, *type;
633 DBusMessageIter sub2;
635 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
638 id = (uint32_t) j->id;
639 state = job_state_to_string(j->state);
640 type = job_type_to_string(j->type);
642 if (!(j_path = job_dbus_path(j)))
645 if (!(u_path = unit_dbus_path(j->unit))) {
650 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
651 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
652 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
653 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
654 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
655 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
664 if (!dbus_message_iter_close_container(&sub, &sub2))
668 if (!dbus_message_iter_close_container(&iter, &sub))
671 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
675 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
676 if (!(s = set_new(string_hash_func, string_compare_func)))
679 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
685 if (!(client = strdup(message_get_sender_with_fallback(message))))
688 if ((r = set_put(s, client)) < 0) {
690 return bus_send_error_reply(m, connection, message, NULL, r);
693 if (!(reply = dbus_message_new_method_return(message)))
696 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
699 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
700 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
701 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
706 if (!(reply = dbus_message_new_method_return(message)))
709 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
714 if (!(reply = dbus_message_new_method_return(message)))
717 if (!(f = open_memstream(&dump, &size)))
720 manager_dump_units(m, f, NULL);
721 manager_dump_jobs(m, f, NULL);
731 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
737 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
742 if (!dbus_message_get_args(
745 DBUS_TYPE_STRING, &name,
746 DBUS_TYPE_BOOLEAN, &cleanup,
748 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
750 if (name && name[0] == 0)
753 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
754 return bus_send_error_reply(m, connection, message, &error, r);
756 if (!(reply = dbus_message_new_method_return(message)))
759 if (!(path = unit_dbus_path(UNIT(s))))
762 if (!dbus_message_append_args(
764 DBUS_TYPE_OBJECT_PATH, &path,
768 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
769 char *introspection = NULL;
777 if (!(reply = dbus_message_new_method_return(message)))
780 /* We roll our own introspection code here, instead of
781 * relying on bus_default_message_handler() because we
782 * need to generate our introspection string
785 if (!(f = open_memstream(&introspection, &size)))
788 fputs(INTROSPECTION_BEGIN, f);
790 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
796 if (!(p = bus_path_escape(k))) {
802 fprintf(f, "<node name=\"unit/%s\"/>", p);
806 HASHMAP_FOREACH(j, m->jobs, i)
807 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
809 fputs(INTROSPECTION_END, f);
822 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
829 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
831 assert(!m->queued_message);
833 /* Instead of sending the reply back right away, we
834 * just remember that we need to and then send it
835 * after the reload is finished. That way the caller
836 * knows when the reload finished. */
838 if (!(m->queued_message = dbus_message_new_method_return(message)))
841 m->queued_message_connection = connection;
842 m->exit_code = MANAGER_RELOAD;
844 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
846 if (!(reply = dbus_message_new_method_return(message)))
849 m->exit_code = MANAGER_REEXECUTE;
851 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
853 if (m->running_as == MANAGER_SYSTEM) {
854 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
855 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
858 if (!(reply = dbus_message_new_method_return(message)))
861 m->exit_code = MANAGER_EXIT;
863 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
865 if (m->running_as != MANAGER_SYSTEM) {
866 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
867 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
870 if (!(reply = dbus_message_new_method_return(message)))
873 m->exit_code = MANAGER_REBOOT;
875 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
877 if (m->running_as != MANAGER_SYSTEM) {
878 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
879 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
882 if (!(reply = dbus_message_new_method_return(message)))
885 m->exit_code = MANAGER_POWEROFF;
887 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
889 if (m->running_as != MANAGER_SYSTEM) {
890 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
891 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
894 if (!(reply = dbus_message_new_method_return(message)))
897 m->exit_code = MANAGER_HALT;
899 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
901 if (m->running_as != MANAGER_SYSTEM) {
902 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
903 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
906 if (!(reply = dbus_message_new_method_return(message)))
909 m->exit_code = MANAGER_KEXEC;
911 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
912 char **l = NULL, **e = NULL;
914 if ((r = bus_parse_strv(message, &l)) < 0) {
918 return bus_send_error_reply(m, connection, message, NULL, r);
921 e = strv_env_merge(2, m->environment, l);
927 if (!(reply = dbus_message_new_method_return(message))) {
932 strv_free(m->environment);
935 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
936 char **l = NULL, **e = NULL;
938 if ((r = bus_parse_strv(message, &l)) < 0) {
942 return bus_send_error_reply(m, connection, message, NULL, r);
945 e = strv_env_delete(m->environment, 1, l);
951 if (!(reply = dbus_message_new_method_return(message)))
954 strv_free(m->environment);
958 return bus_default_message_handler(m, connection, message, NULL, properties);
960 if (job_type != _JOB_TYPE_INVALID) {
961 const char *name, *smode, *old_name = NULL;
967 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
968 b = dbus_message_get_args(
971 DBUS_TYPE_STRING, &old_name,
972 DBUS_TYPE_STRING, &name,
973 DBUS_TYPE_STRING, &smode,
976 b = dbus_message_get_args(
979 DBUS_TYPE_STRING, &name,
980 DBUS_TYPE_STRING, &smode,
984 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
987 if (!(u = manager_get_unit(m, old_name)) ||
989 u->meta.job->type != JOB_START) {
990 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
991 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
995 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
996 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
997 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1000 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1001 return bus_send_error_reply(m, connection, message, &error, r);
1003 if (reload_if_possible && unit_can_reload(u)) {
1004 if (job_type == JOB_RESTART)
1005 job_type = JOB_RELOAD_OR_START;
1006 else if (job_type == JOB_TRY_RESTART)
1007 job_type = JOB_RELOAD;
1010 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1011 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1012 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1013 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1014 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1015 return bus_send_error_reply(m, connection, message, &error, -EPERM);
1018 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1019 return bus_send_error_reply(m, connection, message, &error, r);
1021 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1024 j->bus = connection;
1026 if (!(reply = dbus_message_new_method_return(message)))
1029 if (!(path = job_dbus_path(j)))
1032 if (!dbus_message_append_args(
1034 DBUS_TYPE_OBJECT_PATH, &path,
1042 if (!dbus_connection_send(connection, reply, NULL))
1045 dbus_message_unref(reply);
1048 return DBUS_HANDLER_RESULT_HANDLED;
1054 dbus_message_unref(reply);
1056 dbus_error_free(&error);
1058 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1061 const DBusObjectPathVTable bus_manager_vtable = {
1062 .message_function = bus_manager_message_handler