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/>.
26 #include "dbus-manager.h"
28 #define INTROSPECTION_BEGIN \
29 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
31 " <interface name=\"org.freedesktop.systemd1.Manager\">" \
32 " <method name=\"GetUnit\">" \
33 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
34 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
36 " <method name=\"LoadUnit\">" \
37 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
40 " <method name=\"GetJob\">" \
41 " <arg name=\"id\" type=\"u\" direction=\"in\"/>" \
42 " <arg name=\"job\" type=\"o\" direction=\"out\"/>" \
44 " <method name=\"ClearJobs\"/>" \
45 " <method name=\"ListUnits\">" \
46 " <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>" \
48 " <method name=\"ListJobs\">" \
49 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>" \
51 " <method name=\"Subscribe\"/>" \
52 " <method name=\"Unsubscribe\"/>" \
53 " <method name=\"Dump\"/>" \
54 " <method name=\"CreateSnapshot\">" \
55 " <arg name=\"name\" type=\"s\" direction=\"in\"/>" \
56 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>" \
57 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>" \
59 " <method name=\"Reload\"/>" \
60 " <method name=\"Reexecute\"/>" \
61 " <method name=\"Exit\"/>" \
62 " <signal name=\"UnitNew\">" \
63 " <arg name=\"id\" type=\"s\"/>" \
64 " <arg name=\"unit\" type=\"o\"/>" \
66 " <signal name=\"UnitRemoved\">" \
67 " <arg name=\"id\" type=\"s\"/>" \
68 " <arg name=\"unit\" type=\"o\"/>" \
70 " <signal name=\"JobNew\">" \
71 " <arg name=\"id\" type=\"u\"/>" \
72 " <arg name=\"job\" type=\"o\"/>" \
74 " <signal name=\"JobRemoved\">" \
75 " <arg name=\"id\" type=\"u\"/>" \
76 " <arg name=\"job\" type=\"o\"/>" \
78 " <property name=\"Version\" type=\"s\" access=\"read\"/>" \
79 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>" \
80 " <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>" \
81 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>" \
82 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>" \
83 " <property name=\"NNames\" type=\"u\" access=\"read\"/>" \
84 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>" \
86 BUS_PROPERTIES_INTERFACE \
87 BUS_INTROSPECTABLE_INTERFACE
89 #define INTROSPECTION_END \
92 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as
, manager_running_as
, ManagerRunningAs
);
94 static int bus_manager_append_log_target(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
101 t
= log_target_to_string(log_get_target());
103 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
109 static int bus_manager_append_log_level(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
116 t
= log_level_to_string(log_get_max_level());
118 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
124 static int bus_manager_append_n_names(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
131 u
= hashmap_size(m
->units
);
133 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
139 static int bus_manager_append_n_jobs(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
146 u
= hashmap_size(m
->jobs
);
148 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
154 static DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
157 const BusProperty properties
[] = {
158 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string
, "s", PACKAGE_STRING
},
159 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as
, "s", &m
->running_as
},
160 { "org.freedesktop.systemd1.Manager", "BootTimestamp", bus_property_append_uint64
, "t", &m
->boot_timestamp
},
161 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level
, "s", NULL
},
162 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target
, "s", NULL
},
163 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names
, "u", NULL
},
164 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs
, "u", NULL
},
165 { NULL
, NULL
, NULL
, NULL
, NULL
}
170 DBusMessage
*reply
= NULL
;
177 dbus_error_init(&error
);
179 log_debug("Got D-Bus request: %s.%s() on %s",
180 dbus_message_get_interface(message
),
181 dbus_message_get_member(message
),
182 dbus_message_get_path(message
));
184 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetUnit")) {
188 if (!dbus_message_get_args(
191 DBUS_TYPE_STRING
, &name
,
193 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
195 if (!(u
= manager_get_unit(m
, name
)))
196 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
198 if (!(reply
= dbus_message_new_method_return(message
)))
201 if (!(path
= unit_dbus_path(u
)))
204 if (!dbus_message_append_args(
206 DBUS_TYPE_OBJECT_PATH
, &path
,
210 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
214 if (!dbus_message_get_args(
217 DBUS_TYPE_STRING
, &name
,
219 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
221 if ((r
= manager_load_unit(m
, name
, NULL
, &u
)) < 0)
222 return bus_send_error_reply(m
, message
, NULL
, r
);
224 if (!(reply
= dbus_message_new_method_return(message
)))
227 if (!(path
= unit_dbus_path(u
)))
230 if (!dbus_message_append_args(
232 DBUS_TYPE_OBJECT_PATH
, &path
,
236 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetJob")) {
240 if (!dbus_message_get_args(
243 DBUS_TYPE_UINT32
, &id
,
245 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
247 if (!(j
= manager_get_job(m
, id
)))
248 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
250 if (!(reply
= dbus_message_new_method_return(message
)))
253 if (!(path
= job_dbus_path(j
)))
256 if (!dbus_message_append_args(
258 DBUS_TYPE_OBJECT_PATH
, &path
,
262 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
264 manager_clear_jobs(m
);
266 if (!(reply
= dbus_message_new_method_return(message
)))
269 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListUnits")) {
270 DBusMessageIter iter
, sub
;
275 if (!(reply
= dbus_message_new_method_return(message
)))
278 dbus_message_iter_init_append(reply
, &iter
);
280 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(sssssouso)", &sub
))
283 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
284 char *u_path
, *j_path
;
285 const char *description
, *load_state
, *active_state
, *sub_state
, *job_type
;
286 DBusMessageIter sub2
;
292 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
295 description
= unit_description(u
);
296 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
297 active_state
= unit_active_state_to_string(unit_active_state(u
));
298 sub_state
= unit_sub_state_to_string(u
);
300 if (!(u_path
= unit_dbus_path(u
)))
304 job_id
= (uint32_t) u
->meta
.job
->id
;
306 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
311 job_type
= job_type_to_string(u
->meta
.job
->type
);
318 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &u
->meta
.id
) ||
319 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
320 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
321 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
322 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sub_state
) ||
323 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
324 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
325 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &job_type
) ||
326 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
337 if (!dbus_message_iter_close_container(&sub
, &sub2
))
341 if (!dbus_message_iter_close_container(&iter
, &sub
))
344 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListJobs")) {
345 DBusMessageIter iter
, sub
;
349 if (!(reply
= dbus_message_new_method_return(message
)))
352 dbus_message_iter_init_append(reply
, &iter
);
354 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
357 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
358 char *u_path
, *j_path
;
359 const char *state
, *type
;
361 DBusMessageIter sub2
;
363 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
366 id
= (uint32_t) j
->id
;
367 state
= job_state_to_string(j
->state
);
368 type
= job_type_to_string(j
->type
);
370 if (!(j_path
= job_dbus_path(j
)))
373 if (!(u_path
= unit_dbus_path(j
->unit
))) {
378 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
379 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &j
->unit
->meta
.id
) ||
380 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
381 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
382 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
383 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
392 if (!dbus_message_iter_close_container(&sub
, &sub2
))
396 if (!dbus_message_iter_close_container(&iter
, &sub
))
399 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Subscribe")) {
402 if (!(client
= strdup(dbus_message_get_sender(message
))))
405 r
= set_put(m
->subscribed
, client
);
408 return bus_send_error_reply(m
, message
, NULL
, r
);
410 if (!(reply
= dbus_message_new_method_return(message
)))
413 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
416 if (!(client
= set_remove(m
->subscribed
, (char*) dbus_message_get_sender(message
))))
417 return bus_send_error_reply(m
, message
, NULL
, -ENOENT
);
421 if (!(reply
= dbus_message_new_method_return(message
)))
424 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Dump")) {
429 if (!(reply
= dbus_message_new_method_return(message
)))
432 if (!(f
= open_memstream(&dump
, &size
)))
435 manager_dump_units(m
, f
, NULL
);
436 manager_dump_jobs(m
, f
, NULL
);
446 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &dump
, DBUS_TYPE_INVALID
)) {
452 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
457 if (!dbus_message_get_args(
460 DBUS_TYPE_STRING
, &name
,
461 DBUS_TYPE_BOOLEAN
, &cleanup
,
463 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
465 if (name
&& name
[0] == 0)
468 if ((r
= snapshot_create(m
, name
, cleanup
, &s
)) < 0)
469 return bus_send_error_reply(m
, message
, NULL
, r
);
471 if (!(reply
= dbus_message_new_method_return(message
)))
474 if (!(path
= unit_dbus_path(UNIT(s
))))
477 if (!dbus_message_append_args(
479 DBUS_TYPE_OBJECT_PATH
, &path
,
483 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
484 char *introspection
= NULL
;
492 if (!(reply
= dbus_message_new_method_return(message
)))
495 /* We roll our own introspection code here, instead of
496 * relying on bus_default_message_handler() because we
497 * need to generate our introspection string
500 if (!(f
= open_memstream(&introspection
, &size
)))
503 fputs(INTROSPECTION_BEGIN
, f
);
505 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
511 if (!(p
= bus_path_escape(k
))) {
517 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
521 HASHMAP_FOREACH(j
, m
->jobs
, i
)
522 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
524 fputs(INTROSPECTION_END
, f
);
537 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
544 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reload")) {
546 assert(!m
->queued_message
);
548 /* Instead of sending the reply back right away, we
549 * just remember that we need to and then send it
550 * after the reload is finished. That way the caller
551 * knows when the reload finished. */
553 if (!(m
->queued_message
= dbus_message_new_method_return(message
)))
556 m
->exit_code
= MANAGER_RELOAD
;
558 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reexecute")) {
560 if (!(reply
= dbus_message_new_method_return(message
)))
563 m
->exit_code
= MANAGER_REEXECUTE
;
565 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Exit")) {
567 if (m
->running_as
== MANAGER_INIT
)
568 return bus_send_error_reply(m
, message
, NULL
, -ENOTSUP
);
570 if (!(reply
= dbus_message_new_method_return(message
)))
573 m
->exit_code
= MANAGER_EXIT
;
576 return bus_default_message_handler(m
, message
, NULL
, properties
);
581 if (!dbus_connection_send(connection
, reply
, NULL
))
584 dbus_message_unref(reply
);
587 return DBUS_HANDLER_RESULT_HANDLED
;
593 dbus_message_unref(reply
);
595 dbus_error_free(&error
);
597 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
600 const DBusObjectPathVTable bus_manager_vtable
= {
601 .message_function
= bus_manager_message_handler