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"
30 #include "bus-internal.h"
31 #include "bus-errors.h"
33 static int bus_scope_abandon(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
41 r = bus_verify_manage_unit_async(UNIT(s)->manager, message, error);
45 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
48 if (sd_bus_error_is_set(error))
52 return sd_bus_error_setf(error, BUS_ERROR_SCOPE_NOT_RUNNING, "Scope %s is not running, cannot abandon.", UNIT(s)->id);
54 return sd_bus_reply_method_return(message, NULL);
57 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult);
59 const sd_bus_vtable bus_scope_vtable[] = {
60 SD_BUS_VTABLE_START(0),
61 SD_BUS_PROPERTY("Controller", "s", NULL, offsetof(Scope, controller), SD_BUS_VTABLE_PROPERTY_CONST),
62 SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec, offsetof(Scope, timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST),
63 SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Scope, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
64 SD_BUS_SIGNAL("RequestStop", NULL, 0),
65 SD_BUS_METHOD("Abandon", NULL, NULL, bus_scope_abandon, 0),
69 static int bus_scope_set_transient_property(
72 sd_bus_message *message,
73 UnitSetPropertiesMode mode,
74 sd_bus_error *error) {
82 if (streq(name, "PIDs")) {
86 r = sd_bus_message_enter_container(message, 'a', "u");
90 while ((r = sd_bus_message_read(message, "u", &pid)) > 0) {
95 if (mode != UNIT_CHECK) {
96 r = unit_watch_pid(UNIT(s), pid);
97 if (r < 0 && r != -EEXIST)
106 r = sd_bus_message_exit_container(message);
115 } else if (streq(name, "Controller")) {
116 const char *controller;
119 r = sd_bus_message_read(message, "s", &controller);
123 if (!isempty(controller) && !service_name_is_valid(controller))
124 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Controller '%s' is not a valid bus name.", controller);
126 if (mode != UNIT_CHECK) {
127 if (isempty(controller))
130 c = strdup(controller);
141 } else if (streq(name, "TimeoutStopUSec")) {
143 if (mode != UNIT_CHECK) {
144 r = sd_bus_message_read(message, "t", &s->timeout_stop_usec);
148 unit_write_drop_in_format(UNIT(s), mode, name, "[Scope]\nTimeoutStopSec="USEC_FMT"us\n", s->timeout_stop_usec);
150 r = sd_bus_message_skip(message, "t");
161 int bus_scope_set_property(
164 sd_bus_message *message,
165 UnitSetPropertiesMode mode,
166 sd_bus_error *error) {
175 r = bus_cgroup_set_property(u, &s->cgroup_context, name, message, mode, error);
179 if (u->load_state == UNIT_STUB) {
180 /* While we are created we still accept PIDs */
182 r = bus_scope_set_transient_property(s, name, message, mode, error);
186 r = bus_kill_context_set_transient_property(u, &s->kill_context, name, message, mode, error);
194 int bus_scope_commit_properties(Unit *u) {
197 unit_update_cgroup_members_masks(u);
198 unit_realize_cgroup(u);
203 int bus_scope_send_request_stop(Scope *s) {
204 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
205 _cleanup_free_ char *p = NULL;
213 p = unit_dbus_path(UNIT(s));
217 r = sd_bus_message_new_signal(
218 UNIT(s)->manager->api_bus,
221 "org.freedesktop.systemd1.Scope",
226 return sd_bus_send_to(UNIT(s)->manager->api_bus, m, /* s->controller */ NULL, NULL);