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"
28 #define INTROSPECTION_BEGIN \
29 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
31 " <interface name=\"org.freedesktop.systemd1.Manager\">" \
32 " <method name=\"GetUnit\">" \
33 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
34 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
36 " <method name=\"LoadUnit\">" \
37 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
40 " <method name=\"GetJob\">" \
41 " <arg name=\"id\" type=\"u\" direction=\"in\"/>" \
42 " <arg name=\"job\" type=\"o\" direction=\"out\"/>" \
44 " <method name=\"ClearJobs\"/>" \
45 " <method name=\"ListUnits\">" \
46 " <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>" \
48 " <method name=\"ListJobs\">" \
49 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>" \
51 " <method name=\"Subscribe\"/>" \
52 " <method name=\"Unsubscribe\"/>" \
53 " <method name=\"Dump\"/>" \
54 " <method name=\"CreateSnapshot\">" \
55 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
56 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>" \
57 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
59 " <method name=\"Reload\"/>" \
60 " <method name=\"Reexecute\"/>" \
61 " <method name=\"Exit\"/>" \
62 " <signal name=\"UnitNew\">" \
63 " <arg name=\"id\" type=\"s\"/>" \
64 " <arg name=\"unit\" type=\"o\"/>" \
66 " <signal name=\"UnitRemoved\">" \
67 " <arg name=\"id\" type=\"s\"/>" \
68 " <arg name=\"unit\" type=\"o\"/>" \
70 " <signal name=\"JobNew\">" \
71 " <arg name=\"id\" type=\"u\"/>" \
72 " <arg name=\"job\" type=\"o\"/>" \
74 " <signal name=\"JobRemoved\">" \
75 " <arg name=\"id\" type=\"u\"/>" \
76 " <arg name=\"job\" type=\"o\"/>" \
78 " <property name=\"Version\" type=\"s\" access=\"read\"/>" \
79 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>" \
80 " <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>" \
81 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>" \
82 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>" \
84 BUS_PROPERTIES_INTERFACE \
85 BUS_INTROSPECTABLE_INTERFACE
87 #define INTROSPECTION_END \
90 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
92 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
99 t = log_target_to_string(log_get_target());
101 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
107 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
114 t = log_level_to_string(log_get_max_level());
116 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
122 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
125 const BusProperty properties[] = {
126 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
127 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
128 { "org.freedesktop.systemd1.Manager", "BootTimestamp", bus_property_append_uint64, "t", &m->boot_timestamp },
129 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
130 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
131 { NULL, NULL, NULL, NULL, NULL }
136 DBusMessage *reply = NULL;
143 dbus_error_init(&error);
145 log_debug("Got D-Bus request: %s.%s() on %s",
146 dbus_message_get_interface(message),
147 dbus_message_get_member(message),
148 dbus_message_get_path(message));
150 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
154 if (!dbus_message_get_args(
157 DBUS_TYPE_STRING, &name,
159 return bus_send_error_reply(m, message, &error, -EINVAL);
161 if (!(u = manager_get_unit(m, name)))
162 return bus_send_error_reply(m, message, NULL, -ENOENT);
164 if (!(reply = dbus_message_new_method_return(message)))
167 if (!(path = unit_dbus_path(u)))
170 if (!dbus_message_append_args(
172 DBUS_TYPE_OBJECT_PATH, &path,
176 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
180 if (!dbus_message_get_args(
183 DBUS_TYPE_STRING, &name,
185 return bus_send_error_reply(m, message, &error, -EINVAL);
187 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
188 return bus_send_error_reply(m, message, NULL, r);
190 if (!(reply = dbus_message_new_method_return(message)))
193 if (!(path = unit_dbus_path(u)))
196 if (!dbus_message_append_args(
198 DBUS_TYPE_OBJECT_PATH, &path,
202 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
206 if (!dbus_message_get_args(
209 DBUS_TYPE_UINT32, &id,
211 return bus_send_error_reply(m, message, &error, -EINVAL);
213 if (!(j = manager_get_job(m, id)))
214 return bus_send_error_reply(m, message, NULL, -ENOENT);
216 if (!(reply = dbus_message_new_method_return(message)))
219 if (!(path = job_dbus_path(j)))
222 if (!dbus_message_append_args(
224 DBUS_TYPE_OBJECT_PATH, &path,
228 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
230 manager_clear_jobs(m);
232 if (!(reply = dbus_message_new_method_return(message)))
235 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
236 DBusMessageIter iter, sub;
241 if (!(reply = dbus_message_new_method_return(message)))
244 dbus_message_iter_init_append(reply, &iter);
246 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
249 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
250 char *u_path, *j_path;
251 const char *description, *load_state, *active_state, *sub_state, *job_type;
252 DBusMessageIter sub2;
258 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
261 description = unit_description(u);
262 load_state = unit_load_state_to_string(u->meta.load_state);
263 active_state = unit_active_state_to_string(unit_active_state(u));
264 sub_state = unit_sub_state_to_string(u);
266 if (!(u_path = unit_dbus_path(u)))
270 job_id = (uint32_t) u->meta.job->id;
272 if (!(j_path = job_dbus_path(u->meta.job))) {
277 job_type = job_type_to_string(u->meta.job->type);
284 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
285 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
286 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
287 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
288 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
289 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
290 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
291 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &job_type) ||
292 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
303 if (!dbus_message_iter_close_container(&sub, &sub2))
307 if (!dbus_message_iter_close_container(&iter, &sub))
310 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
311 DBusMessageIter iter, sub;
315 if (!(reply = dbus_message_new_method_return(message)))
318 dbus_message_iter_init_append(reply, &iter);
320 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
323 HASHMAP_FOREACH(j, m->jobs, i) {
324 char *u_path, *j_path;
325 const char *state, *type;
327 DBusMessageIter sub2;
329 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
332 id = (uint32_t) j->id;
333 state = job_state_to_string(j->state);
334 type = job_type_to_string(j->type);
336 if (!(j_path = job_dbus_path(j)))
339 if (!(u_path = unit_dbus_path(j->unit))) {
344 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
345 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
346 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
347 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
348 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
349 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
358 if (!dbus_message_iter_close_container(&sub, &sub2))
362 if (!dbus_message_iter_close_container(&iter, &sub))
365 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
368 if (!(client = strdup(dbus_message_get_sender(message))))
371 r = set_put(m->subscribed, client);
374 return bus_send_error_reply(m, message, NULL, r);
376 if (!(reply = dbus_message_new_method_return(message)))
379 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
382 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
383 return bus_send_error_reply(m, message, NULL, -ENOENT);
387 if (!(reply = dbus_message_new_method_return(message)))
390 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
395 if (!(reply = dbus_message_new_method_return(message)))
398 if (!(f = open_memstream(&dump, &size)))
401 manager_dump_units(m, f, NULL);
402 manager_dump_jobs(m, f, NULL);
412 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
418 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
423 if (!dbus_message_get_args(
426 DBUS_TYPE_STRING, &name,
427 DBUS_TYPE_BOOLEAN, &cleanup,
429 return bus_send_error_reply(m, message, &error, -EINVAL);
431 if (name && name[0] == 0)
434 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
435 return bus_send_error_reply(m, message, NULL, r);
437 if (!(reply = dbus_message_new_method_return(message)))
440 if (!(path = unit_dbus_path(UNIT(s))))
443 if (!dbus_message_append_args(
445 DBUS_TYPE_OBJECT_PATH, &path,
449 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
450 char *introspection = NULL;
458 if (!(reply = dbus_message_new_method_return(message)))
461 /* We roll our own introspection code here, instead of
462 * relying on bus_default_message_handler() because we
463 * need to generate our introspection string
466 if (!(f = open_memstream(&introspection, &size)))
469 fputs(INTROSPECTION_BEGIN, f);
471 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
477 if (!(p = bus_path_escape(k))) {
483 fprintf(f, "<node name=\"unit/%s\"/>", p);
487 HASHMAP_FOREACH(j, m->jobs, i)
488 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
490 fputs(INTROSPECTION_END, f);
503 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
510 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
512 assert(!m->queued_message);
514 /* Instead of sending the reply back right away, we
515 * just remember that we need to and then send it
516 * after the reload is finished. That way the caller
517 * knows when the reload finished. */
519 if (!(m->queued_message = dbus_message_new_method_return(message)))
522 m->exit_code = MANAGER_RELOAD;
524 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
526 if (!(reply = dbus_message_new_method_return(message)))
529 m->exit_code = MANAGER_REEXECUTE;
531 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
533 if (m->running_as == MANAGER_INIT)
534 return bus_send_error_reply(m, message, NULL, -ENOTSUP);
536 if (!(reply = dbus_message_new_method_return(message)))
539 m->exit_code = MANAGER_EXIT;
542 return bus_default_message_handler(m, message, NULL, properties);
547 if (!dbus_connection_send(connection, reply, NULL))
550 dbus_message_unref(reply);
553 return DBUS_HANDLER_RESULT_HANDLED;
559 dbus_message_unref(reply);
561 dbus_error_free(&error);
563 return DBUS_HANDLER_RESULT_NEED_MEMORY;
566 const DBusObjectPathVTable bus_manager_vtable = {
567 .message_function = bus_manager_message_handler