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"
31 #define BUS_MANAGER_INTERFACE_BEGIN \
32 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
34 #define BUS_MANAGER_INTERFACE_METHODS \
35 " <method name=\"GetUnit\">\n" \
36 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
37 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
39 " <method name=\"GetUnitByPID\">\n" \
40 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
41 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
43 " <method name=\"LoadUnit\">\n" \
44 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
45 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
47 " <method name=\"StartUnit\">\n" \
48 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
50 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
52 " <method name=\"StartUnitReplace\">\n" \
53 " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
54 " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
58 " <method name=\"StopUnit\">\n" \
59 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
60 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
61 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
63 " <method name=\"ReloadUnit\">\n" \
64 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
65 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
68 " <method name=\"RestartUnit\">\n" \
69 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
70 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
71 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
73 " <method name=\"TryRestartUnit\">\n" \
74 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
75 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
76 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
78 " <method name=\"ReloadOrRestartUnit\">\n" \
79 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
80 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
81 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
83 " <method name=\"ReloadOrTryRestartUnit\">\n" \
84 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
85 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
86 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
88 " <method name=\"KillUnit\">\n" \
89 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
90 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
91 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
92 " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
94 " <method name=\"ResetFailedUnit\">\n" \
95 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
97 " <method name=\"GetJob\">\n" \
98 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
99 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
101 " <method name=\"ClearJobs\"/>\n" \
102 " <method name=\"ResetFailed\"/>\n" \
103 " <method name=\"ListUnits\">\n" \
104 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
106 " <method name=\"ListJobs\">\n" \
107 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
109 " <method name=\"Subscribe\"/>\n" \
110 " <method name=\"Unsubscribe\"/>\n" \
111 " <method name=\"Dump\"/>\n" \
112 " <method name=\"CreateSnapshot\">\n" \
113 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
114 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
115 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
117 " <method name=\"Reload\"/>\n" \
118 " <method name=\"Reexecute\"/>\n" \
119 " <method name=\"Exit\"/>\n" \
120 " <method name=\"Reboot\"/>\n" \
121 " <method name=\"PowerOff\"/>\n" \
122 " <method name=\"Halt\"/>\n" \
123 " <method name=\"KExec\"/>\n" \
124 " <method name=\"SetEnvironment\">\n" \
125 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
127 " <method name=\"UnsetEnvironment\">\n" \
128 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
131 #define BUS_MANAGER_INTERFACE_SIGNALS \
132 " <signal name=\"UnitNew\">\n" \
133 " <arg name=\"id\" type=\"s\"/>\n" \
134 " <arg name=\"unit\" type=\"o\"/>\n" \
136 " <signal name=\"UnitRemoved\">\n" \
137 " <arg name=\"id\" type=\"s\"/>\n" \
138 " <arg name=\"unit\" type=\"o\"/>\n" \
140 " <signal name=\"JobNew\">\n" \
141 " <arg name=\"id\" type=\"u\"/>\n" \
142 " <arg name=\"job\" type=\"o\"/>\n" \
144 " <signal name=\"JobRemoved\">\n" \
145 " <arg name=\"id\" type=\"u\"/>\n" \
146 " <arg name=\"job\" type=\"o\"/>\n" \
147 " <arg name=\"result\" type=\"s\"/>\n" \
150 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
151 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
152 " <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
153 " <property name=\"Features\" type=\"s\" access=\"read\"/\n" \
154 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
155 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
156 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
157 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
158 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
159 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
160 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
161 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
162 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
163 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
164 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
165 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
166 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
167 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
168 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
169 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
170 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
171 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
172 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
173 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
174 " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
175 " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
177 #ifdef HAVE_SYSV_COMPAT
178 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
179 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
180 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
181 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
183 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
186 #define BUS_MANAGER_INTERFACE_END \
189 #define BUS_MANAGER_INTERFACE \
190 BUS_MANAGER_INTERFACE_BEGIN \
191 BUS_MANAGER_INTERFACE_METHODS \
192 BUS_MANAGER_INTERFACE_SIGNALS \
193 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
194 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
195 BUS_MANAGER_INTERFACE_END
197 #define INTROSPECTION_BEGIN \
198 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
200 BUS_MANAGER_INTERFACE \
201 BUS_PROPERTIES_INTERFACE \
203 BUS_INTROSPECTABLE_INTERFACE
205 #define INTROSPECTION_END \
208 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
210 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
211 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
213 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
220 t = log_target_to_string(log_get_target());
222 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
228 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
235 t = log_level_to_string(log_get_max_level());
237 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
243 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
250 u = hashmap_size(m->units);
252 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
258 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
265 u = hashmap_size(m->jobs);
267 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
273 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
280 if (dual_timestamp_is_set(&m->finish_timestamp))
283 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
285 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
291 static const char *message_get_sender_with_fallback(DBusMessage *m) {
296 if ((s = dbus_message_get_sender(m)))
299 /* When the message came in from a direct connection the
300 * message will have no sender. We fix that here. */
305 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
308 const BusProperty properties[] = {
309 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
310 { "org.freedesktop.systemd1.Manager", "Distribution", bus_property_append_string, "s", DISTRIBUTION },
311 { "org.freedesktop.systemd1.Manager", "Features", bus_property_append_string, "s", SYSTEMD_FEATURES },
312 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
313 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime },
314 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
315 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
316 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
317 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
318 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
319 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
320 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
321 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
322 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
323 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
324 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
325 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
326 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
327 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
328 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
329 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
330 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
331 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
332 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output },
333 { "org.freedesktop.systemd1.Manager", "DefaultStandardError", bus_manager_append_exec_output, "s", &m->default_std_error },
334 #ifdef HAVE_SYSV_COMPAT
335 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
336 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
337 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
339 { NULL, NULL, NULL, NULL, NULL }
344 DBusMessage *reply = NULL;
346 JobType job_type = _JOB_TYPE_INVALID;
347 bool reload_if_possible = false;
353 dbus_error_init(&error);
355 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
359 if (!dbus_message_get_args(
362 DBUS_TYPE_STRING, &name,
364 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
366 if (!(u = manager_get_unit(m, name))) {
367 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
368 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
371 if (!(reply = dbus_message_new_method_return(message)))
374 if (!(path = unit_dbus_path(u)))
377 if (!dbus_message_append_args(
379 DBUS_TYPE_OBJECT_PATH, &path,
382 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
386 if (!dbus_message_get_args(
389 DBUS_TYPE_UINT32, &pid,
391 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
393 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
394 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
395 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
398 if (!(reply = dbus_message_new_method_return(message)))
401 if (!(path = unit_dbus_path(u)))
404 if (!dbus_message_append_args(
406 DBUS_TYPE_OBJECT_PATH, &path,
409 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
413 if (!dbus_message_get_args(
416 DBUS_TYPE_STRING, &name,
418 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
420 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
421 return bus_send_error_reply(m, connection, message, &error, r);
423 if (!(reply = dbus_message_new_method_return(message)))
426 if (!(path = unit_dbus_path(u)))
429 if (!dbus_message_append_args(
431 DBUS_TYPE_OBJECT_PATH, &path,
435 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
436 job_type = JOB_START;
437 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
438 job_type = JOB_START;
439 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
441 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
442 job_type = JOB_RELOAD;
443 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
444 job_type = JOB_RESTART;
445 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
446 job_type = JOB_TRY_RESTART;
447 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
448 reload_if_possible = true;
449 job_type = JOB_RESTART;
450 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
451 reload_if_possible = true;
452 job_type = JOB_TRY_RESTART;
453 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
454 const char *name, *swho, *smode;
460 if (!dbus_message_get_args(
463 DBUS_TYPE_STRING, &name,
464 DBUS_TYPE_STRING, &swho,
465 DBUS_TYPE_STRING, &smode,
466 DBUS_TYPE_INT32, &signo,
468 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
470 if ((mode = kill_mode_from_string(smode)) < 0 ||
471 (who = kill_who_from_string(swho)) < 0 ||
474 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
476 if (!(u = manager_get_unit(m, name))) {
477 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
478 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
481 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
482 return bus_send_error_reply(m, connection, message, &error, r);
484 if (!(reply = dbus_message_new_method_return(message)))
487 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
491 if (!dbus_message_get_args(
494 DBUS_TYPE_UINT32, &id,
496 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
498 if (!(j = manager_get_job(m, id))) {
499 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
500 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
503 if (!(reply = dbus_message_new_method_return(message)))
506 if (!(path = job_dbus_path(j)))
509 if (!dbus_message_append_args(
511 DBUS_TYPE_OBJECT_PATH, &path,
515 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
517 manager_clear_jobs(m);
519 if (!(reply = dbus_message_new_method_return(message)))
522 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
524 manager_reset_failed(m);
526 if (!(reply = dbus_message_new_method_return(message)))
529 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
533 if (!dbus_message_get_args(
536 DBUS_TYPE_STRING, &name,
538 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
540 if (!(u = manager_get_unit(m, name))) {
541 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
542 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
545 unit_reset_failed(u);
547 if (!(reply = dbus_message_new_method_return(message)))
550 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
551 DBusMessageIter iter, sub;
556 if (!(reply = dbus_message_new_method_return(message)))
559 dbus_message_iter_init_append(reply, &iter);
561 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
564 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
565 char *u_path, *j_path;
566 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
567 DBusMessageIter sub2;
574 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
577 description = unit_description(u);
578 load_state = unit_load_state_to_string(u->meta.load_state);
579 active_state = unit_active_state_to_string(unit_active_state(u));
580 sub_state = unit_sub_state_to_string(u);
582 f = unit_following(u);
583 following = f ? f->meta.id : "";
585 if (!(u_path = unit_dbus_path(u)))
589 job_id = (uint32_t) u->meta.job->id;
591 if (!(j_path = job_dbus_path(u->meta.job))) {
596 sjob_type = job_type_to_string(u->meta.job->type);
603 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
604 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
605 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
606 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
607 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
608 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
609 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
610 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
611 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
612 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
623 if (!dbus_message_iter_close_container(&sub, &sub2))
627 if (!dbus_message_iter_close_container(&iter, &sub))
630 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
631 DBusMessageIter iter, sub;
635 if (!(reply = dbus_message_new_method_return(message)))
638 dbus_message_iter_init_append(reply, &iter);
640 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
643 HASHMAP_FOREACH(j, m->jobs, i) {
644 char *u_path, *j_path;
645 const char *state, *type;
647 DBusMessageIter sub2;
649 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
652 id = (uint32_t) j->id;
653 state = job_state_to_string(j->state);
654 type = job_type_to_string(j->type);
656 if (!(j_path = job_dbus_path(j)))
659 if (!(u_path = unit_dbus_path(j->unit))) {
664 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
665 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
666 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
667 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
668 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
669 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
678 if (!dbus_message_iter_close_container(&sub, &sub2))
682 if (!dbus_message_iter_close_container(&iter, &sub))
685 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
689 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
690 if (!(s = set_new(string_hash_func, string_compare_func)))
693 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
699 if (!(client = strdup(message_get_sender_with_fallback(message))))
702 if ((r = set_put(s, client)) < 0) {
704 return bus_send_error_reply(m, connection, message, NULL, r);
707 if (!(reply = dbus_message_new_method_return(message)))
710 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
713 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
714 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
715 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
720 if (!(reply = dbus_message_new_method_return(message)))
723 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
728 if (!(reply = dbus_message_new_method_return(message)))
731 if (!(f = open_memstream(&dump, &size)))
734 manager_dump_units(m, f, NULL);
735 manager_dump_jobs(m, f, NULL);
745 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
751 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
756 if (!dbus_message_get_args(
759 DBUS_TYPE_STRING, &name,
760 DBUS_TYPE_BOOLEAN, &cleanup,
762 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
764 if (name && name[0] == 0)
767 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
768 return bus_send_error_reply(m, connection, message, &error, r);
770 if (!(reply = dbus_message_new_method_return(message)))
773 if (!(path = unit_dbus_path(UNIT(s))))
776 if (!dbus_message_append_args(
778 DBUS_TYPE_OBJECT_PATH, &path,
782 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
783 char *introspection = NULL;
791 if (!(reply = dbus_message_new_method_return(message)))
794 /* We roll our own introspection code here, instead of
795 * relying on bus_default_message_handler() because we
796 * need to generate our introspection string
799 if (!(f = open_memstream(&introspection, &size)))
802 fputs(INTROSPECTION_BEGIN, f);
804 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
810 if (!(p = bus_path_escape(k))) {
816 fprintf(f, "<node name=\"unit/%s\"/>", p);
820 HASHMAP_FOREACH(j, m->jobs, i)
821 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
823 fputs(INTROSPECTION_END, f);
836 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
843 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
845 assert(!m->queued_message);
847 /* Instead of sending the reply back right away, we
848 * just remember that we need to and then send it
849 * after the reload is finished. That way the caller
850 * knows when the reload finished. */
852 if (!(m->queued_message = dbus_message_new_method_return(message)))
855 m->queued_message_connection = connection;
856 m->exit_code = MANAGER_RELOAD;
858 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
860 if (!(reply = dbus_message_new_method_return(message)))
863 m->exit_code = MANAGER_REEXECUTE;
865 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
867 if (m->running_as == MANAGER_SYSTEM) {
868 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
869 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
872 if (!(reply = dbus_message_new_method_return(message)))
875 m->exit_code = MANAGER_EXIT;
877 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
879 if (m->running_as != MANAGER_SYSTEM) {
880 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
881 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
884 if (!(reply = dbus_message_new_method_return(message)))
887 m->exit_code = MANAGER_REBOOT;
889 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
891 if (m->running_as != MANAGER_SYSTEM) {
892 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
893 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
896 if (!(reply = dbus_message_new_method_return(message)))
899 m->exit_code = MANAGER_POWEROFF;
901 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
903 if (m->running_as != MANAGER_SYSTEM) {
904 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
905 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
908 if (!(reply = dbus_message_new_method_return(message)))
911 m->exit_code = MANAGER_HALT;
913 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
915 if (m->running_as != MANAGER_SYSTEM) {
916 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
917 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
920 if (!(reply = dbus_message_new_method_return(message)))
923 m->exit_code = MANAGER_KEXEC;
925 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
926 char **l = NULL, **e = NULL;
928 if ((r = bus_parse_strv(message, &l)) < 0) {
932 return bus_send_error_reply(m, connection, message, NULL, r);
935 e = strv_env_merge(2, m->environment, l);
941 if (!(reply = dbus_message_new_method_return(message))) {
946 strv_free(m->environment);
949 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
950 char **l = NULL, **e = NULL;
952 if ((r = bus_parse_strv(message, &l)) < 0) {
956 return bus_send_error_reply(m, connection, message, NULL, r);
959 e = strv_env_delete(m->environment, 1, l);
965 if (!(reply = dbus_message_new_method_return(message)))
968 strv_free(m->environment);
972 return bus_default_message_handler(m, connection, message, NULL, properties);
974 if (job_type != _JOB_TYPE_INVALID) {
975 const char *name, *smode, *old_name = NULL;
981 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
982 b = dbus_message_get_args(
985 DBUS_TYPE_STRING, &old_name,
986 DBUS_TYPE_STRING, &name,
987 DBUS_TYPE_STRING, &smode,
990 b = dbus_message_get_args(
993 DBUS_TYPE_STRING, &name,
994 DBUS_TYPE_STRING, &smode,
998 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1001 if (!(u = manager_get_unit(m, old_name)) ||
1003 u->meta.job->type != JOB_START) {
1004 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1005 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
1009 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1010 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1011 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1014 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1015 return bus_send_error_reply(m, connection, message, &error, r);
1017 if (reload_if_possible && unit_can_reload(u)) {
1018 if (job_type == JOB_RESTART)
1019 job_type = JOB_RELOAD_OR_START;
1020 else if (job_type == JOB_TRY_RESTART)
1021 job_type = JOB_RELOAD;
1024 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1025 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1026 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1027 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1028 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1029 return bus_send_error_reply(m, connection, message, &error, -EPERM);
1032 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1033 return bus_send_error_reply(m, connection, message, &error, r);
1035 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1038 j->bus = connection;
1040 if (!(reply = dbus_message_new_method_return(message)))
1043 if (!(path = job_dbus_path(j)))
1046 if (!dbus_message_append_args(
1048 DBUS_TYPE_OBJECT_PATH, &path,
1056 if (!dbus_connection_send(connection, reply, NULL))
1059 dbus_message_unref(reply);
1062 return DBUS_HANDLER_RESULT_HANDLED;
1068 dbus_message_unref(reply);
1070 dbus_error_free(&error);
1072 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1075 const DBusObjectPathVTable bus_manager_vtable = {
1076 .message_function = bus_manager_message_handler