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