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\"/>" \
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
, "(ssssouso)", &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
, *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
));
257 if (!(u_path
= unit_dbus_path(u
)))
261 job_id
= (uint32_t) u
->meta
.job
->id
;
263 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
268 job_type
= job_type_to_string(u
->meta
.job
->type
);
275 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &id
) ||
276 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
277 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
278 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
279 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
280 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
281 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &job_type
) ||
282 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
293 if (!dbus_message_iter_close_container(&sub
, &sub2
))
297 if (!dbus_message_iter_close_container(&iter
, &sub
))
300 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "ListJobs")) {
301 DBusMessageIter iter
, sub
;
305 if (!(reply
= dbus_message_new_method_return(message
)))
308 dbus_message_iter_init_append(reply
, &iter
);
310 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
313 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
314 char *u_path
, *j_path
;
315 const char *unit
, *state
, *type
;
317 DBusMessageIter sub2
;
319 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
322 id
= (uint32_t) j
->id
;
323 unit
= unit_id(j
->unit
);
324 state
= job_state_to_string(j
->state
);
325 type
= job_type_to_string(j
->type
);
327 if (!(j_path
= job_dbus_path(j
)))
330 if (!(u_path
= unit_dbus_path(j
->unit
))) {
335 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
336 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &unit
) ||
337 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
338 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
339 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
340 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
349 if (!dbus_message_iter_close_container(&sub
, &sub2
))
353 if (!dbus_message_iter_close_container(&iter
, &sub
))
356 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Subscribe")) {
359 if (!(client
= strdup(dbus_message_get_sender(message
))))
362 r
= set_put(m
->subscribed
, client
);
365 return bus_send_error_reply(m
, message
, NULL
, r
);
367 if (!(reply
= dbus_message_new_method_return(message
)))
370 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Unsubscribe")) {
373 if (!(client
= set_remove(m
->subscribed
, (char*) dbus_message_get_sender(message
))))
374 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
378 if (!(reply
= dbus_message_new_method_return(message
)))
381 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1", "Dump")) {
386 if (!(reply
= dbus_message_new_method_return(message
)))
389 if (!(f
= open_memstream(&dump
, &size
)))
392 manager_dump_units(m
, f
, NULL
);
393 manager_dump_jobs(m
, f
, NULL
);
403 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &dump
, DBUS_TYPE_INVALID
)) {
410 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
411 char *introspection
= NULL
;
419 if (!(reply
= dbus_message_new_method_return(message
)))
422 /* We roll our own introspection code here, instead of
423 * relying on bus_default_message_handler() because we
424 * need to generate our introspection string
427 if (!(f
= open_memstream(&introspection
, &size
)))
430 fputs(INTROSPECTION_BEGIN
, f
);
432 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
438 if (!(p
= bus_path_escape(k
))) {
444 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
448 HASHMAP_FOREACH(j
, m
->jobs
, i
)
449 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
451 fputs(INTROSPECTION_END
, f
);
464 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
472 return bus_default_message_handler(m
, message
, NULL
, properties
);
477 if (!dbus_connection_send(connection
, reply
, NULL
))
480 dbus_message_unref(reply
);
483 return DBUS_HANDLER_RESULT_HANDLED
;
489 dbus_message_unref(reply
);
491 dbus_error_free(&error
);
493 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
496 const DBusObjectPathVTable bus_manager_vtable
= {
497 .message_function
= bus_manager_message_handler