]>
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" |
dafb4063 | 7 | #include "cgroup-setup.h" |
07630cea | 8 | #include "dbus-scope.h" |
6fcbec6f | 9 | #include "dbus-unit.h" |
03860190 | 10 | #include "exit-status.h" |
07630cea | 11 | #include "load-dropin.h" |
6c12b52e | 12 | #include "log.h" |
428a9f6f | 13 | #include "process-util.h" |
5918a933 | 14 | #include "random-util.h" |
b5efdb8a | 15 | #include "scope.h" |
d68c645b | 16 | #include "serialize.h" |
6c12b52e | 17 | #include "special.h" |
8b43440b | 18 | #include "string-table.h" |
07630cea LP |
19 | #include "string-util.h" |
20 | #include "strv.h" | |
6c12b52e | 21 | #include "unit-name.h" |
efdb0237 | 22 | #include "unit.h" |
03860190 | 23 | #include "user-util.h" |
6c12b52e LP |
24 | |
25 | static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = { | |
26 | [SCOPE_DEAD] = UNIT_INACTIVE, | |
03860190 | 27 | [SCOPE_START_CHOWN] = UNIT_ACTIVATING, |
6c12b52e | 28 | [SCOPE_RUNNING] = UNIT_ACTIVE, |
a911bb9a | 29 | [SCOPE_ABANDONED] = UNIT_ACTIVE, |
6c12b52e LP |
30 | [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
31 | [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
fff7c34c | 32 | [SCOPE_FAILED] = UNIT_FAILED, |
6c12b52e LP |
33 | }; |
34 | ||
718db961 LP |
35 | static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
36 | ||
6c12b52e | 37 | static void scope_init(Unit *u) { |
e9fa1bf7 | 38 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 39 | |
6c12b52e LP |
40 | assert(u->load_state == UNIT_STUB); |
41 | ||
9ed7de60 | 42 | s->runtime_max_usec = USEC_INFINITY; |
c9e120e0 | 43 | s->timeout_stop_usec = u->manager->defaults.timeout_stop_usec; |
1b4cd0cf | 44 | u->ignore_on_isolate = true; |
03860190 | 45 | s->user = s->group = NULL; |
5fa09835 | 46 | s->oom_policy = _OOM_POLICY_INVALID; |
6c12b52e LP |
47 | } |
48 | ||
49 | static void scope_done(Unit *u) { | |
e9fa1bf7 | 50 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 51 | |
371c0b79 LP |
52 | s->controller = mfree(s->controller); |
53 | s->controller_track = sd_bus_track_unref(s->controller_track); | |
2d4a39e7 | 54 | |
5dcadb4c | 55 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
03860190 MS |
56 | |
57 | s->user = mfree(s->user); | |
58 | s->group = mfree(s->group); | |
718db961 LP |
59 | } |
60 | ||
ecea250d | 61 | static usec_t scope_running_timeout(Scope *s) { |
5918a933 AB |
62 | usec_t delta = 0; |
63 | ||
64 | assert(s); | |
65 | ||
66 | if (s->runtime_rand_extra_usec != 0) { | |
67 | delta = random_u64_range(s->runtime_rand_extra_usec); | |
68 | log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC)); | |
69 | } | |
70 | ||
71 | return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic, | |
72 | s->runtime_max_usec), | |
73 | delta); | |
74 | } | |
75 | ||
e9276800 | 76 | static int scope_arm_timer(Scope *s, bool relative, usec_t usec) { |
718db961 LP |
77 | assert(s); |
78 | ||
e9276800 | 79 | return unit_arm_timer(UNIT(s), &s->timer_event_source, relative, usec, scope_dispatch_timer); |
6c12b52e LP |
80 | } |
81 | ||
82 | static void scope_set_state(Scope *s, ScopeState state) { | |
83 | ScopeState old_state; | |
e9fa1bf7 | 84 | |
6c12b52e LP |
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 | ||
e1f85b49 | 93 | if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL, SCOPE_START_CHOWN, SCOPE_RUNNING)) |
5dcadb4c | 94 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
6c12b52e | 95 | |
05d1630e | 96 | if (!IN_SET(old_state, SCOPE_DEAD, SCOPE_FAILED) && 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 | |
96b09de5 | 104 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true); |
6c12b52e LP |
105 | } |
106 | ||
107 | static int scope_add_default_dependencies(Scope *s) { | |
3835b9aa LB |
108 | int r; |
109 | ||
6c12b52e LP |
110 | assert(s); |
111 | ||
4c9ea260 LP |
112 | if (!UNIT(s)->default_dependencies) |
113 | return 0; | |
114 | ||
6c12b52e | 115 | /* Make sure scopes are unloaded on shutdown */ |
3835b9aa LB |
116 | r = unit_add_two_dependencies_by_name( |
117 | UNIT(s), | |
118 | UNIT_BEFORE, UNIT_CONFLICTS, | |
119 | SPECIAL_SHUTDOWN_TARGET, true, | |
120 | UNIT_DEPENDENCY_DEFAULT); | |
121 | if (r < 0) | |
122 | return r; | |
123 | ||
124 | return 0; | |
6c12b52e LP |
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) && |
d85ff944 YW |
133 | !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) |
134 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOENT), "Scope has no PIDs. Refusing."); | |
6c12b52e LP |
135 | |
136 | return 0; | |
137 | } | |
138 | ||
8e4e851f LP |
139 | static int scope_load_init_scope(Unit *u) { |
140 | assert(u); | |
141 | ||
142 | if (!unit_has_name(u, SPECIAL_INIT_SCOPE)) | |
143 | return 0; | |
144 | ||
145 | u->transient = true; | |
f5869324 | 146 | u->perpetual = true; |
8e4e851f LP |
147 | |
148 | /* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we | |
149 | * synthesize it here, instead of relying on the unit file on disk. */ | |
150 | ||
151 | u->default_dependencies = false; | |
8e4e851f LP |
152 | |
153 | /* Prettify things, if we can. */ | |
154 | if (!u->description) | |
155 | u->description = strdup("System and Service Manager"); | |
156 | if (!u->documentation) | |
157 | (void) strv_extend(&u->documentation, "man:systemd(1)"); | |
158 | ||
159 | return 1; | |
160 | } | |
161 | ||
75193d41 ZJS |
162 | static int scope_add_extras(Scope *s) { |
163 | int r; | |
164 | ||
165 | r = unit_patch_contexts(UNIT(s)); | |
166 | if (r < 0) | |
167 | return r; | |
168 | ||
169 | r = unit_set_default_slice(UNIT(s)); | |
170 | if (r < 0) | |
171 | return r; | |
172 | ||
5fa09835 | 173 | if (s->oom_policy < 0) |
c9e120e0 | 174 | s->oom_policy = s->cgroup_context.delegate ? OOM_CONTINUE : UNIT(s)->manager->defaults.oom_policy; |
5fa09835 ML |
175 | |
176 | s->cgroup_context.memory_oom_group = s->oom_policy == OOM_KILL; | |
177 | ||
75193d41 ZJS |
178 | return scope_add_default_dependencies(s); |
179 | } | |
180 | ||
6c12b52e | 181 | static int scope_load(Unit *u) { |
e9fa1bf7 | 182 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e LP |
183 | int r; |
184 | ||
6c12b52e LP |
185 | assert(u->load_state == UNIT_STUB); |
186 | ||
2c289ea8 | 187 | if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) |
4f4afc88 | 188 | /* Refuse to load non-transient scope units, but allow them while reloading. */ |
6c12b52e LP |
189 | return -ENOENT; |
190 | ||
8e4e851f LP |
191 | r = scope_load_init_scope(u); |
192 | if (r < 0) | |
193 | return r; | |
c3620770 ZJS |
194 | |
195 | r = unit_load_fragment_and_dropin(u, false); | |
6c12b52e LP |
196 | if (r < 0) |
197 | return r; | |
198 | ||
75193d41 ZJS |
199 | if (u->load_state != UNIT_LOADED) |
200 | return 0; | |
6c12b52e | 201 | |
75193d41 ZJS |
202 | r = scope_add_extras(s); |
203 | if (r < 0) | |
204 | return r; | |
6c12b52e LP |
205 | |
206 | return scope_verify(s); | |
207 | } | |
208 | ||
7508f7f2 PW |
209 | static usec_t scope_coldplug_timeout(Scope *s) { |
210 | assert(s); | |
211 | ||
212 | switch (s->deserialized_state) { | |
213 | ||
9ed7de60 | 214 | case SCOPE_RUNNING: |
5918a933 | 215 | return scope_running_timeout(s); |
9ed7de60 | 216 | |
7508f7f2 PW |
217 | case SCOPE_STOP_SIGKILL: |
218 | case SCOPE_STOP_SIGTERM: | |
219 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec); | |
220 | ||
221 | default: | |
222 | return USEC_INFINITY; | |
223 | } | |
224 | } | |
225 | ||
be847e82 | 226 | static int scope_coldplug(Unit *u) { |
e9fa1bf7 | 227 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e LP |
228 | int r; |
229 | ||
6c12b52e LP |
230 | assert(s->state == SCOPE_DEAD); |
231 | ||
36c16a7c LP |
232 | if (s->deserialized_state == s->state) |
233 | return 0; | |
a911bb9a | 234 | |
e9276800 | 235 | r = scope_arm_timer(s, /* relative= */ false, scope_coldplug_timeout(s)); |
7508f7f2 PW |
236 | if (r < 0) |
237 | return r; | |
6c12b52e | 238 | |
428a9f6f FB |
239 | if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED)) { |
240 | if (u->pids) { | |
495e75ed | 241 | PidRef *pid; |
428a9f6f | 242 | |
495e75ed LP |
243 | SET_FOREACH(pid, u->pids) { |
244 | r = unit_watch_pidref(u, pid, /* exclusive= */ false); | |
428a9f6f FB |
245 | if (r < 0 && r != -EEXIST) |
246 | return r; | |
247 | } | |
248 | } else | |
249 | (void) unit_enqueue_rewatch_pids(u); | |
250 | } | |
36c16a7c | 251 | |
371c0b79 LP |
252 | bus_scope_track_controller(s); |
253 | ||
36c16a7c | 254 | scope_set_state(s, s->deserialized_state); |
6c12b52e LP |
255 | return 0; |
256 | } | |
257 | ||
258 | static void scope_dump(Unit *u, FILE *f, const char *prefix) { | |
e9fa1bf7 | 259 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 260 | |
6c12b52e | 261 | assert(f); |
e9fa1bf7 | 262 | assert(prefix); |
6c12b52e LP |
263 | |
264 | fprintf(f, | |
265 | "%sScope State: %s\n" | |
9ed7de60 | 266 | "%sResult: %s\n" |
5918a933 | 267 | "%sRuntimeMaxSec: %s\n" |
5fa09835 ML |
268 | "%sRuntimeRandomizedExtraSec: %s\n" |
269 | "%sOOMPolicy: %s\n", | |
6c12b52e | 270 | prefix, scope_state_to_string(s->state), |
9ed7de60 | 271 | prefix, scope_result_to_string(s->result), |
5918a933 | 272 | prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC), |
5fa09835 ML |
273 | prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC), |
274 | prefix, oom_policy_to_string(s->oom_policy)); | |
6c12b52e | 275 | |
e9fa1bf7 | 276 | cgroup_context_dump(u, f, prefix); |
6c12b52e LP |
277 | kill_context_dump(&s->kill_context, f, prefix); |
278 | } | |
279 | ||
280 | static void scope_enter_dead(Scope *s, ScopeResult f) { | |
281 | assert(s); | |
282 | ||
a0fef983 | 283 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
284 | s->result = f; |
285 | ||
aac99f30 | 286 | unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result)); |
6c12b52e LP |
287 | scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD); |
288 | } | |
289 | ||
290 | static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { | |
2d4a39e7 | 291 | bool skip_signal = false; |
6c12b52e LP |
292 | int r; |
293 | ||
294 | assert(s); | |
295 | ||
a0fef983 | 296 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
297 | s->result = f; |
298 | ||
50be4f4a LP |
299 | /* Before sending any signal, make sure we track all members of this cgroup */ |
300 | (void) unit_watch_all_pids(UNIT(s)); | |
301 | ||
302 | /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have | |
303 | * died now */ | |
304 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
a911bb9a | 305 | |
371c0b79 LP |
306 | /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going |
307 | * directly into SIGTERM berserk mode */ | |
2d4a39e7 LP |
308 | if (state == SCOPE_STOP_SIGTERM) |
309 | skip_signal = bus_scope_send_request_stop(s) > 0; | |
310 | ||
59ec09a8 ZJS |
311 | if (skip_signal) |
312 | r = 1; /* wait */ | |
313 | else { | |
2d4a39e7 LP |
314 | r = unit_kill_context( |
315 | UNIT(s), | |
3862e809 LP |
316 | state != SCOPE_STOP_SIGTERM ? KILL_KILL : |
317 | s->was_abandoned ? KILL_TERMINATE_AND_LOG : | |
b826e317 | 318 | KILL_TERMINATE); |
c5acfe18 LP |
319 | if (r < 0) { |
320 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); | |
2d4a39e7 | 321 | goto fail; |
c5acfe18 | 322 | } |
59ec09a8 | 323 | } |
6c12b52e LP |
324 | |
325 | if (r > 0) { | |
e9276800 | 326 | r = scope_arm_timer(s, /* relative= */ true, s->timeout_stop_usec); |
c5acfe18 LP |
327 | if (r < 0) { |
328 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
718db961 | 329 | goto fail; |
c5acfe18 | 330 | } |
6c12b52e LP |
331 | |
332 | scope_set_state(s, state); | |
ac84d1fb LP |
333 | } else if (state == SCOPE_STOP_SIGTERM) |
334 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS); | |
335 | else | |
6c12b52e LP |
336 | scope_enter_dead(s, SCOPE_SUCCESS); |
337 | ||
338 | return; | |
339 | ||
340 | fail: | |
6c12b52e LP |
341 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); |
342 | } | |
343 | ||
03860190 | 344 | static int scope_enter_start_chown(Scope *s) { |
e9fa1bf7 | 345 | Unit *u = UNIT(ASSERT_PTR(s)); |
4775b55d | 346 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
6c12b52e LP |
347 | int r; |
348 | ||
03860190 | 349 | assert(s->user); |
6c12b52e | 350 | |
9cc54544 LP |
351 | if (!s->cgroup_runtime) |
352 | return -EINVAL; | |
353 | ||
e9276800 | 354 | r = scope_arm_timer(s, /* relative= */ true, u->manager->defaults.timeout_start_usec); |
03860190 MS |
355 | if (r < 0) |
356 | return r; | |
efdb0237 | 357 | |
4775b55d | 358 | r = unit_fork_helper_process(u, "(sd-chown-cgroup)", &pidref); |
03860190 MS |
359 | if (r < 0) |
360 | goto fail; | |
7b617155 | 361 | |
03860190 MS |
362 | if (r == 0) { |
363 | uid_t uid = UID_INVALID; | |
364 | gid_t gid = GID_INVALID; | |
6c12b52e | 365 | |
03860190 MS |
366 | if (!isempty(s->user)) { |
367 | const char *user = s->user; | |
6c12b52e | 368 | |
03860190 MS |
369 | r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0); |
370 | if (r < 0) { | |
371 | log_unit_error_errno(UNIT(s), r, "Failed to resolve user \"%s\": %m", user); | |
372 | _exit(EXIT_USER); | |
373 | } | |
374 | } | |
375 | ||
376 | if (!isempty(s->group)) { | |
377 | const char *group = s->group; | |
378 | ||
379 | r = get_group_creds(&group, &gid, 0); | |
380 | if (r < 0) { | |
381 | log_unit_error_errno(UNIT(s), r, "Failed to resolve group \"%s\": %m", group); | |
382 | _exit(EXIT_GROUP); | |
383 | } | |
384 | } | |
385 | ||
9cc54544 | 386 | r = cg_set_access(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_runtime->cgroup_path, uid, gid); |
03860190 MS |
387 | if (r < 0) { |
388 | log_unit_error_errno(UNIT(s), r, "Failed to adjust control group access: %m"); | |
389 | _exit(EXIT_CGROUP); | |
390 | } | |
391 | ||
392 | _exit(EXIT_SUCCESS); | |
393 | } | |
394 | ||
495e75ed | 395 | r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true); |
03860190 MS |
396 | if (r < 0) |
397 | goto fail; | |
398 | ||
399 | scope_set_state(s, SCOPE_START_CHOWN); | |
400 | ||
401 | return 1; | |
402 | fail: | |
403 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); | |
404 | return r; | |
405 | } | |
406 | ||
407 | static int scope_enter_running(Scope *s) { | |
e9fa1bf7 | 408 | Unit *u = UNIT(ASSERT_PTR(s)); |
03860190 MS |
409 | int r; |
410 | ||
371c0b79 LP |
411 | (void) bus_scope_track_controller(s); |
412 | ||
4b58153d LP |
413 | r = unit_acquire_invocation_id(u); |
414 | if (r < 0) | |
415 | return r; | |
416 | ||
01542056 | 417 | unit_export_state_files(u); |
d3070fbd | 418 | |
01542056 | 419 | r = unit_attach_pids_to_cgroup(u, u->pids, NULL); |
dd305ec9 | 420 | if (r < 0) { |
01542056 | 421 | log_unit_warning_errno(u, r, "Failed to add PIDs to scope's control group: %m"); |
648cb024 | 422 | goto fail; |
dd305ec9 | 423 | } |
8d3e4ac7 | 424 | if (r == 0) { |
648cb024 LP |
425 | r = log_unit_warning_errno(u, SYNTHETIC_ERRNO(ECHILD), "No PIDs left to attach to the scope's control group, refusing."); |
426 | goto fail; | |
8d3e4ac7 LP |
427 | } |
428 | log_unit_debug(u, "%i %s added to scope's control group.", r, r == 1 ? "process" : "processes"); | |
6c12b52e | 429 | |
6c12b52e LP |
430 | s->result = SCOPE_SUCCESS; |
431 | ||
432 | scope_set_state(s, SCOPE_RUNNING); | |
b91ada2a | 433 | |
9ed7de60 | 434 | /* Set the maximum runtime timeout. */ |
e9276800 | 435 | scope_arm_timer(s, /* relative= */ false, scope_running_timeout(s)); |
9ed7de60 | 436 | |
e9eec8b5 FB |
437 | /* On unified we use proper notifications hence we can unwatch the PIDs |
438 | * we just attached to the scope. This can also be done on legacy as | |
439 | * we're going to update the list of the processes we watch with the | |
440 | * PIDs currently in the scope anyway. */ | |
441 | unit_unwatch_all_pids(u); | |
442 | ||
443 | /* Start watching the PIDs currently in the scope (legacy hierarchy only) */ | |
01542056 | 444 | (void) unit_enqueue_rewatch_pids(u); |
82a2b6bb | 445 | return 1; |
648cb024 LP |
446 | |
447 | fail: | |
448 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); | |
449 | return r; | |
6c12b52e LP |
450 | } |
451 | ||
03860190 | 452 | static int scope_start(Unit *u) { |
e9fa1bf7 | 453 | Scope *s = ASSERT_PTR(SCOPE(u)); |
03860190 MS |
454 | |
455 | if (unit_has_name(u, SPECIAL_INIT_SCOPE)) | |
456 | return -EPERM; | |
457 | ||
458 | if (s->state == SCOPE_FAILED) | |
459 | return -EPERM; | |
460 | ||
461 | /* We can't fulfill this right now, please try again later */ | |
462 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) | |
463 | return -EAGAIN; | |
464 | ||
465 | assert(s->state == SCOPE_DEAD); | |
466 | ||
467 | if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) | |
468 | return -ENOENT; | |
469 | ||
470 | (void) unit_realize_cgroup(u); | |
471 | (void) unit_reset_accounting(u); | |
472 | ||
473 | /* We check only for User= option to keep behavior consistent with logic for service units, | |
5c19169f | 474 | * i.e. having 'Delegate=true Group=foo' w/o specifying User= has no effect. */ |
03860190 MS |
475 | if (s->user && unit_cgroup_delegate(u)) |
476 | return scope_enter_start_chown(s); | |
477 | ||
478 | return scope_enter_running(s); | |
479 | } | |
480 | ||
6c12b52e | 481 | static int scope_stop(Unit *u) { |
e9fa1bf7 | 482 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 483 | |
3742095b | 484 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
6c12b52e LP |
485 | return 0; |
486 | ||
3742095b | 487 | assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)); |
6c12b52e LP |
488 | |
489 | scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS); | |
82a2b6bb | 490 | return 1; |
6c12b52e LP |
491 | } |
492 | ||
8bcca7e2 | 493 | static void scope_reset_failed(Unit *u) { |
e9fa1bf7 | 494 | Scope *s = ASSERT_PTR(SCOPE(u)); |
8bcca7e2 LP |
495 | |
496 | if (s->state == SCOPE_FAILED) | |
497 | scope_set_state(s, SCOPE_DEAD); | |
498 | ||
499 | s->result = SCOPE_SUCCESS; | |
500 | } | |
501 | ||
7a7821c8 | 502 | static int scope_get_timeout(Unit *u, usec_t *timeout) { |
e9fa1bf7 | 503 | Scope *s = ASSERT_PTR(SCOPE(u)); |
7a7821c8 | 504 | usec_t t; |
68db7a3b ZJS |
505 | int r; |
506 | ||
507 | if (!s->timer_event_source) | |
508 | return 0; | |
509 | ||
7a7821c8 | 510 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
511 | if (r < 0) |
512 | return r; | |
7a7821c8 LP |
513 | if (t == USEC_INFINITY) |
514 | return 0; | |
68db7a3b | 515 | |
7a7821c8 | 516 | *timeout = t; |
68db7a3b ZJS |
517 | return 1; |
518 | } | |
519 | ||
6c12b52e | 520 | static int scope_serialize(Unit *u, FILE *f, FDSet *fds) { |
e9fa1bf7 | 521 | Scope *s = ASSERT_PTR(SCOPE(u)); |
495e75ed | 522 | PidRef *pid; |
6c12b52e | 523 | |
6c12b52e LP |
524 | assert(f); |
525 | assert(fds); | |
526 | ||
d68c645b LP |
527 | (void) serialize_item(f, "state", scope_state_to_string(s->state)); |
528 | (void) serialize_bool(f, "was-abandoned", s->was_abandoned); | |
33fe0afe LP |
529 | |
530 | if (s->controller) | |
d68c645b | 531 | (void) serialize_item(f, "controller", s->controller); |
33fe0afe | 532 | |
495e75ed | 533 | SET_FOREACH(pid, u->pids) |
2a7451dc | 534 | serialize_pidref(f, fds, "pids", pid); |
428a9f6f | 535 | |
6c12b52e LP |
536 | return 0; |
537 | } | |
538 | ||
539 | static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
e9fa1bf7 | 540 | Scope *s = ASSERT_PTR(SCOPE(u)); |
33fe0afe | 541 | int r; |
6c12b52e | 542 | |
6c12b52e LP |
543 | assert(key); |
544 | assert(value); | |
545 | assert(fds); | |
546 | ||
547 | if (streq(key, "state")) { | |
548 | ScopeState state; | |
549 | ||
550 | state = scope_state_from_string(value); | |
551 | if (state < 0) | |
f2341e0a | 552 | log_unit_debug(u, "Failed to parse state value: %s", value); |
6c12b52e LP |
553 | else |
554 | s->deserialized_state = state; | |
555 | ||
3862e809 LP |
556 | } else if (streq(key, "was-abandoned")) { |
557 | int k; | |
558 | ||
559 | k = parse_boolean(value); | |
560 | if (k < 0) | |
561 | log_unit_debug(u, "Failed to parse boolean value: %s", value); | |
562 | else | |
563 | s->was_abandoned = k; | |
33fe0afe LP |
564 | } else if (streq(key, "controller")) { |
565 | ||
566 | r = free_and_strdup(&s->controller, value); | |
567 | if (r < 0) | |
d68c645b | 568 | return log_oom(); |
33fe0afe | 569 | |
428a9f6f | 570 | } else if (streq(key, "pids")) { |
2a7451dc | 571 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
495e75ed | 572 | |
2a7451dc LP |
573 | if (deserialize_pidref(fds, value, &pidref) >= 0) { |
574 | r = unit_watch_pidref(u, &pidref, /* exclusive= */ false); | |
575 | if (r < 0) | |
576 | log_unit_debug(u, "Failed to watch PID, ignoring: %s", value); | |
577 | } | |
6c12b52e | 578 | } else |
f2341e0a | 579 | log_unit_debug(u, "Unknown serialization key: %s", key); |
6c12b52e LP |
580 | |
581 | return 0; | |
582 | } | |
583 | ||
a911bb9a | 584 | static void scope_notify_cgroup_empty_event(Unit *u) { |
e9fa1bf7 | 585 | Scope *s = ASSERT_PTR(SCOPE(u)); |
a911bb9a | 586 | |
f2341e0a | 587 | log_unit_debug(u, "cgroup is empty"); |
a911bb9a LP |
588 | |
589 | if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) | |
590 | scope_enter_dead(s, SCOPE_SUCCESS); | |
591 | } | |
592 | ||
7238fd51 | 593 | static void scope_notify_cgroup_oom_event(Unit *u, bool managed_oom) { |
e9fa1bf7 | 594 | Scope *s = ASSERT_PTR(SCOPE(u)); |
7238fd51 | 595 | |
596 | if (managed_oom) | |
597 | log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd."); | |
598 | else | |
599 | log_unit_debug(u, "Process of control group was killed by the OOM killer."); | |
600 | ||
5fa09835 ML |
601 | if (s->oom_policy == OOM_CONTINUE) |
602 | return; | |
603 | ||
7238fd51 | 604 | switch (s->state) { |
605 | ||
606 | case SCOPE_START_CHOWN: | |
607 | case SCOPE_RUNNING: | |
5fa09835 ML |
608 | scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_OOM_KILL); |
609 | break; | |
610 | ||
7238fd51 | 611 | case SCOPE_STOP_SIGTERM: |
612 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_OOM_KILL); | |
613 | break; | |
614 | ||
615 | case SCOPE_STOP_SIGKILL: | |
616 | if (s->result == SCOPE_SUCCESS) | |
617 | s->result = SCOPE_FAILURE_OOM_KILL; | |
618 | break; | |
619 | /* SCOPE_DEAD, SCOPE_ABANDONED, and SCOPE_FAILED end up in default */ | |
620 | default: | |
621 | ; | |
622 | } | |
623 | } | |
624 | ||
a911bb9a | 625 | static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
e9fa1bf7 | 626 | Scope *s = ASSERT_PTR(SCOPE(u)); |
03860190 MS |
627 | |
628 | if (s->state == SCOPE_START_CHOWN) { | |
629 | if (!is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL)) | |
630 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); | |
631 | else | |
632 | scope_enter_running(s); | |
633 | return; | |
634 | } | |
a911bb9a | 635 | |
11aef522 LP |
636 | /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to |
637 | * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original | |
638 | * process we watched was probably the parent of them, and they are hence now our children. */ | |
a911bb9a | 639 | |
50be4f4a | 640 | (void) unit_enqueue_rewatch_pids(u); |
a911bb9a LP |
641 | } |
642 | ||
718db961 | 643 | static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
e9fa1bf7 | 644 | Scope *s = ASSERT_PTR(SCOPE(userdata)); |
6c12b52e | 645 | |
718db961 | 646 | assert(s->timer_event_source == source); |
6c12b52e LP |
647 | |
648 | switch (s->state) { | |
649 | ||
9ed7de60 PW |
650 | case SCOPE_RUNNING: |
651 | log_unit_warning(UNIT(s), "Scope reached runtime time limit. Stopping."); | |
652 | scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_TIMEOUT); | |
653 | break; | |
654 | ||
6c12b52e LP |
655 | case SCOPE_STOP_SIGTERM: |
656 | if (s->kill_context.send_sigkill) { | |
f2341e0a | 657 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); |
6c12b52e LP |
658 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT); |
659 | } else { | |
f2341e0a | 660 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); |
6c12b52e LP |
661 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
662 | } | |
663 | ||
664 | break; | |
665 | ||
666 | case SCOPE_STOP_SIGKILL: | |
f2341e0a | 667 | log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring."); |
6c12b52e LP |
668 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
669 | break; | |
670 | ||
03860190 MS |
671 | case SCOPE_START_CHOWN: |
672 | log_unit_warning(UNIT(s), "User lookup timed out. Entering failed state."); | |
673 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); | |
674 | break; | |
675 | ||
6c12b52e | 676 | default: |
04499a70 | 677 | assert_not_reached(); |
6c12b52e | 678 | } |
718db961 LP |
679 | |
680 | return 0; | |
6c12b52e LP |
681 | } |
682 | ||
a911bb9a LP |
683 | int scope_abandon(Scope *s) { |
684 | assert(s); | |
6c12b52e | 685 | |
efdb0237 LP |
686 | if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) |
687 | return -EPERM; | |
688 | ||
a911bb9a LP |
689 | if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)) |
690 | return -ESTALE; | |
6c12b52e | 691 | |
3862e809 | 692 | s->was_abandoned = true; |
371c0b79 | 693 | |
a1e58e8e | 694 | s->controller = mfree(s->controller); |
371c0b79 LP |
695 | s->controller_track = sd_bus_track_unref(s->controller_track); |
696 | ||
8cb83266 | 697 | scope_set_state(s, SCOPE_ABANDONED); |
6c12b52e | 698 | |
50be4f4a LP |
699 | /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that |
700 | * the remaining processes will be sooner or later reassigned to us as parent. */ | |
701 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
6c12b52e | 702 | |
a911bb9a | 703 | return 0; |
6c12b52e LP |
704 | } |
705 | ||
d1e8e8b5 | 706 | static UnitActiveState scope_active_state(Unit *u) { |
e9fa1bf7 | 707 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 708 | |
e9fa1bf7 | 709 | return state_translation_table[s->state]; |
6c12b52e LP |
710 | } |
711 | ||
d1e8e8b5 | 712 | static const char *scope_sub_state_to_string(Unit *u) { |
e9fa1bf7 | 713 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 714 | |
e9fa1bf7 | 715 | return scope_state_to_string(s->state); |
6c12b52e LP |
716 | } |
717 | ||
04eb582a | 718 | static void scope_enumerate_perpetual(Manager *m) { |
efdb0237 LP |
719 | Unit *u; |
720 | int r; | |
721 | ||
722 | assert(m); | |
723 | ||
724 | /* Let's unconditionally add the "init.scope" special unit | |
725 | * that encapsulates PID 1. Note that PID 1 already is in the | |
726 | * cgroup for this, we hence just need to allocate the object | |
727 | * for it and that's it. */ | |
728 | ||
729 | u = manager_get_unit(m, SPECIAL_INIT_SCOPE); | |
730 | if (!u) { | |
a581e45a | 731 | r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u); |
efdb0237 | 732 | if (r < 0) { |
a581e45a | 733 | log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m"); |
ba64af90 | 734 | return; |
efdb0237 LP |
735 | } |
736 | } | |
737 | ||
738 | u->transient = true; | |
f5869324 | 739 | u->perpetual = true; |
efdb0237 | 740 | SCOPE(u)->deserialized_state = SCOPE_RUNNING; |
efdb0237 LP |
741 | |
742 | unit_add_to_load_queue(u); | |
743 | unit_add_to_dbus_queue(u); | |
020b2e41 LB |
744 | /* Enqueue an explicit cgroup realization here. Unlike other cgroups this one already exists and is |
745 | * populated (by us, after all!) already, even when we are not in a reload cycle. Hence we cannot | |
746 | * apply the settings at creation time anymore, but let's at least apply them asynchronously. */ | |
747 | unit_add_to_cgroup_realize_queue(u); | |
efdb0237 LP |
748 | } |
749 | ||
6c12b52e | 750 | static const char* const scope_result_table[_SCOPE_RESULT_MAX] = { |
48d83e33 | 751 | [SCOPE_SUCCESS] = "success", |
6c12b52e | 752 | [SCOPE_FAILURE_RESOURCES] = "resources", |
48d83e33 | 753 | [SCOPE_FAILURE_TIMEOUT] = "timeout", |
7238fd51 | 754 | [SCOPE_FAILURE_OOM_KILL] = "oom-kill", |
6c12b52e LP |
755 | }; |
756 | ||
757 | DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult); | |
758 | ||
759 | const UnitVTable scope_vtable = { | |
760 | .object_size = sizeof(Scope), | |
718db961 LP |
761 | .cgroup_context_offset = offsetof(Scope, cgroup_context), |
762 | .kill_context_offset = offsetof(Scope, kill_context), | |
9cc54544 | 763 | .cgroup_runtime_offset = offsetof(Scope, cgroup_runtime), |
718db961 | 764 | |
6c12b52e LP |
765 | .sections = |
766 | "Unit\0" | |
767 | "Scope\0" | |
768 | "Install\0", | |
6c12b52e | 769 | .private_section = "Scope", |
6c12b52e | 770 | |
700e2d63 | 771 | .can_transient = true, |
1d9cc876 | 772 | .can_delegate = true, |
c80a9a33 | 773 | .can_fail = true, |
d4fd1cf2 | 774 | .once_only = true, |
4d824a4e | 775 | .can_set_managed_oom = true, |
6c12b52e LP |
776 | |
777 | .init = scope_init, | |
778 | .load = scope_load, | |
779 | .done = scope_done, | |
780 | ||
781 | .coldplug = scope_coldplug, | |
782 | ||
783 | .dump = scope_dump, | |
784 | ||
785 | .start = scope_start, | |
786 | .stop = scope_stop, | |
787 | ||
16b6af6a | 788 | .freezer_action = unit_cgroup_freezer_action, |
d9e45bc3 | 789 | |
68db7a3b ZJS |
790 | .get_timeout = scope_get_timeout, |
791 | ||
6c12b52e LP |
792 | .serialize = scope_serialize, |
793 | .deserialize_item = scope_deserialize_item, | |
794 | ||
795 | .active_state = scope_active_state, | |
796 | .sub_state_to_string = scope_sub_state_to_string, | |
797 | ||
a911bb9a LP |
798 | .sigchld_event = scope_sigchld_event, |
799 | ||
8bcca7e2 LP |
800 | .reset_failed = scope_reset_failed, |
801 | ||
6c12b52e | 802 | .notify_cgroup_empty = scope_notify_cgroup_empty_event, |
7238fd51 | 803 | .notify_cgroup_oom = scope_notify_cgroup_oom_event, |
6c12b52e | 804 | |
6c12b52e LP |
805 | .bus_set_property = bus_scope_set_property, |
806 | .bus_commit_properties = bus_scope_commit_properties, | |
807 | ||
04eb582a | 808 | .enumerate_perpetual = scope_enumerate_perpetual, |
6c12b52e | 809 | }; |