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"
29 #define BUS_MANAGER_INTERFACE \
30 " <interface name=\"org.freedesktop.systemd1.Manager\">\n" \
31 " <method name=\"GetUnit\">\n" \
32 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
33 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
35 " <method name=\"LoadUnit\">\n" \
36 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
37 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
39 " <method name=\"StartUnit\">\n" \
40 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
41 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
42 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
44 " <method name=\"StopUnit\">\n" \
45 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
46 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
47 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
49 " <method name=\"ReloadUnit\">\n" \
50 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
51 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
52 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
54 " <method name=\"RestartUnit\">\n" \
55 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
59 " <method name=\"GetJob\">\n" \
60 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
61 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
63 " <method name=\"ClearJobs\"/>\n" \
64 " <method name=\"ListUnits\">\n" \
65 " <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>\n" \
67 " <method name=\"ListJobs\">\n" \
68 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
70 " <method name=\"Subscribe\"/>\n" \
71 " <method name=\"Unsubscribe\"/>\n" \
72 " <method name=\"Dump\"/>\n" \
73 " <method name=\"CreateSnapshot\">\n" \
74 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
75 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
76 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
78 " <method name=\"Reload\"/>\n" \
79 " <method name=\"Reexecute\"/>\n" \
80 " <method name=\"Exit\"/>\n" \
81 " <method name=\"SetEnvironment\">\n" \
82 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
84 " <method name=\"UnsetEnvironment\">\n" \
85 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
87 " <signal name=\"UnitNew\">\n" \
88 " <arg name=\"id\" type=\"s\"/>\n" \
89 " <arg name=\"unit\" type=\"o\"/>\n" \
91 " <signal name=\"UnitRemoved\">\n" \
92 " <arg name=\"id\" type=\"s\"/>\n" \
93 " <arg name=\"unit\" type=\"o\"/>\n" \
95 " <signal name=\"JobNew\">\n" \
96 " <arg name=\"id\" type=\"u\"/>\n" \
97 " <arg name=\"job\" type=\"o\"/>\n" \
99 " <signal name=\"JobRemoved\">\n" \
100 " <arg name=\"id\" type=\"u\"/>\n" \
101 " <arg name=\"job\" type=\"o\"/>\n" \
102 " <arg name=\"success\" type=\"b\"/>\n" \
104 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
105 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
106 " <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>\n" \
107 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
108 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
109 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
110 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
111 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
114 #define INTROSPECTION_BEGIN \
115 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
117 BUS_MANAGER_INTERFACE \
118 BUS_PROPERTIES_INTERFACE \
119 BUS_INTROSPECTABLE_INTERFACE
121 #define INTROSPECTION_END \
124 const char bus_manager_interface
[] = BUS_MANAGER_INTERFACE
;
126 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as
, manager_running_as
, ManagerRunningAs
);
128 static int bus_manager_append_log_target(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
135 t
= log_target_to_string(log_get_target());
137 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
143 static int bus_manager_append_log_level(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
150 t
= log_level_to_string(log_get_max_level());
152 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
158 static int bus_manager_append_n_names(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
165 u
= hashmap_size(m
->units
);
167 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
173 static int bus_manager_append_n_jobs(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
180 u
= hashmap_size(m
->jobs
);
182 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, &u
))
188 static DBusHandlerResult
bus_manager_message_handler(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
191 const BusProperty properties
[] = {
192 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string
, "s", PACKAGE_STRING
},
193 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as
, "s", &m
->running_as
},
194 { "org.freedesktop.systemd1.Manager", "BootTimestamp", bus_property_append_uint64
, "t", &m
->startup_timestamp
.realtime
},
195 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level
, "s", NULL
},
196 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target
, "s", NULL
},
197 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names
, "u", NULL
},
198 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs
, "u", NULL
},
199 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv
, "as", m
->environment
},
200 { NULL
, NULL
, NULL
, NULL
, NULL
}
205 DBusMessage
*reply
= NULL
;
207 JobType job_type
= _JOB_TYPE_INVALID
;
213 dbus_error_init(&error
);
215 log_debug("Got D-Bus request: %s.%s() on %s",
216 dbus_message_get_interface(message
),
217 dbus_message_get_member(message
),
218 dbus_message_get_path(message
));
220 if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetUnit")) {
224 if (!dbus_message_get_args(
227 DBUS_TYPE_STRING
, &name
,
229 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
231 if (!(u
= manager_get_unit(m
, name
)))
232 return bus_send_error_reply(m
, connection
, message
, NULL
, -ENOENT
);
234 if (!(reply
= dbus_message_new_method_return(message
)))
237 if (!(path
= unit_dbus_path(u
)))
240 if (!dbus_message_append_args(
242 DBUS_TYPE_OBJECT_PATH
, &path
,
246 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
250 if (!dbus_message_get_args(
253 DBUS_TYPE_STRING
, &name
,
255 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
257 if ((r
= manager_load_unit(m
, name
, NULL
, &u
)) < 0)
258 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
260 if (!(reply
= dbus_message_new_method_return(message
)))
263 if (!(path
= unit_dbus_path(u
)))
266 if (!dbus_message_append_args(
268 DBUS_TYPE_OBJECT_PATH
, &path
,
272 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StartUnit"))
273 job_type
= JOB_START
;
274 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "StopUnit"))
276 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
277 job_type
= JOB_RELOAD
;
278 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "RestartUnit"))
279 job_type
= JOB_RESTART
;
280 else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "GetJob")) {
284 if (!dbus_message_get_args(
287 DBUS_TYPE_UINT32
, &id
,
289 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
291 if (!(j
= manager_get_job(m
, id
)))
292 return bus_send_error_reply(m
, connection
, message
, NULL
, -ENOENT
);
294 if (!(reply
= dbus_message_new_method_return(message
)))
297 if (!(path
= job_dbus_path(j
)))
300 if (!dbus_message_append_args(
302 DBUS_TYPE_OBJECT_PATH
, &path
,
306 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
308 manager_clear_jobs(m
);
310 if (!(reply
= dbus_message_new_method_return(message
)))
313 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListUnits")) {
314 DBusMessageIter iter
, sub
;
319 if (!(reply
= dbus_message_new_method_return(message
)))
322 dbus_message_iter_init_append(reply
, &iter
);
324 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(sssssouso)", &sub
))
327 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
328 char *u_path
, *j_path
;
329 const char *description
, *load_state
, *active_state
, *sub_state
, *sjob_type
;
330 DBusMessageIter sub2
;
336 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
339 description
= unit_description(u
);
340 load_state
= unit_load_state_to_string(u
->meta
.load_state
);
341 active_state
= unit_active_state_to_string(unit_active_state(u
));
342 sub_state
= unit_sub_state_to_string(u
);
344 if (!(u_path
= unit_dbus_path(u
)))
348 job_id
= (uint32_t) u
->meta
.job
->id
;
350 if (!(j_path
= job_dbus_path(u
->meta
.job
))) {
355 sjob_type
= job_type_to_string(u
->meta
.job
->type
);
362 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &u
->meta
.id
) ||
363 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &description
) ||
364 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &load_state
) ||
365 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &active_state
) ||
366 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sub_state
) ||
367 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
) ||
368 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &job_id
) ||
369 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &sjob_type
) ||
370 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
)) {
381 if (!dbus_message_iter_close_container(&sub
, &sub2
))
385 if (!dbus_message_iter_close_container(&iter
, &sub
))
388 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "ListJobs")) {
389 DBusMessageIter iter
, sub
;
393 if (!(reply
= dbus_message_new_method_return(message
)))
396 dbus_message_iter_init_append(reply
, &iter
);
398 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "(usssoo)", &sub
))
401 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
402 char *u_path
, *j_path
;
403 const char *state
, *type
;
405 DBusMessageIter sub2
;
407 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_STRUCT
, NULL
, &sub2
))
410 id
= (uint32_t) j
->id
;
411 state
= job_state_to_string(j
->state
);
412 type
= job_type_to_string(j
->type
);
414 if (!(j_path
= job_dbus_path(j
)))
417 if (!(u_path
= unit_dbus_path(j
->unit
))) {
422 if (!dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_UINT32
, &id
) ||
423 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &j
->unit
->meta
.id
) ||
424 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &type
) ||
425 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &state
) ||
426 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &j_path
) ||
427 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_OBJECT_PATH
, &u_path
)) {
436 if (!dbus_message_iter_close_container(&sub
, &sub2
))
440 if (!dbus_message_iter_close_container(&iter
, &sub
))
443 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Subscribe")) {
446 if (!(client
= strdup(dbus_message_get_sender(message
))))
449 r
= set_put(m
->subscribed
, client
);
452 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
454 if (!(reply
= dbus_message_new_method_return(message
)))
457 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
460 if (!(client
= set_remove(m
->subscribed
, (char*) dbus_message_get_sender(message
))))
461 return bus_send_error_reply(m
, connection
, message
, NULL
, -ENOENT
);
465 if (!(reply
= dbus_message_new_method_return(message
)))
468 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Dump")) {
473 if (!(reply
= dbus_message_new_method_return(message
)))
476 if (!(f
= open_memstream(&dump
, &size
)))
479 manager_dump_units(m
, f
, NULL
);
480 manager_dump_jobs(m
, f
, NULL
);
490 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &dump
, DBUS_TYPE_INVALID
)) {
496 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
501 if (!dbus_message_get_args(
504 DBUS_TYPE_STRING
, &name
,
505 DBUS_TYPE_BOOLEAN
, &cleanup
,
507 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
509 if (name
&& name
[0] == 0)
512 if ((r
= snapshot_create(m
, name
, cleanup
, &s
)) < 0)
513 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
515 if (!(reply
= dbus_message_new_method_return(message
)))
518 if (!(path
= unit_dbus_path(UNIT(s
))))
521 if (!dbus_message_append_args(
523 DBUS_TYPE_OBJECT_PATH
, &path
,
527 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
528 char *introspection
= NULL
;
536 if (!(reply
= dbus_message_new_method_return(message
)))
539 /* We roll our own introspection code here, instead of
540 * relying on bus_default_message_handler() because we
541 * need to generate our introspection string
544 if (!(f
= open_memstream(&introspection
, &size
)))
547 fputs(INTROSPECTION_BEGIN
, f
);
549 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
555 if (!(p
= bus_path_escape(k
))) {
561 fprintf(f
, "<node name=\"unit/%s\"/>", p
);
565 HASHMAP_FOREACH(j
, m
->jobs
, i
)
566 fprintf(f
, "<node name=\"job/%lu\"/>", (unsigned long) j
->id
);
568 fputs(INTROSPECTION_END
, f
);
581 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
)) {
588 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reload")) {
590 assert(!m
->queued_message
);
592 /* Instead of sending the reply back right away, we
593 * just remember that we need to and then send it
594 * after the reload is finished. That way the caller
595 * knows when the reload finished. */
597 if (!(m
->queued_message
= dbus_message_new_method_return(message
)))
600 m
->exit_code
= MANAGER_RELOAD
;
602 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Reexecute")) {
604 if (!(reply
= dbus_message_new_method_return(message
)))
607 m
->exit_code
= MANAGER_REEXECUTE
;
609 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "Exit")) {
611 if (m
->running_as
== MANAGER_INIT
)
612 return bus_send_error_reply(m
, connection
, message
, NULL
, -ENOTSUP
);
614 if (!(reply
= dbus_message_new_method_return(message
)))
617 m
->exit_code
= MANAGER_EXIT
;
619 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
620 char **l
= NULL
, **e
= NULL
;
622 if ((r
= bus_parse_strv(message
, &l
)) < 0) {
626 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
629 e
= strv_env_merge(2, m
->environment
, l
);
635 if (!(reply
= dbus_message_new_method_return(message
))) {
640 strv_free(m
->environment
);
643 } else if (dbus_message_is_method_call(message
, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
644 char **l
= NULL
, **e
= NULL
;
646 if ((r
= bus_parse_strv(message
, &l
)) < 0) {
650 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
653 e
= strv_env_delete(m
->environment
, 1, l
);
659 if (!(reply
= dbus_message_new_method_return(message
)))
662 strv_free(m
->environment
);
666 return bus_default_message_handler(m
, connection
, message
, NULL
, properties
);
669 if (job_type
!= _JOB_TYPE_INVALID
) {
670 const char *name
, *smode
;
675 if (!dbus_message_get_args(
678 DBUS_TYPE_STRING
, &name
,
679 DBUS_TYPE_STRING
, &smode
,
681 return bus_send_error_reply(m
, connection
, message
, &error
, -EINVAL
);
683 if ((mode
= job_mode_from_string(smode
)) == _JOB_MODE_INVALID
)
684 return bus_send_error_reply(m
, connection
, message
, NULL
, -EINVAL
);
686 if ((r
= manager_load_unit(m
, name
, NULL
, &u
)) < 0)
687 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
689 if (job_type
== JOB_START
&& u
->meta
.only_by_dependency
)
690 return bus_send_error_reply(m
, connection
, message
, NULL
, -EPERM
);
692 if ((r
= manager_add_job(m
, job_type
, u
, mode
, true, &j
)) < 0)
693 return bus_send_error_reply(m
, connection
, message
, NULL
, r
);
695 if (!(reply
= dbus_message_new_method_return(message
)))
698 if (!(path
= job_dbus_path(j
)))
701 if (!dbus_message_append_args(
703 DBUS_TYPE_OBJECT_PATH
, &path
,
711 if (!dbus_connection_send(connection
, reply
, NULL
))
714 dbus_message_unref(reply
);
717 return DBUS_HANDLER_RESULT_HANDLED
;
723 dbus_message_unref(reply
);
725 dbus_error_free(&error
);
727 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
730 const DBusObjectPathVTable bus_manager_vtable
= {
731 .message_function
= bus_manager_message_handler