1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "dbus-manager.h"
29 #define BUS_MANAGER_INTERFACE \
30 " <interface name=\"org.freedesktop.systemd1.Manager\">\n" \
31 " <method name=\"GetUnit\">\n" \
32 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
33 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
35 " <method name=\"LoadUnit\">\n" \
36 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
37 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
39 " <method name=\"StartUnit\">\n" \
40 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
41 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
42 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
44 " <method name=\"StopUnit\">\n" \
45 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
46 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
47 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
49 " <method name=\"ReloadUnit\">\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=\"RestartUnit\">\n" \
55 " <arg name=\"name\" 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=\"TryRestartUnit\">\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=\"GetJob\">\n" \
65 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
66 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
68 " <method name=\"ClearJobs\"/>\n" \
69 " <method name=\"ListUnits\">\n" \
70 " <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>\n" \
72 " <method name=\"ListJobs\">\n" \
73 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
75 " <method name=\"Subscribe\"/>\n" \
76 " <method name=\"Unsubscribe\"/>\n" \
77 " <method name=\"Dump\"/>\n" \
78 " <method name=\"CreateSnapshot\">\n" \
79 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
80 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
81 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
83 " <method name=\"Reload\"/>\n" \
84 " <method name=\"Reexecute\"/>\n" \
85 " <method name=\"Exit\"/>\n" \
86 " <method name=\"SetEnvironment\">\n" \
87 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
89 " <method name=\"UnsetEnvironment\">\n" \
90 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
92 " <signal name=\"UnitNew\">\n" \
93 " <arg name=\"id\" type=\"s\"/>\n" \
94 " <arg name=\"unit\" type=\"o\"/>\n" \
96 " <signal name=\"UnitRemoved\">\n" \
97 " <arg name=\"id\" type=\"s\"/>\n" \
98 " <arg name=\"unit\" type=\"o\"/>\n" \
100 " <signal name=\"JobNew\">\n" \
101 " <arg name=\"id\" type=\"u\"/>\n" \
102 " <arg name=\"job\" type=\"o\"/>\n" \
104 " <signal name=\"JobRemoved\">\n" \
105 " <arg name=\"id\" type=\"u\"/>\n" \
106 " <arg name=\"job\" type=\"o\"/>\n" \
107 " <arg name=\"success\" type=\"b\"/>\n" \
109 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
110 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
111 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
112 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
113 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
114 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
115 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
116 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
117 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
118 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
119 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
120 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
121 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \
122 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
125 #define INTROSPECTION_BEGIN \
126 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
128 BUS_MANAGER_INTERFACE \
129 BUS_PROPERTIES_INTERFACE \
130 BUS_INTROSPECTABLE_INTERFACE
132 #define INTROSPECTION_END \
135 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
137 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
139 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
146 t = log_target_to_string(log_get_target());
148 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
154 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
161 t = log_level_to_string(log_get_max_level());
163 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
169 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
176 u = hashmap_size(m->units);
178 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
184 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
191 u = hashmap_size(m->jobs);
193 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
199 static const char *message_get_sender_with_fallback(DBusMessage *m) {
204 if ((s = dbus_message_get_sender(m)))
207 /* When the message came in from a direct connection the
208 * message will have no sender. We fix that here. */
213 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
216 const BusProperty properties[] = {
217 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
218 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
219 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
220 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
221 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
222 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
223 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
224 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
225 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
226 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
227 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
228 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
229 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
230 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
231 { NULL, NULL, NULL, NULL, NULL }
236 DBusMessage *reply = NULL;
238 JobType job_type = _JOB_TYPE_INVALID;
244 dbus_error_init(&error);
246 log_debug("Got D-Bus request: %s.%s() on %s",
247 dbus_message_get_interface(message),
248 dbus_message_get_member(message),
249 dbus_message_get_path(message));
251 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
255 if (!dbus_message_get_args(
258 DBUS_TYPE_STRING, &name,
260 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
262 if (!(u = manager_get_unit(m, name)))
263 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
265 if (!(reply = dbus_message_new_method_return(message)))
268 if (!(path = unit_dbus_path(u)))
271 if (!dbus_message_append_args(
273 DBUS_TYPE_OBJECT_PATH, &path,
277 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
281 if (!dbus_message_get_args(
284 DBUS_TYPE_STRING, &name,
286 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
288 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
289 return bus_send_error_reply(m, connection, message, NULL, r);
291 if (!(reply = dbus_message_new_method_return(message)))
294 if (!(path = unit_dbus_path(u)))
297 if (!dbus_message_append_args(
299 DBUS_TYPE_OBJECT_PATH, &path,
303 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
304 job_type = JOB_START;
305 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
307 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
308 job_type = JOB_RELOAD;
309 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
310 job_type = JOB_RESTART;
311 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
312 job_type = JOB_TRY_RESTART;
313 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
317 if (!dbus_message_get_args(
320 DBUS_TYPE_UINT32, &id,
322 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
324 if (!(j = manager_get_job(m, id)))
325 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
327 if (!(reply = dbus_message_new_method_return(message)))
330 if (!(path = job_dbus_path(j)))
333 if (!dbus_message_append_args(
335 DBUS_TYPE_OBJECT_PATH, &path,
339 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
341 manager_clear_jobs(m);
343 if (!(reply = dbus_message_new_method_return(message)))
346 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
347 DBusMessageIter iter, sub;
352 if (!(reply = dbus_message_new_method_return(message)))
355 dbus_message_iter_init_append(reply, &iter);
357 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
360 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
361 char *u_path, *j_path;
362 const char *description, *load_state, *active_state, *sub_state, *sjob_type;
363 DBusMessageIter sub2;
369 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
372 description = unit_description(u);
373 load_state = unit_load_state_to_string(u->meta.load_state);
374 active_state = unit_active_state_to_string(unit_active_state(u));
375 sub_state = unit_sub_state_to_string(u);
377 if (!(u_path = unit_dbus_path(u)))
381 job_id = (uint32_t) u->meta.job->id;
383 if (!(j_path = job_dbus_path(u->meta.job))) {
388 sjob_type = job_type_to_string(u->meta.job->type);
395 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
396 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
397 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
398 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
399 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
400 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
401 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
402 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
403 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
414 if (!dbus_message_iter_close_container(&sub, &sub2))
418 if (!dbus_message_iter_close_container(&iter, &sub))
421 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
422 DBusMessageIter iter, sub;
426 if (!(reply = dbus_message_new_method_return(message)))
429 dbus_message_iter_init_append(reply, &iter);
431 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
434 HASHMAP_FOREACH(j, m->jobs, i) {
435 char *u_path, *j_path;
436 const char *state, *type;
438 DBusMessageIter sub2;
440 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
443 id = (uint32_t) j->id;
444 state = job_state_to_string(j->state);
445 type = job_type_to_string(j->type);
447 if (!(j_path = job_dbus_path(j)))
450 if (!(u_path = unit_dbus_path(j->unit))) {
455 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
456 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
457 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
458 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
459 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
460 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
469 if (!dbus_message_iter_close_container(&sub, &sub2))
473 if (!dbus_message_iter_close_container(&iter, &sub))
476 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
480 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
481 if (!(s = set_new(string_hash_func, string_compare_func)))
484 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
490 if (!(client = strdup(message_get_sender_with_fallback(message))))
493 if ((r = set_put(s, client)) < 0) {
495 return bus_send_error_reply(m, connection, message, NULL, r);
498 if (!(reply = dbus_message_new_method_return(message)))
501 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
504 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message))))
505 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
509 if (!(reply = dbus_message_new_method_return(message)))
512 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
517 if (!(reply = dbus_message_new_method_return(message)))
520 if (!(f = open_memstream(&dump, &size)))
523 manager_dump_units(m, f, NULL);
524 manager_dump_jobs(m, f, NULL);
534 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
540 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
545 if (!dbus_message_get_args(
548 DBUS_TYPE_STRING, &name,
549 DBUS_TYPE_BOOLEAN, &cleanup,
551 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
553 if (name && name[0] == 0)
556 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
557 return bus_send_error_reply(m, connection, message, NULL, r);
559 if (!(reply = dbus_message_new_method_return(message)))
562 if (!(path = unit_dbus_path(UNIT(s))))
565 if (!dbus_message_append_args(
567 DBUS_TYPE_OBJECT_PATH, &path,
571 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
572 char *introspection = NULL;
580 if (!(reply = dbus_message_new_method_return(message)))
583 /* We roll our own introspection code here, instead of
584 * relying on bus_default_message_handler() because we
585 * need to generate our introspection string
588 if (!(f = open_memstream(&introspection, &size)))
591 fputs(INTROSPECTION_BEGIN, f);
593 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
599 if (!(p = bus_path_escape(k))) {
605 fprintf(f, "<node name=\"unit/%s\"/>", p);
609 HASHMAP_FOREACH(j, m->jobs, i)
610 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
612 fputs(INTROSPECTION_END, f);
625 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
632 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
634 assert(!m->queued_message);
636 /* Instead of sending the reply back right away, we
637 * just remember that we need to and then send it
638 * after the reload is finished. That way the caller
639 * knows when the reload finished. */
641 if (!(m->queued_message = dbus_message_new_method_return(message)))
644 m->exit_code = MANAGER_RELOAD;
646 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
648 if (!(reply = dbus_message_new_method_return(message)))
651 m->exit_code = MANAGER_REEXECUTE;
653 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
655 if (m->running_as == MANAGER_SYSTEM)
656 return bus_send_error_reply(m, connection, message, NULL, -ENOTSUP);
658 if (!(reply = dbus_message_new_method_return(message)))
661 m->exit_code = MANAGER_EXIT;
663 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
664 char **l = NULL, **e = NULL;
666 if ((r = bus_parse_strv(message, &l)) < 0) {
670 return bus_send_error_reply(m, connection, message, NULL, r);
673 e = strv_env_merge(2, m->environment, l);
679 if (!(reply = dbus_message_new_method_return(message))) {
684 strv_free(m->environment);
687 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
688 char **l = NULL, **e = NULL;
690 if ((r = bus_parse_strv(message, &l)) < 0) {
694 return bus_send_error_reply(m, connection, message, NULL, r);
697 e = strv_env_delete(m->environment, 1, l);
703 if (!(reply = dbus_message_new_method_return(message)))
706 strv_free(m->environment);
710 return bus_default_message_handler(m, connection, message, NULL, properties);
713 if (job_type != _JOB_TYPE_INVALID) {
714 const char *name, *smode;
719 if (!dbus_message_get_args(
722 DBUS_TYPE_STRING, &name,
723 DBUS_TYPE_STRING, &smode,
725 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
727 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
728 return bus_send_error_reply(m, connection, message, NULL, -EINVAL);
730 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
731 return bus_send_error_reply(m, connection, message, NULL, r);
733 if (job_type == JOB_START && u->meta.only_by_dependency)
734 return bus_send_error_reply(m, connection, message, NULL, -EPERM);
736 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
737 return bus_send_error_reply(m, connection, message, NULL, r);
739 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
744 if (!(reply = dbus_message_new_method_return(message)))
747 if (!(path = job_dbus_path(j)))
750 if (!dbus_message_append_args(
752 DBUS_TYPE_OBJECT_PATH, &path,
760 if (!dbus_connection_send(connection, reply, NULL))
763 dbus_message_unref(reply);
766 return DBUS_HANDLER_RESULT_HANDLED;
772 dbus_message_unref(reply);
774 dbus_error_free(&error);
776 return DBUS_HANDLER_RESULT_NEED_MEMORY;
779 const DBusObjectPathVTable bus_manager_vtable = {
780 .message_function = bus_manager_message_handler