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/>.
27 #define INTROSPECTION_BEGIN \
28 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
30 " <interface name=\"org.freedesktop.systemd1\">" \
31 " <method name=\"GetUnit\">" \
32 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
33 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
35 " <method name=\"LoadUnit\">" \
36 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
37 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
39 " <method name=\"GetJob\">" \
40 " <arg name=\"id\" type=\"u\" direction=\"in\"/>" \
41 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
43 " <method name=\"ClearJobs\"/>" \
44 " <method name=\"ListUnits\">" \
45 " <arg name=\"units\" type=\"a(ssssouso)\" direction=\"out\"/>" \
47 " <method name=\"ListJobs\">" \
48 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>" \
51 BUS_PROPERTIES_INTERFACE \
52 BUS_INTROSPECTABLE_INTERFACE
54 #define INTROSPECTION_END \
57 DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
61 DBusMessage *reply = NULL;
68 dbus_error_init(&error);
70 log_debug("Got D-Bus request: %s.%s() on %s",
71 dbus_message_get_interface(message),
72 dbus_message_get_member(message),
73 dbus_message_get_path(message));
75 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "GetUnit")) {
79 if (!dbus_message_get_args(
82 DBUS_TYPE_STRING, &name,
84 return bus_send_error_reply(m, message, &error, -EINVAL);
86 if (!(u = manager_get_unit(m, name)))
87 return bus_send_error_reply(m, message, NULL, -ENOENT);
89 if (!(reply = dbus_message_new_method_return(message)))
92 if (!(path = unit_dbus_path(u)))
95 if (!dbus_message_append_args(
97 DBUS_TYPE_OBJECT_PATH, &path,
101 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "LoadUnit")) {
105 if (!dbus_message_get_args(
108 DBUS_TYPE_STRING, &name,
110 return bus_send_error_reply(m, message, &error, -EINVAL);
112 if ((r = manager_load_unit(m, name, &u)) < 0)
113 return bus_send_error_reply(m, message, NULL, r);
115 if (!(reply = dbus_message_new_method_return(message)))
118 if (!(path = unit_dbus_path(u)))
121 if (!dbus_message_append_args(
123 DBUS_TYPE_OBJECT_PATH, &path,
127 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "GetJob")) {
131 if (!dbus_message_get_args(
134 DBUS_TYPE_UINT32, &id,
136 return bus_send_error_reply(m, message, &error, -EINVAL);
138 if (!(j = manager_get_job(m, id)))
139 return bus_send_error_reply(m, message, NULL, -ENOENT);
141 if (!(reply = dbus_message_new_method_return(message)))
144 if (!(path = job_dbus_path(j)))
147 if (!dbus_message_append_args(
149 DBUS_TYPE_OBJECT_PATH, &path,
153 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "ClearJobs")) {
155 manager_clear_jobs(m);
157 if (!(reply = dbus_message_new_method_return(message)))
160 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "ListUnits")) {
161 DBusMessageIter iter, sub;
166 if (!(reply = dbus_message_new_method_return(message)))
169 dbus_message_iter_init_append(reply, &iter);
171 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssouso)", &sub))
174 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
175 char *unit_path, *job_path;
176 const char *id, *description, *load_state, *active_state, *job_type;
177 DBusMessageIter sub2;
184 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
187 description = unit_description(u);
188 load_state = unit_load_state_to_string(u->meta.load_state);
189 active_state = unit_active_state_to_string(unit_active_state(u));
191 if (!(unit_path = unit_dbus_path(u)))
195 job_id = (uint32_t) u->meta.job->id;
197 if (!(job_path = job_dbus_path(u->meta.job))) {
202 job_type = job_type_to_string(u->meta.job->type);
205 job_path = unit_path;
209 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &id) ||
210 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
211 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
212 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
213 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path) ||
214 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
215 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &job_type) ||
216 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path)) {
227 if (!dbus_message_iter_close_container(&sub, &sub2))
231 if (!dbus_message_iter_close_container(&iter, &sub))
234 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "ListJobs")) {
235 DBusMessageIter iter, sub;
239 if (!(reply = dbus_message_new_method_return(message)))
242 dbus_message_iter_init_append(reply, &iter);
244 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
247 HASHMAP_FOREACH(j, m->jobs, i) {
248 char *unit_path, *job_path;
249 const char *unit, *state, *type;
251 DBusMessageIter sub2;
253 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
256 id = (uint32_t) j->id;
257 unit = unit_id(j->unit);
258 state = job_state_to_string(j->state);
259 type = job_type_to_string(j->type);
261 if (!(job_path = job_dbus_path(j)))
264 if (!(unit_path = unit_dbus_path(j->unit))) {
269 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
270 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &unit) ||
271 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
272 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
273 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path) ||
274 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path)) {
283 if (!dbus_message_iter_close_container(&sub, &sub2))
287 if (!dbus_message_iter_close_container(&iter, &sub))
290 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
291 char *introspection = NULL;
299 if (!(reply = dbus_message_new_method_return(message)))
302 /* We roll our own introspection code here, instead of
303 * relying on bus_default_message_handler() because we
304 * need to generate our introspection string
307 if (!(f = open_memstream(&introspection, &size)))
310 fputs(INTROSPECTION_BEGIN, f);
312 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
318 if (!(p = bus_path_escape(k))) {
324 fprintf(f, "<node name=\"unit/%s\"/>", p);
328 HASHMAP_FOREACH(j, m->jobs, i)
329 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
331 fputs(INTROSPECTION_END, f);
344 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
352 return bus_default_message_handler(m, message, NULL, NULL);
357 if (!dbus_connection_send(connection, reply, NULL))
360 dbus_message_unref(reply);
363 return DBUS_HANDLER_RESULT_HANDLED;
369 dbus_message_unref(reply);
371 dbus_error_free(&error);
373 return DBUS_HANDLER_RESULT_NEED_MEMORY;
376 const DBusObjectPathVTable bus_manager_vtable = {
377 .message_function = bus_manager_message_handler