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=\"ResetFailedUnit\">\n" \
88 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
90 " <method name=\"GetJob\">\n" \
91 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
92 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
94 " <method name=\"ClearJobs\"/>\n" \
95 " <method name=\"ResetFailed\"/>\n" \
96 " <method name=\"ListUnits\">\n" \
97 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
99 " <method name=\"ListJobs\">\n" \
100 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
102 " <method name=\"Subscribe\"/>\n" \
103 " <method name=\"Unsubscribe\"/>\n" \
104 " <method name=\"Dump\"/>\n" \
105 " <method name=\"CreateSnapshot\">\n" \
106 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
107 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
108 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
110 " <method name=\"Reload\"/>\n" \
111 " <method name=\"Reexecute\"/>\n" \
112 " <method name=\"Exit\"/>\n" \
113 " <method name=\"Reboot\"/>\n" \
114 " <method name=\"PowerOff\"/>\n" \
115 " <method name=\"Halt\"/>\n" \
116 " <method name=\"KExec\"/>\n" \
117 " <method name=\"SetEnvironment\">\n" \
118 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
120 " <method name=\"UnsetEnvironment\">\n" \
121 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
124 #define BUS_MANAGER_INTERFACE_SIGNALS \
125 " <signal name=\"UnitNew\">\n" \
126 " <arg name=\"id\" type=\"s\"/>\n" \
127 " <arg name=\"unit\" type=\"o\"/>\n" \
129 " <signal name=\"UnitRemoved\">\n" \
130 " <arg name=\"id\" type=\"s\"/>\n" \
131 " <arg name=\"unit\" type=\"o\"/>\n" \
133 " <signal name=\"JobNew\">\n" \
134 " <arg name=\"id\" type=\"u\"/>\n" \
135 " <arg name=\"job\" type=\"o\"/>\n" \
137 " <signal name=\"JobRemoved\">\n" \
138 " <arg name=\"id\" type=\"u\"/>\n" \
139 " <arg name=\"job\" type=\"o\"/>\n" \
140 " <arg name=\"success\" type=\"b\"/>\n" \
144 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
145 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
146 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
147 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
148 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
149 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
150 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
151 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
152 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
153 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
154 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
155 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
156 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
157 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
158 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
159 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
160 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
161 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
162 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n"
164 #ifdef HAVE_SYSV_COMPAT
165 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
166 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
167 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
168 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
170 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
173 #define BUS_MANAGER_INTERFACE_END \
176 #define BUS_MANAGER_INTERFACE \
177 BUS_MANAGER_INTERFACE_BEGIN \
178 BUS_MANAGER_INTERFACE_METHODS \
179 BUS_MANAGER_INTERFACE_SIGNALS \
180 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
181 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
182 BUS_MANAGER_INTERFACE_END
184 #define INTROSPECTION_BEGIN \
185 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
187 BUS_MANAGER_INTERFACE \
188 BUS_PROPERTIES_INTERFACE \
190 BUS_INTROSPECTABLE_INTERFACE
192 #define INTROSPECTION_END \
195 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
197 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
199 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
206 t = log_target_to_string(log_get_target());
208 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
214 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
221 t = log_level_to_string(log_get_max_level());
223 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
229 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
236 u = hashmap_size(m->units);
238 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
244 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
251 u = hashmap_size(m->jobs);
253 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
259 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
266 if (dual_timestamp_is_set(&m->finish_timestamp))
269 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
271 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
277 static const char *message_get_sender_with_fallback(DBusMessage *m) {
282 if ((s = dbus_message_get_sender(m)))
285 /* When the message came in from a direct connection the
286 * message will have no sender. We fix that here. */
291 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
294 const BusProperty properties[] = {
295 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
296 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
297 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
298 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
299 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
300 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
301 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
302 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
303 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
304 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
305 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
306 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
307 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
308 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
309 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
310 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
311 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
312 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
313 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
314 #ifdef HAVE_SYSV_COMPAT
315 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
316 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
317 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
319 { NULL, NULL, NULL, NULL, NULL }
324 DBusMessage *reply = NULL;
326 JobType job_type = _JOB_TYPE_INVALID;
327 bool reload_if_possible = false;
333 dbus_error_init(&error);
335 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
339 if (!dbus_message_get_args(
342 DBUS_TYPE_STRING, &name,
344 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
346 if (!(u = manager_get_unit(m, name))) {
347 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
348 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
351 if (!(reply = dbus_message_new_method_return(message)))
354 if (!(path = unit_dbus_path(u)))
357 if (!dbus_message_append_args(
359 DBUS_TYPE_OBJECT_PATH, &path,
362 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
366 if (!dbus_message_get_args(
369 DBUS_TYPE_UINT32, &pid,
371 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
373 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
374 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
375 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
378 if (!(reply = dbus_message_new_method_return(message)))
381 if (!(path = unit_dbus_path(u)))
384 if (!dbus_message_append_args(
386 DBUS_TYPE_OBJECT_PATH, &path,
389 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
393 if (!dbus_message_get_args(
396 DBUS_TYPE_STRING, &name,
398 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
400 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
401 return bus_send_error_reply(m, connection, message, &error, r);
403 if (!(reply = dbus_message_new_method_return(message)))
406 if (!(path = unit_dbus_path(u)))
409 if (!dbus_message_append_args(
411 DBUS_TYPE_OBJECT_PATH, &path,
415 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
416 job_type = JOB_START;
417 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
418 job_type = JOB_START;
419 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
421 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
422 job_type = JOB_RELOAD;
423 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
424 job_type = JOB_RESTART;
425 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
426 job_type = JOB_TRY_RESTART;
427 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
428 reload_if_possible = true;
429 job_type = JOB_RESTART;
430 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
431 reload_if_possible = true;
432 job_type = JOB_TRY_RESTART;
433 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
437 if (!dbus_message_get_args(
440 DBUS_TYPE_UINT32, &id,
442 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
444 if (!(j = manager_get_job(m, id))) {
445 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
446 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
449 if (!(reply = dbus_message_new_method_return(message)))
452 if (!(path = job_dbus_path(j)))
455 if (!dbus_message_append_args(
457 DBUS_TYPE_OBJECT_PATH, &path,
461 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
463 manager_clear_jobs(m);
465 if (!(reply = dbus_message_new_method_return(message)))
468 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
470 manager_reset_failed(m);
472 if (!(reply = dbus_message_new_method_return(message)))
475 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
479 if (!dbus_message_get_args(
482 DBUS_TYPE_STRING, &name,
484 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
486 if (!(u = manager_get_unit(m, name))) {
487 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
488 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
491 unit_reset_failed(u);
493 if (!(reply = dbus_message_new_method_return(message)))
496 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
497 DBusMessageIter iter, sub;
502 if (!(reply = dbus_message_new_method_return(message)))
505 dbus_message_iter_init_append(reply, &iter);
507 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
510 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
511 char *u_path, *j_path;
512 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
513 DBusMessageIter sub2;
520 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
523 description = unit_description(u);
524 load_state = unit_load_state_to_string(u->meta.load_state);
525 active_state = unit_active_state_to_string(unit_active_state(u));
526 sub_state = unit_sub_state_to_string(u);
528 f = unit_following(u);
529 following = f ? f->meta.id : "";
531 if (!(u_path = unit_dbus_path(u)))
535 job_id = (uint32_t) u->meta.job->id;
537 if (!(j_path = job_dbus_path(u->meta.job))) {
542 sjob_type = job_type_to_string(u->meta.job->type);
549 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
550 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
551 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
552 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
553 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
554 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
555 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
556 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
557 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
558 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
569 if (!dbus_message_iter_close_container(&sub, &sub2))
573 if (!dbus_message_iter_close_container(&iter, &sub))
576 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
577 DBusMessageIter iter, sub;
581 if (!(reply = dbus_message_new_method_return(message)))
584 dbus_message_iter_init_append(reply, &iter);
586 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
589 HASHMAP_FOREACH(j, m->jobs, i) {
590 char *u_path, *j_path;
591 const char *state, *type;
593 DBusMessageIter sub2;
595 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
598 id = (uint32_t) j->id;
599 state = job_state_to_string(j->state);
600 type = job_type_to_string(j->type);
602 if (!(j_path = job_dbus_path(j)))
605 if (!(u_path = unit_dbus_path(j->unit))) {
610 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
611 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
612 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
613 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
614 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
615 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
624 if (!dbus_message_iter_close_container(&sub, &sub2))
628 if (!dbus_message_iter_close_container(&iter, &sub))
631 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
635 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
636 if (!(s = set_new(string_hash_func, string_compare_func)))
639 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
645 if (!(client = strdup(message_get_sender_with_fallback(message))))
648 if ((r = set_put(s, client)) < 0) {
650 return bus_send_error_reply(m, connection, message, NULL, r);
653 if (!(reply = dbus_message_new_method_return(message)))
656 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
659 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
660 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
661 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
666 if (!(reply = dbus_message_new_method_return(message)))
669 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
674 if (!(reply = dbus_message_new_method_return(message)))
677 if (!(f = open_memstream(&dump, &size)))
680 manager_dump_units(m, f, NULL);
681 manager_dump_jobs(m, f, NULL);
691 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
697 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
702 if (!dbus_message_get_args(
705 DBUS_TYPE_STRING, &name,
706 DBUS_TYPE_BOOLEAN, &cleanup,
708 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
710 if (name && name[0] == 0)
713 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
714 return bus_send_error_reply(m, connection, message, &error, r);
716 if (!(reply = dbus_message_new_method_return(message)))
719 if (!(path = unit_dbus_path(UNIT(s))))
722 if (!dbus_message_append_args(
724 DBUS_TYPE_OBJECT_PATH, &path,
728 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
729 char *introspection = NULL;
737 if (!(reply = dbus_message_new_method_return(message)))
740 /* We roll our own introspection code here, instead of
741 * relying on bus_default_message_handler() because we
742 * need to generate our introspection string
745 if (!(f = open_memstream(&introspection, &size)))
748 fputs(INTROSPECTION_BEGIN, f);
750 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
756 if (!(p = bus_path_escape(k))) {
762 fprintf(f, "<node name=\"unit/%s\"/>", p);
766 HASHMAP_FOREACH(j, m->jobs, i)
767 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
769 fputs(INTROSPECTION_END, f);
782 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
789 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
791 assert(!m->queued_message);
793 /* Instead of sending the reply back right away, we
794 * just remember that we need to and then send it
795 * after the reload is finished. That way the caller
796 * knows when the reload finished. */
798 if (!(m->queued_message = dbus_message_new_method_return(message)))
801 m->queued_message_connection = connection;
802 m->exit_code = MANAGER_RELOAD;
804 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
806 if (!(reply = dbus_message_new_method_return(message)))
809 m->exit_code = MANAGER_REEXECUTE;
811 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
813 if (m->running_as == MANAGER_SYSTEM) {
814 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
815 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
818 if (!(reply = dbus_message_new_method_return(message)))
821 m->exit_code = MANAGER_EXIT;
823 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
825 if (m->running_as != MANAGER_SYSTEM) {
826 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
827 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
830 if (!(reply = dbus_message_new_method_return(message)))
833 m->exit_code = MANAGER_REBOOT;
835 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
837 if (m->running_as != MANAGER_SYSTEM) {
838 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
839 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
842 if (!(reply = dbus_message_new_method_return(message)))
845 m->exit_code = MANAGER_POWEROFF;
847 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
849 if (m->running_as != MANAGER_SYSTEM) {
850 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
851 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
854 if (!(reply = dbus_message_new_method_return(message)))
857 m->exit_code = MANAGER_HALT;
859 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
861 if (m->running_as != MANAGER_SYSTEM) {
862 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
863 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
866 if (!(reply = dbus_message_new_method_return(message)))
869 m->exit_code = MANAGER_KEXEC;
871 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
872 char **l = NULL, **e = NULL;
874 if ((r = bus_parse_strv(message, &l)) < 0) {
878 return bus_send_error_reply(m, connection, message, NULL, r);
881 e = strv_env_merge(2, m->environment, l);
887 if (!(reply = dbus_message_new_method_return(message))) {
892 strv_free(m->environment);
895 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
896 char **l = NULL, **e = NULL;
898 if ((r = bus_parse_strv(message, &l)) < 0) {
902 return bus_send_error_reply(m, connection, message, NULL, r);
905 e = strv_env_delete(m->environment, 1, l);
911 if (!(reply = dbus_message_new_method_return(message)))
914 strv_free(m->environment);
918 return bus_default_message_handler(m, connection, message, NULL, properties);
920 if (job_type != _JOB_TYPE_INVALID) {
921 const char *name, *smode, *old_name = NULL;
927 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
928 b = dbus_message_get_args(
931 DBUS_TYPE_STRING, &old_name,
932 DBUS_TYPE_STRING, &name,
933 DBUS_TYPE_STRING, &smode,
936 b = dbus_message_get_args(
939 DBUS_TYPE_STRING, &name,
940 DBUS_TYPE_STRING, &smode,
944 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
947 if (!(u = manager_get_unit(m, old_name)) ||
949 u->meta.job->type != JOB_START) {
950 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
951 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
955 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
956 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
957 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
960 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
961 return bus_send_error_reply(m, connection, message, &error, r);
963 if (reload_if_possible && unit_can_reload(u)) {
964 if (job_type == JOB_RESTART)
965 job_type = JOB_RELOAD_OR_START;
966 else if (job_type == JOB_TRY_RESTART)
967 job_type = JOB_RELOAD;
970 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
971 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
972 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
973 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
974 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
975 return bus_send_error_reply(m, connection, message, &error, -EPERM);
978 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
979 return bus_send_error_reply(m, connection, message, &error, r);
981 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
986 if (!(reply = dbus_message_new_method_return(message)))
989 if (!(path = job_dbus_path(j)))
992 if (!dbus_message_append_args(
994 DBUS_TYPE_OBJECT_PATH, &path,
1002 if (!dbus_connection_send(connection, reply, NULL))
1005 dbus_message_unref(reply);
1008 return DBUS_HANDLER_RESULT_HANDLED;
1014 dbus_message_unref(reply);
1016 dbus_error_free(&error);
1018 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1021 const DBusObjectPathVTable bus_manager_vtable = {
1022 .message_function = bus_manager_message_handler