1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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-unit.h"
27 #include "bus-errors.h"
29 const char bus_unit_interface[] = BUS_UNIT_INTERFACE;
31 #define INVALIDATING_PROPERTIES \
35 "InactiveExitTimestamp\0" \
36 "ActiveEnterTimestamp\0" \
37 "ActiveExitTimestamp\0" \
38 "InactiveEnterTimestamp\0" \
40 "NeedDaemonReload\0" \
43 int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
49 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
52 SET_FOREACH(t, u->meta.names, j)
53 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
56 if (!dbus_message_iter_close_container(i, &sub))
62 int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *property, void *data) {
71 f = unit_following(u);
72 d = f ? f->meta.id : "";
74 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
80 int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) {
86 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
90 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id))
93 if (!dbus_message_iter_close_container(i, &sub))
99 int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
108 d = unit_description(u);
110 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
116 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
118 int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
127 state = unit_active_state_to_string(unit_active_state(u));
129 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
135 int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
144 state = unit_sub_state_to_string(u);
146 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
152 int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
161 b = unit_can_start(u) &&
162 !u->meta.refuse_manual_start;
164 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
170 int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *property, void *data) {
179 /* On the lower levels we assume that every unit we can start
180 * we can also stop */
182 b = unit_can_start(u) &&
183 !u->meta.refuse_manual_stop;
185 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
191 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
200 b = unit_can_reload(u);
202 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
208 int bus_unit_append_can_isolate(Manager *m, DBusMessageIter *i, const char *property, void *data) {
217 b = unit_can_isolate(u) &&
218 !u->meta.refuse_manual_start;
220 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
226 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
236 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
241 if (!(p = job_dbus_path(u->meta.job)))
244 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
245 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
252 /* No job, so let's fill in some placeholder
253 * data. Since we need to fill in a valid path we
254 * simple point to ourselves. */
256 if (!(p = unit_dbus_path(u)))
259 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
260 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
268 if (!dbus_message_iter_close_container(i, &sub))
274 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
285 if ((cgb = unit_get_default_cgroup(u))) {
286 if (!(t = cgroup_bonding_to_string(cgb)))
291 success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
296 return success ? 0 : -ENOMEM;
299 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
304 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
307 LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
311 if (!(t = cgroup_bonding_to_string(cgb)))
314 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
321 if (!dbus_message_iter_close_container(i, &sub))
327 int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
336 b = unit_need_daemon_reload(u);
338 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
344 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
345 DBusMessage *reply = NULL;
346 Manager *m = u->meta.manager;
348 JobType job_type = _JOB_TYPE_INVALID;
350 bool reload_if_possible = false;
352 dbus_error_init(&error);
354 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
355 job_type = JOB_START;
356 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
358 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
359 job_type = JOB_RELOAD;
360 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
361 job_type = JOB_RESTART;
362 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
363 job_type = JOB_TRY_RESTART;
364 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
365 reload_if_possible = true;
366 job_type = JOB_RESTART;
367 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
368 reload_if_possible = true;
369 job_type = JOB_TRY_RESTART;
370 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Kill")) {
371 const char *swho, *smode;
377 if (!dbus_message_get_args(
380 DBUS_TYPE_STRING, &swho,
381 DBUS_TYPE_STRING, &smode,
382 DBUS_TYPE_INT32, &signo,
384 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
386 if ((mode = kill_mode_from_string(smode)) < 0 ||
387 (who = kill_who_from_string(swho)) < 0 ||
390 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
392 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
393 return bus_send_error_reply(m, connection, message, &error, r);
395 if (!(reply = dbus_message_new_method_return(message)))
398 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetFailed")) {
400 unit_reset_failed(u);
402 if (!(reply = dbus_message_new_method_return(message)))
405 } else if (UNIT_VTABLE(u)->bus_message_handler)
406 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
408 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
410 if (job_type != _JOB_TYPE_INVALID) {
416 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
417 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
418 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
419 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
420 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
421 return bus_send_error_reply(m, connection, message, &error, -EPERM);
424 if (!dbus_message_get_args(
427 DBUS_TYPE_STRING, &smode,
429 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
431 if (reload_if_possible && unit_can_reload(u)) {
432 if (job_type == JOB_RESTART)
433 job_type = JOB_RELOAD_OR_START;
434 else if (job_type == JOB_TRY_RESTART)
435 job_type = JOB_RELOAD;
438 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
439 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
440 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
443 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
444 return bus_send_error_reply(m, connection, message, &error, r);
446 if (!(reply = dbus_message_new_method_return(message)))
449 if (!(path = job_dbus_path(j)))
452 if (!dbus_message_append_args(
454 DBUS_TYPE_OBJECT_PATH, &path,
462 if (!dbus_connection_send(connection, reply, NULL))
465 dbus_message_unref(reply);
468 return DBUS_HANDLER_RESULT_HANDLED;
474 dbus_message_unref(reply);
476 dbus_error_free(&error);
478 return DBUS_HANDLER_RESULT_NEED_MEMORY;
481 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
491 if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/unit")) {
492 /* Be nice to gdbus and return introspection data for our mid-level paths */
494 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
495 char *introspection = NULL;
501 if (!(reply = dbus_message_new_method_return(message)))
504 /* We roll our own introspection code here, instead of
505 * relying on bus_default_message_handler() because we
506 * need to generate our introspection string
509 if (!(f = open_memstream(&introspection, &size)))
512 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
515 fputs(BUS_INTROSPECTABLE_INTERFACE, f);
516 fputs(BUS_PEER_INTERFACE, f);
518 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
524 if (!(p = bus_path_escape(k))) {
530 fprintf(f, "<node name=\"%s\"/>", p);
534 fputs("</node>\n", f);
547 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
554 if (!dbus_connection_send(connection, reply, NULL))
557 dbus_message_unref(reply);
559 return DBUS_HANDLER_RESULT_HANDLED;
562 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
565 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
568 return DBUS_HANDLER_RESULT_NEED_MEMORY;
571 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
573 return bus_send_error_reply(m, connection, message, NULL, r);
576 return bus_unit_message_dispatch(u, connection, message);
580 dbus_message_unref(reply);
582 return DBUS_HANDLER_RESULT_NEED_MEMORY;
585 const DBusObjectPathVTable bus_unit_vtable = {
586 .message_function = bus_unit_message_handler
589 void bus_unit_send_change_signal(Unit *u) {
591 DBusMessage *m = NULL;
595 if (u->meta.in_dbus_queue) {
596 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
597 u->meta.in_dbus_queue = false;
603 if (!bus_has_subscriber(u->meta.manager)) {
604 u->meta.sent_dbus_new_signal = true;
608 if (!(p = unit_dbus_path(u)))
611 if (u->meta.sent_dbus_new_signal) {
612 /* Send a properties changed signal. First for the
613 * specific type, then for the generic unit. The
614 * clients may rely on this order to get atomic
615 * behaviour if needed. */
617 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
619 if (!(m = bus_properties_changed_new(p,
620 UNIT_VTABLE(u)->bus_interface,
621 UNIT_VTABLE(u)->bus_invalidating_properties)))
624 if (bus_broadcast(u->meta.manager, m) < 0)
627 dbus_message_unref(m);
630 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
634 /* Send a new signal */
636 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
639 if (!dbus_message_append_args(m,
640 DBUS_TYPE_STRING, &u->meta.id,
641 DBUS_TYPE_OBJECT_PATH, &p,
646 if (bus_broadcast(u->meta.manager, m) < 0)
650 dbus_message_unref(m);
652 u->meta.sent_dbus_new_signal = true;
660 dbus_message_unref(m);
662 log_error("Failed to allocate unit change/new signal.");
665 void bus_unit_send_removed_signal(Unit *u) {
667 DBusMessage *m = NULL;
671 if (!bus_has_subscriber(u->meta.manager))
674 if (!u->meta.sent_dbus_new_signal)
675 bus_unit_send_change_signal(u);
680 if (!(p = unit_dbus_path(u)))
683 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
686 if (!dbus_message_append_args(m,
687 DBUS_TYPE_STRING, &u->meta.id,
688 DBUS_TYPE_OBJECT_PATH, &p,
692 if (bus_broadcast(u->meta.manager, m) < 0)
696 dbus_message_unref(m);
704 dbus_message_unref(m);
706 log_error("Failed to allocate unit remove signal.");