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=\"StopUnit\">\n" \
52 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
53 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
54 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
56 " <method name=\"ReloadUnit\">\n" \
57 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
58 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
59 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
61 " <method name=\"RestartUnit\">\n" \
62 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
63 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
66 " <method name=\"TryRestartUnit\">\n" \
67 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
68 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
69 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
71 " <method name=\"ReloadOrRestartUnit\">\n" \
72 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
73 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
74 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
76 " <method name=\"ReloadOrTryRestartUnit\">\n" \
77 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
78 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
79 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
81 " <method name=\"ResetFailedUnit\">\n" \
82 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
84 " <method name=\"GetJob\">\n" \
85 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
86 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
88 " <method name=\"ClearJobs\"/>\n" \
89 " <method name=\"ResetFailed\"/>\n" \
90 " <method name=\"ListUnits\">\n" \
91 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
93 " <method name=\"ListJobs\">\n" \
94 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
96 " <method name=\"Subscribe\"/>\n" \
97 " <method name=\"Unsubscribe\"/>\n" \
98 " <method name=\"Dump\"/>\n" \
99 " <method name=\"CreateSnapshot\">\n" \
100 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
101 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
102 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
104 " <method name=\"Reload\"/>\n" \
105 " <method name=\"Reexecute\"/>\n" \
106 " <method name=\"Exit\"/>\n" \
107 " <method name=\"Reboot\"/>\n" \
108 " <method name=\"PowerOff\"/>\n" \
109 " <method name=\"Halt\"/>\n" \
110 " <method name=\"KExec\"/>\n" \
111 " <method name=\"SetEnvironment\">\n" \
112 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
114 " <method name=\"UnsetEnvironment\">\n" \
115 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
118 #define BUS_MANAGER_INTERFACE_SIGNALS \
119 " <signal name=\"UnitNew\">\n" \
120 " <arg name=\"id\" type=\"s\"/>\n" \
121 " <arg name=\"unit\" type=\"o\"/>\n" \
123 " <signal name=\"UnitRemoved\">\n" \
124 " <arg name=\"id\" type=\"s\"/>\n" \
125 " <arg name=\"unit\" type=\"o\"/>\n" \
127 " <signal name=\"JobNew\">\n" \
128 " <arg name=\"id\" type=\"u\"/>\n" \
129 " <arg name=\"job\" type=\"o\"/>\n" \
131 " <signal name=\"JobRemoved\">\n" \
132 " <arg name=\"id\" type=\"u\"/>\n" \
133 " <arg name=\"job\" type=\"o\"/>\n" \
134 " <arg name=\"success\" type=\"b\"/>\n" \
138 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
139 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
140 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
141 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
142 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
143 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
144 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
145 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
146 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
147 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
148 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
149 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
150 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
151 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
152 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
153 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
154 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
155 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
156 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n"
158 #ifdef HAVE_SYSV_COMPAT
159 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
160 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
161 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
162 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
164 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
167 #define BUS_MANAGER_INTERFACE_END \
170 #define BUS_MANAGER_INTERFACE \
171 BUS_MANAGER_INTERFACE_BEGIN \
172 BUS_MANAGER_INTERFACE_METHODS \
173 BUS_MANAGER_INTERFACE_SIGNALS \
174 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
175 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
176 BUS_MANAGER_INTERFACE_END
178 #define INTROSPECTION_BEGIN \
179 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
181 BUS_MANAGER_INTERFACE \
182 BUS_PROPERTIES_INTERFACE \
184 BUS_INTROSPECTABLE_INTERFACE
186 #define INTROSPECTION_END \
189 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
191 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
193 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
200 t = log_target_to_string(log_get_target());
202 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
208 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
215 t = log_level_to_string(log_get_max_level());
217 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
223 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
230 u = hashmap_size(m->units);
232 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
238 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
245 u = hashmap_size(m->jobs);
247 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
253 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
260 if (dual_timestamp_is_set(&m->finish_timestamp))
263 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
265 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
271 static const char *message_get_sender_with_fallback(DBusMessage *m) {
276 if ((s = dbus_message_get_sender(m)))
279 /* When the message came in from a direct connection the
280 * message will have no sender. We fix that here. */
285 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
288 const BusProperty properties[] = {
289 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
290 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
291 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
292 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
293 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
294 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
295 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
296 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
297 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
298 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
299 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
300 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
301 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
302 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
303 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
304 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
305 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
306 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
307 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
308 #ifdef HAVE_SYSV_COMPAT
309 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
310 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
311 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
313 { NULL, NULL, NULL, NULL, NULL }
318 DBusMessage *reply = NULL;
320 JobType job_type = _JOB_TYPE_INVALID;
321 bool reload_if_possible = false;
327 dbus_error_init(&error);
329 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
333 if (!dbus_message_get_args(
336 DBUS_TYPE_STRING, &name,
338 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
340 if (!(u = manager_get_unit(m, name))) {
341 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
342 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
345 if (!(reply = dbus_message_new_method_return(message)))
348 if (!(path = unit_dbus_path(u)))
351 if (!dbus_message_append_args(
353 DBUS_TYPE_OBJECT_PATH, &path,
356 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
360 if (!dbus_message_get_args(
363 DBUS_TYPE_UINT32, &pid,
365 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
367 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
368 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
369 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
372 if (!(reply = dbus_message_new_method_return(message)))
375 if (!(path = unit_dbus_path(u)))
378 if (!dbus_message_append_args(
380 DBUS_TYPE_OBJECT_PATH, &path,
383 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
387 if (!dbus_message_get_args(
390 DBUS_TYPE_STRING, &name,
392 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
394 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
395 return bus_send_error_reply(m, connection, message, &error, r);
397 if (!(reply = dbus_message_new_method_return(message)))
400 if (!(path = unit_dbus_path(u)))
403 if (!dbus_message_append_args(
405 DBUS_TYPE_OBJECT_PATH, &path,
409 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
410 job_type = JOB_START;
411 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
413 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
414 job_type = JOB_RELOAD;
415 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
416 job_type = JOB_RESTART;
417 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
418 job_type = JOB_TRY_RESTART;
419 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
420 reload_if_possible = true;
421 job_type = JOB_RESTART;
422 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
423 reload_if_possible = true;
424 job_type = JOB_TRY_RESTART;
425 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
429 if (!dbus_message_get_args(
432 DBUS_TYPE_UINT32, &id,
434 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
436 if (!(j = manager_get_job(m, id))) {
437 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
438 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
441 if (!(reply = dbus_message_new_method_return(message)))
444 if (!(path = job_dbus_path(j)))
447 if (!dbus_message_append_args(
449 DBUS_TYPE_OBJECT_PATH, &path,
453 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
455 manager_clear_jobs(m);
457 if (!(reply = dbus_message_new_method_return(message)))
460 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
462 manager_reset_failed(m);
464 if (!(reply = dbus_message_new_method_return(message)))
467 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
471 if (!dbus_message_get_args(
474 DBUS_TYPE_STRING, &name,
476 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
478 if (!(u = manager_get_unit(m, name))) {
479 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
480 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
483 unit_reset_failed(u);
485 if (!(reply = dbus_message_new_method_return(message)))
488 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
489 DBusMessageIter iter, sub;
494 if (!(reply = dbus_message_new_method_return(message)))
497 dbus_message_iter_init_append(reply, &iter);
499 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
502 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
503 char *u_path, *j_path;
504 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
505 DBusMessageIter sub2;
512 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
515 description = unit_description(u);
516 load_state = unit_load_state_to_string(u->meta.load_state);
517 active_state = unit_active_state_to_string(unit_active_state(u));
518 sub_state = unit_sub_state_to_string(u);
520 f = unit_following(u);
521 following = f ? f->meta.id : "";
523 if (!(u_path = unit_dbus_path(u)))
527 job_id = (uint32_t) u->meta.job->id;
529 if (!(j_path = job_dbus_path(u->meta.job))) {
534 sjob_type = job_type_to_string(u->meta.job->type);
541 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
542 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
543 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
544 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
545 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
546 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
547 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
548 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
549 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
550 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
561 if (!dbus_message_iter_close_container(&sub, &sub2))
565 if (!dbus_message_iter_close_container(&iter, &sub))
568 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
569 DBusMessageIter iter, sub;
573 if (!(reply = dbus_message_new_method_return(message)))
576 dbus_message_iter_init_append(reply, &iter);
578 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
581 HASHMAP_FOREACH(j, m->jobs, i) {
582 char *u_path, *j_path;
583 const char *state, *type;
585 DBusMessageIter sub2;
587 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
590 id = (uint32_t) j->id;
591 state = job_state_to_string(j->state);
592 type = job_type_to_string(j->type);
594 if (!(j_path = job_dbus_path(j)))
597 if (!(u_path = unit_dbus_path(j->unit))) {
602 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
603 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
604 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
605 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
606 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
607 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
616 if (!dbus_message_iter_close_container(&sub, &sub2))
620 if (!dbus_message_iter_close_container(&iter, &sub))
623 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
627 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
628 if (!(s = set_new(string_hash_func, string_compare_func)))
631 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
637 if (!(client = strdup(message_get_sender_with_fallback(message))))
640 if ((r = set_put(s, client)) < 0) {
642 return bus_send_error_reply(m, connection, message, NULL, r);
645 if (!(reply = dbus_message_new_method_return(message)))
648 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
651 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
652 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
653 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
658 if (!(reply = dbus_message_new_method_return(message)))
661 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
666 if (!(reply = dbus_message_new_method_return(message)))
669 if (!(f = open_memstream(&dump, &size)))
672 manager_dump_units(m, f, NULL);
673 manager_dump_jobs(m, f, NULL);
683 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
689 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
694 if (!dbus_message_get_args(
697 DBUS_TYPE_STRING, &name,
698 DBUS_TYPE_BOOLEAN, &cleanup,
700 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
702 if (name && name[0] == 0)
705 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
706 return bus_send_error_reply(m, connection, message, &error, r);
708 if (!(reply = dbus_message_new_method_return(message)))
711 if (!(path = unit_dbus_path(UNIT(s))))
714 if (!dbus_message_append_args(
716 DBUS_TYPE_OBJECT_PATH, &path,
720 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
721 char *introspection = NULL;
729 if (!(reply = dbus_message_new_method_return(message)))
732 /* We roll our own introspection code here, instead of
733 * relying on bus_default_message_handler() because we
734 * need to generate our introspection string
737 if (!(f = open_memstream(&introspection, &size)))
740 fputs(INTROSPECTION_BEGIN, f);
742 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
748 if (!(p = bus_path_escape(k))) {
754 fprintf(f, "<node name=\"unit/%s\"/>", p);
758 HASHMAP_FOREACH(j, m->jobs, i)
759 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
761 fputs(INTROSPECTION_END, f);
774 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
781 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
783 assert(!m->queued_message);
785 /* Instead of sending the reply back right away, we
786 * just remember that we need to and then send it
787 * after the reload is finished. That way the caller
788 * knows when the reload finished. */
790 if (!(m->queued_message = dbus_message_new_method_return(message)))
793 m->queued_message_connection = connection;
794 m->exit_code = MANAGER_RELOAD;
796 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
798 if (!(reply = dbus_message_new_method_return(message)))
801 m->exit_code = MANAGER_REEXECUTE;
803 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
805 if (m->running_as == MANAGER_SYSTEM) {
806 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
807 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
810 if (!(reply = dbus_message_new_method_return(message)))
813 m->exit_code = MANAGER_EXIT;
815 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
817 if (m->running_as != MANAGER_SYSTEM) {
818 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
819 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
822 if (!(reply = dbus_message_new_method_return(message)))
825 m->exit_code = MANAGER_REBOOT;
827 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
829 if (m->running_as != MANAGER_SYSTEM) {
830 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
831 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
834 if (!(reply = dbus_message_new_method_return(message)))
837 m->exit_code = MANAGER_POWEROFF;
839 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
841 if (m->running_as != MANAGER_SYSTEM) {
842 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
843 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
846 if (!(reply = dbus_message_new_method_return(message)))
849 m->exit_code = MANAGER_HALT;
851 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
853 if (m->running_as != MANAGER_SYSTEM) {
854 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system 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_KEXEC;
863 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
864 char **l = NULL, **e = NULL;
866 if ((r = bus_parse_strv(message, &l)) < 0) {
870 return bus_send_error_reply(m, connection, message, NULL, r);
873 e = strv_env_merge(2, m->environment, l);
879 if (!(reply = dbus_message_new_method_return(message))) {
884 strv_free(m->environment);
887 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
888 char **l = NULL, **e = NULL;
890 if ((r = bus_parse_strv(message, &l)) < 0) {
894 return bus_send_error_reply(m, connection, message, NULL, r);
897 e = strv_env_delete(m->environment, 1, l);
903 if (!(reply = dbus_message_new_method_return(message)))
906 strv_free(m->environment);
910 return bus_default_message_handler(m, connection, message, NULL, properties);
912 if (job_type != _JOB_TYPE_INVALID) {
913 const char *name, *smode;
918 if (!dbus_message_get_args(
921 DBUS_TYPE_STRING, &name,
922 DBUS_TYPE_STRING, &smode,
924 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
926 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
927 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
928 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
931 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
932 return bus_send_error_reply(m, connection, message, &error, r);
934 if (reload_if_possible && unit_can_reload(u)) {
935 if (job_type == JOB_RESTART)
936 job_type = JOB_RELOAD_OR_START;
937 else if (job_type == JOB_TRY_RESTART)
938 job_type = JOB_RELOAD;
941 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
942 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
943 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
944 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
945 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
946 return bus_send_error_reply(m, connection, message, &error, -EPERM);
949 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
950 return bus_send_error_reply(m, connection, message, &error, r);
952 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
957 if (!(reply = dbus_message_new_method_return(message)))
960 if (!(path = job_dbus_path(j)))
963 if (!dbus_message_append_args(
965 DBUS_TYPE_OBJECT_PATH, &path,
973 if (!dbus_connection_send(connection, reply, NULL))
976 dbus_message_unref(reply);
979 return DBUS_HANDLER_RESULT_HANDLED;
985 dbus_message_unref(reply);
987 dbus_error_free(&error);
989 return DBUS_HANDLER_RESULT_NEED_MEMORY;
992 const DBusObjectPathVTable bus_manager_vtable = {
993 .message_function = bus_manager_message_handler