chiark / gitweb /
core: introduce new stop protocol for unit scopes
[elogind.git] / src / core / dbus-scope.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include "unit.h"
23 #include "scope.h"
24 #include "dbus-unit.h"
25 #include "dbus-cgroup.h"
26 #include "dbus-kill.h"
27 #include "dbus-scope.h"
28 #include "bus-util.h"
29 #include "bus-internal.h"
30
31 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, scope_result, ScopeResult);
32
33 const sd_bus_vtable bus_scope_vtable[] = {
34         SD_BUS_VTABLE_START(0),
35         SD_BUS_PROPERTY("Controller", "s", NULL, offsetof(Scope, controller), SD_BUS_VTABLE_PROPERTY_CONST),
36         SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec, offsetof(Scope, timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST),
37         SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Scope, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
38         SD_BUS_SIGNAL("RequestStop", NULL, 0),
39         SD_BUS_VTABLE_END
40 };
41
42 static int bus_scope_set_transient_property(
43                 Scope *s,
44                 const char *name,
45                 sd_bus_message *message,
46                 UnitSetPropertiesMode mode,
47                 sd_bus_error *error) {
48
49         int r;
50
51         assert(s);
52         assert(name);
53         assert(message);
54
55         if (streq(name, "PIDs")) {
56                 unsigned n = 0;
57                 uint32_t pid;
58
59                 r = set_ensure_allocated(&s->pids, trivial_hash_func, trivial_compare_func);
60                 if (r < 0)
61                         return r;
62
63                 r = sd_bus_message_enter_container(message, 'a', "u");
64                 if (r < 0)
65                         return r;
66
67                 while ((r = sd_bus_message_read(message, "u", &pid)) > 0) {
68
69                         if (pid <= 1)
70                                 return -EINVAL;
71
72                         if (mode != UNIT_CHECK) {
73                                 r = set_put(s->pids, LONG_TO_PTR(pid));
74                                 if (r < 0 && r != -EEXIST)
75                                         return r;
76                         }
77
78                         n++;
79                 }
80                 if (r < 0)
81                         return r;
82
83                 r = sd_bus_message_exit_container(message);
84                 if (r < 0)
85                         return r;
86
87                 if (n <= 0)
88                         return -EINVAL;
89
90                 return 1;
91
92         } else if (streq(name, "Controller")) {
93                 const char *controller;
94                 char *c;
95
96                 r = sd_bus_message_read(message, "s", &controller);
97                 if (r < 0)
98                         return r;
99
100                 if (!isempty(controller) && !service_name_is_valid(controller))
101                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Controller '%s' is not a valid bus name.", controller);
102
103                 if (mode != UNIT_CHECK) {
104                         if (isempty(controller))
105                                 c = NULL;
106                         else {
107                                 c = strdup(controller);
108                                 if (!c)
109                                         return -ENOMEM;
110                         }
111
112                         free(s->controller);
113                         s->controller = c;
114                 }
115
116                 return 1;
117
118         } else if (streq(name, "TimeoutStopUSec")) {
119
120                 if (mode != UNIT_CHECK) {
121                         r = sd_bus_message_read(message, "t", &s->timeout_stop_usec);
122                         if (r < 0)
123                                 return r;
124
125                         unit_write_drop_in_format(UNIT(s), mode, name, "[Scope]\nTimeoutStopSec=%lluus\n", (unsigned long long) s->timeout_stop_usec);
126                 } else {
127                         r = sd_bus_message_skip(message, "t");
128                         if (r < 0)
129                                 return r;
130                 }
131
132                 return 1;
133         }
134
135         return 0;
136 }
137
138 int bus_scope_set_property(
139                 Unit *u,
140                 const char *name,
141                 sd_bus_message *message,
142                 UnitSetPropertiesMode mode,
143                 sd_bus_error *error) {
144
145         Scope *s = SCOPE(u);
146         int r;
147
148         assert(s);
149         assert(name);
150         assert(message);
151
152         r = bus_cgroup_set_property(u, &s->cgroup_context, name, message, mode, error);
153         if (r != 0)
154                 return r;
155
156         if (u->load_state == UNIT_STUB) {
157                 /* While we are created we still accept PIDs */
158
159                 r = bus_scope_set_transient_property(s, name, message, mode, error);
160                 if (r != 0)
161                         return r;
162
163                 r = bus_kill_context_set_transient_property(u, &s->kill_context, name, message, mode, error);
164                 if (r != 0)
165                         return r;
166         }
167
168         return 0;
169 }
170
171 int bus_scope_commit_properties(Unit *u) {
172         assert(u);
173
174         unit_realize_cgroup(u);
175         return 0;
176 }
177
178 int bus_scope_send_request_stop(Scope *s) {
179         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
180         _cleanup_free_ char *p = NULL;
181         int r;
182
183         assert(s);
184
185         if (!s->controller)
186                 return 0;
187
188         p = unit_dbus_path(UNIT(s));
189         if (!p)
190                 return -ENOMEM;
191
192         r = sd_bus_message_new_signal(
193                         UNIT(s)->manager->api_bus,
194                         p,
195                         "org.freedesktop.systemd1.Scope",
196                         "RequestStop",
197                         &m);
198         if (r < 0)
199                 return r;
200
201         return sd_bus_send_to(UNIT(s)->manager->api_bus, m, /* s->controller */ NULL, NULL);
202 }