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