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\"/>" \
50 " <method name=\"Subscribe\"/>" \
51 " <method name=\"Unsubscribe\"/>" \
52 " <signal name=\"UnitNew\">" \
53 " <arg name=\"id\" type=\"s\"/>" \
54 " <arg name=\"unit\" type=\"o\"/>" \
56 " <signal name=\"UnitRemoved\">" \
57 " <arg name=\"id\" type=\"s\"/>" \
58 " <arg name=\"unit\" type=\"o\"/>" \
60 " <signal name=\"JobNew\">" \
61 " <arg name=\"id\" type=\"u\"/>" \
62 " <arg name=\"job\" type=\"o\"/>" \
64 " <signal name=\"JobRemoved\">" \
65 " <arg name=\"id\" type=\"u\"/>" \
66 " <arg name=\"job\" type=\"o\"/>" \
69 BUS_PROPERTIES_INTERFACE \
70 BUS_INTROSPECTABLE_INTERFACE
72 #define INTROSPECTION_END \
75 static DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
79 DBusMessage
*reply
= NULL
;
86 dbus_error_init(&error
);
88 log_debug("Got D-Bus request: %s.%s() on %s",
89 dbus_message_get_interface(message
),
90 dbus_message_get_member(message
),
91 dbus_message_get_path(message
));
93 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "GetUnit")) {
97 if (!dbus_message_get_args(
100 DBUS_TYPE_STRING
, &name
,
102 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
104 if (!(u
= manager_get_unit(m
, name
)))
105 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
107 if (!(reply
= dbus_message_new_method_return(message
)))
110 if (!(path
= unit_dbus_path(u
)))
113 if (!dbus_message_append_args(
115 DBUS_TYPE_OBJECT_PATH
, &path
,
119 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "LoadUnit")) {
123 if (!dbus_message_get_args(
126 DBUS_TYPE_STRING
, &name
,
128 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
130 if ((r
= manager_load_unit(m
, name
, &u
)) < 0)
131 return bus_send_error_reply(m
, message
, NULL
, r
);
133 if (!(reply
= dbus_message_new_method_return(message
)))
136 if (!(path
= unit_dbus_path(u
)))
139 if (!dbus_message_append_args(
141 DBUS_TYPE_OBJECT_PATH
, &path
,
145 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "GetJob")) {
149 if (!dbus_message_get_args(
152 DBUS_TYPE_UINT32
, &id
,
154 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
156 if (!(j
= manager_get_job(m
, id
)))
157 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
159 if (!(reply
= dbus_message_new_method_return(message
)))
162 if (!(path
= job_dbus_path(j
)))
165 if (!dbus_message_append_args(
167 DBUS_TYPE_OBJECT_PATH
, &path
,
171 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ClearJobs")) {
173 manager_clear_jobs(m
);
175 if (!(reply
= dbus_message_new_method_return(message
)))
178 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListUnits")) {
179 DBusMessageIter iter
, sub
;
184 if (!(reply
= dbus_message_new_method_return(message
)))
187 dbus_message_iter_init_append(reply
, &iter
);
189 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(ssssouso)", &sub
))
192 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
193 char *u_path
, *j_path
;
194 const char *id
, *description
, *load_state
, *active_state
, *job_type
;
195 DBusMessageIter sub2
;
202 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
205 description
= unit_description(u
);
206 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
207 active_state
= unit_active_state_to_string(unit_active_state(u
));
209 if (!(u_path
= unit_dbus_path(u
)))
213 job_id
= (uint32_t) u
->meta
.job
->id
;
215 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
220 job_type
= job_type_to_string(u
->meta
.job
->type
);
227 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &id
) ||
228 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
229 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
230 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
231 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
232 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
233 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &job_type
) ||
234 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
245 if (!dbus_message_iter_close_container(&sub
, &sub2
))
249 if (!dbus_message_iter_close_container(&iter
, &sub
))
252 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListJobs")) {
253 DBusMessageIter iter
, sub
;
257 if (!(reply
= dbus_message_new_method_return(message
)))
260 dbus_message_iter_init_append(reply
, &iter
);
262 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
265 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
266 char *u_path
, *j_path
;
267 const char *unit
, *state
, *type
;
269 DBusMessageIter sub2
;
271 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
274 id
= (uint32_t) j
->id
;
275 unit
= unit_id(j
->unit
);
276 state
= job_state_to_string(j
->state
);
277 type
= job_type_to_string(j
->type
);
279 if (!(j_path
= job_dbus_path(j
)))
282 if (!(u_path
= unit_dbus_path(j
->unit
))) {
287 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
288 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &unit
) ||
289 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
290 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
291 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
292 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
301 if (!dbus_message_iter_close_container(&sub
, &sub2
))
305 if (!dbus_message_iter_close_container(&iter
, &sub
))
308 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Subscribe")) {
311 if (!(client
= strdup(dbus_message_get_sender(message
))))
314 r
= set_put(m
->subscribed
, client
);
317 return bus_send_error_reply(m
, message
, NULL
, r
);
319 if (!(reply
= dbus_message_new_method_return(message
)))
322 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Unsubscribe")) {
325 if (!(client
= set_remove(m
->subscribed
, (char*) dbus_message_get_sender(message
))))
326 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
330 if (!(reply
= dbus_message_new_method_return(message
)))
333 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
334 char *introspection
= NULL
;
342 if (!(reply
= dbus_message_new_method_return(message
)))
345 /* We roll our own introspection code here, instead of
346 * relying on bus_default_message_handler() because we
347 * need to generate our introspection string
350 if (!(f
= open_memstream(&introspection
, &size
)))
353 fputs(INTROSPECTION_BEGIN
, f
);
355 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
361 if (!(p
= bus_path_escape(k
))) {
367 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
371 HASHMAP_FOREACH(j
, m
->jobs
, i
)
372 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
374 fputs(INTROSPECTION_END
, f
);
387 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
395 return bus_default_message_handler(m
, message
, NULL
, NULL
);
400 if (!dbus_connection_send(connection
, reply
, NULL
))
403 dbus_message_unref(reply
);
406 return DBUS_HANDLER_RESULT_HANDLED
;
412 dbus_message_unref(reply
);
414 dbus_error_free(&error
);
416 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
419 const DBusObjectPathVTable bus_manager_vtable
= {
420 .message_function
= bus_manager_message_handler