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