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"
28 #include "dbus-common.h"
30 const char bus_unit_interface[] _introspect_("Unit") = BUS_UNIT_INTERFACE;
32 #define INVALIDATING_PROPERTIES \
36 "InactiveExitTimestamp\0" \
37 "ActiveEnterTimestamp\0" \
38 "ActiveExitTimestamp\0" \
39 "InactiveEnterTimestamp\0" \
43 int bus_unit_append_names(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(DBusMessageIter *i, const char *property, void *data) {
70 f = unit_following(u);
71 d = f ? f->meta.id : "";
73 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
79 int bus_unit_append_dependencies(DBusMessageIter *i, const char *property, void *data) {
85 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
89 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id))
92 if (!dbus_message_iter_close_container(i, &sub))
98 int bus_unit_append_description(DBusMessageIter *i, const char *property, void *data) {
106 d = unit_description(u);
108 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
114 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
116 int bus_unit_append_active_state(DBusMessageIter *i, const char *property, void *data) {
124 state = unit_active_state_to_string(unit_active_state(u));
126 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
132 int bus_unit_append_sub_state(DBusMessageIter *i, const char *property, void *data) {
140 state = unit_sub_state_to_string(u);
142 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
148 int bus_unit_append_file_state(DBusMessageIter *i, const char *property, void *data) {
156 state = strempty(unit_file_state_to_string(unit_get_unit_file_state(u)));
158 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
164 int bus_unit_append_can_start(DBusMessageIter *i, const char *property, void *data) {
172 b = unit_can_start(u) &&
173 !u->meta.refuse_manual_start;
175 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
181 int bus_unit_append_can_stop(DBusMessageIter *i, const char *property, void *data) {
189 /* On the lower levels we assume that every unit we can start
190 * we can also stop */
192 b = unit_can_start(u) &&
193 !u->meta.refuse_manual_stop;
195 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
201 int bus_unit_append_can_reload(DBusMessageIter *i, const char *property, void *data) {
209 b = unit_can_reload(u);
211 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
217 int bus_unit_append_can_isolate(DBusMessageIter *i, const char *property, void *data) {
225 b = unit_can_isolate(u) &&
226 !u->meta.refuse_manual_start;
228 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
234 int bus_unit_append_job(DBusMessageIter *i, const char *property, void *data) {
243 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
248 if (!(p = job_dbus_path(u->meta.job)))
251 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
252 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
259 /* No job, so let's fill in some placeholder
260 * data. Since we need to fill in a valid path we
261 * simple point to ourselves. */
263 if (!(p = unit_dbus_path(u)))
266 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
267 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
275 if (!dbus_message_iter_close_container(i, &sub))
281 int bus_unit_append_default_cgroup(DBusMessageIter *i, const char *property, void *data) {
291 if ((cgb = unit_get_default_cgroup(u))) {
292 if (!(t = cgroup_bonding_to_string(cgb)))
297 success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
302 return success ? 0 : -ENOMEM;
305 int bus_unit_append_cgroups(DBusMessageIter *i, const char *property, void *data) {
310 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
313 LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
317 if (!(t = cgroup_bonding_to_string(cgb)))
320 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
327 if (!dbus_message_iter_close_container(i, &sub))
333 int bus_unit_append_need_daemon_reload(DBusMessageIter *i, const char *property, void *data) {
341 b = unit_need_daemon_reload(u);
343 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
349 int bus_unit_append_load_error(DBusMessageIter *i, const char *property, void *data) {
351 const char *name, *message;
358 if (u->meta.load_error != 0) {
359 name = bus_errno_to_dbus(u->meta.load_error);
360 message = strempty(strerror(-u->meta.load_error));
364 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub) ||
365 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name) ||
366 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &message) ||
367 !dbus_message_iter_close_container(i, &sub))
373 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
374 DBusMessage *reply = NULL;
375 Manager *m = u->meta.manager;
377 JobType job_type = _JOB_TYPE_INVALID;
379 bool reload_if_possible = false;
381 dbus_error_init(&error);
383 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
384 job_type = JOB_START;
385 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
387 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
388 job_type = JOB_RELOAD;
389 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
390 job_type = JOB_RESTART;
391 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
392 job_type = JOB_TRY_RESTART;
393 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
394 reload_if_possible = true;
395 job_type = JOB_RESTART;
396 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
397 reload_if_possible = true;
398 job_type = JOB_TRY_RESTART;
399 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Kill")) {
400 const char *swho, *smode;
406 if (!dbus_message_get_args(
409 DBUS_TYPE_STRING, &swho,
410 DBUS_TYPE_STRING, &smode,
411 DBUS_TYPE_INT32, &signo,
413 return bus_send_error_reply(connection, message, &error, -EINVAL);
418 who = kill_who_from_string(swho);
420 return bus_send_error_reply(connection, message, &error, -EINVAL);
424 mode = KILL_CONTROL_GROUP;
426 mode = kill_mode_from_string(smode);
428 return bus_send_error_reply(connection, message, &error, -EINVAL);
431 if (signo <= 0 || signo >= _NSIG)
432 return bus_send_error_reply(connection, message, &error, -EINVAL);
434 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
435 return bus_send_error_reply(connection, message, &error, r);
437 if (!(reply = dbus_message_new_method_return(message)))
440 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetFailed")) {
442 unit_reset_failed(u);
444 if (!(reply = dbus_message_new_method_return(message)))
447 } else if (UNIT_VTABLE(u)->bus_message_handler)
448 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
450 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
452 if (job_type != _JOB_TYPE_INVALID) {
458 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
459 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
460 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
461 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
462 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
463 return bus_send_error_reply(connection, message, &error, -EPERM);
466 if (!dbus_message_get_args(
469 DBUS_TYPE_STRING, &smode,
471 return bus_send_error_reply(connection, message, &error, -EINVAL);
473 if (reload_if_possible && unit_can_reload(u)) {
474 if (job_type == JOB_RESTART)
475 job_type = JOB_RELOAD_OR_START;
476 else if (job_type == JOB_TRY_RESTART)
477 job_type = JOB_RELOAD;
480 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
481 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
482 return bus_send_error_reply(connection, message, &error, -EINVAL);
485 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
486 return bus_send_error_reply(connection, message, &error, r);
488 if (!(reply = dbus_message_new_method_return(message)))
491 if (!(path = job_dbus_path(j)))
494 if (!dbus_message_append_args(
496 DBUS_TYPE_OBJECT_PATH, &path,
502 if (!dbus_connection_send(connection, reply, NULL))
505 dbus_message_unref(reply);
510 return DBUS_HANDLER_RESULT_HANDLED;
516 dbus_message_unref(reply);
518 dbus_error_free(&error);
520 return DBUS_HANDLER_RESULT_NEED_MEMORY;
523 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
533 if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/unit")) {
534 /* Be nice to gdbus and return introspection data for our mid-level paths */
536 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
537 char *introspection = NULL;
543 if (!(reply = dbus_message_new_method_return(message)))
546 /* We roll our own introspection code here, instead of
547 * relying on bus_default_message_handler() because we
548 * need to generate our introspection string
551 if (!(f = open_memstream(&introspection, &size)))
554 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
557 fputs(BUS_INTROSPECTABLE_INTERFACE, f);
558 fputs(BUS_PEER_INTERFACE, f);
560 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
566 if (!(p = bus_path_escape(k))) {
572 fprintf(f, "<node name=\"%s\"/>", p);
576 fputs("</node>\n", f);
589 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
596 if (!dbus_connection_send(connection, reply, NULL))
599 dbus_message_unref(reply);
601 return DBUS_HANDLER_RESULT_HANDLED;
604 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
607 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
610 return DBUS_HANDLER_RESULT_NEED_MEMORY;
616 dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown unit");
617 return bus_send_error_reply(connection, message, &e, r);
620 return bus_send_error_reply(connection, message, NULL, r);
623 return bus_unit_message_dispatch(u, connection, message);
627 dbus_message_unref(reply);
629 return DBUS_HANDLER_RESULT_NEED_MEMORY;
632 const DBusObjectPathVTable bus_unit_vtable = {
633 .message_function = bus_unit_message_handler
636 void bus_unit_send_change_signal(Unit *u) {
638 DBusMessage *m = NULL;
642 if (u->meta.in_dbus_queue) {
643 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
644 u->meta.in_dbus_queue = false;
650 if (!bus_has_subscriber(u->meta.manager)) {
651 u->meta.sent_dbus_new_signal = true;
655 if (!(p = unit_dbus_path(u)))
658 if (u->meta.sent_dbus_new_signal) {
659 /* Send a properties changed signal. First for the
660 * specific type, then for the generic unit. The
661 * clients may rely on this order to get atomic
662 * behaviour if needed. */
664 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
666 if (!(m = bus_properties_changed_new(p,
667 UNIT_VTABLE(u)->bus_interface,
668 UNIT_VTABLE(u)->bus_invalidating_properties)))
671 if (bus_broadcast(u->meta.manager, m) < 0)
674 dbus_message_unref(m);
677 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
681 /* Send a new signal */
683 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
686 if (!dbus_message_append_args(m,
687 DBUS_TYPE_STRING, &u->meta.id,
688 DBUS_TYPE_OBJECT_PATH, &p,
693 if (bus_broadcast(u->meta.manager, m) < 0)
697 dbus_message_unref(m);
699 u->meta.sent_dbus_new_signal = true;
707 dbus_message_unref(m);
709 log_error("Failed to allocate unit change/new signal.");
712 void bus_unit_send_removed_signal(Unit *u) {
714 DBusMessage *m = NULL;
718 if (!bus_has_subscriber(u->meta.manager))
721 if (!u->meta.sent_dbus_new_signal)
722 bus_unit_send_change_signal(u);
727 if (!(p = unit_dbus_path(u)))
730 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
733 if (!dbus_message_append_args(m,
734 DBUS_TYPE_STRING, &u->meta.id,
735 DBUS_TYPE_OBJECT_PATH, &p,
739 if (bus_broadcast(u->meta.manager, m) < 0)
743 dbus_message_unref(m);
751 dbus_message_unref(m);
753 log_error("Failed to allocate unit remove signal.");