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" \
155 " <signal name=\"StartupFinished\">\n" \
156 " <arg name=\"kernel\" type=\"t\"/>\n" \
157 " <arg name=\"initrd\" type=\"t\"/>\n" \
158 " <arg name=\"userspace\" type=\"t\"/>\n" \
159 " <arg name=\"total\" type=\"t\"/>\n" \
162 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
163 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
164 " <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
165 " <property name=\"Features\" type=\"s\" access=\"read\"/>\n" \
166 " <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n" \
167 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
168 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
169 " <property name=\"InitRDTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
170 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
171 " <property name=\"StartupTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
172 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
173 " <property name=\"FinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
174 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n" \
175 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
176 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
177 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
178 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
179 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
180 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
181 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
182 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
183 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
184 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
185 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
186 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
187 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
188 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
189 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
190 " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
191 " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
193 #ifdef HAVE_SYSV_COMPAT
194 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
195 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
196 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
197 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
199 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
202 #define BUS_MANAGER_INTERFACE_END \
205 #define BUS_MANAGER_INTERFACE \
206 BUS_MANAGER_INTERFACE_BEGIN \
207 BUS_MANAGER_INTERFACE_METHODS \
208 BUS_MANAGER_INTERFACE_SIGNALS \
209 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
210 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
211 BUS_MANAGER_INTERFACE_END
213 #define INTROSPECTION_BEGIN \
214 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
216 BUS_MANAGER_INTERFACE \
217 BUS_PROPERTIES_INTERFACE \
219 BUS_INTROSPECTABLE_INTERFACE
221 #define INTROSPECTION_END \
224 #define INTERFACES_LIST \
225 BUS_GENERIC_INTERFACES_LIST \
226 "org.freedesktop.systemd1.Manager\0"
228 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
230 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
231 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
233 static int bus_manager_append_tainted(DBusMessageIter *i, const char *property, void *data) {
236 char buf[LINE_MAX] = "", *e = buf, *p = NULL;
243 e = stpcpy(e, "usr-separate-fs ");
245 if (readlink_malloc("/etc/mtab", &p) < 0)
246 e = stpcpy(e, "etc-mtab-not-symlink ");
250 if (access("/proc/cgroups", F_OK) < 0)
251 e = stpcpy(e, "cgroups-missing ");
255 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
261 static int bus_manager_append_log_target(DBusMessageIter *i, const char *property, void *data) {
267 t = log_target_to_string(log_get_target());
269 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
275 static int bus_manager_set_log_target(DBusMessageIter *i, const char *property) {
281 dbus_message_iter_get_basic(i, &t);
283 return log_set_target_from_string(t);
286 static int bus_manager_append_log_level(DBusMessageIter *i, const char *property, void *data) {
292 t = log_level_to_string(log_get_max_level());
294 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
300 static int bus_manager_set_log_level(DBusMessageIter *i, const char *property) {
306 dbus_message_iter_get_basic(i, &t);
308 return log_set_max_level_from_string(t);
311 static int bus_manager_append_n_names(DBusMessageIter *i, const char *property, void *data) {
319 u = hashmap_size(m->units);
321 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
327 static int bus_manager_append_n_jobs(DBusMessageIter *i, const char *property, void *data) {
335 u = hashmap_size(m->jobs);
337 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
343 static int bus_manager_append_progress(DBusMessageIter *i, const char *property, void *data) {
351 if (dual_timestamp_is_set(&m->finish_timestamp))
354 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
356 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
362 static const char *message_get_sender_with_fallback(DBusMessage *m) {
367 if ((s = dbus_message_get_sender(m)))
370 /* When the message came in from a direct connection the
371 * message will have no sender. We fix that here. */
376 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
379 const BusProperty properties[] = {
380 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
381 { "org.freedesktop.systemd1.Manager", "Distribution", bus_property_append_string, "s", DISTRIBUTION },
382 { "org.freedesktop.systemd1.Manager", "Features", bus_property_append_string, "s", SYSTEMD_FEATURES },
383 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
384 { "org.freedesktop.systemd1.Manager", "Tainted", bus_manager_append_tainted, "s", m },
385 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime },
386 { "org.freedesktop.systemd1.Manager", "InitRDTimestampMonotonic", bus_property_append_uint64, "t", &m->initrd_timestamp.monotonic },
387 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
388 { "org.freedesktop.systemd1.Manager", "StartupTimestampMonotonic", bus_property_append_uint64, "t", &m->startup_timestamp.monotonic },
389 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
390 { "org.freedesktop.systemd1.Manager", "FinishTimestampMonotonic", bus_property_append_uint64, "t",&m->finish_timestamp.monotonic },
391 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", m, bus_manager_set_log_level },
392 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", m, bus_manager_set_log_target },
393 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", m },
394 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", m },
395 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
396 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
397 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", m },
398 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
399 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
400 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
401 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
402 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
403 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
404 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
405 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
406 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
407 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output },
408 { "org.freedesktop.systemd1.Manager", "DefaultStandardError", bus_manager_append_exec_output, "s", &m->default_std_error },
409 #ifdef HAVE_SYSV_COMPAT
410 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
411 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
412 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
414 { NULL, NULL, NULL, NULL, NULL }
419 DBusMessage *reply = NULL;
421 JobType job_type = _JOB_TYPE_INVALID;
422 bool reload_if_possible = false;
428 dbus_error_init(&error);
430 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
434 if (!dbus_message_get_args(
437 DBUS_TYPE_STRING, &name,
439 return bus_send_error_reply(connection, message, &error, -EINVAL);
441 if (!(u = manager_get_unit(m, name))) {
442 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
443 return bus_send_error_reply(connection, message, &error, -ENOENT);
446 if (!(reply = dbus_message_new_method_return(message)))
449 if (!(path = unit_dbus_path(u)))
452 if (!dbus_message_append_args(
454 DBUS_TYPE_OBJECT_PATH, &path,
457 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
461 if (!dbus_message_get_args(
464 DBUS_TYPE_UINT32, &pid,
466 return bus_send_error_reply(connection, message, &error, -EINVAL);
468 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
469 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
470 return bus_send_error_reply(connection, message, &error, -ENOENT);
473 if (!(reply = dbus_message_new_method_return(message)))
476 if (!(path = unit_dbus_path(u)))
479 if (!dbus_message_append_args(
481 DBUS_TYPE_OBJECT_PATH, &path,
484 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
488 if (!dbus_message_get_args(
491 DBUS_TYPE_STRING, &name,
493 return bus_send_error_reply(connection, message, &error, -EINVAL);
495 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
496 return bus_send_error_reply(connection, message, &error, r);
498 if (!(reply = dbus_message_new_method_return(message)))
501 if (!(path = unit_dbus_path(u)))
504 if (!dbus_message_append_args(
506 DBUS_TYPE_OBJECT_PATH, &path,
510 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
511 job_type = JOB_START;
512 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
513 job_type = JOB_START;
514 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
516 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
517 job_type = JOB_RELOAD;
518 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
519 job_type = JOB_RESTART;
520 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
521 job_type = JOB_TRY_RESTART;
522 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
523 reload_if_possible = true;
524 job_type = JOB_RESTART;
525 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
526 reload_if_possible = true;
527 job_type = JOB_TRY_RESTART;
528 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
529 const char *name, *swho, *smode;
535 if (!dbus_message_get_args(
538 DBUS_TYPE_STRING, &name,
539 DBUS_TYPE_STRING, &swho,
540 DBUS_TYPE_STRING, &smode,
541 DBUS_TYPE_INT32, &signo,
543 return bus_send_error_reply(connection, message, &error, -EINVAL);
545 if ((mode = kill_mode_from_string(smode)) < 0 ||
546 (who = kill_who_from_string(swho)) < 0 ||
549 return bus_send_error_reply(connection, message, &error, -EINVAL);
551 if (!(u = manager_get_unit(m, name))) {
552 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
553 return bus_send_error_reply(connection, message, &error, -ENOENT);
556 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
557 return bus_send_error_reply(connection, message, &error, r);
559 if (!(reply = dbus_message_new_method_return(message)))
562 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
566 if (!dbus_message_get_args(
569 DBUS_TYPE_UINT32, &id,
571 return bus_send_error_reply(connection, message, &error, -EINVAL);
573 if (!(j = manager_get_job(m, id))) {
574 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
575 return bus_send_error_reply(connection, message, &error, -ENOENT);
578 if (!(reply = dbus_message_new_method_return(message)))
581 if (!(path = job_dbus_path(j)))
584 if (!dbus_message_append_args(
586 DBUS_TYPE_OBJECT_PATH, &path,
590 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
592 manager_clear_jobs(m);
594 if (!(reply = dbus_message_new_method_return(message)))
597 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
599 manager_reset_failed(m);
601 if (!(reply = dbus_message_new_method_return(message)))
604 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
608 if (!dbus_message_get_args(
611 DBUS_TYPE_STRING, &name,
613 return bus_send_error_reply(connection, message, &error, -EINVAL);
615 if (!(u = manager_get_unit(m, name))) {
616 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
617 return bus_send_error_reply(connection, message, &error, -ENOENT);
620 unit_reset_failed(u);
622 if (!(reply = dbus_message_new_method_return(message)))
625 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
626 DBusMessageIter iter, sub;
631 if (!(reply = dbus_message_new_method_return(message)))
634 dbus_message_iter_init_append(reply, &iter);
636 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
639 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
640 char *u_path, *j_path;
641 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
642 DBusMessageIter sub2;
649 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
652 description = unit_description(u);
653 load_state = unit_load_state_to_string(u->meta.load_state);
654 active_state = unit_active_state_to_string(unit_active_state(u));
655 sub_state = unit_sub_state_to_string(u);
657 f = unit_following(u);
658 following = f ? f->meta.id : "";
660 if (!(u_path = unit_dbus_path(u)))
664 job_id = (uint32_t) u->meta.job->id;
666 if (!(j_path = job_dbus_path(u->meta.job))) {
671 sjob_type = job_type_to_string(u->meta.job->type);
678 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
679 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
680 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
681 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
682 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
683 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
684 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
685 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
686 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
687 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
698 if (!dbus_message_iter_close_container(&sub, &sub2))
702 if (!dbus_message_iter_close_container(&iter, &sub))
705 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
706 DBusMessageIter iter, sub;
710 if (!(reply = dbus_message_new_method_return(message)))
713 dbus_message_iter_init_append(reply, &iter);
715 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
718 HASHMAP_FOREACH(j, m->jobs, i) {
719 char *u_path, *j_path;
720 const char *state, *type;
722 DBusMessageIter sub2;
724 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
727 id = (uint32_t) j->id;
728 state = job_state_to_string(j->state);
729 type = job_type_to_string(j->type);
731 if (!(j_path = job_dbus_path(j)))
734 if (!(u_path = unit_dbus_path(j->unit))) {
739 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
740 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
741 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
742 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
743 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
744 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
753 if (!dbus_message_iter_close_container(&sub, &sub2))
757 if (!dbus_message_iter_close_container(&iter, &sub))
760 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
764 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
765 if (!(s = set_new(string_hash_func, string_compare_func)))
768 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
774 if (!(client = strdup(message_get_sender_with_fallback(message))))
777 if ((r = set_put(s, client)) < 0) {
779 return bus_send_error_reply(connection, message, NULL, r);
782 if (!(reply = dbus_message_new_method_return(message)))
785 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
788 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
789 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
790 return bus_send_error_reply(connection, message, &error, -ENOENT);
795 if (!(reply = dbus_message_new_method_return(message)))
798 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
803 if (!(reply = dbus_message_new_method_return(message)))
806 if (!(f = open_memstream(&dump, &size)))
809 manager_dump_units(m, f, NULL);
810 manager_dump_jobs(m, f, NULL);
820 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
826 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
831 if (!dbus_message_get_args(
834 DBUS_TYPE_STRING, &name,
835 DBUS_TYPE_BOOLEAN, &cleanup,
837 return bus_send_error_reply(connection, message, &error, -EINVAL);
839 if (name && name[0] == 0)
842 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
843 return bus_send_error_reply(connection, message, &error, r);
845 if (!(reply = dbus_message_new_method_return(message)))
848 if (!(path = unit_dbus_path(UNIT(s))))
851 if (!dbus_message_append_args(
853 DBUS_TYPE_OBJECT_PATH, &path,
857 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
858 char *introspection = NULL;
866 if (!(reply = dbus_message_new_method_return(message)))
869 /* We roll our own introspection code here, instead of
870 * relying on bus_default_message_handler() because we
871 * need to generate our introspection string
874 if (!(f = open_memstream(&introspection, &size)))
877 fputs(INTROSPECTION_BEGIN, f);
879 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
885 if (!(p = bus_path_escape(k))) {
891 fprintf(f, "<node name=\"unit/%s\"/>", p);
895 HASHMAP_FOREACH(j, m->jobs, i)
896 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
898 fputs(INTROSPECTION_END, f);
911 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
918 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
920 assert(!m->queued_message);
922 /* Instead of sending the reply back right away, we
923 * just remember that we need to and then send it
924 * after the reload is finished. That way the caller
925 * knows when the reload finished. */
927 if (!(m->queued_message = dbus_message_new_method_return(message)))
930 m->queued_message_connection = connection;
931 m->exit_code = MANAGER_RELOAD;
933 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
935 /* We don't send a reply back here, the client should
936 * just wait for us disconnecting. */
938 m->exit_code = MANAGER_REEXECUTE;
940 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
942 if (m->running_as == MANAGER_SYSTEM) {
943 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
944 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
947 if (!(reply = dbus_message_new_method_return(message)))
950 m->exit_code = MANAGER_EXIT;
952 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
954 if (m->running_as != MANAGER_SYSTEM) {
955 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
956 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
959 if (!(reply = dbus_message_new_method_return(message)))
962 m->exit_code = MANAGER_REBOOT;
964 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
966 if (m->running_as != MANAGER_SYSTEM) {
967 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
968 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
971 if (!(reply = dbus_message_new_method_return(message)))
974 m->exit_code = MANAGER_POWEROFF;
976 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
978 if (m->running_as != MANAGER_SYSTEM) {
979 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
980 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
983 if (!(reply = dbus_message_new_method_return(message)))
986 m->exit_code = MANAGER_HALT;
988 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
990 if (m->running_as != MANAGER_SYSTEM) {
991 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
992 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
995 if (!(reply = dbus_message_new_method_return(message)))
998 m->exit_code = MANAGER_KEXEC;
1000 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
1001 char **l = NULL, **e = NULL;
1003 if ((r = bus_parse_strv(message, &l)) < 0) {
1007 return bus_send_error_reply(connection, message, NULL, r);
1010 e = strv_env_merge(2, m->environment, l);
1016 if (!(reply = dbus_message_new_method_return(message))) {
1021 strv_free(m->environment);
1024 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1025 char **l = NULL, **e = NULL;
1027 if ((r = bus_parse_strv(message, &l)) < 0) {
1031 return bus_send_error_reply(connection, message, NULL, r);
1034 e = strv_env_delete(m->environment, 1, l);
1040 if (!(reply = dbus_message_new_method_return(message))) {
1045 strv_free(m->environment);
1048 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment")) {
1049 char **l_set = NULL, **l_unset = NULL, **e = NULL, **f = NULL;
1050 DBusMessageIter iter;
1052 if (!dbus_message_iter_init(message, &iter))
1053 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1055 r = bus_parse_strv_iter(&iter, &l_unset);
1060 return bus_send_error_reply(connection, message, NULL, r);
1063 if (!dbus_message_iter_next(&iter)) {
1065 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1068 r = bus_parse_strv_iter(&iter, &l_set);
1074 return bus_send_error_reply(connection, message, NULL, r);
1077 e = strv_env_delete(m->environment, 1, l_unset);
1085 f = strv_env_merge(2, e, l_set);
1092 if (!(reply = dbus_message_new_method_return(message))) {
1097 strv_free(m->environment);
1101 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1103 if (job_type != _JOB_TYPE_INVALID) {
1104 const char *name, *smode, *old_name = NULL;
1110 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1111 b = dbus_message_get_args(
1114 DBUS_TYPE_STRING, &old_name,
1115 DBUS_TYPE_STRING, &name,
1116 DBUS_TYPE_STRING, &smode,
1119 b = dbus_message_get_args(
1122 DBUS_TYPE_STRING, &name,
1123 DBUS_TYPE_STRING, &smode,
1127 return bus_send_error_reply(connection, message, &error, -EINVAL);
1130 if (!(u = manager_get_unit(m, old_name)) ||
1132 u->meta.job->type != JOB_START) {
1133 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1134 return bus_send_error_reply(connection, message, &error, -ENOENT);
1138 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1139 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1140 return bus_send_error_reply(connection, message, &error, -EINVAL);
1143 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1144 return bus_send_error_reply(connection, message, &error, r);
1146 if (reload_if_possible && unit_can_reload(u)) {
1147 if (job_type == JOB_RESTART)
1148 job_type = JOB_RELOAD_OR_START;
1149 else if (job_type == JOB_TRY_RESTART)
1150 job_type = JOB_RELOAD;
1153 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1154 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1155 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1156 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1157 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1158 return bus_send_error_reply(connection, message, &error, -EPERM);
1161 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1162 return bus_send_error_reply(connection, message, &error, r);
1164 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1167 j->bus = connection;
1169 if (!(reply = dbus_message_new_method_return(message)))
1172 if (!(path = job_dbus_path(j)))
1175 if (!dbus_message_append_args(
1177 DBUS_TYPE_OBJECT_PATH, &path,
1183 if (!dbus_connection_send(connection, reply, NULL))
1186 dbus_message_unref(reply);
1191 return DBUS_HANDLER_RESULT_HANDLED;
1197 dbus_message_unref(reply);
1199 dbus_error_free(&error);
1201 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1204 const DBusObjectPathVTable bus_manager_vtable = {
1205 .message_function = bus_manager_message_handler