]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
6c12b52e | 2 | |
6c12b52e LP |
3 | #include <unistd.h> |
4 | ||
836e4e7e DDM |
5 | #include "sd-bus.h" |
6 | ||
dafb4063 | 7 | #include "cgroup-setup.h" |
07630cea | 8 | #include "dbus-scope.h" |
6fcbec6f | 9 | #include "dbus-unit.h" |
03860190 | 10 | #include "exit-status.h" |
6c12b52e | 11 | #include "log.h" |
4ea4abb6 | 12 | #include "manager.h" |
c94f6ab1 | 13 | #include "parse-util.h" |
836e4e7e | 14 | #include "pidref.h" |
5918a933 | 15 | #include "random-util.h" |
b5efdb8a | 16 | #include "scope.h" |
d68c645b | 17 | #include "serialize.h" |
836e4e7e | 18 | #include "set.h" |
6c12b52e | 19 | #include "special.h" |
8b43440b | 20 | #include "string-table.h" |
07630cea LP |
21 | #include "string-util.h" |
22 | #include "strv.h" | |
efdb0237 | 23 | #include "unit.h" |
03860190 | 24 | #include "user-util.h" |
6c12b52e LP |
25 | |
26 | static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = { | |
17f6b640 YW |
27 | [SCOPE_DEAD] = UNIT_INACTIVE, |
28 | [SCOPE_START_CHOWN] = UNIT_ACTIVATING, | |
29 | [SCOPE_RUNNING] = UNIT_ACTIVE, | |
30 | [SCOPE_ABANDONED] = UNIT_ACTIVE, | |
6c12b52e LP |
31 | [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
32 | [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
17f6b640 | 33 | [SCOPE_FAILED] = UNIT_FAILED, |
6c12b52e LP |
34 | }; |
35 | ||
718db961 LP |
36 | static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
37 | ||
6c12b52e | 38 | static void scope_init(Unit *u) { |
e9fa1bf7 | 39 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 40 | |
6c12b52e LP |
41 | assert(u->load_state == UNIT_STUB); |
42 | ||
9ed7de60 | 43 | s->runtime_max_usec = USEC_INFINITY; |
c9e120e0 | 44 | s->timeout_stop_usec = u->manager->defaults.timeout_stop_usec; |
1b4cd0cf | 45 | u->ignore_on_isolate = true; |
03860190 | 46 | s->user = s->group = NULL; |
5fa09835 | 47 | s->oom_policy = _OOM_POLICY_INVALID; |
6c12b52e LP |
48 | } |
49 | ||
50 | static void scope_done(Unit *u) { | |
e9fa1bf7 | 51 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 52 | |
371c0b79 LP |
53 | s->controller = mfree(s->controller); |
54 | s->controller_track = sd_bus_track_unref(s->controller_track); | |
2d4a39e7 | 55 | |
5dcadb4c | 56 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
03860190 MS |
57 | |
58 | s->user = mfree(s->user); | |
59 | s->group = mfree(s->group); | |
718db961 LP |
60 | } |
61 | ||
ecea250d | 62 | static usec_t scope_running_timeout(Scope *s) { |
5918a933 AB |
63 | usec_t delta = 0; |
64 | ||
65 | assert(s); | |
66 | ||
67 | if (s->runtime_rand_extra_usec != 0) { | |
68 | delta = random_u64_range(s->runtime_rand_extra_usec); | |
69 | log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC)); | |
70 | } | |
71 | ||
72 | return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic, | |
73 | s->runtime_max_usec), | |
74 | delta); | |
75 | } | |
76 | ||
e9276800 | 77 | static int scope_arm_timer(Scope *s, bool relative, usec_t usec) { |
718db961 LP |
78 | assert(s); |
79 | ||
e9276800 | 80 | return unit_arm_timer(UNIT(s), &s->timer_event_source, relative, usec, scope_dispatch_timer); |
6c12b52e LP |
81 | } |
82 | ||
83 | static void scope_set_state(Scope *s, ScopeState state) { | |
84 | ScopeState old_state; | |
e9fa1bf7 | 85 | |
6c12b52e LP |
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 | ||
e1f85b49 | 94 | if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL, SCOPE_START_CHOWN, SCOPE_RUNNING)) |
5dcadb4c | 95 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
6c12b52e | 96 | |
55e4df21 | 97 | if (!IN_SET(old_state, SCOPE_DEAD, SCOPE_FAILED) && IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) |
a911bb9a LP |
98 | unit_unwatch_all_pids(UNIT(s)); |
99 | ||
6c12b52e | 100 | if (state != old_state) |
b7e4e152 MY |
101 | log_unit_debug(UNIT(s), "Changed %s -> %s", |
102 | 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 | |
371c0b79 LP |
239 | bus_scope_track_controller(s); |
240 | ||
36c16a7c | 241 | scope_set_state(s, s->deserialized_state); |
6c12b52e LP |
242 | return 0; |
243 | } | |
244 | ||
245 | static void scope_dump(Unit *u, FILE *f, const char *prefix) { | |
e9fa1bf7 | 246 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 247 | |
6c12b52e | 248 | assert(f); |
e9fa1bf7 | 249 | assert(prefix); |
6c12b52e LP |
250 | |
251 | fprintf(f, | |
252 | "%sScope State: %s\n" | |
9ed7de60 | 253 | "%sResult: %s\n" |
5918a933 | 254 | "%sRuntimeMaxSec: %s\n" |
5fa09835 ML |
255 | "%sRuntimeRandomizedExtraSec: %s\n" |
256 | "%sOOMPolicy: %s\n", | |
6c12b52e | 257 | prefix, scope_state_to_string(s->state), |
9ed7de60 | 258 | prefix, scope_result_to_string(s->result), |
5918a933 | 259 | prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC), |
5fa09835 ML |
260 | prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC), |
261 | prefix, oom_policy_to_string(s->oom_policy)); | |
6c12b52e | 262 | |
e9fa1bf7 | 263 | cgroup_context_dump(u, f, prefix); |
6c12b52e LP |
264 | kill_context_dump(&s->kill_context, f, prefix); |
265 | } | |
266 | ||
267 | static void scope_enter_dead(Scope *s, ScopeResult f) { | |
268 | assert(s); | |
269 | ||
a0fef983 | 270 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
271 | s->result = f; |
272 | ||
aac99f30 | 273 | unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result)); |
6c12b52e LP |
274 | scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD); |
275 | } | |
276 | ||
277 | static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { | |
2d4a39e7 | 278 | bool skip_signal = false; |
6c12b52e LP |
279 | int r; |
280 | ||
281 | assert(s); | |
282 | ||
a0fef983 | 283 | if (s->result == SCOPE_SUCCESS) |
6c12b52e LP |
284 | s->result = f; |
285 | ||
371c0b79 LP |
286 | /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going |
287 | * directly into SIGTERM berserk mode */ | |
2d4a39e7 LP |
288 | if (state == SCOPE_STOP_SIGTERM) |
289 | skip_signal = bus_scope_send_request_stop(s) > 0; | |
290 | ||
59ec09a8 ZJS |
291 | if (skip_signal) |
292 | r = 1; /* wait */ | |
293 | else { | |
2d4a39e7 LP |
294 | r = unit_kill_context( |
295 | UNIT(s), | |
3862e809 LP |
296 | state != SCOPE_STOP_SIGTERM ? KILL_KILL : |
297 | s->was_abandoned ? KILL_TERMINATE_AND_LOG : | |
b826e317 | 298 | KILL_TERMINATE); |
c5acfe18 LP |
299 | if (r < 0) { |
300 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); | |
2d4a39e7 | 301 | goto fail; |
c5acfe18 | 302 | } |
59ec09a8 | 303 | } |
6c12b52e LP |
304 | |
305 | if (r > 0) { | |
e9276800 | 306 | r = scope_arm_timer(s, /* relative= */ true, s->timeout_stop_usec); |
c5acfe18 LP |
307 | if (r < 0) { |
308 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
718db961 | 309 | goto fail; |
c5acfe18 | 310 | } |
6c12b52e LP |
311 | |
312 | scope_set_state(s, state); | |
ac84d1fb LP |
313 | } else if (state == SCOPE_STOP_SIGTERM) |
314 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS); | |
315 | else | |
6c12b52e LP |
316 | scope_enter_dead(s, SCOPE_SUCCESS); |
317 | ||
318 | return; | |
319 | ||
320 | fail: | |
6c12b52e LP |
321 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); |
322 | } | |
323 | ||
03860190 | 324 | static int scope_enter_start_chown(Scope *s) { |
e9fa1bf7 | 325 | Unit *u = UNIT(ASSERT_PTR(s)); |
4775b55d | 326 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
6c12b52e LP |
327 | int r; |
328 | ||
03860190 | 329 | assert(s->user); |
6c12b52e | 330 | |
9cc54544 LP |
331 | if (!s->cgroup_runtime) |
332 | return -EINVAL; | |
333 | ||
e9276800 | 334 | r = scope_arm_timer(s, /* relative= */ true, u->manager->defaults.timeout_start_usec); |
03860190 MS |
335 | if (r < 0) |
336 | return r; | |
efdb0237 | 337 | |
5162829e | 338 | r = unit_fork_helper_process(u, "(sd-chown-cgroup)", /* into_cgroup= */ true, &pidref); |
03860190 MS |
339 | if (r < 0) |
340 | goto fail; | |
7b617155 | 341 | |
03860190 MS |
342 | if (r == 0) { |
343 | uid_t uid = UID_INVALID; | |
344 | gid_t gid = GID_INVALID; | |
6c12b52e | 345 | |
03860190 MS |
346 | if (!isempty(s->user)) { |
347 | const char *user = s->user; | |
6c12b52e | 348 | |
03860190 MS |
349 | r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0); |
350 | if (r < 0) { | |
351 | log_unit_error_errno(UNIT(s), r, "Failed to resolve user \"%s\": %m", user); | |
352 | _exit(EXIT_USER); | |
353 | } | |
354 | } | |
355 | ||
356 | if (!isempty(s->group)) { | |
357 | const char *group = s->group; | |
358 | ||
359 | r = get_group_creds(&group, &gid, 0); | |
360 | if (r < 0) { | |
361 | log_unit_error_errno(UNIT(s), r, "Failed to resolve group \"%s\": %m", group); | |
362 | _exit(EXIT_GROUP); | |
363 | } | |
364 | } | |
365 | ||
188286ee | 366 | r = cg_set_access(s->cgroup_runtime->cgroup_path, uid, gid); |
03860190 MS |
367 | if (r < 0) { |
368 | log_unit_error_errno(UNIT(s), r, "Failed to adjust control group access: %m"); | |
369 | _exit(EXIT_CGROUP); | |
370 | } | |
371 | ||
372 | _exit(EXIT_SUCCESS); | |
373 | } | |
374 | ||
495e75ed | 375 | r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true); |
03860190 MS |
376 | if (r < 0) |
377 | goto fail; | |
378 | ||
379 | scope_set_state(s, SCOPE_START_CHOWN); | |
380 | ||
381 | return 1; | |
382 | fail: | |
383 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); | |
384 | return r; | |
385 | } | |
386 | ||
387 | static int scope_enter_running(Scope *s) { | |
e9fa1bf7 | 388 | Unit *u = UNIT(ASSERT_PTR(s)); |
03860190 MS |
389 | int r; |
390 | ||
371c0b79 LP |
391 | (void) bus_scope_track_controller(s); |
392 | ||
4b58153d LP |
393 | r = unit_acquire_invocation_id(u); |
394 | if (r < 0) | |
395 | return r; | |
396 | ||
01542056 | 397 | unit_export_state_files(u); |
d3070fbd | 398 | |
01542056 | 399 | r = unit_attach_pids_to_cgroup(u, u->pids, NULL); |
dd305ec9 | 400 | if (r < 0) { |
01542056 | 401 | log_unit_warning_errno(u, r, "Failed to add PIDs to scope's control group: %m"); |
648cb024 | 402 | goto fail; |
dd305ec9 | 403 | } |
8d3e4ac7 | 404 | if (r == 0) { |
648cb024 LP |
405 | r = log_unit_warning_errno(u, SYNTHETIC_ERRNO(ECHILD), "No PIDs left to attach to the scope's control group, refusing."); |
406 | goto fail; | |
8d3e4ac7 LP |
407 | } |
408 | log_unit_debug(u, "%i %s added to scope's control group.", r, r == 1 ? "process" : "processes"); | |
6c12b52e | 409 | |
6c12b52e LP |
410 | s->result = SCOPE_SUCCESS; |
411 | ||
412 | scope_set_state(s, SCOPE_RUNNING); | |
b91ada2a | 413 | |
9ed7de60 | 414 | /* Set the maximum runtime timeout. */ |
e9276800 | 415 | scope_arm_timer(s, /* relative= */ false, scope_running_timeout(s)); |
9ed7de60 | 416 | |
55e4df21 | 417 | /* Unwatch all pids we've just added to cgroup. We rely on empty notifications there. */ |
e9eec8b5 FB |
418 | unit_unwatch_all_pids(u); |
419 | ||
82a2b6bb | 420 | return 1; |
648cb024 LP |
421 | |
422 | fail: | |
423 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); | |
424 | return r; | |
6c12b52e LP |
425 | } |
426 | ||
03860190 | 427 | static int scope_start(Unit *u) { |
e9fa1bf7 | 428 | Scope *s = ASSERT_PTR(SCOPE(u)); |
03860190 MS |
429 | |
430 | if (unit_has_name(u, SPECIAL_INIT_SCOPE)) | |
431 | return -EPERM; | |
432 | ||
433 | if (s->state == SCOPE_FAILED) | |
434 | return -EPERM; | |
435 | ||
436 | /* We can't fulfill this right now, please try again later */ | |
437 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) | |
438 | return -EAGAIN; | |
439 | ||
440 | assert(s->state == SCOPE_DEAD); | |
441 | ||
442 | if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) | |
443 | return -ENOENT; | |
444 | ||
445 | (void) unit_realize_cgroup(u); | |
446 | (void) unit_reset_accounting(u); | |
447 | ||
448 | /* We check only for User= option to keep behavior consistent with logic for service units, | |
5c19169f | 449 | * i.e. having 'Delegate=true Group=foo' w/o specifying User= has no effect. */ |
03860190 MS |
450 | if (s->user && unit_cgroup_delegate(u)) |
451 | return scope_enter_start_chown(s); | |
452 | ||
453 | return scope_enter_running(s); | |
454 | } | |
455 | ||
6c12b52e | 456 | static int scope_stop(Unit *u) { |
e9fa1bf7 | 457 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 458 | |
3742095b | 459 | if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
6c12b52e LP |
460 | return 0; |
461 | ||
3742095b | 462 | assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)); |
6c12b52e LP |
463 | |
464 | scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS); | |
82a2b6bb | 465 | return 1; |
6c12b52e LP |
466 | } |
467 | ||
8bcca7e2 | 468 | static void scope_reset_failed(Unit *u) { |
e9fa1bf7 | 469 | Scope *s = ASSERT_PTR(SCOPE(u)); |
8bcca7e2 LP |
470 | |
471 | if (s->state == SCOPE_FAILED) | |
472 | scope_set_state(s, SCOPE_DEAD); | |
473 | ||
474 | s->result = SCOPE_SUCCESS; | |
475 | } | |
476 | ||
7a7821c8 | 477 | static int scope_get_timeout(Unit *u, usec_t *timeout) { |
e9fa1bf7 | 478 | Scope *s = ASSERT_PTR(SCOPE(u)); |
7a7821c8 | 479 | usec_t t; |
68db7a3b ZJS |
480 | int r; |
481 | ||
482 | if (!s->timer_event_source) | |
483 | return 0; | |
484 | ||
7a7821c8 | 485 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
486 | if (r < 0) |
487 | return r; | |
7a7821c8 LP |
488 | if (t == USEC_INFINITY) |
489 | return 0; | |
68db7a3b | 490 | |
7a7821c8 | 491 | *timeout = t; |
68db7a3b ZJS |
492 | return 1; |
493 | } | |
494 | ||
6c12b52e | 495 | static int scope_serialize(Unit *u, FILE *f, FDSet *fds) { |
e9fa1bf7 | 496 | Scope *s = ASSERT_PTR(SCOPE(u)); |
495e75ed | 497 | PidRef *pid; |
6c12b52e | 498 | |
6c12b52e LP |
499 | assert(f); |
500 | assert(fds); | |
501 | ||
d68c645b LP |
502 | (void) serialize_item(f, "state", scope_state_to_string(s->state)); |
503 | (void) serialize_bool(f, "was-abandoned", s->was_abandoned); | |
33fe0afe | 504 | |
66867d53 | 505 | (void) serialize_item(f, "controller", s->controller); |
33fe0afe | 506 | |
495e75ed | 507 | SET_FOREACH(pid, u->pids) |
2a7451dc | 508 | serialize_pidref(f, fds, "pids", pid); |
428a9f6f | 509 | |
6c12b52e LP |
510 | return 0; |
511 | } | |
512 | ||
513 | static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
e9fa1bf7 | 514 | Scope *s = ASSERT_PTR(SCOPE(u)); |
33fe0afe | 515 | int r; |
6c12b52e | 516 | |
6c12b52e LP |
517 | assert(key); |
518 | assert(value); | |
519 | assert(fds); | |
520 | ||
521 | if (streq(key, "state")) { | |
522 | ScopeState state; | |
523 | ||
524 | state = scope_state_from_string(value); | |
525 | if (state < 0) | |
f2341e0a | 526 | log_unit_debug(u, "Failed to parse state value: %s", value); |
6c12b52e LP |
527 | else |
528 | s->deserialized_state = state; | |
529 | ||
3862e809 LP |
530 | } else if (streq(key, "was-abandoned")) { |
531 | int k; | |
532 | ||
533 | k = parse_boolean(value); | |
534 | if (k < 0) | |
535 | log_unit_debug(u, "Failed to parse boolean value: %s", value); | |
536 | else | |
537 | s->was_abandoned = k; | |
33fe0afe LP |
538 | } else if (streq(key, "controller")) { |
539 | ||
540 | r = free_and_strdup(&s->controller, value); | |
541 | if (r < 0) | |
d68c645b | 542 | return log_oom(); |
33fe0afe | 543 | |
428a9f6f | 544 | } else if (streq(key, "pids")) { |
2a7451dc | 545 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
495e75ed | 546 | |
70727771 DDM |
547 | /* We don't check if we already received the pid before here because unit_watch_pidref() |
548 | * does this check internally and discards the new pidref if we already received it before. */ | |
2a7451dc LP |
549 | if (deserialize_pidref(fds, value, &pidref) >= 0) { |
550 | r = unit_watch_pidref(u, &pidref, /* exclusive= */ false); | |
551 | if (r < 0) | |
552 | log_unit_debug(u, "Failed to watch PID, ignoring: %s", value); | |
553 | } | |
6c12b52e | 554 | } else |
f2341e0a | 555 | log_unit_debug(u, "Unknown serialization key: %s", key); |
6c12b52e LP |
556 | |
557 | return 0; | |
558 | } | |
559 | ||
a911bb9a | 560 | static void scope_notify_cgroup_empty_event(Unit *u) { |
e9fa1bf7 | 561 | Scope *s = ASSERT_PTR(SCOPE(u)); |
a911bb9a | 562 | |
f2341e0a | 563 | log_unit_debug(u, "cgroup is empty"); |
a911bb9a LP |
564 | |
565 | if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) | |
566 | scope_enter_dead(s, SCOPE_SUCCESS); | |
567 | } | |
568 | ||
7238fd51 | 569 | static void scope_notify_cgroup_oom_event(Unit *u, bool managed_oom) { |
e9fa1bf7 | 570 | Scope *s = ASSERT_PTR(SCOPE(u)); |
7238fd51 | 571 | |
572 | if (managed_oom) | |
573 | log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd."); | |
574 | else | |
575 | log_unit_debug(u, "Process of control group was killed by the OOM killer."); | |
576 | ||
5fa09835 ML |
577 | if (s->oom_policy == OOM_CONTINUE) |
578 | return; | |
579 | ||
7238fd51 | 580 | switch (s->state) { |
581 | ||
582 | case SCOPE_START_CHOWN: | |
583 | case SCOPE_RUNNING: | |
5fa09835 ML |
584 | scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_OOM_KILL); |
585 | break; | |
586 | ||
7238fd51 | 587 | case SCOPE_STOP_SIGTERM: |
588 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_OOM_KILL); | |
589 | break; | |
590 | ||
591 | case SCOPE_STOP_SIGKILL: | |
592 | if (s->result == SCOPE_SUCCESS) | |
593 | s->result = SCOPE_FAILURE_OOM_KILL; | |
594 | break; | |
595 | /* SCOPE_DEAD, SCOPE_ABANDONED, and SCOPE_FAILED end up in default */ | |
596 | default: | |
597 | ; | |
598 | } | |
599 | } | |
600 | ||
a911bb9a | 601 | static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
e9fa1bf7 | 602 | Scope *s = ASSERT_PTR(SCOPE(u)); |
03860190 MS |
603 | |
604 | if (s->state == SCOPE_START_CHOWN) { | |
605 | if (!is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL)) | |
606 | scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); | |
607 | else | |
608 | scope_enter_running(s); | |
609 | return; | |
610 | } | |
a911bb9a LP |
611 | } |
612 | ||
718db961 | 613 | static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
e9fa1bf7 | 614 | Scope *s = ASSERT_PTR(SCOPE(userdata)); |
6c12b52e | 615 | |
718db961 | 616 | assert(s->timer_event_source == source); |
6c12b52e LP |
617 | |
618 | switch (s->state) { | |
619 | ||
9ed7de60 PW |
620 | case SCOPE_RUNNING: |
621 | log_unit_warning(UNIT(s), "Scope reached runtime time limit. Stopping."); | |
622 | scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_TIMEOUT); | |
623 | break; | |
624 | ||
6c12b52e LP |
625 | case SCOPE_STOP_SIGTERM: |
626 | if (s->kill_context.send_sigkill) { | |
f2341e0a | 627 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); |
6c12b52e LP |
628 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT); |
629 | } else { | |
f2341e0a | 630 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); |
6c12b52e LP |
631 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
632 | } | |
633 | ||
634 | break; | |
635 | ||
636 | case SCOPE_STOP_SIGKILL: | |
f2341e0a | 637 | log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring."); |
6c12b52e LP |
638 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
639 | break; | |
640 | ||
03860190 MS |
641 | case SCOPE_START_CHOWN: |
642 | log_unit_warning(UNIT(s), "User lookup timed out. Entering failed state."); | |
643 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); | |
644 | break; | |
645 | ||
6c12b52e | 646 | default: |
04499a70 | 647 | assert_not_reached(); |
6c12b52e | 648 | } |
718db961 LP |
649 | |
650 | return 0; | |
6c12b52e LP |
651 | } |
652 | ||
a911bb9a LP |
653 | int scope_abandon(Scope *s) { |
654 | assert(s); | |
6c12b52e | 655 | |
efdb0237 LP |
656 | if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) |
657 | return -EPERM; | |
658 | ||
a911bb9a LP |
659 | if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)) |
660 | return -ESTALE; | |
6c12b52e | 661 | |
3862e809 | 662 | s->was_abandoned = true; |
371c0b79 | 663 | |
a1e58e8e | 664 | s->controller = mfree(s->controller); |
371c0b79 LP |
665 | s->controller_track = sd_bus_track_unref(s->controller_track); |
666 | ||
8cb83266 | 667 | scope_set_state(s, SCOPE_ABANDONED); |
6c12b52e | 668 | |
a911bb9a | 669 | return 0; |
6c12b52e LP |
670 | } |
671 | ||
d1e8e8b5 | 672 | static UnitActiveState scope_active_state(Unit *u) { |
e9fa1bf7 | 673 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 674 | |
e9fa1bf7 | 675 | return state_translation_table[s->state]; |
6c12b52e LP |
676 | } |
677 | ||
d1e8e8b5 | 678 | static const char *scope_sub_state_to_string(Unit *u) { |
e9fa1bf7 | 679 | Scope *s = ASSERT_PTR(SCOPE(u)); |
6c12b52e | 680 | |
e9fa1bf7 | 681 | return scope_state_to_string(s->state); |
6c12b52e LP |
682 | } |
683 | ||
04eb582a | 684 | static void scope_enumerate_perpetual(Manager *m) { |
efdb0237 LP |
685 | Unit *u; |
686 | int r; | |
687 | ||
688 | assert(m); | |
689 | ||
690 | /* Let's unconditionally add the "init.scope" special unit | |
691 | * that encapsulates PID 1. Note that PID 1 already is in the | |
692 | * cgroup for this, we hence just need to allocate the object | |
693 | * for it and that's it. */ | |
694 | ||
695 | u = manager_get_unit(m, SPECIAL_INIT_SCOPE); | |
696 | if (!u) { | |
a581e45a | 697 | r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u); |
2b2ca7ff ZJS |
698 | if (r < 0) |
699 | return (void) log_error_errno(r, "Failed to allocate the special %s unit: %m", | |
700 | SPECIAL_INIT_SCOPE); | |
efdb0237 LP |
701 | } |
702 | ||
703 | u->transient = true; | |
f5869324 | 704 | u->perpetual = true; |
efdb0237 | 705 | SCOPE(u)->deserialized_state = SCOPE_RUNNING; |
efdb0237 LP |
706 | |
707 | unit_add_to_load_queue(u); | |
708 | unit_add_to_dbus_queue(u); | |
020b2e41 LB |
709 | /* Enqueue an explicit cgroup realization here. Unlike other cgroups this one already exists and is |
710 | * populated (by us, after all!) already, even when we are not in a reload cycle. Hence we cannot | |
711 | * apply the settings at creation time anymore, but let's at least apply them asynchronously. */ | |
712 | unit_add_to_cgroup_realize_queue(u); | |
efdb0237 LP |
713 | } |
714 | ||
6c12b52e | 715 | static const char* const scope_result_table[_SCOPE_RESULT_MAX] = { |
48d83e33 | 716 | [SCOPE_SUCCESS] = "success", |
6c12b52e | 717 | [SCOPE_FAILURE_RESOURCES] = "resources", |
48d83e33 | 718 | [SCOPE_FAILURE_TIMEOUT] = "timeout", |
7238fd51 | 719 | [SCOPE_FAILURE_OOM_KILL] = "oom-kill", |
6c12b52e LP |
720 | }; |
721 | ||
722 | DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult); | |
723 | ||
724 | const UnitVTable scope_vtable = { | |
725 | .object_size = sizeof(Scope), | |
718db961 LP |
726 | .cgroup_context_offset = offsetof(Scope, cgroup_context), |
727 | .kill_context_offset = offsetof(Scope, kill_context), | |
9cc54544 | 728 | .cgroup_runtime_offset = offsetof(Scope, cgroup_runtime), |
718db961 | 729 | |
6c12b52e LP |
730 | .sections = |
731 | "Unit\0" | |
732 | "Scope\0" | |
733 | "Install\0", | |
6c12b52e | 734 | .private_section = "Scope", |
6c12b52e | 735 | |
700e2d63 | 736 | .can_transient = true, |
1d9cc876 | 737 | .can_delegate = true, |
c80a9a33 | 738 | .can_fail = true, |
d4fd1cf2 | 739 | .once_only = true, |
4d824a4e | 740 | .can_set_managed_oom = true, |
6c12b52e LP |
741 | |
742 | .init = scope_init, | |
743 | .load = scope_load, | |
744 | .done = scope_done, | |
745 | ||
746 | .coldplug = scope_coldplug, | |
747 | ||
748 | .dump = scope_dump, | |
749 | ||
750 | .start = scope_start, | |
751 | .stop = scope_stop, | |
752 | ||
16b6af6a | 753 | .freezer_action = unit_cgroup_freezer_action, |
d9e45bc3 | 754 | |
68db7a3b ZJS |
755 | .get_timeout = scope_get_timeout, |
756 | ||
6c12b52e LP |
757 | .serialize = scope_serialize, |
758 | .deserialize_item = scope_deserialize_item, | |
759 | ||
760 | .active_state = scope_active_state, | |
761 | .sub_state_to_string = scope_sub_state_to_string, | |
762 | ||
a911bb9a LP |
763 | .sigchld_event = scope_sigchld_event, |
764 | ||
8bcca7e2 LP |
765 | .reset_failed = scope_reset_failed, |
766 | ||
6c12b52e | 767 | .notify_cgroup_empty = scope_notify_cgroup_empty_event, |
7238fd51 | 768 | .notify_cgroup_oom = scope_notify_cgroup_oom_event, |
6c12b52e | 769 | |
6c12b52e LP |
770 | .bus_set_property = bus_scope_set_property, |
771 | .bus_commit_properties = bus_scope_commit_properties, | |
772 | ||
04eb582a | 773 | .enumerate_perpetual = scope_enumerate_perpetual, |
6c12b52e | 774 | }; |