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