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