1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 #define INTROSPECTION_BEGIN \
9 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
11 " <interface name=\"org.freedesktop.systemd1\">" \
12 " <method name=\"GetUnit\">" \
13 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
14 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
16 " <method name=\"LoadUnit\">" \
17 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
18 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
20 " <method name=\"GetJob\">" \
21 " <arg name=\"id\" type=\"u\" direction=\"in\"/>" \
22 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
24 " <method name=\"ClearJobs\"/>" \
25 " <method name=\"ListUnits\">" \
26 " <arg name=\"units\" type=\"a(ssssouso)\" direction=\"out\"/>" \
28 " <method name=\"ListJobs\">" \
29 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>" \
32 BUS_PROPERTIES_INTERFACE \
33 BUS_INTROSPECTABLE_INTERFACE
35 #define INTROSPECTION_END \
38 DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
42 DBusMessage
*reply
= NULL
;
49 dbus_error_init(&error
);
51 log_debug("Got D-Bus request: %s.%s() on %s",
52 dbus_message_get_interface(message
),
53 dbus_message_get_member(message
),
54 dbus_message_get_path(message
));
56 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "GetUnit")) {
60 if (!dbus_message_get_args(
63 DBUS_TYPE_STRING
, &name
,
65 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
67 if (!(u
= manager_get_unit(m
, name
)))
68 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
70 if (!(reply
= dbus_message_new_method_return(message
)))
73 if (!(path
= unit_dbus_path(u
)))
76 if (!dbus_message_append_args(
78 DBUS_TYPE_OBJECT_PATH
, &path
,
82 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "LoadUnit")) {
86 if (!dbus_message_get_args(
89 DBUS_TYPE_STRING
, &name
,
91 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
93 if ((r
= manager_load_unit(m
, name
, &u
)) < 0)
94 return bus_send_error_reply(m
, message
, NULL
, r
);
96 if (!(reply
= dbus_message_new_method_return(message
)))
99 if (!(path
= unit_dbus_path(u
)))
102 if (!dbus_message_append_args(
104 DBUS_TYPE_OBJECT_PATH
, &path
,
108 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "GetJob")) {
112 if (!dbus_message_get_args(
115 DBUS_TYPE_UINT32
, &id
,
117 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
119 if (!(j
= manager_get_job(m
, id
)))
120 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
122 if (!(reply
= dbus_message_new_method_return(message
)))
125 if (!(path
= job_dbus_path(j
)))
128 if (!dbus_message_append_args(
130 DBUS_TYPE_OBJECT_PATH
, &path
,
134 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ClearJobs")) {
136 manager_clear_jobs(m
);
138 if (!(reply
= dbus_message_new_method_return(message
)))
141 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListUnits")) {
142 DBusMessageIter iter
, sub
;
147 if (!(reply
= dbus_message_new_method_return(message
)))
150 dbus_message_iter_init_append(reply
, &iter
);
152 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(ssssouso)", &sub
))
155 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
156 char *unit_path
, *job_path
;
157 const char *id
, *description
, *load_state
, *active_state
, *job_type
;
158 DBusMessageIter sub2
;
165 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
168 description
= unit_description(u
);
169 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
170 active_state
= unit_active_state_to_string(unit_active_state(u
));
172 if (!(unit_path
= unit_dbus_path(u
)))
176 job_id
= (uint32_t) u
->meta
.job
->id
;
178 if (!(job_path
= job_dbus_path(u
->meta
.job
))) {
183 job_type
= job_type_to_string(u
->meta
.job
->state
);
186 job_path
= unit_path
;
190 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &id
) ||
191 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
192 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
193 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
194 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
) ||
195 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
196 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &job_type
) ||
197 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &job_path
)) {
208 if (!dbus_message_iter_close_container(&sub
, &sub2
))
212 if (!dbus_message_iter_close_container(&iter
, &sub
))
215 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListJobs")) {
216 DBusMessageIter iter
, sub
;
220 if (!(reply
= dbus_message_new_method_return(message
)))
223 dbus_message_iter_init_append(reply
, &iter
);
225 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
228 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
229 char *unit_path
, *job_path
;
230 const char *unit
, *state
, *type
;
232 DBusMessageIter sub2
;
234 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
237 id
= (uint32_t) j
->id
;
238 unit
= unit_id(j
->unit
);
239 state
= job_state_to_string(j
->state
);
240 type
= job_type_to_string(j
->state
);
242 if (!(job_path
= job_dbus_path(j
)))
245 if (!(unit_path
= unit_dbus_path(j
->unit
))) {
250 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
251 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &unit
) ||
252 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
253 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
254 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &job_path
) ||
255 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &unit_path
)) {
264 if (!dbus_message_iter_close_container(&sub
, &sub2
))
268 if (!dbus_message_iter_close_container(&iter
, &sub
))
271 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
272 char *introspection
= NULL
;
280 if (!(reply
= dbus_message_new_method_return(message
)))
283 /* We roll our own introspection code here, instead of
284 * relying on bus_default_message_handler() because we
285 * need to generate our introspection string
288 if (!(f
= open_memstream(&introspection
, &size
)))
291 fputs(INTROSPECTION_BEGIN
, f
);
293 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
299 if (!(p
= bus_path_escape(k
))) {
305 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
309 HASHMAP_FOREACH(j
, m
->jobs
, i
)
310 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
312 fputs(INTROSPECTION_END
, f
);
325 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
333 return bus_default_message_handler(m
, message
, NULL
, NULL
);
338 if (!dbus_connection_send(connection
, reply
, NULL
))
341 dbus_message_unref(reply
);
344 return DBUS_HANDLER_RESULT_HANDLED
;
350 dbus_message_unref(reply
);
352 dbus_error_free(&error
);
354 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
357 const DBusObjectPathVTable bus_manager_vtable
= {
358 .message_function
= bus_manager_message_handler