]>
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 | ||
aac99f30 | 243 | unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result)); |
6c12b52e LP |
244 | scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD); |
245 | } | |
246 | ||
247 | static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { | |
2d4a39e7 | 248 | bool skip_signal = false; |
6c12b52e LP |
249 | int r; |
250 | ||
251 | assert(s); | |
252 | ||
a0fef983 | 253 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
254 | s->result = f; |
255 | ||
50be4f4a LP |
256 | /* Before sending any signal, make sure we track all members of this cgroup */ |
257 | (void) unit_watch_all_pids(UNIT(s)); | |
258 | ||
259 | /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have | |
260 | * died now */ | |
261 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
a911bb9a | 262 | |
371c0b79 LP |
263 | /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going |
264 | * directly into SIGTERM berserk mode */ | |
2d4a39e7 LP |
265 | if (state == SCOPE_STOP_SIGTERM) |
266 | skip_signal = bus_scope_send_request_stop(s) > 0; | |
267 | ||
59ec09a8 ZJS |
268 | if (skip_signal) |
269 | r = 1; /* wait */ | |
270 | else { | |
2d4a39e7 LP |
271 | r = unit_kill_context( |
272 | UNIT(s), | |
273 | &s->kill_context, | |
3862e809 LP |
274 | state != SCOPE_STOP_SIGTERM ? KILL_KILL : |
275 | s->was_abandoned ? KILL_TERMINATE_AND_LOG : | |
276 | KILL_TERMINATE, | |
2d4a39e7 LP |
277 | -1, -1, false); |
278 | if (r < 0) | |
279 | goto fail; | |
59ec09a8 | 280 | } |
6c12b52e LP |
281 | |
282 | if (r > 0) { | |
36c16a7c | 283 | r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec)); |
718db961 LP |
284 | if (r < 0) |
285 | goto fail; | |
6c12b52e LP |
286 | |
287 | scope_set_state(s, state); | |
ac84d1fb LP |
288 | } else if (state == SCOPE_STOP_SIGTERM) |
289 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS); | |
290 | else | |
6c12b52e LP |
291 | scope_enter_dead(s, SCOPE_SUCCESS); |
292 | ||
293 | return; | |
294 | ||
295 | fail: | |
f2341e0a | 296 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
6c12b52e LP |
297 | |
298 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); | |
299 | } | |
300 | ||
301 | static int scope_start(Unit *u) { | |
302 | Scope *s = SCOPE(u); | |
303 | int r; | |
304 | ||
305 | assert(s); | |
306 | ||
efdb0237 LP |
307 | if (unit_has_name(u, SPECIAL_INIT_SCOPE)) |
308 | return -EPERM; | |
309 | ||
7b617155 LP |
310 | if (s->state == SCOPE_FAILED) |
311 | return -EPERM; | |
312 | ||
dd305ec9 | 313 | /* We can't fulfill this right now, please try again later */ |
3742095b | 314 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
6c12b52e LP |
315 | return -EAGAIN; |
316 | ||
317 | assert(s->state == SCOPE_DEAD); | |
318 | ||
2c289ea8 | 319 | if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) |
6c12b52e LP |
320 | return -ENOENT; |
321 | ||
371c0b79 LP |
322 | (void) bus_scope_track_controller(s); |
323 | ||
4b58153d LP |
324 | r = unit_acquire_invocation_id(u); |
325 | if (r < 0) | |
326 | return r; | |
327 | ||
5ad096b3 | 328 | (void) unit_realize_cgroup(u); |
906c06f6 DM |
329 | (void) unit_reset_cpu_accounting(u); |
330 | (void) unit_reset_ip_accounting(u); | |
5ad096b3 | 331 | |
d3070fbd LP |
332 | unit_export_state_files(UNIT(s)); |
333 | ||
6592b975 | 334 | r = unit_attach_pids_to_cgroup(u, UNIT(s)->pids, NULL); |
dd305ec9 | 335 | if (r < 0) { |
f2341e0a | 336 | log_unit_warning_errno(UNIT(s), r, "Failed to add PIDs to scope's control group: %m"); |
68a01fb6 | 337 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); |
6c12b52e | 338 | return r; |
dd305ec9 | 339 | } |
6c12b52e | 340 | |
6c12b52e LP |
341 | s->result = SCOPE_SUCCESS; |
342 | ||
343 | scope_set_state(s, SCOPE_RUNNING); | |
b91ada2a LP |
344 | |
345 | /* Start watching the PIDs currently in the scope */ | |
346 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
82a2b6bb | 347 | return 1; |
6c12b52e LP |
348 | } |
349 | ||
350 | static int scope_stop(Unit *u) { | |
351 | Scope *s = SCOPE(u); | |
352 | ||
353 | assert(s); | |
6c12b52e | 354 | |
3742095b | 355 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
6c12b52e LP |
356 | return 0; |
357 | ||
3742095b | 358 | assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)); |
6c12b52e LP |
359 | |
360 | scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS); | |
82a2b6bb | 361 | return 1; |
6c12b52e LP |
362 | } |
363 | ||
8bcca7e2 LP |
364 | static void scope_reset_failed(Unit *u) { |
365 | Scope *s = SCOPE(u); | |
366 | ||
367 | assert(s); | |
368 | ||
369 | if (s->state == SCOPE_FAILED) | |
370 | scope_set_state(s, SCOPE_DEAD); | |
371 | ||
372 | s->result = SCOPE_SUCCESS; | |
373 | } | |
374 | ||
718db961 | 375 | static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
6c12b52e LP |
376 | return unit_kill_common(u, who, signo, -1, -1, error); |
377 | } | |
378 | ||
7a7821c8 | 379 | static int scope_get_timeout(Unit *u, usec_t *timeout) { |
68db7a3b | 380 | Scope *s = SCOPE(u); |
7a7821c8 | 381 | usec_t t; |
68db7a3b ZJS |
382 | int r; |
383 | ||
384 | if (!s->timer_event_source) | |
385 | return 0; | |
386 | ||
7a7821c8 | 387 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
388 | if (r < 0) |
389 | return r; | |
7a7821c8 LP |
390 | if (t == USEC_INFINITY) |
391 | return 0; | |
68db7a3b | 392 | |
7a7821c8 | 393 | *timeout = t; |
68db7a3b ZJS |
394 | return 1; |
395 | } | |
396 | ||
6c12b52e LP |
397 | static int scope_serialize(Unit *u, FILE *f, FDSet *fds) { |
398 | Scope *s = SCOPE(u); | |
399 | ||
400 | assert(s); | |
401 | assert(f); | |
402 | assert(fds); | |
403 | ||
d68c645b LP |
404 | (void) serialize_item(f, "state", scope_state_to_string(s->state)); |
405 | (void) serialize_bool(f, "was-abandoned", s->was_abandoned); | |
33fe0afe LP |
406 | |
407 | if (s->controller) | |
d68c645b | 408 | (void) serialize_item(f, "controller", s->controller); |
33fe0afe | 409 | |
6c12b52e LP |
410 | return 0; |
411 | } | |
412 | ||
413 | static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
414 | Scope *s = SCOPE(u); | |
33fe0afe | 415 | int r; |
6c12b52e LP |
416 | |
417 | assert(u); | |
418 | assert(key); | |
419 | assert(value); | |
420 | assert(fds); | |
421 | ||
422 | if (streq(key, "state")) { | |
423 | ScopeState state; | |
424 | ||
425 | state = scope_state_from_string(value); | |
426 | if (state < 0) | |
f2341e0a | 427 | log_unit_debug(u, "Failed to parse state value: %s", value); |
6c12b52e LP |
428 | else |
429 | s->deserialized_state = state; | |
430 | ||
3862e809 LP |
431 | } else if (streq(key, "was-abandoned")) { |
432 | int k; | |
433 | ||
434 | k = parse_boolean(value); | |
435 | if (k < 0) | |
436 | log_unit_debug(u, "Failed to parse boolean value: %s", value); | |
437 | else | |
438 | s->was_abandoned = k; | |
33fe0afe LP |
439 | } else if (streq(key, "controller")) { |
440 | ||
441 | r = free_and_strdup(&s->controller, value); | |
442 | if (r < 0) | |
d68c645b | 443 | return log_oom(); |
33fe0afe | 444 | |
6c12b52e | 445 | } else |
f2341e0a | 446 | log_unit_debug(u, "Unknown serialization key: %s", key); |
6c12b52e LP |
447 | |
448 | return 0; | |
449 | } | |
450 | ||
a911bb9a LP |
451 | static void scope_notify_cgroup_empty_event(Unit *u) { |
452 | Scope *s = SCOPE(u); | |
453 | assert(u); | |
454 | ||
f2341e0a | 455 | log_unit_debug(u, "cgroup is empty"); |
a911bb9a LP |
456 | |
457 | if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) | |
458 | scope_enter_dead(s, SCOPE_SUCCESS); | |
459 | } | |
460 | ||
461 | static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) { | |
11aef522 | 462 | assert(u); |
a911bb9a | 463 | |
11aef522 LP |
464 | /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to |
465 | * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original | |
466 | * process we watched was probably the parent of them, and they are hence now our children. */ | |
a911bb9a | 467 | |
50be4f4a | 468 | (void) unit_enqueue_rewatch_pids(u); |
a911bb9a LP |
469 | } |
470 | ||
718db961 LP |
471 | static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
472 | Scope *s = SCOPE(userdata); | |
6c12b52e LP |
473 | |
474 | assert(s); | |
718db961 | 475 | assert(s->timer_event_source == source); |
6c12b52e LP |
476 | |
477 | switch (s->state) { | |
478 | ||
479 | case SCOPE_STOP_SIGTERM: | |
480 | if (s->kill_context.send_sigkill) { | |
f2341e0a | 481 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); |
6c12b52e LP |
482 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT); |
483 | } else { | |
f2341e0a | 484 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); |
6c12b52e LP |
485 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
486 | } | |
487 | ||
488 | break; | |
489 | ||
490 | case SCOPE_STOP_SIGKILL: | |
f2341e0a | 491 | log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring."); |
6c12b52e LP |
492 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
493 | break; | |
494 | ||
495 | default: | |
496 | assert_not_reached("Timeout at wrong time."); | |
497 | } | |
718db961 LP |
498 | |
499 | return 0; | |
6c12b52e LP |
500 | } |
501 | ||
a911bb9a LP |
502 | int scope_abandon(Scope *s) { |
503 | assert(s); | |
6c12b52e | 504 | |
efdb0237 LP |
505 | if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) |
506 | return -EPERM; | |
507 | ||
a911bb9a LP |
508 | if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)) |
509 | return -ESTALE; | |
6c12b52e | 510 | |
3862e809 | 511 | s->was_abandoned = true; |
371c0b79 | 512 | |
a1e58e8e | 513 | s->controller = mfree(s->controller); |
371c0b79 LP |
514 | s->controller_track = sd_bus_track_unref(s->controller_track); |
515 | ||
8cb83266 | 516 | scope_set_state(s, SCOPE_ABANDONED); |
6c12b52e | 517 | |
50be4f4a LP |
518 | /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that |
519 | * the remaining processes will be sooner or later reassigned to us as parent. */ | |
520 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
6c12b52e | 521 | |
a911bb9a | 522 | return 0; |
6c12b52e LP |
523 | } |
524 | ||
525 | _pure_ static UnitActiveState scope_active_state(Unit *u) { | |
526 | assert(u); | |
527 | ||
528 | return state_translation_table[SCOPE(u)->state]; | |
529 | } | |
530 | ||
531 | _pure_ static const char *scope_sub_state_to_string(Unit *u) { | |
532 | assert(u); | |
533 | ||
534 | return scope_state_to_string(SCOPE(u)->state); | |
535 | } | |
536 | ||
04eb582a | 537 | static void scope_enumerate_perpetual(Manager *m) { |
efdb0237 LP |
538 | Unit *u; |
539 | int r; | |
540 | ||
541 | assert(m); | |
542 | ||
543 | /* Let's unconditionally add the "init.scope" special unit | |
544 | * that encapsulates PID 1. Note that PID 1 already is in the | |
545 | * cgroup for this, we hence just need to allocate the object | |
546 | * for it and that's it. */ | |
547 | ||
548 | u = manager_get_unit(m, SPECIAL_INIT_SCOPE); | |
549 | if (!u) { | |
a581e45a | 550 | r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u); |
efdb0237 | 551 | if (r < 0) { |
a581e45a | 552 | log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m"); |
ba64af90 | 553 | return; |
efdb0237 LP |
554 | } |
555 | } | |
556 | ||
557 | u->transient = true; | |
f5869324 | 558 | u->perpetual = true; |
efdb0237 | 559 | SCOPE(u)->deserialized_state = SCOPE_RUNNING; |
efdb0237 LP |
560 | |
561 | unit_add_to_load_queue(u); | |
562 | unit_add_to_dbus_queue(u); | |
efdb0237 LP |
563 | } |
564 | ||
6c12b52e LP |
565 | static const char* const scope_result_table[_SCOPE_RESULT_MAX] = { |
566 | [SCOPE_SUCCESS] = "success", | |
567 | [SCOPE_FAILURE_RESOURCES] = "resources", | |
568 | [SCOPE_FAILURE_TIMEOUT] = "timeout", | |
569 | }; | |
570 | ||
571 | DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult); | |
572 | ||
573 | const UnitVTable scope_vtable = { | |
574 | .object_size = sizeof(Scope), | |
718db961 LP |
575 | .cgroup_context_offset = offsetof(Scope, cgroup_context), |
576 | .kill_context_offset = offsetof(Scope, kill_context), | |
577 | ||
6c12b52e LP |
578 | .sections = |
579 | "Unit\0" | |
580 | "Scope\0" | |
581 | "Install\0", | |
6c12b52e | 582 | .private_section = "Scope", |
6c12b52e | 583 | |
700e2d63 | 584 | .can_transient = true, |
1d9cc876 | 585 | .can_delegate = true, |
d4fd1cf2 | 586 | .once_only = true, |
6c12b52e LP |
587 | |
588 | .init = scope_init, | |
589 | .load = scope_load, | |
590 | .done = scope_done, | |
591 | ||
592 | .coldplug = scope_coldplug, | |
593 | ||
594 | .dump = scope_dump, | |
595 | ||
596 | .start = scope_start, | |
597 | .stop = scope_stop, | |
598 | ||
599 | .kill = scope_kill, | |
600 | ||
68db7a3b ZJS |
601 | .get_timeout = scope_get_timeout, |
602 | ||
6c12b52e LP |
603 | .serialize = scope_serialize, |
604 | .deserialize_item = scope_deserialize_item, | |
605 | ||
606 | .active_state = scope_active_state, | |
607 | .sub_state_to_string = scope_sub_state_to_string, | |
608 | ||
a911bb9a LP |
609 | .sigchld_event = scope_sigchld_event, |
610 | ||
8bcca7e2 LP |
611 | .reset_failed = scope_reset_failed, |
612 | ||
6c12b52e LP |
613 | .notify_cgroup_empty = scope_notify_cgroup_empty_event, |
614 | ||
718db961 | 615 | .bus_vtable = bus_scope_vtable, |
6c12b52e LP |
616 | .bus_set_property = bus_scope_set_property, |
617 | .bus_commit_properties = bus_scope_commit_properties, | |
618 | ||
04eb582a | 619 | .enumerate_perpetual = scope_enumerate_perpetual, |
6c12b52e | 620 | }; |