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=\"GetJob\">\n" \
40 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
41 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
43 " <method name=\"ClearJobs\"/>\n" \
44 " <method name=\"ListUnits\">\n" \
45 " <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>\n" \
47 " <method name=\"ListJobs\">\n" \
48 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
50 " <method name=\"Subscribe\"/>\n" \
51 " <method name=\"Unsubscribe\"/>\n" \
52 " <method name=\"Dump\"/>\n" \
53 " <method name=\"CreateSnapshot\">\n" \
54 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
56 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
58 " <method name=\"Reload\"/>\n" \
59 " <method name=\"Reexecute\"/>\n" \
60 " <method name=\"Exit\"/>\n" \
61 " <method name=\"SetEnvironment\">\n" \
62 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
64 " <method name=\"UnsetEnvironment\">\n" \
65 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
67 " <signal name=\"UnitNew\">\n" \
68 " <arg name=\"id\" type=\"s\"/>\n" \
69 " <arg name=\"unit\" type=\"o\"/>\n" \
71 " <signal name=\"UnitRemoved\">\n" \
72 " <arg name=\"id\" type=\"s\"/>\n" \
73 " <arg name=\"unit\" type=\"o\"/>\n" \
75 " <signal name=\"JobNew\">\n" \
76 " <arg name=\"id\" type=\"u\"/>\n" \
77 " <arg name=\"job\" type=\"o\"/>\n" \
79 " <signal name=\"JobRemoved\">\n" \
80 " <arg name=\"id\" type=\"u\"/>\n" \
81 " <arg name=\"job\" type=\"o\"/>\n" \
82 " <arg name=\"success\" type=\"b\"/>\n" \
84 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
85 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
86 " <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>\n" \
87 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
88 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
89 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
90 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
91 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
94 #define INTROSPECTION_BEGIN \
95 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
97 BUS_MANAGER_INTERFACE \
98 BUS_PROPERTIES_INTERFACE \
99 BUS_INTROSPECTABLE_INTERFACE
101 #define INTROSPECTION_END \
104 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
106 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
108 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
115 t = log_target_to_string(log_get_target());
117 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
123 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
130 t = log_level_to_string(log_get_max_level());
132 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
138 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
145 u = hashmap_size(m->units);
147 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
153 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
160 u = hashmap_size(m->jobs);
162 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
168 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
171 const BusProperty properties[] = {
172 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
173 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
174 { "org.freedesktop.systemd1.Manager", "BootTimestamp", bus_property_append_uint64, "t", &m->boot_timestamp },
175 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
176 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
177 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
178 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
179 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
180 { NULL, NULL, NULL, NULL, NULL }
185 DBusMessage *reply = NULL;
192 dbus_error_init(&error);
194 log_debug("Got D-Bus request: %s.%s() on %s",
195 dbus_message_get_interface(message),
196 dbus_message_get_member(message),
197 dbus_message_get_path(message));
199 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
203 if (!dbus_message_get_args(
206 DBUS_TYPE_STRING, &name,
208 return bus_send_error_reply(m, message, &error, -EINVAL);
210 if (!(u = manager_get_unit(m, name)))
211 return bus_send_error_reply(m, message, NULL, -ENOENT);
213 if (!(reply = dbus_message_new_method_return(message)))
216 if (!(path = unit_dbus_path(u)))
219 if (!dbus_message_append_args(
221 DBUS_TYPE_OBJECT_PATH, &path,
225 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
229 if (!dbus_message_get_args(
232 DBUS_TYPE_STRING, &name,
234 return bus_send_error_reply(m, message, &error, -EINVAL);
236 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
237 return bus_send_error_reply(m, message, NULL, r);
239 if (!(reply = dbus_message_new_method_return(message)))
242 if (!(path = unit_dbus_path(u)))
245 if (!dbus_message_append_args(
247 DBUS_TYPE_OBJECT_PATH, &path,
251 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
255 if (!dbus_message_get_args(
258 DBUS_TYPE_UINT32, &id,
260 return bus_send_error_reply(m, message, &error, -EINVAL);
262 if (!(j = manager_get_job(m, id)))
263 return bus_send_error_reply(m, message, NULL, -ENOENT);
265 if (!(reply = dbus_message_new_method_return(message)))
268 if (!(path = job_dbus_path(j)))
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", "ClearJobs")) {
279 manager_clear_jobs(m);
281 if (!(reply = dbus_message_new_method_return(message)))
284 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
285 DBusMessageIter iter, sub;
290 if (!(reply = dbus_message_new_method_return(message)))
293 dbus_message_iter_init_append(reply, &iter);
295 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
298 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
299 char *u_path, *j_path;
300 const char *description, *load_state, *active_state, *sub_state, *job_type;
301 DBusMessageIter sub2;
307 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
310 description = unit_description(u);
311 load_state = unit_load_state_to_string(u->meta.load_state);
312 active_state = unit_active_state_to_string(unit_active_state(u));
313 sub_state = unit_sub_state_to_string(u);
315 if (!(u_path = unit_dbus_path(u)))
319 job_id = (uint32_t) u->meta.job->id;
321 if (!(j_path = job_dbus_path(u->meta.job))) {
326 job_type = job_type_to_string(u->meta.job->type);
333 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
334 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
335 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
336 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
337 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
338 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
339 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
340 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &job_type) ||
341 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
352 if (!dbus_message_iter_close_container(&sub, &sub2))
356 if (!dbus_message_iter_close_container(&iter, &sub))
359 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
360 DBusMessageIter iter, sub;
364 if (!(reply = dbus_message_new_method_return(message)))
367 dbus_message_iter_init_append(reply, &iter);
369 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
372 HASHMAP_FOREACH(j, m->jobs, i) {
373 char *u_path, *j_path;
374 const char *state, *type;
376 DBusMessageIter sub2;
378 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
381 id = (uint32_t) j->id;
382 state = job_state_to_string(j->state);
383 type = job_type_to_string(j->type);
385 if (!(j_path = job_dbus_path(j)))
388 if (!(u_path = unit_dbus_path(j->unit))) {
393 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
394 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
395 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
396 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
397 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
398 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
407 if (!dbus_message_iter_close_container(&sub, &sub2))
411 if (!dbus_message_iter_close_container(&iter, &sub))
414 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
417 if (!(client = strdup(dbus_message_get_sender(message))))
420 r = set_put(m->subscribed, client);
423 return bus_send_error_reply(m, message, NULL, r);
425 if (!(reply = dbus_message_new_method_return(message)))
428 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
431 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
432 return bus_send_error_reply(m, message, NULL, -ENOENT);
436 if (!(reply = dbus_message_new_method_return(message)))
439 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
444 if (!(reply = dbus_message_new_method_return(message)))
447 if (!(f = open_memstream(&dump, &size)))
450 manager_dump_units(m, f, NULL);
451 manager_dump_jobs(m, f, NULL);
461 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
467 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
472 if (!dbus_message_get_args(
475 DBUS_TYPE_STRING, &name,
476 DBUS_TYPE_BOOLEAN, &cleanup,
478 return bus_send_error_reply(m, message, &error, -EINVAL);
480 if (name && name[0] == 0)
483 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
484 return bus_send_error_reply(m, message, NULL, r);
486 if (!(reply = dbus_message_new_method_return(message)))
489 if (!(path = unit_dbus_path(UNIT(s))))
492 if (!dbus_message_append_args(
494 DBUS_TYPE_OBJECT_PATH, &path,
498 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
499 char *introspection = NULL;
507 if (!(reply = dbus_message_new_method_return(message)))
510 /* We roll our own introspection code here, instead of
511 * relying on bus_default_message_handler() because we
512 * need to generate our introspection string
515 if (!(f = open_memstream(&introspection, &size)))
518 fputs(INTROSPECTION_BEGIN, f);
520 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
526 if (!(p = bus_path_escape(k))) {
532 fprintf(f, "<node name=\"unit/%s\"/>", p);
536 HASHMAP_FOREACH(j, m->jobs, i)
537 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
539 fputs(INTROSPECTION_END, f);
552 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
559 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
561 assert(!m->queued_message);
563 /* Instead of sending the reply back right away, we
564 * just remember that we need to and then send it
565 * after the reload is finished. That way the caller
566 * knows when the reload finished. */
568 if (!(m->queued_message = dbus_message_new_method_return(message)))
571 m->exit_code = MANAGER_RELOAD;
573 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
575 if (!(reply = dbus_message_new_method_return(message)))
578 m->exit_code = MANAGER_REEXECUTE;
580 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
582 if (m->running_as == MANAGER_INIT)
583 return bus_send_error_reply(m, message, NULL, -ENOTSUP);
585 if (!(reply = dbus_message_new_method_return(message)))
588 m->exit_code = MANAGER_EXIT;
590 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
591 char **l = NULL, **e = NULL;
593 if ((r = bus_parse_strv(message, &l)) < 0) {
597 return bus_send_error_reply(m, message, NULL, r);
600 e = strv_env_merge(m->environment, l, NULL);
606 if (!(reply = dbus_message_new_method_return(message))) {
611 strv_free(m->environment);
614 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
615 char **l = NULL, **e = NULL;
617 if ((r = bus_parse_strv(message, &l)) < 0) {
621 return bus_send_error_reply(m, message, NULL, r);
624 e = strv_env_delete(m->environment, l, NULL);
630 if (!(reply = dbus_message_new_method_return(message)))
633 strv_free(m->environment);
637 return bus_default_message_handler(m, message, NULL, properties);
642 if (!dbus_connection_send(connection, reply, NULL))
645 dbus_message_unref(reply);
648 return DBUS_HANDLER_RESULT_HANDLED;
654 dbus_message_unref(reply);
656 dbus_error_free(&error);
658 return DBUS_HANDLER_RESULT_NEED_MEMORY;
661 const DBusObjectPathVTable bus_manager_vtable = {
662 .message_function = bus_manager_message_handler