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/>.
27 #include "dbus-manager.h"
29 #include "bus-errors.h"
31 #include "dbus-common.h"
33 #define BUS_MANAGER_INTERFACE_BEGIN \
34 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
36 #define BUS_MANAGER_INTERFACE_METHODS \
37 " <method name=\"GetUnit\">\n" \
38 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
39 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
41 " <method name=\"GetUnitByPID\">\n" \
42 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
43 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
45 " <method name=\"LoadUnit\">\n" \
46 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
47 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
49 " <method name=\"StartUnit\">\n" \
50 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
51 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
52 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
54 " <method name=\"StartUnitReplace\">\n" \
55 " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
58 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
60 " <method name=\"StopUnit\">\n" \
61 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
62 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
63 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
65 " <method name=\"ReloadUnit\">\n" \
66 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
68 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
70 " <method name=\"RestartUnit\">\n" \
71 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
72 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
73 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
75 " <method name=\"TryRestartUnit\">\n" \
76 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
77 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
78 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
80 " <method name=\"ReloadOrRestartUnit\">\n" \
81 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
82 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
83 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
85 " <method name=\"ReloadOrTryRestartUnit\">\n" \
86 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
87 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
88 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
90 " <method name=\"KillUnit\">\n" \
91 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
92 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
93 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
94 " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
96 " <method name=\"ResetFailedUnit\">\n" \
97 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
99 " <method name=\"GetJob\">\n" \
100 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
101 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
103 " <method name=\"ClearJobs\"/>\n" \
104 " <method name=\"ResetFailed\"/>\n" \
105 " <method name=\"ListUnits\">\n" \
106 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
108 " <method name=\"ListJobs\">\n" \
109 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
111 " <method name=\"Subscribe\"/>\n" \
112 " <method name=\"Unsubscribe\"/>\n" \
113 " <method name=\"Dump\"/>\n" \
114 " <method name=\"CreateSnapshot\">\n" \
115 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
116 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
117 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
119 " <method name=\"Reload\"/>\n" \
120 " <method name=\"Reexecute\"/>\n" \
121 " <method name=\"Exit\"/>\n" \
122 " <method name=\"Reboot\"/>\n" \
123 " <method name=\"PowerOff\"/>\n" \
124 " <method name=\"Halt\"/>\n" \
125 " <method name=\"KExec\"/>\n" \
126 " <method name=\"SetEnvironment\">\n" \
127 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
129 " <method name=\"UnsetEnvironment\">\n" \
130 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
132 " <method name=\"UnsetAndSetEnvironment\">\n" \
133 " <arg name=\"unset\" type=\"as\" direction=\"in\"/>\n" \
134 " <arg name=\"set\" type=\"as\" direction=\"in\"/>\n" \
137 #define BUS_MANAGER_INTERFACE_SIGNALS \
138 " <signal name=\"UnitNew\">\n" \
139 " <arg name=\"id\" type=\"s\"/>\n" \
140 " <arg name=\"unit\" type=\"o\"/>\n" \
142 " <signal name=\"UnitRemoved\">\n" \
143 " <arg name=\"id\" type=\"s\"/>\n" \
144 " <arg name=\"unit\" type=\"o\"/>\n" \
146 " <signal name=\"JobNew\">\n" \
147 " <arg name=\"id\" type=\"u\"/>\n" \
148 " <arg name=\"job\" type=\"o\"/>\n" \
150 " <signal name=\"JobRemoved\">\n" \
151 " <arg name=\"id\" type=\"u\"/>\n" \
152 " <arg name=\"job\" type=\"o\"/>\n" \
153 " <arg name=\"result\" type=\"s\"/>\n" \
156 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
157 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
158 " <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
159 " <property name=\"Features\" type=\"s\" access=\"read\"/>\n" \
160 " <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n" \
161 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
162 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
163 " <property name=\"InitRDTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
164 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
165 " <property name=\"StartupTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
166 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
167 " <property name=\"FinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
168 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n" \
169 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
170 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
171 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
172 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
173 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
174 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
175 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
176 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
177 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
178 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
179 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
180 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
181 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
182 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
183 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
184 " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
185 " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
187 #ifdef HAVE_SYSV_COMPAT
188 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
189 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
190 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
191 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
193 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
196 #define BUS_MANAGER_INTERFACE_END \
199 #define BUS_MANAGER_INTERFACE \
200 BUS_MANAGER_INTERFACE_BEGIN \
201 BUS_MANAGER_INTERFACE_METHODS \
202 BUS_MANAGER_INTERFACE_SIGNALS \
203 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
204 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
205 BUS_MANAGER_INTERFACE_END
207 #define INTROSPECTION_BEGIN \
208 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
210 BUS_MANAGER_INTERFACE \
211 BUS_PROPERTIES_INTERFACE \
213 BUS_INTROSPECTABLE_INTERFACE
215 #define INTROSPECTION_END \
218 #define INTERFACES_LIST \
219 BUS_GENERIC_INTERFACES_LIST \
220 "org.freedesktop.systemd1.Manager\0"
222 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
224 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
225 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
227 static int bus_manager_append_tainted(DBusMessageIter *i, const char *property, void *data) {
230 char buf[LINE_MAX] = "", *e = buf, *p = NULL;
237 e = stpcpy(e, "usr-separate-fs ");
239 if (readlink_malloc("/etc/mtab", &p) < 0)
240 e = stpcpy(e, "etc-mtab-not-symlink ");
244 if (access("/proc/cgroups", F_OK) < 0)
245 e = stpcpy(e, "cgroups-missing ");
249 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
255 static int bus_manager_append_log_target(DBusMessageIter *i, const char *property, void *data) {
261 t = log_target_to_string(log_get_target());
263 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
269 static int bus_manager_set_log_target(DBusMessageIter *i, const char *property) {
275 dbus_message_iter_get_basic(i, &t);
277 return log_set_target_from_string(t);
280 static int bus_manager_append_log_level(DBusMessageIter *i, const char *property, void *data) {
286 t = log_level_to_string(log_get_max_level());
288 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
294 static int bus_manager_set_log_level(DBusMessageIter *i, const char *property) {
300 dbus_message_iter_get_basic(i, &t);
302 return log_set_max_level_from_string(t);
305 static int bus_manager_append_n_names(DBusMessageIter *i, const char *property, void *data) {
313 u = hashmap_size(m->units);
315 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
321 static int bus_manager_append_n_jobs(DBusMessageIter *i, const char *property, void *data) {
329 u = hashmap_size(m->jobs);
331 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
337 static int bus_manager_append_progress(DBusMessageIter *i, const char *property, void *data) {
345 if (dual_timestamp_is_set(&m->finish_timestamp))
348 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
350 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
356 static const char *message_get_sender_with_fallback(DBusMessage *m) {
361 if ((s = dbus_message_get_sender(m)))
364 /* When the message came in from a direct connection the
365 * message will have no sender. We fix that here. */
370 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
373 const BusProperty properties[] = {
374 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
375 { "org.freedesktop.systemd1.Manager", "Distribution", bus_property_append_string, "s", DISTRIBUTION },
376 { "org.freedesktop.systemd1.Manager", "Features", bus_property_append_string, "s", SYSTEMD_FEATURES },
377 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
378 { "org.freedesktop.systemd1.Manager", "Tainted", bus_manager_append_tainted, "s", m },
379 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime },
380 { "org.freedesktop.systemd1.Manager", "InitRDTimestampMonotonic", bus_property_append_uint64, "t", &m->initrd_timestamp.monotonic },
381 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
382 { "org.freedesktop.systemd1.Manager", "StartupTimestampMonotonic", bus_property_append_uint64, "t", &m->startup_timestamp.monotonic },
383 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
384 { "org.freedesktop.systemd1.Manager", "FinishTimestampMonotonic", bus_property_append_uint64, "t",&m->finish_timestamp.monotonic },
385 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", m, bus_manager_set_log_level },
386 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", m, bus_manager_set_log_target },
387 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", m },
388 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", m },
389 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
390 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
391 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", m },
392 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
393 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
394 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
395 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
396 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
397 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
398 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
399 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
400 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
401 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output },
402 { "org.freedesktop.systemd1.Manager", "DefaultStandardError", bus_manager_append_exec_output, "s", &m->default_std_error },
403 #ifdef HAVE_SYSV_COMPAT
404 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
405 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
406 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
408 { NULL, NULL, NULL, NULL, NULL }
413 DBusMessage *reply = NULL;
415 JobType job_type = _JOB_TYPE_INVALID;
416 bool reload_if_possible = false;
422 dbus_error_init(&error);
424 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
428 if (!dbus_message_get_args(
431 DBUS_TYPE_STRING, &name,
433 return bus_send_error_reply(connection, message, &error, -EINVAL);
435 if (!(u = manager_get_unit(m, name))) {
436 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
437 return bus_send_error_reply(connection, message, &error, -ENOENT);
440 if (!(reply = dbus_message_new_method_return(message)))
443 if (!(path = unit_dbus_path(u)))
446 if (!dbus_message_append_args(
448 DBUS_TYPE_OBJECT_PATH, &path,
451 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
455 if (!dbus_message_get_args(
458 DBUS_TYPE_UINT32, &pid,
460 return bus_send_error_reply(connection, message, &error, -EINVAL);
462 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
463 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
464 return bus_send_error_reply(connection, message, &error, -ENOENT);
467 if (!(reply = dbus_message_new_method_return(message)))
470 if (!(path = unit_dbus_path(u)))
473 if (!dbus_message_append_args(
475 DBUS_TYPE_OBJECT_PATH, &path,
478 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
482 if (!dbus_message_get_args(
485 DBUS_TYPE_STRING, &name,
487 return bus_send_error_reply(connection, message, &error, -EINVAL);
489 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
490 return bus_send_error_reply(connection, message, &error, r);
492 if (!(reply = dbus_message_new_method_return(message)))
495 if (!(path = unit_dbus_path(u)))
498 if (!dbus_message_append_args(
500 DBUS_TYPE_OBJECT_PATH, &path,
504 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
505 job_type = JOB_START;
506 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
507 job_type = JOB_START;
508 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
510 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
511 job_type = JOB_RELOAD;
512 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
513 job_type = JOB_RESTART;
514 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
515 job_type = JOB_TRY_RESTART;
516 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
517 reload_if_possible = true;
518 job_type = JOB_RESTART;
519 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
520 reload_if_possible = true;
521 job_type = JOB_TRY_RESTART;
522 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
523 const char *name, *swho, *smode;
529 if (!dbus_message_get_args(
532 DBUS_TYPE_STRING, &name,
533 DBUS_TYPE_STRING, &swho,
534 DBUS_TYPE_STRING, &smode,
535 DBUS_TYPE_INT32, &signo,
537 return bus_send_error_reply(connection, message, &error, -EINVAL);
539 if ((mode = kill_mode_from_string(smode)) < 0 ||
540 (who = kill_who_from_string(swho)) < 0 ||
543 return bus_send_error_reply(connection, message, &error, -EINVAL);
545 if (!(u = manager_get_unit(m, name))) {
546 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
547 return bus_send_error_reply(connection, message, &error, -ENOENT);
550 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
551 return bus_send_error_reply(connection, message, &error, r);
553 if (!(reply = dbus_message_new_method_return(message)))
556 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
560 if (!dbus_message_get_args(
563 DBUS_TYPE_UINT32, &id,
565 return bus_send_error_reply(connection, message, &error, -EINVAL);
567 if (!(j = manager_get_job(m, id))) {
568 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
569 return bus_send_error_reply(connection, message, &error, -ENOENT);
572 if (!(reply = dbus_message_new_method_return(message)))
575 if (!(path = job_dbus_path(j)))
578 if (!dbus_message_append_args(
580 DBUS_TYPE_OBJECT_PATH, &path,
584 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
586 manager_clear_jobs(m);
588 if (!(reply = dbus_message_new_method_return(message)))
591 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
593 manager_reset_failed(m);
595 if (!(reply = dbus_message_new_method_return(message)))
598 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
602 if (!dbus_message_get_args(
605 DBUS_TYPE_STRING, &name,
607 return bus_send_error_reply(connection, message, &error, -EINVAL);
609 if (!(u = manager_get_unit(m, name))) {
610 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
611 return bus_send_error_reply(connection, message, &error, -ENOENT);
614 unit_reset_failed(u);
616 if (!(reply = dbus_message_new_method_return(message)))
619 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
620 DBusMessageIter iter, sub;
625 if (!(reply = dbus_message_new_method_return(message)))
628 dbus_message_iter_init_append(reply, &iter);
630 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
633 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
634 char *u_path, *j_path;
635 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
636 DBusMessageIter sub2;
643 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
646 description = unit_description(u);
647 load_state = unit_load_state_to_string(u->meta.load_state);
648 active_state = unit_active_state_to_string(unit_active_state(u));
649 sub_state = unit_sub_state_to_string(u);
651 f = unit_following(u);
652 following = f ? f->meta.id : "";
654 if (!(u_path = unit_dbus_path(u)))
658 job_id = (uint32_t) u->meta.job->id;
660 if (!(j_path = job_dbus_path(u->meta.job))) {
665 sjob_type = job_type_to_string(u->meta.job->type);
672 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
673 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
674 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
675 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
676 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
677 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
678 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
679 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
680 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
681 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
692 if (!dbus_message_iter_close_container(&sub, &sub2))
696 if (!dbus_message_iter_close_container(&iter, &sub))
699 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
700 DBusMessageIter iter, sub;
704 if (!(reply = dbus_message_new_method_return(message)))
707 dbus_message_iter_init_append(reply, &iter);
709 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
712 HASHMAP_FOREACH(j, m->jobs, i) {
713 char *u_path, *j_path;
714 const char *state, *type;
716 DBusMessageIter sub2;
718 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
721 id = (uint32_t) j->id;
722 state = job_state_to_string(j->state);
723 type = job_type_to_string(j->type);
725 if (!(j_path = job_dbus_path(j)))
728 if (!(u_path = unit_dbus_path(j->unit))) {
733 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
734 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
735 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
736 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
737 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
738 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
747 if (!dbus_message_iter_close_container(&sub, &sub2))
751 if (!dbus_message_iter_close_container(&iter, &sub))
754 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
758 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
759 if (!(s = set_new(string_hash_func, string_compare_func)))
762 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
768 if (!(client = strdup(message_get_sender_with_fallback(message))))
771 if ((r = set_put(s, client)) < 0) {
773 return bus_send_error_reply(connection, message, NULL, r);
776 if (!(reply = dbus_message_new_method_return(message)))
779 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
782 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
783 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
784 return bus_send_error_reply(connection, message, &error, -ENOENT);
789 if (!(reply = dbus_message_new_method_return(message)))
792 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
797 if (!(reply = dbus_message_new_method_return(message)))
800 if (!(f = open_memstream(&dump, &size)))
803 manager_dump_units(m, f, NULL);
804 manager_dump_jobs(m, f, NULL);
814 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
820 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
825 if (!dbus_message_get_args(
828 DBUS_TYPE_STRING, &name,
829 DBUS_TYPE_BOOLEAN, &cleanup,
831 return bus_send_error_reply(connection, message, &error, -EINVAL);
833 if (name && name[0] == 0)
836 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
837 return bus_send_error_reply(connection, message, &error, r);
839 if (!(reply = dbus_message_new_method_return(message)))
842 if (!(path = unit_dbus_path(UNIT(s))))
845 if (!dbus_message_append_args(
847 DBUS_TYPE_OBJECT_PATH, &path,
851 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
852 char *introspection = NULL;
860 if (!(reply = dbus_message_new_method_return(message)))
863 /* We roll our own introspection code here, instead of
864 * relying on bus_default_message_handler() because we
865 * need to generate our introspection string
868 if (!(f = open_memstream(&introspection, &size)))
871 fputs(INTROSPECTION_BEGIN, f);
873 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
879 if (!(p = bus_path_escape(k))) {
885 fprintf(f, "<node name=\"unit/%s\"/>", p);
889 HASHMAP_FOREACH(j, m->jobs, i)
890 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
892 fputs(INTROSPECTION_END, f);
905 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
912 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
914 assert(!m->queued_message);
916 /* Instead of sending the reply back right away, we
917 * just remember that we need to and then send it
918 * after the reload is finished. That way the caller
919 * knows when the reload finished. */
921 if (!(m->queued_message = dbus_message_new_method_return(message)))
924 m->queued_message_connection = connection;
925 m->exit_code = MANAGER_RELOAD;
927 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
929 /* We don't send a reply back here, the client should
930 * just wait for us disconnecting. */
932 m->exit_code = MANAGER_REEXECUTE;
934 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
936 if (m->running_as == MANAGER_SYSTEM) {
937 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
938 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
941 if (!(reply = dbus_message_new_method_return(message)))
944 m->exit_code = MANAGER_EXIT;
946 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
948 if (m->running_as != MANAGER_SYSTEM) {
949 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
950 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
953 if (!(reply = dbus_message_new_method_return(message)))
956 m->exit_code = MANAGER_REBOOT;
958 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
960 if (m->running_as != MANAGER_SYSTEM) {
961 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
962 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
965 if (!(reply = dbus_message_new_method_return(message)))
968 m->exit_code = MANAGER_POWEROFF;
970 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
972 if (m->running_as != MANAGER_SYSTEM) {
973 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
974 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
977 if (!(reply = dbus_message_new_method_return(message)))
980 m->exit_code = MANAGER_HALT;
982 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
984 if (m->running_as != MANAGER_SYSTEM) {
985 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
986 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
989 if (!(reply = dbus_message_new_method_return(message)))
992 m->exit_code = MANAGER_KEXEC;
994 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
995 char **l = NULL, **e = NULL;
997 if ((r = bus_parse_strv(message, &l)) < 0) {
1001 return bus_send_error_reply(connection, message, NULL, r);
1004 e = strv_env_merge(2, m->environment, l);
1010 if (!(reply = dbus_message_new_method_return(message))) {
1015 strv_free(m->environment);
1018 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1019 char **l = NULL, **e = NULL;
1021 if ((r = bus_parse_strv(message, &l)) < 0) {
1025 return bus_send_error_reply(connection, message, NULL, r);
1028 e = strv_env_delete(m->environment, 1, l);
1034 if (!(reply = dbus_message_new_method_return(message))) {
1039 strv_free(m->environment);
1042 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment")) {
1043 char **l_set = NULL, **l_unset = NULL, **e = NULL, **f = NULL;
1044 DBusMessageIter iter;
1046 if (!dbus_message_iter_init(message, &iter))
1047 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1049 r = bus_parse_strv_iter(&iter, &l_unset);
1054 return bus_send_error_reply(connection, message, NULL, r);
1057 if (!dbus_message_iter_next(&iter)) {
1059 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1062 r = bus_parse_strv_iter(&iter, &l_set);
1068 return bus_send_error_reply(connection, message, NULL, r);
1071 e = strv_env_delete(m->environment, 1, l_unset);
1079 f = strv_env_merge(2, e, l_set);
1086 if (!(reply = dbus_message_new_method_return(message))) {
1091 strv_free(m->environment);
1095 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1097 if (job_type != _JOB_TYPE_INVALID) {
1098 const char *name, *smode, *old_name = NULL;
1104 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1105 b = dbus_message_get_args(
1108 DBUS_TYPE_STRING, &old_name,
1109 DBUS_TYPE_STRING, &name,
1110 DBUS_TYPE_STRING, &smode,
1113 b = dbus_message_get_args(
1116 DBUS_TYPE_STRING, &name,
1117 DBUS_TYPE_STRING, &smode,
1121 return bus_send_error_reply(connection, message, &error, -EINVAL);
1124 if (!(u = manager_get_unit(m, old_name)) ||
1126 u->meta.job->type != JOB_START) {
1127 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1128 return bus_send_error_reply(connection, message, &error, -ENOENT);
1132 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1133 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1134 return bus_send_error_reply(connection, message, &error, -EINVAL);
1137 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1138 return bus_send_error_reply(connection, message, &error, r);
1140 if (reload_if_possible && unit_can_reload(u)) {
1141 if (job_type == JOB_RESTART)
1142 job_type = JOB_RELOAD_OR_START;
1143 else if (job_type == JOB_TRY_RESTART)
1144 job_type = JOB_RELOAD;
1147 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1148 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1149 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1150 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1151 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1152 return bus_send_error_reply(connection, message, &error, -EPERM);
1155 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1156 return bus_send_error_reply(connection, message, &error, r);
1158 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1161 j->bus = connection;
1163 if (!(reply = dbus_message_new_method_return(message)))
1166 if (!(path = job_dbus_path(j)))
1169 if (!dbus_message_append_args(
1171 DBUS_TYPE_OBJECT_PATH, &path,
1177 if (!dbus_connection_send(connection, reply, NULL))
1180 dbus_message_unref(reply);
1185 return DBUS_HANDLER_RESULT_HANDLED;
1191 dbus_message_unref(reply);
1193 dbus_error_free(&error);
1195 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1198 const DBusObjectPathVTable bus_manager_vtable = {
1199 .message_function = bus_manager_message_handler