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"
32 #define BUS_MANAGER_INTERFACE_BEGIN \
33 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
35 #define BUS_MANAGER_INTERFACE_METHODS \
36 " <method name=\"GetUnit\">\n" \
37 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
40 " <method name=\"GetUnitByPID\">\n" \
41 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
42 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
44 " <method name=\"LoadUnit\">\n" \
45 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
46 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
48 " <method name=\"StartUnit\">\n" \
49 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
50 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
51 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
53 " <method name=\"StartUnitReplace\">\n" \
54 " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
59 " <method name=\"StopUnit\">\n" \
60 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
61 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
62 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
64 " <method name=\"ReloadUnit\">\n" \
65 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
69 " <method name=\"RestartUnit\">\n" \
70 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
71 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
72 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
74 " <method name=\"TryRestartUnit\">\n" \
75 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
76 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
77 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
79 " <method name=\"ReloadOrRestartUnit\">\n" \
80 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
81 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
82 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
84 " <method name=\"ReloadOrTryRestartUnit\">\n" \
85 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
86 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
87 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
89 " <method name=\"KillUnit\">\n" \
90 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
91 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
92 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
93 " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
95 " <method name=\"ResetFailedUnit\">\n" \
96 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
98 " <method name=\"GetJob\">\n" \
99 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
100 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
102 " <method name=\"ClearJobs\"/>\n" \
103 " <method name=\"ResetFailed\"/>\n" \
104 " <method name=\"ListUnits\">\n" \
105 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
107 " <method name=\"ListJobs\">\n" \
108 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
110 " <method name=\"Subscribe\"/>\n" \
111 " <method name=\"Unsubscribe\"/>\n" \
112 " <method name=\"Dump\"/>\n" \
113 " <method name=\"CreateSnapshot\">\n" \
114 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
115 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
116 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
118 " <method name=\"Reload\"/>\n" \
119 " <method name=\"Reexecute\"/>\n" \
120 " <method name=\"Exit\"/>\n" \
121 " <method name=\"Reboot\"/>\n" \
122 " <method name=\"PowerOff\"/>\n" \
123 " <method name=\"Halt\"/>\n" \
124 " <method name=\"KExec\"/>\n" \
125 " <method name=\"SetEnvironment\">\n" \
126 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
128 " <method name=\"UnsetEnvironment\">\n" \
129 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
132 #define BUS_MANAGER_INTERFACE_SIGNALS \
133 " <signal name=\"UnitNew\">\n" \
134 " <arg name=\"id\" type=\"s\"/>\n" \
135 " <arg name=\"unit\" type=\"o\"/>\n" \
137 " <signal name=\"UnitRemoved\">\n" \
138 " <arg name=\"id\" type=\"s\"/>\n" \
139 " <arg name=\"unit\" type=\"o\"/>\n" \
141 " <signal name=\"JobNew\">\n" \
142 " <arg name=\"id\" type=\"u\"/>\n" \
143 " <arg name=\"job\" type=\"o\"/>\n" \
145 " <signal name=\"JobRemoved\">\n" \
146 " <arg name=\"id\" type=\"u\"/>\n" \
147 " <arg name=\"job\" type=\"o\"/>\n" \
148 " <arg name=\"result\" type=\"s\"/>\n" \
151 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
152 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
153 " <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
154 " <property name=\"Features\" type=\"s\" access=\"read\"/>\n" \
155 " <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n" \
156 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
157 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
158 " <property name=\"InitRDTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
159 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
160 " <property name=\"StartupTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
161 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
162 " <property name=\"FinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
163 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n" \
164 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
165 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
166 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
167 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
168 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
169 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
170 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
171 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
172 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
173 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
174 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
175 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
176 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
177 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
178 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
179 " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
180 " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
182 #ifdef HAVE_SYSV_COMPAT
183 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
184 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
185 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
186 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
188 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
191 #define BUS_MANAGER_INTERFACE_END \
194 #define BUS_MANAGER_INTERFACE \
195 BUS_MANAGER_INTERFACE_BEGIN \
196 BUS_MANAGER_INTERFACE_METHODS \
197 BUS_MANAGER_INTERFACE_SIGNALS \
198 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
199 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
200 BUS_MANAGER_INTERFACE_END
202 #define INTROSPECTION_BEGIN \
203 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
205 BUS_MANAGER_INTERFACE \
206 BUS_PROPERTIES_INTERFACE \
208 BUS_INTROSPECTABLE_INTERFACE
210 #define INTROSPECTION_END \
213 #define INTERFACES_LIST \
214 BUS_GENERIC_INTERFACES_LIST \
215 "org.freedesktop.systemd1.Manager\0"
217 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
219 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
220 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
222 static int bus_manager_append_tainted(Manager *m, DBusMessageIter *i, const char *property, void *data) {
224 char buf[LINE_MAX] = "", *e = buf, *p = NULL;
231 e = stpcpy(e, "usr-separate-fs ");
233 if (readlink_malloc("/etc/mtab", &p) < 0)
234 e = stpcpy(e, "etc-mtab-not-symlink ");
238 if (access("/proc/cgroups", F_OK) < 0)
239 e = stpcpy(e, "cgroups-missing ");
243 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
249 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
256 t = log_target_to_string(log_get_target());
258 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
264 static int bus_manager_set_log_target(Manager *m, DBusMessageIter *i, const char *property) {
271 dbus_message_iter_get_basic(i, &t);
273 return log_set_target_from_string(t);
276 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
283 t = log_level_to_string(log_get_max_level());
285 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
291 static int bus_manager_set_log_level(Manager *m, DBusMessageIter *i, const char *property) {
298 dbus_message_iter_get_basic(i, &t);
300 return log_set_max_level_from_string(t);
303 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
310 u = hashmap_size(m->units);
312 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
318 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
325 u = hashmap_size(m->jobs);
327 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
333 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
340 if (dual_timestamp_is_set(&m->finish_timestamp))
343 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
345 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
351 static const char *message_get_sender_with_fallback(DBusMessage *m) {
356 if ((s = dbus_message_get_sender(m)))
359 /* When the message came in from a direct connection the
360 * message will have no sender. We fix that here. */
365 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
368 const BusProperty properties[] = {
369 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
370 { "org.freedesktop.systemd1.Manager", "Distribution", bus_property_append_string, "s", DISTRIBUTION },
371 { "org.freedesktop.systemd1.Manager", "Features", bus_property_append_string, "s", SYSTEMD_FEATURES },
372 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
373 { "org.freedesktop.systemd1.Manager", "Tainted", bus_manager_append_tainted, "s", m },
374 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime },
375 { "org.freedesktop.systemd1.Manager", "InitRDTimestampMonotonic", bus_property_append_uint64, "t", &m->initrd_timestamp.monotonic },
376 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
377 { "org.freedesktop.systemd1.Manager", "StartupTimestampMonotonic", bus_property_append_uint64, "t", &m->startup_timestamp.monotonic },
378 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
379 { "org.freedesktop.systemd1.Manager", "FinishTimestampMonotonic", bus_property_append_uint64, "t",&m->finish_timestamp.monotonic },
380 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL, bus_manager_set_log_level},
381 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL, bus_manager_set_log_target},
382 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
383 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
384 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
385 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
386 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
387 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
388 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
389 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
390 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
391 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
392 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
393 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
394 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
395 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
396 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output },
397 { "org.freedesktop.systemd1.Manager", "DefaultStandardError", bus_manager_append_exec_output, "s", &m->default_std_error },
398 #ifdef HAVE_SYSV_COMPAT
399 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
400 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
401 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
403 { NULL, NULL, NULL, NULL, NULL }
408 DBusMessage *reply = NULL;
410 JobType job_type = _JOB_TYPE_INVALID;
411 bool reload_if_possible = false;
417 dbus_error_init(&error);
419 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
423 if (!dbus_message_get_args(
426 DBUS_TYPE_STRING, &name,
428 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
430 if (!(u = manager_get_unit(m, name))) {
431 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
432 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
435 if (!(reply = dbus_message_new_method_return(message)))
438 if (!(path = unit_dbus_path(u)))
441 if (!dbus_message_append_args(
443 DBUS_TYPE_OBJECT_PATH, &path,
446 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
450 if (!dbus_message_get_args(
453 DBUS_TYPE_UINT32, &pid,
455 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
457 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
458 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
459 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
462 if (!(reply = dbus_message_new_method_return(message)))
465 if (!(path = unit_dbus_path(u)))
468 if (!dbus_message_append_args(
470 DBUS_TYPE_OBJECT_PATH, &path,
473 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
477 if (!dbus_message_get_args(
480 DBUS_TYPE_STRING, &name,
482 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
484 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
485 return bus_send_error_reply(m, connection, message, &error, r);
487 if (!(reply = dbus_message_new_method_return(message)))
490 if (!(path = unit_dbus_path(u)))
493 if (!dbus_message_append_args(
495 DBUS_TYPE_OBJECT_PATH, &path,
499 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
500 job_type = JOB_START;
501 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
502 job_type = JOB_START;
503 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
505 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
506 job_type = JOB_RELOAD;
507 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
508 job_type = JOB_RESTART;
509 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
510 job_type = JOB_TRY_RESTART;
511 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
512 reload_if_possible = true;
513 job_type = JOB_RESTART;
514 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
515 reload_if_possible = true;
516 job_type = JOB_TRY_RESTART;
517 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
518 const char *name, *swho, *smode;
524 if (!dbus_message_get_args(
527 DBUS_TYPE_STRING, &name,
528 DBUS_TYPE_STRING, &swho,
529 DBUS_TYPE_STRING, &smode,
530 DBUS_TYPE_INT32, &signo,
532 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
534 if ((mode = kill_mode_from_string(smode)) < 0 ||
535 (who = kill_who_from_string(swho)) < 0 ||
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 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
546 return bus_send_error_reply(m, connection, message, &error, r);
548 if (!(reply = dbus_message_new_method_return(message)))
551 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
555 if (!dbus_message_get_args(
558 DBUS_TYPE_UINT32, &id,
560 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
562 if (!(j = manager_get_job(m, id))) {
563 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
564 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
567 if (!(reply = dbus_message_new_method_return(message)))
570 if (!(path = job_dbus_path(j)))
573 if (!dbus_message_append_args(
575 DBUS_TYPE_OBJECT_PATH, &path,
579 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
581 manager_clear_jobs(m);
583 if (!(reply = dbus_message_new_method_return(message)))
586 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
588 manager_reset_failed(m);
590 if (!(reply = dbus_message_new_method_return(message)))
593 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
597 if (!dbus_message_get_args(
600 DBUS_TYPE_STRING, &name,
602 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
604 if (!(u = manager_get_unit(m, name))) {
605 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
606 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
609 unit_reset_failed(u);
611 if (!(reply = dbus_message_new_method_return(message)))
614 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
615 DBusMessageIter iter, sub;
620 if (!(reply = dbus_message_new_method_return(message)))
623 dbus_message_iter_init_append(reply, &iter);
625 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
628 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
629 char *u_path, *j_path;
630 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
631 DBusMessageIter sub2;
638 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
641 description = unit_description(u);
642 load_state = unit_load_state_to_string(u->meta.load_state);
643 active_state = unit_active_state_to_string(unit_active_state(u));
644 sub_state = unit_sub_state_to_string(u);
646 f = unit_following(u);
647 following = f ? f->meta.id : "";
649 if (!(u_path = unit_dbus_path(u)))
653 job_id = (uint32_t) u->meta.job->id;
655 if (!(j_path = job_dbus_path(u->meta.job))) {
660 sjob_type = job_type_to_string(u->meta.job->type);
667 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
668 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
669 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
670 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
671 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
672 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
673 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
674 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
675 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
676 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
687 if (!dbus_message_iter_close_container(&sub, &sub2))
691 if (!dbus_message_iter_close_container(&iter, &sub))
694 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
695 DBusMessageIter iter, sub;
699 if (!(reply = dbus_message_new_method_return(message)))
702 dbus_message_iter_init_append(reply, &iter);
704 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
707 HASHMAP_FOREACH(j, m->jobs, i) {
708 char *u_path, *j_path;
709 const char *state, *type;
711 DBusMessageIter sub2;
713 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
716 id = (uint32_t) j->id;
717 state = job_state_to_string(j->state);
718 type = job_type_to_string(j->type);
720 if (!(j_path = job_dbus_path(j)))
723 if (!(u_path = unit_dbus_path(j->unit))) {
728 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
729 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
730 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
731 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
732 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
733 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
742 if (!dbus_message_iter_close_container(&sub, &sub2))
746 if (!dbus_message_iter_close_container(&iter, &sub))
749 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
753 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
754 if (!(s = set_new(string_hash_func, string_compare_func)))
757 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
763 if (!(client = strdup(message_get_sender_with_fallback(message))))
766 if ((r = set_put(s, client)) < 0) {
768 return bus_send_error_reply(m, connection, message, NULL, r);
771 if (!(reply = dbus_message_new_method_return(message)))
774 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
777 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
778 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
779 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
784 if (!(reply = dbus_message_new_method_return(message)))
787 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
792 if (!(reply = dbus_message_new_method_return(message)))
795 if (!(f = open_memstream(&dump, &size)))
798 manager_dump_units(m, f, NULL);
799 manager_dump_jobs(m, f, NULL);
809 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
815 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
820 if (!dbus_message_get_args(
823 DBUS_TYPE_STRING, &name,
824 DBUS_TYPE_BOOLEAN, &cleanup,
826 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
828 if (name && name[0] == 0)
831 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
832 return bus_send_error_reply(m, connection, message, &error, r);
834 if (!(reply = dbus_message_new_method_return(message)))
837 if (!(path = unit_dbus_path(UNIT(s))))
840 if (!dbus_message_append_args(
842 DBUS_TYPE_OBJECT_PATH, &path,
846 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
847 char *introspection = NULL;
855 if (!(reply = dbus_message_new_method_return(message)))
858 /* We roll our own introspection code here, instead of
859 * relying on bus_default_message_handler() because we
860 * need to generate our introspection string
863 if (!(f = open_memstream(&introspection, &size)))
866 fputs(INTROSPECTION_BEGIN, f);
868 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
874 if (!(p = bus_path_escape(k))) {
880 fprintf(f, "<node name=\"unit/%s\"/>", p);
884 HASHMAP_FOREACH(j, m->jobs, i)
885 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
887 fputs(INTROSPECTION_END, f);
900 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
907 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
909 assert(!m->queued_message);
911 /* Instead of sending the reply back right away, we
912 * just remember that we need to and then send it
913 * after the reload is finished. That way the caller
914 * knows when the reload finished. */
916 if (!(m->queued_message = dbus_message_new_method_return(message)))
919 m->queued_message_connection = connection;
920 m->exit_code = MANAGER_RELOAD;
922 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
924 if (!(reply = dbus_message_new_method_return(message)))
927 m->exit_code = MANAGER_REEXECUTE;
929 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
931 if (m->running_as == MANAGER_SYSTEM) {
932 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
933 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
936 if (!(reply = dbus_message_new_method_return(message)))
939 m->exit_code = MANAGER_EXIT;
941 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
943 if (m->running_as != MANAGER_SYSTEM) {
944 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
945 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
948 if (!(reply = dbus_message_new_method_return(message)))
951 m->exit_code = MANAGER_REBOOT;
953 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
955 if (m->running_as != MANAGER_SYSTEM) {
956 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
957 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
960 if (!(reply = dbus_message_new_method_return(message)))
963 m->exit_code = MANAGER_POWEROFF;
965 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
967 if (m->running_as != MANAGER_SYSTEM) {
968 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
969 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
972 if (!(reply = dbus_message_new_method_return(message)))
975 m->exit_code = MANAGER_HALT;
977 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
979 if (m->running_as != MANAGER_SYSTEM) {
980 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
981 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
984 if (!(reply = dbus_message_new_method_return(message)))
987 m->exit_code = MANAGER_KEXEC;
989 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
990 char **l = NULL, **e = NULL;
992 if ((r = bus_parse_strv(message, &l)) < 0) {
996 return bus_send_error_reply(m, connection, message, NULL, r);
999 e = strv_env_merge(2, m->environment, l);
1005 if (!(reply = dbus_message_new_method_return(message))) {
1010 strv_free(m->environment);
1013 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1014 char **l = NULL, **e = NULL;
1016 if ((r = bus_parse_strv(message, &l)) < 0) {
1020 return bus_send_error_reply(m, connection, message, NULL, r);
1023 e = strv_env_delete(m->environment, 1, l);
1029 if (!(reply = dbus_message_new_method_return(message))) {
1034 strv_free(m->environment);
1038 return bus_default_message_handler(m, connection, message, NULL, INTERFACES_LIST, properties);
1040 if (job_type != _JOB_TYPE_INVALID) {
1041 const char *name, *smode, *old_name = NULL;
1047 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1048 b = dbus_message_get_args(
1051 DBUS_TYPE_STRING, &old_name,
1052 DBUS_TYPE_STRING, &name,
1053 DBUS_TYPE_STRING, &smode,
1056 b = dbus_message_get_args(
1059 DBUS_TYPE_STRING, &name,
1060 DBUS_TYPE_STRING, &smode,
1064 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1067 if (!(u = manager_get_unit(m, old_name)) ||
1069 u->meta.job->type != JOB_START) {
1070 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1071 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
1075 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1076 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1077 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1080 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1081 return bus_send_error_reply(m, connection, message, &error, r);
1083 if (reload_if_possible && unit_can_reload(u)) {
1084 if (job_type == JOB_RESTART)
1085 job_type = JOB_RELOAD_OR_START;
1086 else if (job_type == JOB_TRY_RESTART)
1087 job_type = JOB_RELOAD;
1090 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1091 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1092 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1093 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1094 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1095 return bus_send_error_reply(m, connection, message, &error, -EPERM);
1098 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1099 return bus_send_error_reply(m, connection, message, &error, r);
1101 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1104 j->bus = connection;
1106 if (!(reply = dbus_message_new_method_return(message)))
1109 if (!(path = job_dbus_path(j)))
1112 if (!dbus_message_append_args(
1114 DBUS_TYPE_OBJECT_PATH, &path,
1120 if (!dbus_connection_send(connection, reply, NULL))
1123 dbus_message_unref(reply);
1128 return DBUS_HANDLER_RESULT_HANDLED;
1134 dbus_message_unref(reply);
1136 dbus_error_free(&error);
1138 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1141 const DBusObjectPathVTable bus_manager_vtable = {
1142 .message_function = bus_manager_message_handler