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