]>
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; | |
c3620770 ZJS |
179 | |
180 | r = unit_load_fragment_and_dropin(u, false); | |
6c12b52e LP |
181 | if (r < 0) |
182 | return r; | |
183 | ||
4f4afc88 LP |
184 | if (u->load_state == UNIT_LOADED) { |
185 | r = unit_patch_contexts(u); | |
186 | if (r < 0) | |
187 | return r; | |
598459ce | 188 | |
4f4afc88 LP |
189 | r = unit_set_default_slice(u); |
190 | if (r < 0) | |
191 | return r; | |
6c12b52e | 192 | |
4f4afc88 LP |
193 | r = scope_add_default_dependencies(s); |
194 | if (r < 0) | |
195 | return r; | |
196 | } | |
6c12b52e LP |
197 | |
198 | return scope_verify(s); | |
199 | } | |
200 | ||
be847e82 | 201 | static int scope_coldplug(Unit *u) { |
6c12b52e LP |
202 | Scope *s = SCOPE(u); |
203 | int r; | |
204 | ||
205 | assert(s); | |
206 | assert(s->state == SCOPE_DEAD); | |
207 | ||
36c16a7c LP |
208 | if (s->deserialized_state == s->state) |
209 | return 0; | |
a911bb9a | 210 | |
36c16a7c LP |
211 | if (IN_SET(s->deserialized_state, SCOPE_STOP_SIGKILL, SCOPE_STOP_SIGTERM)) { |
212 | r = scope_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_stop_usec)); | |
213 | if (r < 0) | |
214 | return r; | |
6c12b52e LP |
215 | } |
216 | ||
36c16a7c | 217 | if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED)) |
50be4f4a | 218 | (void) unit_enqueue_rewatch_pids(u); |
36c16a7c | 219 | |
371c0b79 LP |
220 | bus_scope_track_controller(s); |
221 | ||
36c16a7c | 222 | scope_set_state(s, s->deserialized_state); |
6c12b52e LP |
223 | return 0; |
224 | } | |
225 | ||
226 | static void scope_dump(Unit *u, FILE *f, const char *prefix) { | |
227 | Scope *s = SCOPE(u); | |
228 | ||
229 | assert(s); | |
230 | assert(f); | |
231 | ||
232 | fprintf(f, | |
233 | "%sScope State: %s\n" | |
234 | "%sResult: %s\n", | |
235 | prefix, scope_state_to_string(s->state), | |
236 | prefix, scope_result_to_string(s->result)); | |
237 | ||
bc0623df | 238 | cgroup_context_dump(UNIT(s), f, prefix); |
6c12b52e LP |
239 | kill_context_dump(&s->kill_context, f, prefix); |
240 | } | |
241 | ||
242 | static void scope_enter_dead(Scope *s, ScopeResult f) { | |
243 | assert(s); | |
244 | ||
a0fef983 | 245 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
246 | s->result = f; |
247 | ||
aac99f30 | 248 | unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result)); |
6c12b52e LP |
249 | scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD); |
250 | } | |
251 | ||
252 | static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { | |
2d4a39e7 | 253 | bool skip_signal = false; |
6c12b52e LP |
254 | int r; |
255 | ||
256 | assert(s); | |
257 | ||
a0fef983 | 258 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
259 | s->result = f; |
260 | ||
50be4f4a LP |
261 | /* Before sending any signal, make sure we track all members of this cgroup */ |
262 | (void) unit_watch_all_pids(UNIT(s)); | |
263 | ||
264 | /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have | |
265 | * died now */ | |
266 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
a911bb9a | 267 | |
371c0b79 LP |
268 | /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going |
269 | * directly into SIGTERM berserk mode */ | |
2d4a39e7 LP |
270 | if (state == SCOPE_STOP_SIGTERM) |
271 | skip_signal = bus_scope_send_request_stop(s) > 0; | |
272 | ||
59ec09a8 ZJS |
273 | if (skip_signal) |
274 | r = 1; /* wait */ | |
275 | else { | |
2d4a39e7 LP |
276 | r = unit_kill_context( |
277 | UNIT(s), | |
278 | &s->kill_context, | |
3862e809 LP |
279 | state != SCOPE_STOP_SIGTERM ? KILL_KILL : |
280 | s->was_abandoned ? KILL_TERMINATE_AND_LOG : | |
281 | KILL_TERMINATE, | |
2d4a39e7 LP |
282 | -1, -1, false); |
283 | if (r < 0) | |
284 | goto fail; | |
59ec09a8 | 285 | } |
6c12b52e LP |
286 | |
287 | if (r > 0) { | |
36c16a7c | 288 | r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec)); |
718db961 LP |
289 | if (r < 0) |
290 | goto fail; | |
6c12b52e LP |
291 | |
292 | scope_set_state(s, state); | |
ac84d1fb LP |
293 | } else if (state == SCOPE_STOP_SIGTERM) |
294 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS); | |
295 | else | |
6c12b52e LP |
296 | scope_enter_dead(s, SCOPE_SUCCESS); |
297 | ||
298 | return; | |
299 | ||
300 | fail: | |
f2341e0a | 301 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
6c12b52e LP |
302 | |
303 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); | |
304 | } | |
305 | ||
306 | static int scope_start(Unit *u) { | |
307 | Scope *s = SCOPE(u); | |
308 | int r; | |
309 | ||
310 | assert(s); | |
311 | ||
efdb0237 LP |
312 | if (unit_has_name(u, SPECIAL_INIT_SCOPE)) |
313 | return -EPERM; | |
314 | ||
7b617155 LP |
315 | if (s->state == SCOPE_FAILED) |
316 | return -EPERM; | |
317 | ||
dd305ec9 | 318 | /* We can't fulfill this right now, please try again later */ |
3742095b | 319 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
6c12b52e LP |
320 | return -EAGAIN; |
321 | ||
322 | assert(s->state == SCOPE_DEAD); | |
323 | ||
2c289ea8 | 324 | if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) |
6c12b52e LP |
325 | return -ENOENT; |
326 | ||
371c0b79 LP |
327 | (void) bus_scope_track_controller(s); |
328 | ||
4b58153d LP |
329 | r = unit_acquire_invocation_id(u); |
330 | if (r < 0) | |
331 | return r; | |
332 | ||
5ad096b3 | 333 | (void) unit_realize_cgroup(u); |
9b2559a1 | 334 | (void) unit_reset_accounting(u); |
5ad096b3 | 335 | |
01542056 | 336 | unit_export_state_files(u); |
d3070fbd | 337 | |
01542056 | 338 | r = unit_attach_pids_to_cgroup(u, u->pids, NULL); |
dd305ec9 | 339 | if (r < 0) { |
01542056 | 340 | log_unit_warning_errno(u, 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 */ | |
01542056 | 350 | (void) unit_enqueue_rewatch_pids(u); |
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 | }; |