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) {
462 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
465 return DBUS_HANDLER_RESULT_NEED_MEMORY;
468 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
470 return bus_send_error_reply(m, connection, message, NULL, r);
473 return bus_unit_message_dispatch(u, connection, message);
476 const DBusObjectPathVTable bus_unit_vtable = {
477 .message_function = bus_unit_message_handler
480 void bus_unit_send_change_signal(Unit *u) {
482 DBusMessage *m = NULL;
486 if (u->meta.in_dbus_queue) {
487 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
488 u->meta.in_dbus_queue = false;
494 if (!bus_has_subscriber(u->meta.manager)) {
495 u->meta.sent_dbus_new_signal = true;
499 if (!(p = unit_dbus_path(u)))
502 if (u->meta.sent_dbus_new_signal) {
503 /* Send a properties changed signal. First for the
504 * specific type, then for the generic unit. The
505 * clients may rely on this order to get atomic
506 * behaviour if needed. */
508 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
510 if (!(m = bus_properties_changed_new(p,
511 UNIT_VTABLE(u)->bus_interface,
512 UNIT_VTABLE(u)->bus_invalidating_properties)))
515 if (bus_broadcast(u->meta.manager, m) < 0)
518 dbus_message_unref(m);
521 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
525 /* Send a new signal */
527 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
530 if (!dbus_message_append_args(m,
531 DBUS_TYPE_STRING, &u->meta.id,
532 DBUS_TYPE_OBJECT_PATH, &p,
537 if (bus_broadcast(u->meta.manager, m) < 0)
541 dbus_message_unref(m);
543 u->meta.sent_dbus_new_signal = true;
551 dbus_message_unref(m);
553 log_error("Failed to allocate unit change/new signal.");
556 void bus_unit_send_removed_signal(Unit *u) {
558 DBusMessage *m = NULL;
562 if (!bus_has_subscriber(u->meta.manager))
565 if (!u->meta.sent_dbus_new_signal)
566 bus_unit_send_change_signal(u);
571 if (!(p = unit_dbus_path(u)))
574 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
577 if (!dbus_message_append_args(m,
578 DBUS_TYPE_STRING, &u->meta.id,
579 DBUS_TYPE_OBJECT_PATH, &p,
583 if (bus_broadcast(u->meta.manager, m) < 0)
587 dbus_message_unref(m);
595 dbus_message_unref(m);
597 log_error("Failed to allocate unit remove signal.");