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 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
250 if (!dbus_message_get_args(
253 DBUS_TYPE_STRING, &name,
255 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
257 if (!(u = manager_get_unit(m, name)))
258 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
260 if (!(reply = dbus_message_new_method_return(message)))
263 if (!(path = unit_dbus_path(u)))
266 if (!dbus_message_append_args(
268 DBUS_TYPE_OBJECT_PATH, &path,
272 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
276 if (!dbus_message_get_args(
279 DBUS_TYPE_STRING, &name,
281 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
283 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
284 return bus_send_error_reply(m, connection, message, NULL, r);
286 if (!(reply = dbus_message_new_method_return(message)))
289 if (!(path = unit_dbus_path(u)))
292 if (!dbus_message_append_args(
294 DBUS_TYPE_OBJECT_PATH, &path,
298 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
299 job_type = JOB_START;
300 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
302 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
303 job_type = JOB_RELOAD;
304 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
305 job_type = JOB_RESTART;
306 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
307 job_type = JOB_TRY_RESTART;
308 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
312 if (!dbus_message_get_args(
315 DBUS_TYPE_UINT32, &id,
317 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
319 if (!(j = manager_get_job(m, id)))
320 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
322 if (!(reply = dbus_message_new_method_return(message)))
325 if (!(path = job_dbus_path(j)))
328 if (!dbus_message_append_args(
330 DBUS_TYPE_OBJECT_PATH, &path,
334 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
336 manager_clear_jobs(m);
338 if (!(reply = dbus_message_new_method_return(message)))
341 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
342 DBusMessageIter iter, sub;
347 if (!(reply = dbus_message_new_method_return(message)))
350 dbus_message_iter_init_append(reply, &iter);
352 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
355 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
356 char *u_path, *j_path;
357 const char *description, *load_state, *active_state, *sub_state, *sjob_type;
358 DBusMessageIter sub2;
364 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
367 description = unit_description(u);
368 load_state = unit_load_state_to_string(u->meta.load_state);
369 active_state = unit_active_state_to_string(unit_active_state(u));
370 sub_state = unit_sub_state_to_string(u);
372 if (!(u_path = unit_dbus_path(u)))
376 job_id = (uint32_t) u->meta.job->id;
378 if (!(j_path = job_dbus_path(u->meta.job))) {
383 sjob_type = job_type_to_string(u->meta.job->type);
390 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
391 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
392 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
393 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
394 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
395 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
396 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
397 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
398 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
409 if (!dbus_message_iter_close_container(&sub, &sub2))
413 if (!dbus_message_iter_close_container(&iter, &sub))
416 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
417 DBusMessageIter iter, sub;
421 if (!(reply = dbus_message_new_method_return(message)))
424 dbus_message_iter_init_append(reply, &iter);
426 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
429 HASHMAP_FOREACH(j, m->jobs, i) {
430 char *u_path, *j_path;
431 const char *state, *type;
433 DBusMessageIter sub2;
435 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
438 id = (uint32_t) j->id;
439 state = job_state_to_string(j->state);
440 type = job_type_to_string(j->type);
442 if (!(j_path = job_dbus_path(j)))
445 if (!(u_path = unit_dbus_path(j->unit))) {
450 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
451 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
452 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
453 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
454 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
455 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
464 if (!dbus_message_iter_close_container(&sub, &sub2))
468 if (!dbus_message_iter_close_container(&iter, &sub))
471 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
475 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
476 if (!(s = set_new(string_hash_func, string_compare_func)))
479 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
485 if (!(client = strdup(message_get_sender_with_fallback(message))))
488 if ((r = set_put(s, client)) < 0) {
490 return bus_send_error_reply(m, connection, message, NULL, r);
493 if (!(reply = dbus_message_new_method_return(message)))
496 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
499 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message))))
500 return bus_send_error_reply(m, connection, message, NULL, -ENOENT);
504 if (!(reply = dbus_message_new_method_return(message)))
507 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
512 if (!(reply = dbus_message_new_method_return(message)))
515 if (!(f = open_memstream(&dump, &size)))
518 manager_dump_units(m, f, NULL);
519 manager_dump_jobs(m, f, NULL);
529 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
535 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
540 if (!dbus_message_get_args(
543 DBUS_TYPE_STRING, &name,
544 DBUS_TYPE_BOOLEAN, &cleanup,
546 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
548 if (name && name[0] == 0)
551 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
552 return bus_send_error_reply(m, connection, message, NULL, r);
554 if (!(reply = dbus_message_new_method_return(message)))
557 if (!(path = unit_dbus_path(UNIT(s))))
560 if (!dbus_message_append_args(
562 DBUS_TYPE_OBJECT_PATH, &path,
566 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
567 char *introspection = NULL;
575 if (!(reply = dbus_message_new_method_return(message)))
578 /* We roll our own introspection code here, instead of
579 * relying on bus_default_message_handler() because we
580 * need to generate our introspection string
583 if (!(f = open_memstream(&introspection, &size)))
586 fputs(INTROSPECTION_BEGIN, f);
588 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
594 if (!(p = bus_path_escape(k))) {
600 fprintf(f, "<node name=\"unit/%s\"/>", p);
604 HASHMAP_FOREACH(j, m->jobs, i)
605 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
607 fputs(INTROSPECTION_END, f);
620 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
627 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
629 assert(!m->queued_message);
631 /* Instead of sending the reply back right away, we
632 * just remember that we need to and then send it
633 * after the reload is finished. That way the caller
634 * knows when the reload finished. */
636 if (!(m->queued_message = dbus_message_new_method_return(message)))
639 m->queued_message_connection = connection;
640 m->exit_code = MANAGER_RELOAD;
642 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
644 if (!(reply = dbus_message_new_method_return(message)))
647 m->exit_code = MANAGER_REEXECUTE;
649 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
651 if (m->running_as == MANAGER_SYSTEM)
652 return bus_send_error_reply(m, connection, message, NULL, -ENOTSUP);
654 if (!(reply = dbus_message_new_method_return(message)))
657 m->exit_code = MANAGER_EXIT;
659 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
660 char **l = NULL, **e = NULL;
662 if ((r = bus_parse_strv(message, &l)) < 0) {
666 return bus_send_error_reply(m, connection, message, NULL, r);
669 e = strv_env_merge(2, m->environment, l);
675 if (!(reply = dbus_message_new_method_return(message))) {
680 strv_free(m->environment);
683 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
684 char **l = NULL, **e = NULL;
686 if ((r = bus_parse_strv(message, &l)) < 0) {
690 return bus_send_error_reply(m, connection, message, NULL, r);
693 e = strv_env_delete(m->environment, 1, l);
699 if (!(reply = dbus_message_new_method_return(message)))
702 strv_free(m->environment);
706 return bus_default_message_handler(m, connection, message, NULL, properties);
709 if (job_type != _JOB_TYPE_INVALID) {
710 const char *name, *smode;
715 if (!dbus_message_get_args(
718 DBUS_TYPE_STRING, &name,
719 DBUS_TYPE_STRING, &smode,
721 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
723 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID)
724 return bus_send_error_reply(m, connection, message, NULL, -EINVAL);
726 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
727 return bus_send_error_reply(m, connection, message, NULL, r);
729 if (job_type == JOB_START && u->meta.only_by_dependency)
730 return bus_send_error_reply(m, connection, message, NULL, -EPERM);
732 if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0)
733 return bus_send_error_reply(m, connection, message, NULL, r);
735 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
740 if (!(reply = dbus_message_new_method_return(message)))
743 if (!(path = job_dbus_path(j)))
746 if (!dbus_message_append_args(
748 DBUS_TYPE_OBJECT_PATH, &path,
756 if (!dbus_connection_send(connection, reply, NULL))
759 dbus_message_unref(reply);
762 return DBUS_HANDLER_RESULT_HANDLED;
768 dbus_message_unref(reply);
770 dbus_error_free(&error);
772 return DBUS_HANDLER_RESULT_NEED_MEMORY;
775 const DBusObjectPathVTable bus_manager_vtable = {
776 .message_function = bus_manager_message_handler