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", "ResetFailed")) {
372 unit_reset_failed(u);
374 if (!(reply = dbus_message_new_method_return(message)))
377 } else if (UNIT_VTABLE(u)->bus_message_handler)
378 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
380 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
382 if (job_type != _JOB_TYPE_INVALID) {
388 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
389 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
390 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
391 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
392 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
393 return bus_send_error_reply(m, connection, message, &error, -EPERM);
396 if (!dbus_message_get_args(
399 DBUS_TYPE_STRING, &smode,
401 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
403 if (reload_if_possible && unit_can_reload(u)) {
404 if (job_type == JOB_RESTART)
405 job_type = JOB_RELOAD_OR_START;
406 else if (job_type == JOB_TRY_RESTART)
407 job_type = JOB_RELOAD;
410 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
411 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
412 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
415 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
416 return bus_send_error_reply(m, connection, message, &error, r);
418 if (!(reply = dbus_message_new_method_return(message)))
421 if (!(path = job_dbus_path(j)))
424 if (!dbus_message_append_args(
426 DBUS_TYPE_OBJECT_PATH, &path,
434 if (!dbus_connection_send(connection, reply, NULL))
437 dbus_message_unref(reply);
440 return DBUS_HANDLER_RESULT_HANDLED;
446 dbus_message_unref(reply);
448 dbus_error_free(&error);
450 return DBUS_HANDLER_RESULT_NEED_MEMORY;
453 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
463 if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/unit")) {
464 /* Be nice to gdbus and return introspection data for our mid-level paths */
466 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
467 char *introspection = NULL;
473 if (!(reply = dbus_message_new_method_return(message)))
476 /* We roll our own introspection code here, instead of
477 * relying on bus_default_message_handler() because we
478 * need to generate our introspection string
481 if (!(f = open_memstream(&introspection, &size)))
484 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
487 fputs(BUS_INTROSPECTABLE_INTERFACE, f);
488 fputs(BUS_PEER_INTERFACE, f);
490 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
496 if (!(p = bus_path_escape(k))) {
502 fprintf(f, "<node name=\"%s\"/>", p);
506 fputs("</node>\n", f);
519 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
526 if (!dbus_connection_send(connection, reply, NULL))
529 dbus_message_unref(reply);
531 return DBUS_HANDLER_RESULT_HANDLED;
534 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
537 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
540 return DBUS_HANDLER_RESULT_NEED_MEMORY;
543 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
545 return bus_send_error_reply(m, connection, message, NULL, r);
548 return bus_unit_message_dispatch(u, connection, message);
552 dbus_message_unref(reply);
554 return DBUS_HANDLER_RESULT_NEED_MEMORY;
557 const DBusObjectPathVTable bus_unit_vtable = {
558 .message_function = bus_unit_message_handler
561 void bus_unit_send_change_signal(Unit *u) {
563 DBusMessage *m = NULL;
567 if (u->meta.in_dbus_queue) {
568 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
569 u->meta.in_dbus_queue = false;
575 if (!bus_has_subscriber(u->meta.manager)) {
576 u->meta.sent_dbus_new_signal = true;
580 if (!(p = unit_dbus_path(u)))
583 if (u->meta.sent_dbus_new_signal) {
584 /* Send a properties changed signal. First for the
585 * specific type, then for the generic unit. The
586 * clients may rely on this order to get atomic
587 * behaviour if needed. */
589 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
591 if (!(m = bus_properties_changed_new(p,
592 UNIT_VTABLE(u)->bus_interface,
593 UNIT_VTABLE(u)->bus_invalidating_properties)))
596 if (bus_broadcast(u->meta.manager, m) < 0)
599 dbus_message_unref(m);
602 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
606 /* Send a new signal */
608 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
611 if (!dbus_message_append_args(m,
612 DBUS_TYPE_STRING, &u->meta.id,
613 DBUS_TYPE_OBJECT_PATH, &p,
618 if (bus_broadcast(u->meta.manager, m) < 0)
622 dbus_message_unref(m);
624 u->meta.sent_dbus_new_signal = true;
632 dbus_message_unref(m);
634 log_error("Failed to allocate unit change/new signal.");
637 void bus_unit_send_removed_signal(Unit *u) {
639 DBusMessage *m = NULL;
643 if (!bus_has_subscriber(u->meta.manager))
646 if (!u->meta.sent_dbus_new_signal)
647 bus_unit_send_change_signal(u);
652 if (!(p = unit_dbus_path(u)))
655 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
658 if (!dbus_message_append_args(m,
659 DBUS_TYPE_STRING, &u->meta.id,
660 DBUS_TYPE_OBJECT_PATH, &p,
664 if (bus_broadcast(u->meta.manager, m) < 0)
668 dbus_message_unref(m);
676 dbus_message_unref(m);
678 log_error("Failed to allocate unit remove signal.");