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(sssssouso)\" 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\"/>" \
69 " <property name=\"Version\" type=\"s\" access=\"read\"/>" \
70 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>" \
71 " <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>" \
72 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>" \
73 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>" \
75 BUS_PROPERTIES_INTERFACE \
76 BUS_INTROSPECTABLE_INTERFACE
78 #define INTROSPECTION_END \
81 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as
, manager_running_as
, ManagerRunningAs
);
83 static int bus_manager_append_log_target(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
90 t
= log_target_to_string(log_get_target());
92 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
98 static int bus_manager_append_log_level(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
105 t
= log_level_to_string(log_get_max_level());
107 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
113 static DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
116 const BusProperty properties
[] = {
117 { "org.freedesktop.systemd1", "Version", bus_property_append_string
, "s", PACKAGE_VERSION
},
118 { "org.freedesktop.systemd1", "RunningAs", bus_manager_append_running_as
, "s", &m
->running_as
},
119 { "org.freedesktop.systemd1", "BootTimestamp", bus_property_append_uint64
, "t", &m
->boot_timestamp
},
120 { "org.freedesktop.systemd1", "LogLevel", bus_manager_append_log_level
, "s", NULL
},
121 { "org.freedesktop.systemd1", "LogTarget", bus_manager_append_log_target
, "s", NULL
},
122 { NULL
, NULL
, NULL
, NULL
, NULL
}
127 DBusMessage
*reply
= NULL
;
134 dbus_error_init(&error
);
136 log_debug("Got D-Bus request: %s.%s() on %s",
137 dbus_message_get_interface(message
),
138 dbus_message_get_member(message
),
139 dbus_message_get_path(message
));
141 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "GetUnit")) {
145 if (!dbus_message_get_args(
148 DBUS_TYPE_STRING
, &name
,
150 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
152 if (!(u
= manager_get_unit(m
, name
)))
153 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
155 if (!(reply
= dbus_message_new_method_return(message
)))
158 if (!(path
= unit_dbus_path(u
)))
161 if (!dbus_message_append_args(
163 DBUS_TYPE_OBJECT_PATH
, &path
,
167 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "LoadUnit")) {
171 if (!dbus_message_get_args(
174 DBUS_TYPE_STRING
, &name
,
176 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
178 if ((r
= manager_load_unit(m
, name
, &u
)) < 0)
179 return bus_send_error_reply(m
, message
, NULL
, r
);
181 if (!(reply
= dbus_message_new_method_return(message
)))
184 if (!(path
= unit_dbus_path(u
)))
187 if (!dbus_message_append_args(
189 DBUS_TYPE_OBJECT_PATH
, &path
,
193 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "GetJob")) {
197 if (!dbus_message_get_args(
200 DBUS_TYPE_UINT32
, &id
,
202 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
204 if (!(j
= manager_get_job(m
, id
)))
205 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
207 if (!(reply
= dbus_message_new_method_return(message
)))
210 if (!(path
= job_dbus_path(j
)))
213 if (!dbus_message_append_args(
215 DBUS_TYPE_OBJECT_PATH
, &path
,
219 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ClearJobs")) {
221 manager_clear_jobs(m
);
223 if (!(reply
= dbus_message_new_method_return(message
)))
226 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListUnits")) {
227 DBusMessageIter iter
, sub
;
232 if (!(reply
= dbus_message_new_method_return(message
)))
235 dbus_message_iter_init_append(reply
, &iter
);
237 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(sssssouso)", &sub
))
240 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
241 char *u_path
, *j_path
;
242 const char *id
, *description
, *load_state
, *active_state
, *sub_state
, *job_type
;
243 DBusMessageIter sub2
;
250 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
253 description
= unit_description(u
);
254 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
255 active_state
= unit_active_state_to_string(unit_active_state(u
));
256 sub_state
= unit_sub_state_to_string(u
);
258 if (!(u_path
= unit_dbus_path(u
)))
262 job_id
= (uint32_t) u
->meta
.job
->id
;
264 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
269 job_type
= job_type_to_string(u
->meta
.job
->type
);
276 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &id
) ||
277 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
278 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
279 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
280 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sub_state
) ||
281 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
282 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
283 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &job_type
) ||
284 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
295 if (!dbus_message_iter_close_container(&sub
, &sub2
))
299 if (!dbus_message_iter_close_container(&iter
, &sub
))
302 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListJobs")) {
303 DBusMessageIter iter
, sub
;
307 if (!(reply
= dbus_message_new_method_return(message
)))
310 dbus_message_iter_init_append(reply
, &iter
);
312 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
315 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
316 char *u_path
, *j_path
;
317 const char *unit
, *state
, *type
;
319 DBusMessageIter sub2
;
321 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
324 id
= (uint32_t) j
->id
;
325 unit
= unit_id(j
->unit
);
326 state
= job_state_to_string(j
->state
);
327 type
= job_type_to_string(j
->type
);
329 if (!(j_path
= job_dbus_path(j
)))
332 if (!(u_path
= unit_dbus_path(j
->unit
))) {
337 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
338 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &unit
) ||
339 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
340 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
341 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
342 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
351 if (!dbus_message_iter_close_container(&sub
, &sub2
))
355 if (!dbus_message_iter_close_container(&iter
, &sub
))
358 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Subscribe")) {
361 if (!(client
= strdup(dbus_message_get_sender(message
))))
364 r
= set_put(m
->subscribed
, client
);
367 return bus_send_error_reply(m
, message
, NULL
, r
);
369 if (!(reply
= dbus_message_new_method_return(message
)))
372 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Unsubscribe")) {
375 if (!(client
= set_remove(m
->subscribed
, (char*) dbus_message_get_sender(message
))))
376 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
380 if (!(reply
= dbus_message_new_method_return(message
)))
383 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Dump")) {
388 if (!(reply
= dbus_message_new_method_return(message
)))
391 if (!(f
= open_memstream(&dump
, &size
)))
394 manager_dump_units(m
, f
, NULL
);
395 manager_dump_jobs(m
, f
, NULL
);
405 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &dump
, DBUS_TYPE_INVALID
)) {
412 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
413 char *introspection
= NULL
;
421 if (!(reply
= dbus_message_new_method_return(message
)))
424 /* We roll our own introspection code here, instead of
425 * relying on bus_default_message_handler() because we
426 * need to generate our introspection string
429 if (!(f
= open_memstream(&introspection
, &size
)))
432 fputs(INTROSPECTION_BEGIN
, f
);
434 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
440 if (!(p
= bus_path_escape(k
))) {
446 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
450 HASHMAP_FOREACH(j
, m
->jobs
, i
)
451 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
453 fputs(INTROSPECTION_END
, f
);
466 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
474 return bus_default_message_handler(m
, message
, NULL
, properties
);
479 if (!dbus_connection_send(connection
, reply
, NULL
))
482 dbus_message_unref(reply
);
485 return DBUS_HANDLER_RESULT_HANDLED
;
491 dbus_message_unref(reply
);
493 dbus_error_free(&error
);
495 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
498 const DBusObjectPathVTable bus_manager_vtable
= {
499 .message_function
= bus_manager_message_handler