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