1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
37 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
40 SET_FOREACH(t, u->meta.names, j)
41 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
44 if (!dbus_message_iter_close_container(i, &sub))
50 int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *property, void *data) {
59 f = unit_following(u);
60 d = f ? f->meta.id : "";
62 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
68 int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) {
74 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
78 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id))
81 if (!dbus_message_iter_close_container(i, &sub))
87 int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
96 d = unit_description(u);
98 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
104 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
106 int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
115 state = unit_active_state_to_string(unit_active_state(u));
117 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
123 int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
132 state = unit_sub_state_to_string(u);
134 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
140 int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
149 b = unit_can_start(u) &&
150 !u->meta.refuse_manual_start;
152 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
158 int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *property, void *data) {
167 /* On the lower levels we assume that every unit we can start
168 * we can also stop */
170 b = unit_can_start(u) &&
171 !u->meta.refuse_manual_stop;
173 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
179 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
188 b = unit_can_reload(u);
190 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
196 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
206 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
211 if (!(p = job_dbus_path(u->meta.job)))
214 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
215 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
222 /* No job, so let's fill in some placeholder
223 * data. Since we need to fill in a valid path we
224 * simple point to ourselves. */
226 if (!(p = unit_dbus_path(u)))
229 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
230 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
238 if (!dbus_message_iter_close_container(i, &sub))
244 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
255 if ((cgb = unit_get_default_cgroup(u))) {
256 if (!(t = cgroup_bonding_to_string(cgb)))
261 success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
266 return success ? 0 : -ENOMEM;
269 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
274 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
277 LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
281 if (!(t = cgroup_bonding_to_string(cgb)))
284 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
291 if (!dbus_message_iter_close_container(i, &sub))
297 int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
306 b = unit_need_daemon_reload(u);
308 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
314 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
315 DBusMessage *reply = NULL;
316 Manager *m = u->meta.manager;
318 JobType job_type = _JOB_TYPE_INVALID;
320 bool reload_if_possible = false;
322 dbus_error_init(&error);
324 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
325 job_type = JOB_START;
326 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
328 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
329 job_type = JOB_RELOAD;
330 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
331 job_type = JOB_RESTART;
332 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
333 job_type = JOB_TRY_RESTART;
334 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
335 reload_if_possible = true;
336 job_type = JOB_RESTART;
337 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
338 reload_if_possible = true;
339 job_type = JOB_TRY_RESTART;
340 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetMaintenance")) {
342 unit_reset_maintenance(u);
344 if (!(reply = dbus_message_new_method_return(message)))
347 } else if (UNIT_VTABLE(u)->bus_message_handler)
348 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
350 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
352 if (job_type != _JOB_TYPE_INVALID) {
358 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
359 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
360 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
361 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
362 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
363 return bus_send_error_reply(m, connection, message, &error, -EPERM);
366 if (!dbus_message_get_args(
369 DBUS_TYPE_STRING, &smode,
371 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
373 if (reload_if_possible && unit_can_reload(u)) {
374 if (job_type == JOB_RESTART)
375 job_type = JOB_RELOAD_OR_START;
376 else if (job_type == JOB_TRY_RESTART)
377 job_type = JOB_RELOAD;
380 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
381 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
382 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
385 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
386 return bus_send_error_reply(m, connection, message, &error, r);
388 if (!(reply = dbus_message_new_method_return(message)))
391 if (!(path = job_dbus_path(j)))
394 if (!dbus_message_append_args(
396 DBUS_TYPE_OBJECT_PATH, &path,
404 if (!dbus_connection_send(connection, reply, NULL))
407 dbus_message_unref(reply);
410 return DBUS_HANDLER_RESULT_HANDLED;
416 dbus_message_unref(reply);
418 dbus_error_free(&error);
420 return DBUS_HANDLER_RESULT_NEED_MEMORY;
423 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
432 if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
435 return DBUS_HANDLER_RESULT_NEED_MEMORY;
438 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
440 return bus_send_error_reply(m, connection, message, NULL, r);
443 return bus_unit_message_dispatch(u, connection, message);
446 const DBusObjectPathVTable bus_unit_vtable = {
447 .message_function = bus_unit_message_handler
450 void bus_unit_send_change_signal(Unit *u) {
452 DBusMessage *m = NULL;
456 if (u->meta.in_dbus_queue) {
457 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
458 u->meta.in_dbus_queue = false;
464 if (!bus_has_subscriber(u->meta.manager)) {
465 u->meta.sent_dbus_new_signal = true;
469 if (!(p = unit_dbus_path(u)))
472 if (u->meta.sent_dbus_new_signal) {
473 /* Send a change signal */
475 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
478 /* Send a new signal */
480 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
483 if (!dbus_message_append_args(m,
484 DBUS_TYPE_STRING, &u->meta.id,
485 DBUS_TYPE_OBJECT_PATH, &p,
490 if (bus_broadcast(u->meta.manager, m) < 0)
494 dbus_message_unref(m);
496 u->meta.sent_dbus_new_signal = true;
504 dbus_message_unref(m);
506 log_error("Failed to allocate unit change/new signal.");
509 void bus_unit_send_removed_signal(Unit *u) {
511 DBusMessage *m = NULL;
515 if (!bus_has_subscriber(u->meta.manager))
518 if (!u->meta.sent_dbus_new_signal)
519 bus_unit_send_change_signal(u);
524 if (!(p = unit_dbus_path(u)))
527 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
530 if (!dbus_message_append_args(m,
531 DBUS_TYPE_STRING, &u->meta.id,
532 DBUS_TYPE_OBJECT_PATH, &p,
536 if (bus_broadcast(u->meta.manager, m) < 0)
540 dbus_message_unref(m);
548 dbus_message_unref(m);
550 log_error("Failed to allocate unit remove signal.");