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" \
171 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n"
174 #ifdef HAVE_SYSV_COMPAT
175 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
176 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
177 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
178 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
180 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
183 #define BUS_MANAGER_INTERFACE_END \
186 #define BUS_MANAGER_INTERFACE \
187 BUS_MANAGER_INTERFACE_BEGIN \
188 BUS_MANAGER_INTERFACE_METHODS \
189 BUS_MANAGER_INTERFACE_SIGNALS \
190 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
191 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
192 BUS_MANAGER_INTERFACE_END
194 #define INTROSPECTION_BEGIN \
195 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
197 BUS_MANAGER_INTERFACE \
198 BUS_PROPERTIES_INTERFACE \
200 BUS_INTROSPECTABLE_INTERFACE
202 #define INTROSPECTION_END \
205 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
207 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
209 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
216 t = log_target_to_string(log_get_target());
218 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
224 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
231 t = log_level_to_string(log_get_max_level());
233 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
239 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
246 u = hashmap_size(m->units);
248 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
254 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
261 u = hashmap_size(m->jobs);
263 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
269 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
276 if (dual_timestamp_is_set(&m->finish_timestamp))
279 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
281 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
287 static const char *message_get_sender_with_fallback(DBusMessage *m) {
292 if ((s = dbus_message_get_sender(m)))
295 /* When the message came in from a direct connection the
296 * message will have no sender. We fix that here. */
301 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
304 const BusProperty properties[] = {
305 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
306 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
307 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime },
308 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
309 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
310 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
311 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
312 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
313 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
314 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
315 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
316 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
317 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
318 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
319 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
320 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
321 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
322 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
323 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
324 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
325 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
326 #ifdef HAVE_SYSV_COMPAT
327 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
328 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
329 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
331 { NULL, NULL, NULL, NULL, NULL }
336 DBusMessage *reply = NULL;
338 JobType job_type = _JOB_TYPE_INVALID;
339 bool reload_if_possible = false;
345 dbus_error_init(&error);
347 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
351 if (!dbus_message_get_args(
354 DBUS_TYPE_STRING, &name,
356 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
358 if (!(u = manager_get_unit(m, name))) {
359 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
360 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
363 if (!(reply = dbus_message_new_method_return(message)))
366 if (!(path = unit_dbus_path(u)))
369 if (!dbus_message_append_args(
371 DBUS_TYPE_OBJECT_PATH, &path,
374 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
378 if (!dbus_message_get_args(
381 DBUS_TYPE_UINT32, &pid,
383 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
385 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
386 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
387 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
390 if (!(reply = dbus_message_new_method_return(message)))
393 if (!(path = unit_dbus_path(u)))
396 if (!dbus_message_append_args(
398 DBUS_TYPE_OBJECT_PATH, &path,
401 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
405 if (!dbus_message_get_args(
408 DBUS_TYPE_STRING, &name,
410 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
412 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
413 return bus_send_error_reply(m, connection, message, &error, r);
415 if (!(reply = dbus_message_new_method_return(message)))
418 if (!(path = unit_dbus_path(u)))
421 if (!dbus_message_append_args(
423 DBUS_TYPE_OBJECT_PATH, &path,
427 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
428 job_type = JOB_START;
429 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
430 job_type = JOB_START;
431 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
433 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
434 job_type = JOB_RELOAD;
435 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
436 job_type = JOB_RESTART;
437 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
438 job_type = JOB_TRY_RESTART;
439 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
440 reload_if_possible = true;
441 job_type = JOB_RESTART;
442 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
443 reload_if_possible = true;
444 job_type = JOB_TRY_RESTART;
445 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
446 const char *name, *swho, *smode;
452 if (!dbus_message_get_args(
455 DBUS_TYPE_STRING, &name,
456 DBUS_TYPE_STRING, &swho,
457 DBUS_TYPE_STRING, &smode,
458 DBUS_TYPE_INT32, &signo,
460 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
462 if ((mode = kill_mode_from_string(smode)) < 0 ||
463 (who = kill_who_from_string(swho)) < 0 ||
466 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
468 if (!(u = manager_get_unit(m, name))) {
469 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
470 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
473 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
474 return bus_send_error_reply(m, connection, message, &error, r);
476 if (!(reply = dbus_message_new_method_return(message)))
479 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
483 if (!dbus_message_get_args(
486 DBUS_TYPE_UINT32, &id,
488 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
490 if (!(j = manager_get_job(m, id))) {
491 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
492 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
495 if (!(reply = dbus_message_new_method_return(message)))
498 if (!(path = job_dbus_path(j)))
501 if (!dbus_message_append_args(
503 DBUS_TYPE_OBJECT_PATH, &path,
507 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
509 manager_clear_jobs(m);
511 if (!(reply = dbus_message_new_method_return(message)))
514 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
516 manager_reset_failed(m);
518 if (!(reply = dbus_message_new_method_return(message)))
521 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
525 if (!dbus_message_get_args(
528 DBUS_TYPE_STRING, &name,
530 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
532 if (!(u = manager_get_unit(m, name))) {
533 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
534 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
537 unit_reset_failed(u);
539 if (!(reply = dbus_message_new_method_return(message)))
542 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
543 DBusMessageIter iter, sub;
548 if (!(reply = dbus_message_new_method_return(message)))
551 dbus_message_iter_init_append(reply, &iter);
553 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
556 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
557 char *u_path, *j_path;
558 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
559 DBusMessageIter sub2;
566 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
569 description = unit_description(u);
570 load_state = unit_load_state_to_string(u->meta.load_state);
571 active_state = unit_active_state_to_string(unit_active_state(u));
572 sub_state = unit_sub_state_to_string(u);
574 f = unit_following(u);
575 following = f ? f->meta.id : "";
577 if (!(u_path = unit_dbus_path(u)))
581 job_id = (uint32_t) u->meta.job->id;
583 if (!(j_path = job_dbus_path(u->meta.job))) {
588 sjob_type = job_type_to_string(u->meta.job->type);
595 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
596 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
597 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
598 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
599 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
600 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
601 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
602 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
603 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
604 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
615 if (!dbus_message_iter_close_container(&sub, &sub2))
619 if (!dbus_message_iter_close_container(&iter, &sub))
622 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
623 DBusMessageIter iter, sub;
627 if (!(reply = dbus_message_new_method_return(message)))
630 dbus_message_iter_init_append(reply, &iter);
632 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
635 HASHMAP_FOREACH(j, m->jobs, i) {
636 char *u_path, *j_path;
637 const char *state, *type;
639 DBusMessageIter sub2;
641 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
644 id = (uint32_t) j->id;
645 state = job_state_to_string(j->state);
646 type = job_type_to_string(j->type);
648 if (!(j_path = job_dbus_path(j)))
651 if (!(u_path = unit_dbus_path(j->unit))) {
656 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
657 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
658 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
659 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
660 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
661 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
670 if (!dbus_message_iter_close_container(&sub, &sub2))
674 if (!dbus_message_iter_close_container(&iter, &sub))
677 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
681 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
682 if (!(s = set_new(string_hash_func, string_compare_func)))
685 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
691 if (!(client = strdup(message_get_sender_with_fallback(message))))
694 if ((r = set_put(s, client)) < 0) {
696 return bus_send_error_reply(m, connection, message, NULL, r);
699 if (!(reply = dbus_message_new_method_return(message)))
702 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
705 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
706 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
707 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
712 if (!(reply = dbus_message_new_method_return(message)))
715 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
720 if (!(reply = dbus_message_new_method_return(message)))
723 if (!(f = open_memstream(&dump, &size)))
726 manager_dump_units(m, f, NULL);
727 manager_dump_jobs(m, f, NULL);
737 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
743 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
748 if (!dbus_message_get_args(
751 DBUS_TYPE_STRING, &name,
752 DBUS_TYPE_BOOLEAN, &cleanup,
754 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
756 if (name && name[0] == 0)
759 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
760 return bus_send_error_reply(m, connection, message, &error, r);
762 if (!(reply = dbus_message_new_method_return(message)))
765 if (!(path = unit_dbus_path(UNIT(s))))
768 if (!dbus_message_append_args(
770 DBUS_TYPE_OBJECT_PATH, &path,
774 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
775 char *introspection = NULL;
783 if (!(reply = dbus_message_new_method_return(message)))
786 /* We roll our own introspection code here, instead of
787 * relying on bus_default_message_handler() because we
788 * need to generate our introspection string
791 if (!(f = open_memstream(&introspection, &size)))
794 fputs(INTROSPECTION_BEGIN, f);
796 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
802 if (!(p = bus_path_escape(k))) {
808 fprintf(f, "<node name=\"unit/%s\"/>", p);
812 HASHMAP_FOREACH(j, m->jobs, i)
813 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
815 fputs(INTROSPECTION_END, f);
828 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
835 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
837 assert(!m->queued_message);
839 /* Instead of sending the reply back right away, we
840 * just remember that we need to and then send it
841 * after the reload is finished. That way the caller
842 * knows when the reload finished. */
844 if (!(m->queued_message = dbus_message_new_method_return(message)))
847 m->queued_message_connection = connection;
848 m->exit_code = MANAGER_RELOAD;
850 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
852 if (!(reply = dbus_message_new_method_return(message)))
855 m->exit_code = MANAGER_REEXECUTE;
857 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
859 if (m->running_as == MANAGER_SYSTEM) {
860 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
861 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
864 if (!(reply = dbus_message_new_method_return(message)))
867 m->exit_code = MANAGER_EXIT;
869 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
871 if (m->running_as != MANAGER_SYSTEM) {
872 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
873 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
876 if (!(reply = dbus_message_new_method_return(message)))
879 m->exit_code = MANAGER_REBOOT;
881 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
883 if (m->running_as != MANAGER_SYSTEM) {
884 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
885 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
888 if (!(reply = dbus_message_new_method_return(message)))
891 m->exit_code = MANAGER_POWEROFF;
893 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
895 if (m->running_as != MANAGER_SYSTEM) {
896 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
897 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
900 if (!(reply = dbus_message_new_method_return(message)))
903 m->exit_code = MANAGER_HALT;
905 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
907 if (m->running_as != MANAGER_SYSTEM) {
908 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
909 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
912 if (!(reply = dbus_message_new_method_return(message)))
915 m->exit_code = MANAGER_KEXEC;
917 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
918 char **l = NULL, **e = NULL;
920 if ((r = bus_parse_strv(message, &l)) < 0) {
924 return bus_send_error_reply(m, connection, message, NULL, r);
927 e = strv_env_merge(2, m->environment, l);
933 if (!(reply = dbus_message_new_method_return(message))) {
938 strv_free(m->environment);
941 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
942 char **l = NULL, **e = NULL;
944 if ((r = bus_parse_strv(message, &l)) < 0) {
948 return bus_send_error_reply(m, connection, message, NULL, r);
951 e = strv_env_delete(m->environment, 1, l);
957 if (!(reply = dbus_message_new_method_return(message)))
960 strv_free(m->environment);
964 return bus_default_message_handler(m, connection, message, NULL, properties);
966 if (job_type != _JOB_TYPE_INVALID) {
967 const char *name, *smode, *old_name = NULL;
973 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
974 b = dbus_message_get_args(
977 DBUS_TYPE_STRING, &old_name,
978 DBUS_TYPE_STRING, &name,
979 DBUS_TYPE_STRING, &smode,
982 b = dbus_message_get_args(
985 DBUS_TYPE_STRING, &name,
986 DBUS_TYPE_STRING, &smode,
990 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
993 if (!(u = manager_get_unit(m, old_name)) ||
995 u->meta.job->type != JOB_START) {
996 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
997 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
1001 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1002 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1003 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1006 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1007 return bus_send_error_reply(m, connection, message, &error, r);
1009 if (reload_if_possible && unit_can_reload(u)) {
1010 if (job_type == JOB_RESTART)
1011 job_type = JOB_RELOAD_OR_START;
1012 else if (job_type == JOB_TRY_RESTART)
1013 job_type = JOB_RELOAD;
1016 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1017 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1018 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1019 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1020 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1021 return bus_send_error_reply(m, connection, message, &error, -EPERM);
1024 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1025 return bus_send_error_reply(m, connection, message, &error, r);
1027 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1030 j->bus = connection;
1032 if (!(reply = dbus_message_new_method_return(message)))
1035 if (!(path = job_dbus_path(j)))
1038 if (!dbus_message_append_args(
1040 DBUS_TYPE_OBJECT_PATH, &path,
1048 if (!dbus_connection_send(connection, reply, NULL))
1051 dbus_message_unref(reply);
1054 return DBUS_HANDLER_RESULT_HANDLED;
1060 dbus_message_unref(reply);
1062 dbus_error_free(&error);
1064 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1067 const DBusObjectPathVTable bus_manager_vtable = {
1068 .message_function = bus_manager_message_handler