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=\"job\" 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\"/>" \
50 " <method name=\"Subscribe\"/>" \
51 " <method name=\"Unsubscribe\"/>" \
52 " <method name=\"Dump\"/>" \
53 " <signal name=\"UnitNew\">" \
54 " <arg name=\"id\" type=\"s\"/>" \
55 " <arg name=\"unit\" type=\"o\"/>" \
57 " <signal name=\"UnitRemoved\">" \
58 " <arg name=\"id\" type=\"s\"/>" \
59 " <arg name=\"unit\" type=\"o\"/>" \
61 " <signal name=\"JobNew\">" \
62 " <arg name=\"id\" type=\"u\"/>" \
63 " <arg name=\"job\" type=\"o\"/>" \
65 " <signal name=\"JobRemoved\">" \
66 " <arg name=\"id\" type=\"u\"/>" \
67 " <arg name=\"job\" type=\"o\"/>" \
70 BUS_PROPERTIES_INTERFACE \
71 BUS_INTROSPECTABLE_INTERFACE
73 #define INTROSPECTION_END \
76 static DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
80 DBusMessage
*reply
= NULL
;
87 dbus_error_init(&error
);
89 log_debug("Got D-Bus request: %s.%s() on %s",
90 dbus_message_get_interface(message
),
91 dbus_message_get_member(message
),
92 dbus_message_get_path(message
));
94 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "GetUnit")) {
98 if (!dbus_message_get_args(
101 DBUS_TYPE_STRING
, &name
,
103 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
105 if (!(u
= manager_get_unit(m
, name
)))
106 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
108 if (!(reply
= dbus_message_new_method_return(message
)))
111 if (!(path
= unit_dbus_path(u
)))
114 if (!dbus_message_append_args(
116 DBUS_TYPE_OBJECT_PATH
, &path
,
120 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "LoadUnit")) {
124 if (!dbus_message_get_args(
127 DBUS_TYPE_STRING
, &name
,
129 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
131 if ((r
= manager_load_unit(m
, name
, &u
)) < 0)
132 return bus_send_error_reply(m
, message
, NULL
, r
);
134 if (!(reply
= dbus_message_new_method_return(message
)))
137 if (!(path
= unit_dbus_path(u
)))
140 if (!dbus_message_append_args(
142 DBUS_TYPE_OBJECT_PATH
, &path
,
146 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "GetJob")) {
150 if (!dbus_message_get_args(
153 DBUS_TYPE_UINT32
, &id
,
155 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
157 if (!(j
= manager_get_job(m
, id
)))
158 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
160 if (!(reply
= dbus_message_new_method_return(message
)))
163 if (!(path
= job_dbus_path(j
)))
166 if (!dbus_message_append_args(
168 DBUS_TYPE_OBJECT_PATH
, &path
,
172 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ClearJobs")) {
174 manager_clear_jobs(m
);
176 if (!(reply
= dbus_message_new_method_return(message
)))
179 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListUnits")) {
180 DBusMessageIter iter
, sub
;
185 if (!(reply
= dbus_message_new_method_return(message
)))
188 dbus_message_iter_init_append(reply
, &iter
);
190 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(ssssouso)", &sub
))
193 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
194 char *u_path
, *j_path
;
195 const char *id
, *description
, *load_state
, *active_state
, *job_type
;
196 DBusMessageIter sub2
;
203 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
206 description
= unit_description(u
);
207 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
208 active_state
= unit_active_state_to_string(unit_active_state(u
));
210 if (!(u_path
= unit_dbus_path(u
)))
214 job_id
= (uint32_t) u
->meta
.job
->id
;
216 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
221 job_type
= job_type_to_string(u
->meta
.job
->type
);
228 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &id
) ||
229 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
230 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
231 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
232 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
233 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
234 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &job_type
) ||
235 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
246 if (!dbus_message_iter_close_container(&sub
, &sub2
))
250 if (!dbus_message_iter_close_container(&iter
, &sub
))
253 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListJobs")) {
254 DBusMessageIter iter
, sub
;
258 if (!(reply
= dbus_message_new_method_return(message
)))
261 dbus_message_iter_init_append(reply
, &iter
);
263 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
266 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
267 char *u_path
, *j_path
;
268 const char *unit
, *state
, *type
;
270 DBusMessageIter sub2
;
272 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
275 id
= (uint32_t) j
->id
;
276 unit
= unit_id(j
->unit
);
277 state
= job_state_to_string(j
->state
);
278 type
= job_type_to_string(j
->type
);
280 if (!(j_path
= job_dbus_path(j
)))
283 if (!(u_path
= unit_dbus_path(j
->unit
))) {
288 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
289 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &unit
) ||
290 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
291 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
292 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
293 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
302 if (!dbus_message_iter_close_container(&sub
, &sub2
))
306 if (!dbus_message_iter_close_container(&iter
, &sub
))
309 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Subscribe")) {
312 if (!(client
= strdup(dbus_message_get_sender(message
))))
315 r
= set_put(m
->subscribed
, client
);
318 return bus_send_error_reply(m
, message
, NULL
, r
);
320 if (!(reply
= dbus_message_new_method_return(message
)))
323 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Unsubscribe")) {
326 if (!(client
= set_remove(m
->subscribed
, (char*) dbus_message_get_sender(message
))))
327 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
331 if (!(reply
= dbus_message_new_method_return(message
)))
334 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Dump")) {
339 if (!(reply
= dbus_message_new_method_return(message
)))
342 if (!(f
= open_memstream(&dump
, &size
)))
345 manager_dump_units(m
, f
, NULL
);
346 manager_dump_jobs(m
, f
, NULL
);
356 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &dump
, DBUS_TYPE_INVALID
)) {
363 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
364 char *introspection
= NULL
;
372 if (!(reply
= dbus_message_new_method_return(message
)))
375 /* We roll our own introspection code here, instead of
376 * relying on bus_default_message_handler() because we
377 * need to generate our introspection string
380 if (!(f
= open_memstream(&introspection
, &size
)))
383 fputs(INTROSPECTION_BEGIN
, f
);
385 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
391 if (!(p
= bus_path_escape(k
))) {
397 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
401 HASHMAP_FOREACH(j
, m
->jobs
, i
)
402 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
404 fputs(INTROSPECTION_END
, f
);
417 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
425 return bus_default_message_handler(m
, message
, NULL
, NULL
);
430 if (!dbus_connection_send(connection
, reply
, NULL
))
433 dbus_message_unref(reply
);
436 return DBUS_HANDLER_RESULT_HANDLED
;
442 dbus_message_unref(reply
);
444 dbus_error_free(&error
);
446 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
449 const DBusObjectPathVTable bus_manager_vtable
= {
450 .message_function
= bus_manager_message_handler