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[] _introspect_("Unit") = BUS_UNIT_INTERFACE;
31 #define INVALIDATING_PROPERTIES \
35 "InactiveExitTimestamp\0" \
36 "ActiveEnterTimestamp\0" \
37 "ActiveExitTimestamp\0" \
38 "InactiveEnterTimestamp\0" \
42 int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
48 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
51 SET_FOREACH(t, u->meta.names, j)
52 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
55 if (!dbus_message_iter_close_container(i, &sub))
61 int bus_unit_append_following(Manager *m, 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(Manager *m, 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(Manager *m, DBusMessageIter *i, const char *property, void *data) {
107 d = unit_description(u);
109 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
115 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
117 int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
126 state = unit_active_state_to_string(unit_active_state(u));
128 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
134 int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
143 state = unit_sub_state_to_string(u);
145 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
151 int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
160 b = unit_can_start(u) &&
161 !u->meta.refuse_manual_start;
163 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
169 int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *property, void *data) {
178 /* On the lower levels we assume that every unit we can start
179 * we can also stop */
181 b = unit_can_start(u) &&
182 !u->meta.refuse_manual_stop;
184 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
190 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
199 b = unit_can_reload(u);
201 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
207 int bus_unit_append_can_isolate(Manager *m, DBusMessageIter *i, const char *property, void *data) {
216 b = unit_can_isolate(u) &&
217 !u->meta.refuse_manual_start;
219 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
225 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
235 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
240 if (!(p = job_dbus_path(u->meta.job)))
243 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
244 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
251 /* No job, so let's fill in some placeholder
252 * data. Since we need to fill in a valid path we
253 * simple point to ourselves. */
255 if (!(p = unit_dbus_path(u)))
258 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
259 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
267 if (!dbus_message_iter_close_container(i, &sub))
273 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
284 if ((cgb = unit_get_default_cgroup(u))) {
285 if (!(t = cgroup_bonding_to_string(cgb)))
290 success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
295 return success ? 0 : -ENOMEM;
298 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
303 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
306 LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
310 if (!(t = cgroup_bonding_to_string(cgb)))
313 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
320 if (!dbus_message_iter_close_container(i, &sub))
326 int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
335 b = unit_need_daemon_reload(u);
337 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
343 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
344 DBusMessage *reply = NULL;
345 Manager *m = u->meta.manager;
347 JobType job_type = _JOB_TYPE_INVALID;
349 bool reload_if_possible = false;
351 dbus_error_init(&error);
353 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
354 job_type = JOB_START;
355 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
357 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
358 job_type = JOB_RELOAD;
359 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
360 job_type = JOB_RESTART;
361 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
362 job_type = JOB_TRY_RESTART;
363 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
364 reload_if_possible = true;
365 job_type = JOB_RESTART;
366 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
367 reload_if_possible = true;
368 job_type = JOB_TRY_RESTART;
369 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Kill")) {
370 const char *swho, *smode;
376 if (!dbus_message_get_args(
379 DBUS_TYPE_STRING, &swho,
380 DBUS_TYPE_STRING, &smode,
381 DBUS_TYPE_INT32, &signo,
383 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
385 if ((mode = kill_mode_from_string(smode)) < 0 ||
386 (who = kill_who_from_string(swho)) < 0 ||
389 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
391 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
392 return bus_send_error_reply(m, connection, message, &error, r);
394 if (!(reply = dbus_message_new_method_return(message)))
397 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetFailed")) {
399 unit_reset_failed(u);
401 if (!(reply = dbus_message_new_method_return(message)))
404 } else if (UNIT_VTABLE(u)->bus_message_handler)
405 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
407 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
409 if (job_type != _JOB_TYPE_INVALID) {
415 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
416 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
417 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
418 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
419 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
420 return bus_send_error_reply(m, connection, message, &error, -EPERM);
423 if (!dbus_message_get_args(
426 DBUS_TYPE_STRING, &smode,
428 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
430 if (reload_if_possible && unit_can_reload(u)) {
431 if (job_type == JOB_RESTART)
432 job_type = JOB_RELOAD_OR_START;
433 else if (job_type == JOB_TRY_RESTART)
434 job_type = JOB_RELOAD;
437 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
438 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
439 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
442 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
443 return bus_send_error_reply(m, connection, message, &error, r);
445 if (!(reply = dbus_message_new_method_return(message)))
448 if (!(path = job_dbus_path(j)))
451 if (!dbus_message_append_args(
453 DBUS_TYPE_OBJECT_PATH, &path,
461 if (!dbus_connection_send(connection, reply, NULL))
464 dbus_message_unref(reply);
467 return DBUS_HANDLER_RESULT_HANDLED;
473 dbus_message_unref(reply);
475 dbus_error_free(&error);
477 return DBUS_HANDLER_RESULT_NEED_MEMORY;
480 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
490 if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/unit")) {
491 /* Be nice to gdbus and return introspection data for our mid-level paths */
493 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
494 char *introspection = NULL;
500 if (!(reply = dbus_message_new_method_return(message)))
503 /* We roll our own introspection code here, instead of
504 * relying on bus_default_message_handler() because we
505 * need to generate our introspection string
508 if (!(f = open_memstream(&introspection, &size)))
511 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
514 fputs(BUS_INTROSPECTABLE_INTERFACE, f);
515 fputs(BUS_PEER_INTERFACE, f);
517 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
523 if (!(p = bus_path_escape(k))) {
529 fprintf(f, "<node name=\"%s\"/>", p);
533 fputs("</node>\n", f);
546 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
553 if (!dbus_connection_send(connection, reply, NULL))
556 dbus_message_unref(reply);
558 return DBUS_HANDLER_RESULT_HANDLED;
561 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
564 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
567 return DBUS_HANDLER_RESULT_NEED_MEMORY;
573 dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown unit");
574 return bus_send_error_reply(m, connection, message, &e, r);
577 return bus_send_error_reply(m, connection, message, NULL, r);
580 return bus_unit_message_dispatch(u, connection, message);
584 dbus_message_unref(reply);
586 return DBUS_HANDLER_RESULT_NEED_MEMORY;
589 const DBusObjectPathVTable bus_unit_vtable = {
590 .message_function = bus_unit_message_handler
593 void bus_unit_send_change_signal(Unit *u) {
595 DBusMessage *m = NULL;
599 if (u->meta.in_dbus_queue) {
600 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
601 u->meta.in_dbus_queue = false;
607 if (!bus_has_subscriber(u->meta.manager)) {
608 u->meta.sent_dbus_new_signal = true;
612 if (!(p = unit_dbus_path(u)))
615 if (u->meta.sent_dbus_new_signal) {
616 /* Send a properties changed signal. First for the
617 * specific type, then for the generic unit. The
618 * clients may rely on this order to get atomic
619 * behaviour if needed. */
621 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
623 if (!(m = bus_properties_changed_new(p,
624 UNIT_VTABLE(u)->bus_interface,
625 UNIT_VTABLE(u)->bus_invalidating_properties)))
628 if (bus_broadcast(u->meta.manager, m) < 0)
631 dbus_message_unref(m);
634 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
638 /* Send a new signal */
640 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
643 if (!dbus_message_append_args(m,
644 DBUS_TYPE_STRING, &u->meta.id,
645 DBUS_TYPE_OBJECT_PATH, &p,
650 if (bus_broadcast(u->meta.manager, m) < 0)
654 dbus_message_unref(m);
656 u->meta.sent_dbus_new_signal = true;
664 dbus_message_unref(m);
666 log_error("Failed to allocate unit change/new signal.");
669 void bus_unit_send_removed_signal(Unit *u) {
671 DBusMessage *m = NULL;
675 if (!bus_has_subscriber(u->meta.manager))
678 if (!u->meta.sent_dbus_new_signal)
679 bus_unit_send_change_signal(u);
684 if (!(p = unit_dbus_path(u)))
687 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
690 if (!dbus_message_append_args(m,
691 DBUS_TYPE_STRING, &u->meta.id,
692 DBUS_TYPE_OBJECT_PATH, &p,
696 if (bus_broadcast(u->meta.manager, m) < 0)
700 dbus_message_unref(m);
708 dbus_message_unref(m);
710 log_error("Failed to allocate unit remove signal.");