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=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
119 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
120 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \
121 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
124 #define INTROSPECTION_BEGIN \
125 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
127 BUS_MANAGER_INTERFACE \
128 BUS_PROPERTIES_INTERFACE \
129 BUS_INTROSPECTABLE_INTERFACE
131 #define INTROSPECTION_END \
134 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
136 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
138 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
145 t = log_target_to_string(log_get_target());
147 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
153 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
160 t = log_level_to_string(log_get_max_level());
162 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
168 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
175 u = hashmap_size(m->units);
177 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
183 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
190 u = hashmap_size(m->jobs);
192 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
198 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
201 const BusProperty properties[] = {
202 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
203 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
204 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
205 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
206 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
207 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
208 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
209 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
210 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
211 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
212 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
213 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
214 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
215 { NULL, NULL, NULL, NULL, NULL }
220 DBusMessage *reply = NULL;
222 JobType job_type = _JOB_TYPE_INVALID;
228 dbus_error_init(&error);
230 log_debug("Got D-Bus request: %s.%s() on %s",
231 dbus_message_get_interface(message),
232 dbus_message_get_member(message),
233 dbus_message_get_path(message));
235 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
239 if (!dbus_message_get_args(
242 DBUS_TYPE_STRING, &name,
244 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
246 if (!(u = manager_get_unit(m, name)))
247 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
249 if (!(reply = dbus_message_new_method_return(message)))
252 if (!(path = unit_dbus_path(u)))
255 if (!dbus_message_append_args(
257 DBUS_TYPE_OBJECT_PATH, &path,
261 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
265 if (!dbus_message_get_args(
268 DBUS_TYPE_STRING, &name,
270 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
272 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
273 return bus_send_error_reply(m, connection, message, NULL, r);
275 if (!(reply = dbus_message_new_method_return(message)))
278 if (!(path = unit_dbus_path(u)))
281 if (!dbus_message_append_args(
283 DBUS_TYPE_OBJECT_PATH, &path,
287 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
288 job_type = JOB_START;
289 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
291 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
292 job_type = JOB_RELOAD;
293 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
294 job_type = JOB_RESTART;
295 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
296 job_type = JOB_TRY_RESTART;
297 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
301 if (!dbus_message_get_args(
304 DBUS_TYPE_UINT32, &id,
306 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
308 if (!(j = manager_get_job(m, id)))
309 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
311 if (!(reply = dbus_message_new_method_return(message)))
314 if (!(path = job_dbus_path(j)))
317 if (!dbus_message_append_args(
319 DBUS_TYPE_OBJECT_PATH, &path,
323 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
325 manager_clear_jobs(m);
327 if (!(reply = dbus_message_new_method_return(message)))
330 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
331 DBusMessageIter iter, sub;
336 if (!(reply = dbus_message_new_method_return(message)))
339 dbus_message_iter_init_append(reply, &iter);
341 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
344 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
345 char *u_path, *j_path;
346 const char *description, *load_state, *active_state, *sub_state, *sjob_type;
347 DBusMessageIter sub2;
353 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
356 description = unit_description(u);
357 load_state = unit_load_state_to_string(u->meta.load_state);
358 active_state = unit_active_state_to_string(unit_active_state(u));
359 sub_state = unit_sub_state_to_string(u);
361 if (!(u_path = unit_dbus_path(u)))
365 job_id = (uint32_t) u->meta.job->id;
367 if (!(j_path = job_dbus_path(u->meta.job))) {
372 sjob_type = job_type_to_string(u->meta.job->type);
379 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
380 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
381 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
382 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
383 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
384 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
385 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
386 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
387 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
398 if (!dbus_message_iter_close_container(&sub, &sub2))
402 if (!dbus_message_iter_close_container(&iter, &sub))
405 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
406 DBusMessageIter iter, sub;
410 if (!(reply = dbus_message_new_method_return(message)))
413 dbus_message_iter_init_append(reply, &iter);
415 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
418 HASHMAP_FOREACH(j, m->jobs, i) {
419 char *u_path, *j_path;
420 const char *state, *type;
422 DBusMessageIter sub2;
424 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
427 id = (uint32_t) j->id;
428 state = job_state_to_string(j->state);
429 type = job_type_to_string(j->type);
431 if (!(j_path = job_dbus_path(j)))
434 if (!(u_path = unit_dbus_path(j->unit))) {
439 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
440 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
441 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
442 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
443 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
444 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
453 if (!dbus_message_iter_close_container(&sub, &sub2))
457 if (!dbus_message_iter_close_container(&iter, &sub))
460 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
464 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
465 if (!(s = set_new(string_hash_func, string_compare_func)))
468 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
474 if (!(client = strdup(dbus_message_get_sender(message))))
477 if ((r = set_put(s, client)) < 0) {
479 return bus_send_error_reply(m, connection, message, NULL, r);
482 if (!(reply = dbus_message_new_method_return(message)))
485 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
488 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) dbus_message_get_sender(message))))
489 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
493 if (!(reply = dbus_message_new_method_return(message)))
496 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
501 if (!(reply = dbus_message_new_method_return(message)))
504 if (!(f = open_memstream(&dump, &size)))
507 manager_dump_units(m, f, NULL);
508 manager_dump_jobs(m, f, NULL);
518 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
524 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
529 if (!dbus_message_get_args(
532 DBUS_TYPE_STRING, &name,
533 DBUS_TYPE_BOOLEAN, &cleanup,
535 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
537 if (name && name[0] == 0)
540 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
541 return bus_send_error_reply(m, connection, message, NULL, r);
543 if (!(reply = dbus_message_new_method_return(message)))
546 if (!(path = unit_dbus_path(UNIT(s))))
549 if (!dbus_message_append_args(
551 DBUS_TYPE_OBJECT_PATH, &path,
555 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
556 char *introspection = NULL;
564 if (!(reply = dbus_message_new_method_return(message)))
567 /* We roll our own introspection code here, instead of
568 * relying on bus_default_message_handler() because we
569 * need to generate our introspection string
572 if (!(f = open_memstream(&introspection, &size)))
575 fputs(INTROSPECTION_BEGIN, f);
577 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
583 if (!(p = bus_path_escape(k))) {
589 fprintf(f, "<node name=\"unit/%s\"/>", p);
593 HASHMAP_FOREACH(j, m->jobs, i)
594 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
596 fputs(INTROSPECTION_END, f);
609 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
616 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
618 assert(!m->queued_message);
620 /* Instead of sending the reply back right away, we
621 * just remember that we need to and then send it
622 * after the reload is finished. That way the caller
623 * knows when the reload finished. */
625 if (!(m->queued_message = dbus_message_new_method_return(message)))
628 m->exit_code = MANAGER_RELOAD;
630 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
632 if (!(reply = dbus_message_new_method_return(message)))
635 m->exit_code = MANAGER_REEXECUTE;
637 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
639 if (m->running_as == MANAGER_SYSTEM)
640 return bus_send_error_reply(m, connection, message, NULL, -ENOTSUP);
642 if (!(reply = dbus_message_new_method_return(message)))
645 m->exit_code = MANAGER_EXIT;
647 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
648 char **l = NULL, **e = NULL;
650 if ((r = bus_parse_strv(message, &l)) < 0) {
654 return bus_send_error_reply(m, connection, message, NULL, r);
657 e = strv_env_merge(2, m->environment, l);
663 if (!(reply = dbus_message_new_method_return(message))) {
668 strv_free(m->environment);
671 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
672 char **l = NULL, **e = NULL;
674 if ((r = bus_parse_strv(message, &l)) < 0) {
678 return bus_send_error_reply(m, connection, message, NULL, r);
681 e = strv_env_delete(m->environment, 1, l);
687 if (!(reply = dbus_message_new_method_return(message)))
690 strv_free(m->environment);
694 return bus_default_message_handler(m, connection, message, NULL, properties);
697 if (job_type != _JOB_TYPE_INVALID) {
698 const char *name, *smode;
703 if (!dbus_message_get_args(
706 DBUS_TYPE_STRING, &name,
707 DBUS_TYPE_STRING, &smode,
709 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
711 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
712 return bus_send_error_reply(m, connection, message, NULL, -EINVAL);
714 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
715 return bus_send_error_reply(m, connection, message, NULL, r);
717 if (job_type == JOB_START && u->meta.only_by_dependency)
718 return bus_send_error_reply(m, connection, message, NULL, -EPERM);
720 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
721 return bus_send_error_reply(m, connection, message, NULL, r);
723 if (!(j->bus_client = strdup(dbus_message_get_sender(message))))
728 if (!(reply = dbus_message_new_method_return(message)))
731 if (!(path = job_dbus_path(j)))
734 if (!dbus_message_append_args(
736 DBUS_TYPE_OBJECT_PATH, &path,
745 if (!dbus_connection_send(connection, reply, NULL))
748 dbus_message_unref(reply);
751 return DBUS_HANDLER_RESULT_HANDLED;
757 dbus_message_unref(reply);
759 dbus_error_free(&error);
761 return DBUS_HANDLER_RESULT_NEED_MEMORY;
764 const DBusObjectPathVTable bus_manager_vtable = {
765 .message_function = bus_manager_message_handler