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 INTROSPECTION_BEGIN \
30 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
32 " <interface name=\"org.freedesktop.systemd1.Manager\">" \
33 " <method name=\"GetUnit\">" \
34 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
35 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
37 " <method name=\"LoadUnit\">" \
38 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
39 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
41 " <method name=\"GetJob\">" \
42 " <arg name=\"id\" type=\"u\" direction=\"in\"/>" \
43 " <arg name=\"job\" type=\"o\" direction=\"out\"/>" \
45 " <method name=\"ClearJobs\"/>" \
46 " <method name=\"ListUnits\">" \
47 " <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>" \
49 " <method name=\"ListJobs\">" \
50 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>" \
52 " <method name=\"Subscribe\"/>" \
53 " <method name=\"Unsubscribe\"/>" \
54 " <method name=\"Dump\"/>" \
55 " <method name=\"CreateSnapshot\">" \
56 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
57 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>" \
58 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
60 " <method name=\"Reload\"/>" \
61 " <method name=\"Reexecute\"/>" \
62 " <method name=\"Exit\"/>" \
63 " <method name=\"SetEnvironment\">" \
64 " <arg name=\"names\" type=\"as\" direction=\"in\"/>" \
66 " <method name=\"UnsetEnvironment\">" \
67 " <arg name=\"names\" type=\"as\" direction=\"in\"/>" \
69 " <signal name=\"UnitNew\">" \
70 " <arg name=\"id\" type=\"s\"/>" \
71 " <arg name=\"unit\" type=\"o\"/>" \
73 " <signal name=\"UnitRemoved\">" \
74 " <arg name=\"id\" type=\"s\"/>" \
75 " <arg name=\"unit\" type=\"o\"/>" \
77 " <signal name=\"JobNew\">" \
78 " <arg name=\"id\" type=\"u\"/>" \
79 " <arg name=\"job\" type=\"o\"/>" \
81 " <signal name=\"JobRemoved\">" \
82 " <arg name=\"id\" type=\"u\"/>" \
83 " <arg name=\"job\" type=\"o\"/>" \
85 " <property name=\"Version\" type=\"s\" access=\"read\"/>" \
86 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>" \
87 " <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>" \
88 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>" \
89 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>" \
90 " <property name=\"NNames\" type=\"u\" access=\"read\"/>" \
91 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>" \
92 " <property name=\"Environment\" type=\"as\" access=\"read\"/>" \
94 BUS_PROPERTIES_INTERFACE \
95 BUS_INTROSPECTABLE_INTERFACE
97 #define INTROSPECTION_END \
100 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
102 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
109 t = log_target_to_string(log_get_target());
111 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
117 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
124 t = log_level_to_string(log_get_max_level());
126 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
132 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
139 u = hashmap_size(m->units);
141 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
147 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
154 u = hashmap_size(m->jobs);
156 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
162 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
165 const BusProperty properties[] = {
166 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
167 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
168 { "org.freedesktop.systemd1.Manager", "BootTimestamp", bus_property_append_uint64, "t", &m->boot_timestamp },
169 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
170 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
171 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
172 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
173 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
174 { NULL, NULL, NULL, NULL, NULL }
179 DBusMessage *reply = NULL;
186 dbus_error_init(&error);
188 log_debug("Got D-Bus request: %s.%s() on %s",
189 dbus_message_get_interface(message),
190 dbus_message_get_member(message),
191 dbus_message_get_path(message));
193 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
197 if (!dbus_message_get_args(
200 DBUS_TYPE_STRING, &name,
202 return bus_send_error_reply(m, message, &error, -EINVAL);
204 if (!(u = manager_get_unit(m, name)))
205 return bus_send_error_reply(m, message, NULL, -ENOENT);
207 if (!(reply = dbus_message_new_method_return(message)))
210 if (!(path = unit_dbus_path(u)))
213 if (!dbus_message_append_args(
215 DBUS_TYPE_OBJECT_PATH, &path,
219 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
223 if (!dbus_message_get_args(
226 DBUS_TYPE_STRING, &name,
228 return bus_send_error_reply(m, message, &error, -EINVAL);
230 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
231 return bus_send_error_reply(m, message, NULL, r);
233 if (!(reply = dbus_message_new_method_return(message)))
236 if (!(path = unit_dbus_path(u)))
239 if (!dbus_message_append_args(
241 DBUS_TYPE_OBJECT_PATH, &path,
245 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
249 if (!dbus_message_get_args(
252 DBUS_TYPE_UINT32, &id,
254 return bus_send_error_reply(m, message, &error, -EINVAL);
256 if (!(j = manager_get_job(m, id)))
257 return bus_send_error_reply(m, message, NULL, -ENOENT);
259 if (!(reply = dbus_message_new_method_return(message)))
262 if (!(path = job_dbus_path(j)))
265 if (!dbus_message_append_args(
267 DBUS_TYPE_OBJECT_PATH, &path,
271 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
273 manager_clear_jobs(m);
275 if (!(reply = dbus_message_new_method_return(message)))
278 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
279 DBusMessageIter iter, sub;
284 if (!(reply = dbus_message_new_method_return(message)))
287 dbus_message_iter_init_append(reply, &iter);
289 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
292 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
293 char *u_path, *j_path;
294 const char *description, *load_state, *active_state, *sub_state, *job_type;
295 DBusMessageIter sub2;
301 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
304 description = unit_description(u);
305 load_state = unit_load_state_to_string(u->meta.load_state);
306 active_state = unit_active_state_to_string(unit_active_state(u));
307 sub_state = unit_sub_state_to_string(u);
309 if (!(u_path = unit_dbus_path(u)))
313 job_id = (uint32_t) u->meta.job->id;
315 if (!(j_path = job_dbus_path(u->meta.job))) {
320 job_type = job_type_to_string(u->meta.job->type);
327 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
328 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
329 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
330 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
331 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
332 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
333 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
334 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &job_type) ||
335 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
346 if (!dbus_message_iter_close_container(&sub, &sub2))
350 if (!dbus_message_iter_close_container(&iter, &sub))
353 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
354 DBusMessageIter iter, sub;
358 if (!(reply = dbus_message_new_method_return(message)))
361 dbus_message_iter_init_append(reply, &iter);
363 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
366 HASHMAP_FOREACH(j, m->jobs, i) {
367 char *u_path, *j_path;
368 const char *state, *type;
370 DBusMessageIter sub2;
372 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
375 id = (uint32_t) j->id;
376 state = job_state_to_string(j->state);
377 type = job_type_to_string(j->type);
379 if (!(j_path = job_dbus_path(j)))
382 if (!(u_path = unit_dbus_path(j->unit))) {
387 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
388 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
389 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
390 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
391 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
392 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
401 if (!dbus_message_iter_close_container(&sub, &sub2))
405 if (!dbus_message_iter_close_container(&iter, &sub))
408 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
411 if (!(client = strdup(dbus_message_get_sender(message))))
414 r = set_put(m->subscribed, client);
417 return bus_send_error_reply(m, message, NULL, r);
419 if (!(reply = dbus_message_new_method_return(message)))
422 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
425 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
426 return bus_send_error_reply(m, message, NULL, -ENOENT);
430 if (!(reply = dbus_message_new_method_return(message)))
433 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
438 if (!(reply = dbus_message_new_method_return(message)))
441 if (!(f = open_memstream(&dump, &size)))
444 manager_dump_units(m, f, NULL);
445 manager_dump_jobs(m, f, NULL);
455 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
461 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
466 if (!dbus_message_get_args(
469 DBUS_TYPE_STRING, &name,
470 DBUS_TYPE_BOOLEAN, &cleanup,
472 return bus_send_error_reply(m, message, &error, -EINVAL);
474 if (name && name[0] == 0)
477 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
478 return bus_send_error_reply(m, message, NULL, r);
480 if (!(reply = dbus_message_new_method_return(message)))
483 if (!(path = unit_dbus_path(UNIT(s))))
486 if (!dbus_message_append_args(
488 DBUS_TYPE_OBJECT_PATH, &path,
492 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
493 char *introspection = NULL;
501 if (!(reply = dbus_message_new_method_return(message)))
504 /* We roll our own introspection code here, instead of
505 * relying on bus_default_message_handler() because we
506 * need to generate our introspection string
509 if (!(f = open_memstream(&introspection, &size)))
512 fputs(INTROSPECTION_BEGIN, f);
514 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
520 if (!(p = bus_path_escape(k))) {
526 fprintf(f, "<node name=\"unit/%s\"/>", p);
530 HASHMAP_FOREACH(j, m->jobs, i)
531 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
533 fputs(INTROSPECTION_END, f);
546 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
553 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
555 assert(!m->queued_message);
557 /* Instead of sending the reply back right away, we
558 * just remember that we need to and then send it
559 * after the reload is finished. That way the caller
560 * knows when the reload finished. */
562 if (!(m->queued_message = dbus_message_new_method_return(message)))
565 m->exit_code = MANAGER_RELOAD;
567 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
569 if (!(reply = dbus_message_new_method_return(message)))
572 m->exit_code = MANAGER_REEXECUTE;
574 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
576 if (m->running_as == MANAGER_INIT)
577 return bus_send_error_reply(m, message, NULL, -ENOTSUP);
579 if (!(reply = dbus_message_new_method_return(message)))
582 m->exit_code = MANAGER_EXIT;
584 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
585 char **l = NULL, **e = NULL;
587 if ((r = bus_parse_strv(message, &l)) < 0) {
591 return bus_send_error_reply(m, message, NULL, r);
594 e = strv_env_merge(m->environment, l, NULL);
600 if (!(reply = dbus_message_new_method_return(message))) {
605 strv_free(m->environment);
608 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
609 char **l = NULL, **e = NULL;
611 if ((r = bus_parse_strv(message, &l)) < 0) {
615 return bus_send_error_reply(m, message, NULL, r);
618 e = strv_env_delete(m->environment, l, NULL);
624 if (!(reply = dbus_message_new_method_return(message)))
627 strv_free(m->environment);
631 return bus_default_message_handler(m, message, NULL, properties);
636 if (!dbus_connection_send(connection, reply, NULL))
639 dbus_message_unref(reply);
642 return DBUS_HANDLER_RESULT_HANDLED;
648 dbus_message_unref(reply);
650 dbus_error_free(&error);
652 return DBUS_HANDLER_RESULT_NEED_MEMORY;
655 const DBusObjectPathVTable bus_manager_vtable = {
656 .message_function = bus_manager_message_handler