]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
6c12b52e LP |
2 | |
3 | #include <errno.h> | |
6c12b52e LP |
4 | #include <unistd.h> |
5 | ||
b5efdb8a | 6 | #include "alloc-util.h" |
07630cea | 7 | #include "dbus-scope.h" |
6fcbec6f | 8 | #include "dbus-unit.h" |
07630cea | 9 | #include "load-dropin.h" |
6c12b52e | 10 | #include "log.h" |
b5efdb8a | 11 | #include "scope.h" |
d68c645b | 12 | #include "serialize.h" |
6c12b52e | 13 | #include "special.h" |
8b43440b | 14 | #include "string-table.h" |
07630cea LP |
15 | #include "string-util.h" |
16 | #include "strv.h" | |
6c12b52e | 17 | #include "unit-name.h" |
efdb0237 | 18 | #include "unit.h" |
6c12b52e LP |
19 | |
20 | static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = { | |
21 | [SCOPE_DEAD] = UNIT_INACTIVE, | |
22 | [SCOPE_RUNNING] = UNIT_ACTIVE, | |
a911bb9a | 23 | [SCOPE_ABANDONED] = UNIT_ACTIVE, |
6c12b52e LP |
24 | [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
25 | [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
26 | [SCOPE_FAILED] = UNIT_FAILED | |
27 | }; | |
28 | ||
718db961 LP |
29 | static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
30 | ||
6c12b52e LP |
31 | static void scope_init(Unit *u) { |
32 | Scope *s = SCOPE(u); | |
33 | ||
34 | assert(u); | |
35 | assert(u->load_state == UNIT_STUB); | |
36 | ||
1f19a534 | 37 | s->timeout_stop_usec = u->manager->default_timeout_stop_usec; |
1b4cd0cf | 38 | u->ignore_on_isolate = true; |
6c12b52e LP |
39 | } |
40 | ||
41 | static void scope_done(Unit *u) { | |
42 | Scope *s = SCOPE(u); | |
43 | ||
44 | assert(u); | |
45 | ||
371c0b79 LP |
46 | s->controller = mfree(s->controller); |
47 | s->controller_track = sd_bus_track_unref(s->controller_track); | |
2d4a39e7 | 48 | |
718db961 LP |
49 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
50 | } | |
51 | ||
36c16a7c | 52 | static int scope_arm_timer(Scope *s, usec_t usec) { |
718db961 LP |
53 | int r; |
54 | ||
55 | assert(s); | |
56 | ||
718db961 | 57 | if (s->timer_event_source) { |
36c16a7c | 58 | r = sd_event_source_set_time(s->timer_event_source, usec); |
718db961 LP |
59 | if (r < 0) |
60 | return r; | |
61 | ||
62 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
63 | } | |
64 | ||
36c16a7c LP |
65 | if (usec == USEC_INFINITY) |
66 | return 0; | |
67 | ||
cbf60d0a | 68 | r = sd_event_add_time( |
6a0f1f6d LP |
69 | UNIT(s)->manager->event, |
70 | &s->timer_event_source, | |
71 | CLOCK_MONOTONIC, | |
36c16a7c | 72 | usec, 0, |
6a0f1f6d | 73 | scope_dispatch_timer, s); |
7dfbe2e3 TG |
74 | if (r < 0) |
75 | return r; | |
76 | ||
77 | (void) sd_event_source_set_description(s->timer_event_source, "scope-timer"); | |
78 | ||
79 | return 0; | |
6c12b52e LP |
80 | } |
81 | ||
82 | static void scope_set_state(Scope *s, ScopeState state) { | |
83 | ScopeState old_state; | |
84 | assert(s); | |
85 | ||
6fcbec6f LP |
86 | if (s->state != state) |
87 | bus_unit_send_pending_change_signal(UNIT(s), false); | |
88 | ||
6c12b52e LP |
89 | old_state = s->state; |
90 | s->state = state; | |
91 | ||
a911bb9a | 92 | if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
718db961 | 93 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
6c12b52e | 94 | |
50be4f4a | 95 | if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) { |
a911bb9a | 96 | unit_unwatch_all_pids(UNIT(s)); |
50be4f4a LP |
97 | unit_dequeue_rewatch_pids(UNIT(s)); |
98 | } | |
a911bb9a | 99 | |
6c12b52e | 100 | if (state != old_state) |
a911bb9a | 101 | log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state)); |
6c12b52e | 102 | |
2ad2e41a | 103 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0); |
6c12b52e LP |
104 | } |
105 | ||
106 | static int scope_add_default_dependencies(Scope *s) { | |
107 | int r; | |
108 | ||
109 | assert(s); | |
110 | ||
4c9ea260 LP |
111 | if (!UNIT(s)->default_dependencies) |
112 | return 0; | |
113 | ||
6c12b52e LP |
114 | /* Make sure scopes are unloaded on shutdown */ |
115 | r = unit_add_two_dependencies_by_name( | |
116 | UNIT(s), | |
117 | UNIT_BEFORE, UNIT_CONFLICTS, | |
5a724170 | 118 | SPECIAL_SHUTDOWN_TARGET, true, |
eef85c4a | 119 | UNIT_DEPENDENCY_DEFAULT); |
6c12b52e LP |
120 | if (r < 0) |
121 | return r; | |
122 | ||
123 | return 0; | |
124 | } | |
125 | ||
126 | static int scope_verify(Scope *s) { | |
127 | assert(s); | |
75193d41 | 128 | assert(UNIT(s)->load_state == UNIT_LOADED); |
6c12b52e | 129 | |
efdb0237 | 130 | if (set_isempty(UNIT(s)->pids) && |
2c289ea8 | 131 | !MANAGER_IS_RELOADING(UNIT(s)->manager) && |
efdb0237 | 132 | !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) { |
f2341e0a | 133 | log_unit_error(UNIT(s), "Scope has no PIDs. Refusing."); |
6f40aa45 | 134 | return -ENOENT; |
6c12b52e LP |
135 | } |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
8e4e851f LP |
140 | static int scope_load_init_scope(Unit *u) { |
141 | assert(u); | |
142 | ||
143 | if (!unit_has_name(u, SPECIAL_INIT_SCOPE)) | |
144 | return 0; | |
145 | ||
146 | u->transient = true; | |
f5869324 | 147 | u->perpetual = true; |
8e4e851f LP |
148 | |
149 | /* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we | |
150 | * synthesize it here, instead of relying on the unit file on disk. */ | |
151 | ||
152 | u->default_dependencies = false; | |
8e4e851f LP |
153 | |
154 | /* Prettify things, if we can. */ | |
155 | if (!u->description) | |
156 | u->description = strdup("System and Service Manager"); | |
157 | if (!u->documentation) | |
158 | (void) strv_extend(&u->documentation, "man:systemd(1)"); | |
159 | ||
160 | return 1; | |
161 | } | |
162 | ||
75193d41 ZJS |
163 | static int scope_add_extras(Scope *s) { |
164 | int r; | |
165 | ||
166 | r = unit_patch_contexts(UNIT(s)); | |
167 | if (r < 0) | |
168 | return r; | |
169 | ||
170 | r = unit_set_default_slice(UNIT(s)); | |
171 | if (r < 0) | |
172 | return r; | |
173 | ||
174 | return scope_add_default_dependencies(s); | |
175 | } | |
176 | ||
6c12b52e LP |
177 | static int scope_load(Unit *u) { |
178 | Scope *s = SCOPE(u); | |
179 | int r; | |
180 | ||
181 | assert(s); | |
182 | assert(u->load_state == UNIT_STUB); | |
183 | ||
2c289ea8 | 184 | if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) |
4f4afc88 | 185 | /* Refuse to load non-transient scope units, but allow them while reloading. */ |
6c12b52e LP |
186 | return -ENOENT; |
187 | ||
8e4e851f LP |
188 | r = scope_load_init_scope(u); |
189 | if (r < 0) | |
190 | return r; | |
c3620770 ZJS |
191 | |
192 | r = unit_load_fragment_and_dropin(u, false); | |
6c12b52e LP |
193 | if (r < 0) |
194 | return r; | |
195 | ||
75193d41 ZJS |
196 | if (u->load_state != UNIT_LOADED) |
197 | return 0; | |
6c12b52e | 198 | |
75193d41 ZJS |
199 | r = scope_add_extras(s); |
200 | if (r < 0) | |
201 | return r; | |
6c12b52e LP |
202 | |
203 | return scope_verify(s); | |
204 | } | |
205 | ||
be847e82 | 206 | static int scope_coldplug(Unit *u) { |
6c12b52e LP |
207 | Scope *s = SCOPE(u); |
208 | int r; | |
209 | ||
210 | assert(s); | |
211 | assert(s->state == SCOPE_DEAD); | |
212 | ||
36c16a7c LP |
213 | if (s->deserialized_state == s->state) |
214 | return 0; | |
a911bb9a | 215 | |
36c16a7c LP |
216 | if (IN_SET(s->deserialized_state, SCOPE_STOP_SIGKILL, SCOPE_STOP_SIGTERM)) { |
217 | r = scope_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_stop_usec)); | |
218 | if (r < 0) | |
219 | return r; | |
6c12b52e LP |
220 | } |
221 | ||
36c16a7c | 222 | if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED)) |
50be4f4a | 223 | (void) unit_enqueue_rewatch_pids(u); |
36c16a7c | 224 | |
371c0b79 LP |
225 | bus_scope_track_controller(s); |
226 | ||
36c16a7c | 227 | scope_set_state(s, s->deserialized_state); |
6c12b52e LP |
228 | return 0; |
229 | } | |
230 | ||
231 | static void scope_dump(Unit *u, FILE *f, const char *prefix) { | |
232 | Scope *s = SCOPE(u); | |
233 | ||
234 | assert(s); | |
235 | assert(f); | |
236 | ||
237 | fprintf(f, | |
238 | "%sScope State: %s\n" | |
239 | "%sResult: %s\n", | |
240 | prefix, scope_state_to_string(s->state), | |
241 | prefix, scope_result_to_string(s->result)); | |
242 | ||
bc0623df | 243 | cgroup_context_dump(UNIT(s), f, prefix); |
6c12b52e LP |
244 | kill_context_dump(&s->kill_context, f, prefix); |
245 | } | |
246 | ||
247 | static void scope_enter_dead(Scope *s, ScopeResult f) { | |
248 | assert(s); | |
249 | ||
a0fef983 | 250 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
251 | s->result = f; |
252 | ||
aac99f30 | 253 | unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result)); |
6c12b52e LP |
254 | scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD); |
255 | } | |
256 | ||
257 | static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { | |
2d4a39e7 | 258 | bool skip_signal = false; |
6c12b52e LP |
259 | int r; |
260 | ||
261 | assert(s); | |
262 | ||
a0fef983 | 263 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
264 | s->result = f; |
265 | ||
50be4f4a LP |
266 | /* Before sending any signal, make sure we track all members of this cgroup */ |
267 | (void) unit_watch_all_pids(UNIT(s)); | |
268 | ||
269 | /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have | |
270 | * died now */ | |
271 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
a911bb9a | 272 | |
371c0b79 LP |
273 | /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going |
274 | * directly into SIGTERM berserk mode */ | |
2d4a39e7 LP |
275 | if (state == SCOPE_STOP_SIGTERM) |
276 | skip_signal = bus_scope_send_request_stop(s) > 0; | |
277 | ||
59ec09a8 ZJS |
278 | if (skip_signal) |
279 | r = 1; /* wait */ | |
280 | else { | |
2d4a39e7 LP |
281 | r = unit_kill_context( |
282 | UNIT(s), | |
283 | &s->kill_context, | |
3862e809 LP |
284 | state != SCOPE_STOP_SIGTERM ? KILL_KILL : |
285 | s->was_abandoned ? KILL_TERMINATE_AND_LOG : | |
286 | KILL_TERMINATE, | |
2d4a39e7 LP |
287 | -1, -1, false); |
288 | if (r < 0) | |
289 | goto fail; | |
59ec09a8 | 290 | } |
6c12b52e LP |
291 | |
292 | if (r > 0) { | |
36c16a7c | 293 | r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec)); |
718db961 LP |
294 | if (r < 0) |
295 | goto fail; | |
6c12b52e LP |
296 | |
297 | scope_set_state(s, state); | |
ac84d1fb LP |
298 | } else if (state == SCOPE_STOP_SIGTERM) |
299 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS); | |
300 | else | |
6c12b52e LP |
301 | scope_enter_dead(s, SCOPE_SUCCESS); |
302 | ||
303 | return; | |
304 | ||
305 | fail: | |
f2341e0a | 306 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
6c12b52e LP |
307 | |
308 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); | |
309 | } | |
310 | ||
311 | static int scope_start(Unit *u) { | |
312 | Scope *s = SCOPE(u); | |
313 | int r; | |
314 | ||
315 | assert(s); | |
316 | ||
efdb0237 LP |
317 | if (unit_has_name(u, SPECIAL_INIT_SCOPE)) |
318 | return -EPERM; | |
319 | ||
7b617155 LP |
320 | if (s->state == SCOPE_FAILED) |
321 | return -EPERM; | |
322 | ||
dd305ec9 | 323 | /* We can't fulfill this right now, please try again later */ |
3742095b | 324 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
6c12b52e LP |
325 | return -EAGAIN; |
326 | ||
327 | assert(s->state == SCOPE_DEAD); | |
328 | ||
2c289ea8 | 329 | if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) |
6c12b52e LP |
330 | return -ENOENT; |
331 | ||
371c0b79 LP |
332 | (void) bus_scope_track_controller(s); |
333 | ||
4b58153d LP |
334 | r = unit_acquire_invocation_id(u); |
335 | if (r < 0) | |
336 | return r; | |
337 | ||
5ad096b3 | 338 | (void) unit_realize_cgroup(u); |
9b2559a1 | 339 | (void) unit_reset_accounting(u); |
5ad096b3 | 340 | |
01542056 | 341 | unit_export_state_files(u); |
d3070fbd | 342 | |
01542056 | 343 | r = unit_attach_pids_to_cgroup(u, u->pids, NULL); |
dd305ec9 | 344 | if (r < 0) { |
01542056 | 345 | log_unit_warning_errno(u, r, "Failed to add PIDs to scope's control group: %m"); |
68a01fb6 | 346 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); |
6c12b52e | 347 | return r; |
dd305ec9 | 348 | } |
6c12b52e | 349 | |
6c12b52e LP |
350 | s->result = SCOPE_SUCCESS; |
351 | ||
352 | scope_set_state(s, SCOPE_RUNNING); | |
b91ada2a LP |
353 | |
354 | /* Start watching the PIDs currently in the scope */ | |
01542056 | 355 | (void) unit_enqueue_rewatch_pids(u); |
82a2b6bb | 356 | return 1; |
6c12b52e LP |
357 | } |
358 | ||
359 | static int scope_stop(Unit *u) { | |
360 | Scope *s = SCOPE(u); | |
361 | ||
362 | assert(s); | |
6c12b52e | 363 | |
3742095b | 364 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
6c12b52e LP |
365 | return 0; |
366 | ||
3742095b | 367 | assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)); |
6c12b52e LP |
368 | |
369 | scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS); | |
82a2b6bb | 370 | return 1; |
6c12b52e LP |
371 | } |
372 | ||
8bcca7e2 LP |
373 | static void scope_reset_failed(Unit *u) { |
374 | Scope *s = SCOPE(u); | |
375 | ||
376 | assert(s); | |
377 | ||
378 | if (s->state == SCOPE_FAILED) | |
379 | scope_set_state(s, SCOPE_DEAD); | |
380 | ||
381 | s->result = SCOPE_SUCCESS; | |
382 | } | |
383 | ||
718db961 | 384 | static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
6c12b52e LP |
385 | return unit_kill_common(u, who, signo, -1, -1, error); |
386 | } | |
387 | ||
7a7821c8 | 388 | static int scope_get_timeout(Unit *u, usec_t *timeout) { |
68db7a3b | 389 | Scope *s = SCOPE(u); |
7a7821c8 | 390 | usec_t t; |
68db7a3b ZJS |
391 | int r; |
392 | ||
393 | if (!s->timer_event_source) | |
394 | return 0; | |
395 | ||
7a7821c8 | 396 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
397 | if (r < 0) |
398 | return r; | |
7a7821c8 LP |
399 | if (t == USEC_INFINITY) |
400 | return 0; | |
68db7a3b | 401 | |
7a7821c8 | 402 | *timeout = t; |
68db7a3b ZJS |
403 | return 1; |
404 | } | |
405 | ||
6c12b52e LP |
406 | static int scope_serialize(Unit *u, FILE *f, FDSet *fds) { |
407 | Scope *s = SCOPE(u); | |
408 | ||
409 | assert(s); | |
410 | assert(f); | |
411 | assert(fds); | |
412 | ||
d68c645b LP |
413 | (void) serialize_item(f, "state", scope_state_to_string(s->state)); |
414 | (void) serialize_bool(f, "was-abandoned", s->was_abandoned); | |
33fe0afe LP |
415 | |
416 | if (s->controller) | |
d68c645b | 417 | (void) serialize_item(f, "controller", s->controller); |
33fe0afe | 418 | |
6c12b52e LP |
419 | return 0; |
420 | } | |
421 | ||
422 | static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
423 | Scope *s = SCOPE(u); | |
33fe0afe | 424 | int r; |
6c12b52e LP |
425 | |
426 | assert(u); | |
427 | assert(key); | |
428 | assert(value); | |
429 | assert(fds); | |
430 | ||
431 | if (streq(key, "state")) { | |
432 | ScopeState state; | |
433 | ||
434 | state = scope_state_from_string(value); | |
435 | if (state < 0) | |
f2341e0a | 436 | log_unit_debug(u, "Failed to parse state value: %s", value); |
6c12b52e LP |
437 | else |
438 | s->deserialized_state = state; | |
439 | ||
3862e809 LP |
440 | } else if (streq(key, "was-abandoned")) { |
441 | int k; | |
442 | ||
443 | k = parse_boolean(value); | |
444 | if (k < 0) | |
445 | log_unit_debug(u, "Failed to parse boolean value: %s", value); | |
446 | else | |
447 | s->was_abandoned = k; | |
33fe0afe LP |
448 | } else if (streq(key, "controller")) { |
449 | ||
450 | r = free_and_strdup(&s->controller, value); | |
451 | if (r < 0) | |
d68c645b | 452 | return log_oom(); |
33fe0afe | 453 | |
6c12b52e | 454 | } else |
f2341e0a | 455 | log_unit_debug(u, "Unknown serialization key: %s", key); |
6c12b52e LP |
456 | |
457 | return 0; | |
458 | } | |
459 | ||
a911bb9a LP |
460 | static void scope_notify_cgroup_empty_event(Unit *u) { |
461 | Scope *s = SCOPE(u); | |
462 | assert(u); | |
463 | ||
f2341e0a | 464 | log_unit_debug(u, "cgroup is empty"); |
a911bb9a LP |
465 | |
466 | if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) | |
467 | scope_enter_dead(s, SCOPE_SUCCESS); | |
468 | } | |
469 | ||
470 | static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) { | |
11aef522 | 471 | assert(u); |
a911bb9a | 472 | |
11aef522 LP |
473 | /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to |
474 | * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original | |
475 | * process we watched was probably the parent of them, and they are hence now our children. */ | |
a911bb9a | 476 | |
50be4f4a | 477 | (void) unit_enqueue_rewatch_pids(u); |
a911bb9a LP |
478 | } |
479 | ||
718db961 LP |
480 | static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
481 | Scope *s = SCOPE(userdata); | |
6c12b52e LP |
482 | |
483 | assert(s); | |
718db961 | 484 | assert(s->timer_event_source == source); |
6c12b52e LP |
485 | |
486 | switch (s->state) { | |
487 | ||
488 | case SCOPE_STOP_SIGTERM: | |
489 | if (s->kill_context.send_sigkill) { | |
f2341e0a | 490 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); |
6c12b52e LP |
491 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT); |
492 | } else { | |
f2341e0a | 493 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); |
6c12b52e LP |
494 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
495 | } | |
496 | ||
497 | break; | |
498 | ||
499 | case SCOPE_STOP_SIGKILL: | |
f2341e0a | 500 | log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring."); |
6c12b52e LP |
501 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
502 | break; | |
503 | ||
504 | default: | |
505 | assert_not_reached("Timeout at wrong time."); | |
506 | } | |
718db961 LP |
507 | |
508 | return 0; | |
6c12b52e LP |
509 | } |
510 | ||
a911bb9a LP |
511 | int scope_abandon(Scope *s) { |
512 | assert(s); | |
6c12b52e | 513 | |
efdb0237 LP |
514 | if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) |
515 | return -EPERM; | |
516 | ||
a911bb9a LP |
517 | if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)) |
518 | return -ESTALE; | |
6c12b52e | 519 | |
3862e809 | 520 | s->was_abandoned = true; |
371c0b79 | 521 | |
a1e58e8e | 522 | s->controller = mfree(s->controller); |
371c0b79 LP |
523 | s->controller_track = sd_bus_track_unref(s->controller_track); |
524 | ||
8cb83266 | 525 | scope_set_state(s, SCOPE_ABANDONED); |
6c12b52e | 526 | |
50be4f4a LP |
527 | /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that |
528 | * the remaining processes will be sooner or later reassigned to us as parent. */ | |
529 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
6c12b52e | 530 | |
a911bb9a | 531 | return 0; |
6c12b52e LP |
532 | } |
533 | ||
534 | _pure_ static UnitActiveState scope_active_state(Unit *u) { | |
535 | assert(u); | |
536 | ||
537 | return state_translation_table[SCOPE(u)->state]; | |
538 | } | |
539 | ||
540 | _pure_ static const char *scope_sub_state_to_string(Unit *u) { | |
541 | assert(u); | |
542 | ||
543 | return scope_state_to_string(SCOPE(u)->state); | |
544 | } | |
545 | ||
04eb582a | 546 | static void scope_enumerate_perpetual(Manager *m) { |
efdb0237 LP |
547 | Unit *u; |
548 | int r; | |
549 | ||
550 | assert(m); | |
551 | ||
552 | /* Let's unconditionally add the "init.scope" special unit | |
553 | * that encapsulates PID 1. Note that PID 1 already is in the | |
554 | * cgroup for this, we hence just need to allocate the object | |
555 | * for it and that's it. */ | |
556 | ||
557 | u = manager_get_unit(m, SPECIAL_INIT_SCOPE); | |
558 | if (!u) { | |
a581e45a | 559 | r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u); |
efdb0237 | 560 | if (r < 0) { |
a581e45a | 561 | log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m"); |
ba64af90 | 562 | return; |
efdb0237 LP |
563 | } |
564 | } | |
565 | ||
566 | u->transient = true; | |
f5869324 | 567 | u->perpetual = true; |
efdb0237 | 568 | SCOPE(u)->deserialized_state = SCOPE_RUNNING; |
efdb0237 LP |
569 | |
570 | unit_add_to_load_queue(u); | |
571 | unit_add_to_dbus_queue(u); | |
efdb0237 LP |
572 | } |
573 | ||
6c12b52e LP |
574 | static const char* const scope_result_table[_SCOPE_RESULT_MAX] = { |
575 | [SCOPE_SUCCESS] = "success", | |
576 | [SCOPE_FAILURE_RESOURCES] = "resources", | |
577 | [SCOPE_FAILURE_TIMEOUT] = "timeout", | |
578 | }; | |
579 | ||
580 | DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult); | |
581 | ||
582 | const UnitVTable scope_vtable = { | |
583 | .object_size = sizeof(Scope), | |
718db961 LP |
584 | .cgroup_context_offset = offsetof(Scope, cgroup_context), |
585 | .kill_context_offset = offsetof(Scope, kill_context), | |
586 | ||
6c12b52e LP |
587 | .sections = |
588 | "Unit\0" | |
589 | "Scope\0" | |
590 | "Install\0", | |
6c12b52e | 591 | .private_section = "Scope", |
6c12b52e | 592 | |
700e2d63 | 593 | .can_transient = true, |
1d9cc876 | 594 | .can_delegate = true, |
d4fd1cf2 | 595 | .once_only = true, |
6c12b52e LP |
596 | |
597 | .init = scope_init, | |
598 | .load = scope_load, | |
599 | .done = scope_done, | |
600 | ||
601 | .coldplug = scope_coldplug, | |
602 | ||
603 | .dump = scope_dump, | |
604 | ||
605 | .start = scope_start, | |
606 | .stop = scope_stop, | |
607 | ||
608 | .kill = scope_kill, | |
609 | ||
68db7a3b ZJS |
610 | .get_timeout = scope_get_timeout, |
611 | ||
6c12b52e LP |
612 | .serialize = scope_serialize, |
613 | .deserialize_item = scope_deserialize_item, | |
614 | ||
615 | .active_state = scope_active_state, | |
616 | .sub_state_to_string = scope_sub_state_to_string, | |
617 | ||
a911bb9a LP |
618 | .sigchld_event = scope_sigchld_event, |
619 | ||
8bcca7e2 LP |
620 | .reset_failed = scope_reset_failed, |
621 | ||
6c12b52e LP |
622 | .notify_cgroup_empty = scope_notify_cgroup_empty_event, |
623 | ||
718db961 | 624 | .bus_vtable = bus_scope_vtable, |
6c12b52e LP |
625 | .bus_set_property = bus_scope_set_property, |
626 | .bus_commit_properties = bus_scope_commit_properties, | |
627 | ||
04eb582a | 628 | .enumerate_perpetual = scope_enumerate_perpetual, |
6c12b52e | 629 | }; |