1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "dbus-unit.h"
25 #include "dbus-cgroup.h"
26 #include "dbus-kill.h"
27 #include "dbus-scope.h"
29 #include "bus-internal.h"
30 #include "bus-errors.h"
32 static int bus_scope_abandon(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
41 if (sd_bus_error_is_set(error))
45 return sd_bus_error_setf(error, BUS_ERROR_SCOPE_NOT_RUNNING, "Scope %s is not running, cannot abandon.", UNIT(s)->id);
47 return sd_bus_reply_method_return(message, NULL);
50 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult);
52 const sd_bus_vtable bus_scope_vtable[] = {
53 SD_BUS_VTABLE_START(0),
54 SD_BUS_PROPERTY("Controller", "s", NULL, offsetof(Scope, controller), SD_BUS_VTABLE_PROPERTY_CONST),
55 SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec, offsetof(Scope, timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST),
56 SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Scope, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
57 SD_BUS_SIGNAL("RequestStop", NULL, 0),
58 SD_BUS_METHOD("Abandon", NULL, NULL, bus_scope_abandon, 0),
62 static int bus_scope_set_transient_property(
65 sd_bus_message *message,
66 UnitSetPropertiesMode mode,
67 sd_bus_error *error) {
75 if (streq(name, "PIDs")) {
79 r = sd_bus_message_enter_container(message, 'a', "u");
83 while ((r = sd_bus_message_read(message, "u", &pid)) > 0) {
88 if (mode != UNIT_CHECK) {
89 r = unit_watch_pid(UNIT(s), pid);
90 if (r < 0 && r != -EEXIST)
99 r = sd_bus_message_exit_container(message);
108 } else if (streq(name, "Controller")) {
109 const char *controller;
112 r = sd_bus_message_read(message, "s", &controller);
116 if (!isempty(controller) && !service_name_is_valid(controller))
117 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Controller '%s' is not a valid bus name.", controller);
119 if (mode != UNIT_CHECK) {
120 if (isempty(controller))
123 c = strdup(controller);
134 } else if (streq(name, "TimeoutStopUSec")) {
136 if (mode != UNIT_CHECK) {
137 r = sd_bus_message_read(message, "t", &s->timeout_stop_usec);
141 unit_write_drop_in_format(UNIT(s), mode, name, "[Scope]\nTimeoutStopSec=%lluus\n", (unsigned long long) s->timeout_stop_usec);
143 r = sd_bus_message_skip(message, "t");
154 int bus_scope_set_property(
157 sd_bus_message *message,
158 UnitSetPropertiesMode mode,
159 sd_bus_error *error) {
168 r = bus_cgroup_set_property(u, &s->cgroup_context, name, message, mode, error);
172 if (u->load_state == UNIT_STUB) {
173 /* While we are created we still accept PIDs */
175 r = bus_scope_set_transient_property(s, name, message, mode, error);
179 r = bus_kill_context_set_transient_property(u, &s->kill_context, name, message, mode, error);
187 int bus_scope_commit_properties(Unit *u) {
190 unit_update_cgroup_members_masks(u);
191 unit_realize_cgroup(u);
196 int bus_scope_send_request_stop(Scope *s) {
197 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
198 _cleanup_free_ char *p = NULL;
206 p = unit_dbus_path(UNIT(s));
210 r = sd_bus_message_new_signal(
211 UNIT(s)->manager->api_bus,
214 "org.freedesktop.systemd1.Scope",
219 return sd_bus_send_to(UNIT(s)->manager->api_bus, m, /* s->controller */ NULL, NULL);