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=\"GetJob\">\n" \
60 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
61 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
63 " <method name=\"ClearJobs\"/>\n" \
64 " <method name=\"ListUnits\">\n" \
65 " <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>\n" \
67 " <method name=\"ListJobs\">\n" \
68 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
70 " <method name=\"Subscribe\"/>\n" \
71 " <method name=\"Unsubscribe\"/>\n" \
72 " <method name=\"Dump\"/>\n" \
73 " <method name=\"CreateSnapshot\">\n" \
74 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
75 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
76 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
78 " <method name=\"Reload\"/>\n" \
79 " <method name=\"Reexecute\"/>\n" \
80 " <method name=\"Exit\"/>\n" \
81 " <method name=\"SetEnvironment\">\n" \
82 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
84 " <method name=\"UnsetEnvironment\">\n" \
85 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
87 " <signal name=\"UnitNew\">\n" \
88 " <arg name=\"id\" type=\"s\"/>\n" \
89 " <arg name=\"unit\" type=\"o\"/>\n" \
91 " <signal name=\"UnitRemoved\">\n" \
92 " <arg name=\"id\" type=\"s\"/>\n" \
93 " <arg name=\"unit\" type=\"o\"/>\n" \
95 " <signal name=\"JobNew\">\n" \
96 " <arg name=\"id\" type=\"u\"/>\n" \
97 " <arg name=\"job\" type=\"o\"/>\n" \
99 " <signal name=\"JobRemoved\">\n" \
100 " <arg name=\"id\" type=\"u\"/>\n" \
101 " <arg name=\"job\" type=\"o\"/>\n" \
102 " <arg name=\"success\" type=\"b\"/>\n" \
104 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
105 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
106 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
107 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
108 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
109 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
110 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
111 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
112 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
113 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
114 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
115 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \
116 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
119 #define INTROSPECTION_BEGIN \
120 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
122 BUS_MANAGER_INTERFACE \
123 BUS_PROPERTIES_INTERFACE \
124 BUS_INTROSPECTABLE_INTERFACE
126 #define INTROSPECTION_END \
129 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
131 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
133 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
140 t = log_target_to_string(log_get_target());
142 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
148 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
155 t = log_level_to_string(log_get_max_level());
157 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
163 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
170 u = hashmap_size(m->units);
172 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
178 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
185 u = hashmap_size(m->jobs);
187 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
193 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
196 const BusProperty properties[] = {
197 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
198 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
199 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
200 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
201 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
202 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
203 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
204 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
205 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
206 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
207 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
208 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
209 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
210 { NULL, NULL, NULL, NULL, NULL }
215 DBusMessage *reply = NULL;
217 JobType job_type = _JOB_TYPE_INVALID;
223 dbus_error_init(&error);
225 log_debug("Got D-Bus request: %s.%s() on %s",
226 dbus_message_get_interface(message),
227 dbus_message_get_member(message),
228 dbus_message_get_path(message));
230 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
234 if (!dbus_message_get_args(
237 DBUS_TYPE_STRING, &name,
239 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
241 if (!(u = manager_get_unit(m, name)))
242 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
244 if (!(reply = dbus_message_new_method_return(message)))
247 if (!(path = unit_dbus_path(u)))
250 if (!dbus_message_append_args(
252 DBUS_TYPE_OBJECT_PATH, &path,
256 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
260 if (!dbus_message_get_args(
263 DBUS_TYPE_STRING, &name,
265 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
267 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
268 return bus_send_error_reply(m, connection, message, NULL, r);
270 if (!(reply = dbus_message_new_method_return(message)))
273 if (!(path = unit_dbus_path(u)))
276 if (!dbus_message_append_args(
278 DBUS_TYPE_OBJECT_PATH, &path,
282 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
283 job_type = JOB_START;
284 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
286 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
287 job_type = JOB_RELOAD;
288 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
289 job_type = JOB_RESTART;
290 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
294 if (!dbus_message_get_args(
297 DBUS_TYPE_UINT32, &id,
299 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
301 if (!(j = manager_get_job(m, id)))
302 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
304 if (!(reply = dbus_message_new_method_return(message)))
307 if (!(path = job_dbus_path(j)))
310 if (!dbus_message_append_args(
312 DBUS_TYPE_OBJECT_PATH, &path,
316 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
318 manager_clear_jobs(m);
320 if (!(reply = dbus_message_new_method_return(message)))
323 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
324 DBusMessageIter iter, sub;
329 if (!(reply = dbus_message_new_method_return(message)))
332 dbus_message_iter_init_append(reply, &iter);
334 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
337 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
338 char *u_path, *j_path;
339 const char *description, *load_state, *active_state, *sub_state, *sjob_type;
340 DBusMessageIter sub2;
346 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
349 description = unit_description(u);
350 load_state = unit_load_state_to_string(u->meta.load_state);
351 active_state = unit_active_state_to_string(unit_active_state(u));
352 sub_state = unit_sub_state_to_string(u);
354 if (!(u_path = unit_dbus_path(u)))
358 job_id = (uint32_t) u->meta.job->id;
360 if (!(j_path = job_dbus_path(u->meta.job))) {
365 sjob_type = job_type_to_string(u->meta.job->type);
372 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
373 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
374 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
375 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
376 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
377 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
378 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
379 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
380 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
391 if (!dbus_message_iter_close_container(&sub, &sub2))
395 if (!dbus_message_iter_close_container(&iter, &sub))
398 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
399 DBusMessageIter iter, sub;
403 if (!(reply = dbus_message_new_method_return(message)))
406 dbus_message_iter_init_append(reply, &iter);
408 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
411 HASHMAP_FOREACH(j, m->jobs, i) {
412 char *u_path, *j_path;
413 const char *state, *type;
415 DBusMessageIter sub2;
417 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
420 id = (uint32_t) j->id;
421 state = job_state_to_string(j->state);
422 type = job_type_to_string(j->type);
424 if (!(j_path = job_dbus_path(j)))
427 if (!(u_path = unit_dbus_path(j->unit))) {
432 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
433 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
434 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
435 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
436 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
437 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
446 if (!dbus_message_iter_close_container(&sub, &sub2))
450 if (!dbus_message_iter_close_container(&iter, &sub))
453 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
456 if (!(client = strdup(dbus_message_get_sender(message))))
459 r = set_put(m->subscribed, client);
462 return bus_send_error_reply(m, connection, message, NULL, r);
464 if (!(reply = dbus_message_new_method_return(message)))
467 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
470 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
471 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
475 if (!(reply = dbus_message_new_method_return(message)))
478 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
483 if (!(reply = dbus_message_new_method_return(message)))
486 if (!(f = open_memstream(&dump, &size)))
489 manager_dump_units(m, f, NULL);
490 manager_dump_jobs(m, f, NULL);
500 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
506 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
511 if (!dbus_message_get_args(
514 DBUS_TYPE_STRING, &name,
515 DBUS_TYPE_BOOLEAN, &cleanup,
517 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
519 if (name && name[0] == 0)
522 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
523 return bus_send_error_reply(m, connection, message, NULL, r);
525 if (!(reply = dbus_message_new_method_return(message)))
528 if (!(path = unit_dbus_path(UNIT(s))))
531 if (!dbus_message_append_args(
533 DBUS_TYPE_OBJECT_PATH, &path,
537 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
538 char *introspection = NULL;
546 if (!(reply = dbus_message_new_method_return(message)))
549 /* We roll our own introspection code here, instead of
550 * relying on bus_default_message_handler() because we
551 * need to generate our introspection string
554 if (!(f = open_memstream(&introspection, &size)))
557 fputs(INTROSPECTION_BEGIN, f);
559 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
565 if (!(p = bus_path_escape(k))) {
571 fprintf(f, "<node name=\"unit/%s\"/>", p);
575 HASHMAP_FOREACH(j, m->jobs, i)
576 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
578 fputs(INTROSPECTION_END, f);
591 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
598 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
600 assert(!m->queued_message);
602 /* Instead of sending the reply back right away, we
603 * just remember that we need to and then send it
604 * after the reload is finished. That way the caller
605 * knows when the reload finished. */
607 if (!(m->queued_message = dbus_message_new_method_return(message)))
610 m->exit_code = MANAGER_RELOAD;
612 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
614 if (!(reply = dbus_message_new_method_return(message)))
617 m->exit_code = MANAGER_REEXECUTE;
619 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
621 if (m->running_as == MANAGER_SYSTEM)
622 return bus_send_error_reply(m, connection, message, NULL, -ENOTSUP);
624 if (!(reply = dbus_message_new_method_return(message)))
627 m->exit_code = MANAGER_EXIT;
629 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
630 char **l = NULL, **e = NULL;
632 if ((r = bus_parse_strv(message, &l)) < 0) {
636 return bus_send_error_reply(m, connection, message, NULL, r);
639 e = strv_env_merge(2, m->environment, l);
645 if (!(reply = dbus_message_new_method_return(message))) {
650 strv_free(m->environment);
653 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
654 char **l = NULL, **e = NULL;
656 if ((r = bus_parse_strv(message, &l)) < 0) {
660 return bus_send_error_reply(m, connection, message, NULL, r);
663 e = strv_env_delete(m->environment, 1, l);
669 if (!(reply = dbus_message_new_method_return(message)))
672 strv_free(m->environment);
676 return bus_default_message_handler(m, connection, message, NULL, properties);
679 if (job_type != _JOB_TYPE_INVALID) {
680 const char *name, *smode;
685 if (!dbus_message_get_args(
688 DBUS_TYPE_STRING, &name,
689 DBUS_TYPE_STRING, &smode,
691 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
693 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
694 return bus_send_error_reply(m, connection, message, NULL, -EINVAL);
696 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
697 return bus_send_error_reply(m, connection, message, NULL, r);
699 if (job_type == JOB_START && u->meta.only_by_dependency)
700 return bus_send_error_reply(m, connection, message, NULL, -EPERM);
702 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
703 return bus_send_error_reply(m, connection, message, NULL, r);
705 if (!(reply = dbus_message_new_method_return(message)))
708 if (!(path = job_dbus_path(j)))
711 if (!dbus_message_append_args(
713 DBUS_TYPE_OBJECT_PATH, &path,
721 if (!dbus_connection_send(connection, reply, NULL))
724 dbus_message_unref(reply);
727 return DBUS_HANDLER_RESULT_HANDLED;
733 dbus_message_unref(reply);
735 dbus_error_free(&error);
737 return DBUS_HANDLER_RESULT_NEED_MEMORY;
740 const DBusObjectPathVTable bus_manager_vtable = {
741 .message_function = bus_manager_message_handler