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 " <signal name=\"UnitNew\">" \
60 " <arg name=\"id\" type=\"s\"/>" \
61 " <arg name=\"unit\" type=\"o\"/>" \
63 " <signal name=\"UnitRemoved\">" \
64 " <arg name=\"id\" type=\"s\"/>" \
65 " <arg name=\"unit\" type=\"o\"/>" \
67 " <signal name=\"JobNew\">" \
68 " <arg name=\"id\" type=\"u\"/>" \
69 " <arg name=\"job\" type=\"o\"/>" \
71 " <signal name=\"JobRemoved\">" \
72 " <arg name=\"id\" type=\"u\"/>" \
73 " <arg name=\"job\" type=\"o\"/>" \
75 " <property name=\"Version\" type=\"s\" access=\"read\"/>" \
76 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>" \
77 " <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>" \
78 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>" \
79 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>" \
81 BUS_PROPERTIES_INTERFACE \
82 BUS_INTROSPECTABLE_INTERFACE
84 #define INTROSPECTION_END \
87 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
89 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
96 t = log_target_to_string(log_get_target());
98 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
104 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
111 t = log_level_to_string(log_get_max_level());
113 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
119 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
122 const BusProperty properties[] = {
123 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
124 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
125 { "org.freedesktop.systemd1.Manager", "BootTimestamp", bus_property_append_uint64, "t", &m->boot_timestamp },
126 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
127 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
128 { NULL, NULL, NULL, NULL, NULL }
133 DBusMessage *reply = NULL;
140 dbus_error_init(&error);
142 log_debug("Got D-Bus request: %s.%s() on %s",
143 dbus_message_get_interface(message),
144 dbus_message_get_member(message),
145 dbus_message_get_path(message));
147 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
151 if (!dbus_message_get_args(
154 DBUS_TYPE_STRING, &name,
156 return bus_send_error_reply(m, message, &error, -EINVAL);
158 if (!(u = manager_get_unit(m, name)))
159 return bus_send_error_reply(m, message, NULL, -ENOENT);
161 if (!(reply = dbus_message_new_method_return(message)))
164 if (!(path = unit_dbus_path(u)))
167 if (!dbus_message_append_args(
169 DBUS_TYPE_OBJECT_PATH, &path,
173 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
177 if (!dbus_message_get_args(
180 DBUS_TYPE_STRING, &name,
182 return bus_send_error_reply(m, message, &error, -EINVAL);
184 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
185 return bus_send_error_reply(m, message, NULL, r);
187 if (!(reply = dbus_message_new_method_return(message)))
190 if (!(path = unit_dbus_path(u)))
193 if (!dbus_message_append_args(
195 DBUS_TYPE_OBJECT_PATH, &path,
199 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "GetJob")) {
203 if (!dbus_message_get_args(
206 DBUS_TYPE_UINT32, &id,
208 return bus_send_error_reply(m, message, &error, -EINVAL);
210 if (!(j = manager_get_job(m, id)))
211 return bus_send_error_reply(m, message, NULL, -ENOENT);
213 if (!(reply = dbus_message_new_method_return(message)))
216 if (!(path = job_dbus_path(j)))
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", "ClearJobs")) {
227 manager_clear_jobs(m);
229 if (!(reply = dbus_message_new_method_return(message)))
232 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
233 DBusMessageIter iter, sub;
238 if (!(reply = dbus_message_new_method_return(message)))
241 dbus_message_iter_init_append(reply, &iter);
243 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
246 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
247 char *u_path, *j_path;
248 const char *description, *load_state, *active_state, *sub_state, *job_type;
249 DBusMessageIter sub2;
255 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
258 description = unit_description(u);
259 load_state = unit_load_state_to_string(u->meta.load_state);
260 active_state = unit_active_state_to_string(unit_active_state(u));
261 sub_state = unit_sub_state_to_string(u);
263 if (!(u_path = unit_dbus_path(u)))
267 job_id = (uint32_t) u->meta.job->id;
269 if (!(j_path = job_dbus_path(u->meta.job))) {
274 job_type = job_type_to_string(u->meta.job->type);
281 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
282 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
283 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
284 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
285 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
286 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
287 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
288 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &job_type) ||
289 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
300 if (!dbus_message_iter_close_container(&sub, &sub2))
304 if (!dbus_message_iter_close_container(&iter, &sub))
307 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
308 DBusMessageIter iter, sub;
312 if (!(reply = dbus_message_new_method_return(message)))
315 dbus_message_iter_init_append(reply, &iter);
317 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
320 HASHMAP_FOREACH(j, m->jobs, i) {
321 char *u_path, *j_path;
322 const char *state, *type;
324 DBusMessageIter sub2;
326 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
329 id = (uint32_t) j->id;
330 state = job_state_to_string(j->state);
331 type = job_type_to_string(j->type);
333 if (!(j_path = job_dbus_path(j)))
336 if (!(u_path = unit_dbus_path(j->unit))) {
341 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
342 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
343 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
344 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
345 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
346 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
355 if (!dbus_message_iter_close_container(&sub, &sub2))
359 if (!dbus_message_iter_close_container(&iter, &sub))
362 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
365 if (!(client = strdup(dbus_message_get_sender(message))))
368 r = set_put(m->subscribed, client);
371 return bus_send_error_reply(m, message, NULL, r);
373 if (!(reply = dbus_message_new_method_return(message)))
376 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
379 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
380 return bus_send_error_reply(m, message, NULL, -ENOENT);
384 if (!(reply = dbus_message_new_method_return(message)))
387 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
392 if (!(reply = dbus_message_new_method_return(message)))
395 if (!(f = open_memstream(&dump, &size)))
398 manager_dump_units(m, f, NULL);
399 manager_dump_jobs(m, f, NULL);
409 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
415 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
420 if (!dbus_message_get_args(
423 DBUS_TYPE_STRING, &name,
424 DBUS_TYPE_BOOLEAN, &cleanup,
426 return bus_send_error_reply(m, message, &error, -EINVAL);
428 if (name && name[0] == 0)
431 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
432 return bus_send_error_reply(m, message, NULL, r);
434 if (!(reply = dbus_message_new_method_return(message)))
437 if (!(path = unit_dbus_path(UNIT(s))))
440 if (!dbus_message_append_args(
442 DBUS_TYPE_OBJECT_PATH, &path,
446 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
447 char *introspection = NULL;
455 if (!(reply = dbus_message_new_method_return(message)))
458 /* We roll our own introspection code here, instead of
459 * relying on bus_default_message_handler() because we
460 * need to generate our introspection string
463 if (!(f = open_memstream(&introspection, &size)))
466 fputs(INTROSPECTION_BEGIN, f);
468 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
474 if (!(p = bus_path_escape(k))) {
480 fprintf(f, "<node name=\"unit/%s\"/>", p);
484 HASHMAP_FOREACH(j, m->jobs, i)
485 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
487 fputs(INTROSPECTION_END, f);
500 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
508 return bus_default_message_handler(m, message, NULL, properties);
513 if (!dbus_connection_send(connection, reply, NULL))
516 dbus_message_unref(reply);
519 return DBUS_HANDLER_RESULT_HANDLED;
525 dbus_message_unref(reply);
527 dbus_error_free(&error);
529 return DBUS_HANDLER_RESULT_NEED_MEMORY;
532 const DBusObjectPathVTable bus_manager_vtable = {
533 .message_function = bus_manager_message_handler