]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/scope.c
core: initialize groups list before checking SupplementaryGroups= of a unit (#4533)
[thirdparty/systemd.git] / src / core / scope.c
CommitLineData
6c12b52e
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <errno.h>
6c12b52e
LP
21#include <unistd.h>
22
b5efdb8a 23#include "alloc-util.h"
07630cea
LP
24#include "dbus-scope.h"
25#include "load-dropin.h"
6c12b52e 26#include "log.h"
b5efdb8a 27#include "scope.h"
6c12b52e 28#include "special.h"
8b43440b 29#include "string-table.h"
07630cea
LP
30#include "string-util.h"
31#include "strv.h"
6c12b52e 32#include "unit-name.h"
efdb0237 33#include "unit.h"
6c12b52e
LP
34
35static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
36 [SCOPE_DEAD] = UNIT_INACTIVE,
37 [SCOPE_RUNNING] = UNIT_ACTIVE,
a911bb9a 38 [SCOPE_ABANDONED] = UNIT_ACTIVE,
6c12b52e
LP
39 [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
40 [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
41 [SCOPE_FAILED] = UNIT_FAILED
42};
43
718db961
LP
44static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
45
6c12b52e
LP
46static void scope_init(Unit *u) {
47 Scope *s = SCOPE(u);
48
49 assert(u);
50 assert(u->load_state == UNIT_STUB);
51
1f19a534 52 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
1b4cd0cf 53 u->ignore_on_isolate = true;
6c12b52e
LP
54}
55
56static void scope_done(Unit *u) {
57 Scope *s = SCOPE(u);
58
59 assert(u);
60
2d4a39e7
LP
61 free(s->controller);
62
718db961
LP
63 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
64}
65
36c16a7c 66static int scope_arm_timer(Scope *s, usec_t usec) {
718db961
LP
67 int r;
68
69 assert(s);
70
718db961 71 if (s->timer_event_source) {
36c16a7c 72 r = sd_event_source_set_time(s->timer_event_source, usec);
718db961
LP
73 if (r < 0)
74 return r;
75
76 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
77 }
78
36c16a7c
LP
79 if (usec == USEC_INFINITY)
80 return 0;
81
cbf60d0a 82 r = sd_event_add_time(
6a0f1f6d
LP
83 UNIT(s)->manager->event,
84 &s->timer_event_source,
85 CLOCK_MONOTONIC,
36c16a7c 86 usec, 0,
6a0f1f6d 87 scope_dispatch_timer, s);
7dfbe2e3
TG
88 if (r < 0)
89 return r;
90
91 (void) sd_event_source_set_description(s->timer_event_source, "scope-timer");
92
93 return 0;
6c12b52e
LP
94}
95
96static void scope_set_state(Scope *s, ScopeState state) {
97 ScopeState old_state;
98 assert(s);
99
100 old_state = s->state;
101 s->state = state;
102
a911bb9a 103 if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
718db961 104 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
6c12b52e 105
a911bb9a
LP
106 if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED))
107 unit_unwatch_all_pids(UNIT(s));
108
6c12b52e 109 if (state != old_state)
a911bb9a 110 log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state));
6c12b52e
LP
111
112 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
113}
114
115static int scope_add_default_dependencies(Scope *s) {
116 int r;
117
118 assert(s);
119
4c9ea260
LP
120 if (!UNIT(s)->default_dependencies)
121 return 0;
122
6c12b52e
LP
123 /* Make sure scopes are unloaded on shutdown */
124 r = unit_add_two_dependencies_by_name(
125 UNIT(s),
126 UNIT_BEFORE, UNIT_CONFLICTS,
127 SPECIAL_SHUTDOWN_TARGET, NULL, true);
128 if (r < 0)
129 return r;
130
131 return 0;
132}
133
134static int scope_verify(Scope *s) {
135 assert(s);
136
137 if (UNIT(s)->load_state != UNIT_LOADED)
138 return 0;
139
efdb0237 140 if (set_isempty(UNIT(s)->pids) &&
2c289ea8 141 !MANAGER_IS_RELOADING(UNIT(s)->manager) &&
efdb0237 142 !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) {
f2341e0a 143 log_unit_error(UNIT(s), "Scope has no PIDs. Refusing.");
6c12b52e
LP
144 return -EINVAL;
145 }
146
147 return 0;
148}
149
8e4e851f
LP
150static int scope_load_init_scope(Unit *u) {
151 assert(u);
152
153 if (!unit_has_name(u, SPECIAL_INIT_SCOPE))
154 return 0;
155
156 u->transient = true;
157 u->no_gc = true;
158
159 /* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we
160 * synthesize it here, instead of relying on the unit file on disk. */
161
162 u->default_dependencies = false;
163 u->ignore_on_isolate = true;
164 u->refuse_manual_start = true;
165 u->refuse_manual_stop = true;
166
167 SCOPE(u)->kill_context.kill_signal = SIGRTMIN+14;
168
169 /* Prettify things, if we can. */
170 if (!u->description)
171 u->description = strdup("System and Service Manager");
172 if (!u->documentation)
173 (void) strv_extend(&u->documentation, "man:systemd(1)");
174
175 return 1;
176}
177
6c12b52e
LP
178static int scope_load(Unit *u) {
179 Scope *s = SCOPE(u);
180 int r;
181
182 assert(s);
183 assert(u->load_state == UNIT_STUB);
184
2c289ea8 185 if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
4f4afc88 186 /* Refuse to load non-transient scope units, but allow them while reloading. */
6c12b52e
LP
187 return -ENOENT;
188
8e4e851f
LP
189 r = scope_load_init_scope(u);
190 if (r < 0)
191 return r;
4f4afc88 192 r = unit_load_fragment_and_dropin_optional(u);
6c12b52e
LP
193 if (r < 0)
194 return r;
195
4f4afc88
LP
196 if (u->load_state == UNIT_LOADED) {
197 r = unit_patch_contexts(u);
198 if (r < 0)
199 return r;
598459ce 200
4f4afc88
LP
201 r = unit_set_default_slice(u);
202 if (r < 0)
203 return r;
6c12b52e 204
4f4afc88
LP
205 r = scope_add_default_dependencies(s);
206 if (r < 0)
207 return r;
208 }
6c12b52e
LP
209
210 return scope_verify(s);
211}
212
be847e82 213static int scope_coldplug(Unit *u) {
6c12b52e
LP
214 Scope *s = SCOPE(u);
215 int r;
216
217 assert(s);
218 assert(s->state == SCOPE_DEAD);
219
36c16a7c
LP
220 if (s->deserialized_state == s->state)
221 return 0;
a911bb9a 222
36c16a7c
LP
223 if (IN_SET(s->deserialized_state, SCOPE_STOP_SIGKILL, SCOPE_STOP_SIGTERM)) {
224 r = scope_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_stop_usec));
225 if (r < 0)
226 return r;
6c12b52e
LP
227 }
228
36c16a7c
LP
229 if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED))
230 unit_watch_all_pids(UNIT(s));
231
232 scope_set_state(s, s->deserialized_state);
6c12b52e
LP
233 return 0;
234}
235
236static void scope_dump(Unit *u, FILE *f, const char *prefix) {
237 Scope *s = SCOPE(u);
238
239 assert(s);
240 assert(f);
241
242 fprintf(f,
243 "%sScope State: %s\n"
244 "%sResult: %s\n",
245 prefix, scope_state_to_string(s->state),
246 prefix, scope_result_to_string(s->result));
247
248 cgroup_context_dump(&s->cgroup_context, f, prefix);
249 kill_context_dump(&s->kill_context, f, prefix);
250}
251
252static void scope_enter_dead(Scope *s, ScopeResult f) {
253 assert(s);
254
a0fef983 255 if (s->result == SCOPE_SUCCESS)
6c12b52e
LP
256 s->result = f;
257
258 scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD);
259}
260
261static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
2d4a39e7 262 bool skip_signal = false;
6c12b52e
LP
263 int r;
264
265 assert(s);
266
a0fef983 267 if (s->result == SCOPE_SUCCESS)
6c12b52e
LP
268 s->result = f;
269
a911bb9a
LP
270 unit_watch_all_pids(UNIT(s));
271
2d4a39e7
LP
272 /* If we have a controller set let's ask the controller nicely
273 * to terminate the scope, instead of us going directly into
1d98fef1 274 * SIGTERM berserk mode */
2d4a39e7
LP
275 if (state == SCOPE_STOP_SIGTERM)
276 skip_signal = bus_scope_send_request_stop(s) > 0;
277
278 if (!skip_signal) {
279 r = unit_kill_context(
280 UNIT(s),
281 &s->kill_context,
3862e809
LP
282 state != SCOPE_STOP_SIGTERM ? KILL_KILL :
283 s->was_abandoned ? KILL_TERMINATE_AND_LOG :
284 KILL_TERMINATE,
2d4a39e7
LP
285 -1, -1, false);
286 if (r < 0)
287 goto fail;
288 } else
289 r = 1;
6c12b52e
LP
290
291 if (r > 0) {
36c16a7c 292 r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
718db961
LP
293 if (r < 0)
294 goto fail;
6c12b52e
LP
295
296 scope_set_state(s, state);
ac84d1fb
LP
297 } else if (state == SCOPE_STOP_SIGTERM)
298 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
299 else
6c12b52e
LP
300 scope_enter_dead(s, SCOPE_SUCCESS);
301
302 return;
303
304fail:
f2341e0a 305 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
6c12b52e
LP
306
307 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
308}
309
310static int scope_start(Unit *u) {
311 Scope *s = SCOPE(u);
312 int r;
313
314 assert(s);
315
efdb0237
LP
316 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
317 return -EPERM;
318
7b617155
LP
319 if (s->state == SCOPE_FAILED)
320 return -EPERM;
321
dd305ec9 322 /* We can't fulfill this right now, please try again later */
6c12b52e
LP
323 if (s->state == SCOPE_STOP_SIGTERM ||
324 s->state == SCOPE_STOP_SIGKILL)
325 return -EAGAIN;
326
327 assert(s->state == SCOPE_DEAD);
328
2c289ea8 329 if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
6c12b52e
LP
330 return -ENOENT;
331
4b58153d
LP
332 r = unit_acquire_invocation_id(u);
333 if (r < 0)
334 return r;
335
5ad096b3
LP
336 (void) unit_realize_cgroup(u);
337 (void) unit_reset_cpu_usage(u);
338
7b3fd631 339 r = unit_attach_pids_to_cgroup(u);
dd305ec9 340 if (r < 0) {
f2341e0a 341 log_unit_warning_errno(UNIT(s), r, "Failed to add PIDs to scope's control group: %m");
68a01fb6 342 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
6c12b52e 343 return r;
dd305ec9 344 }
6c12b52e 345
6c12b52e
LP
346 s->result = SCOPE_SUCCESS;
347
348 scope_set_state(s, SCOPE_RUNNING);
82a2b6bb 349 return 1;
6c12b52e
LP
350}
351
352static int scope_stop(Unit *u) {
353 Scope *s = SCOPE(u);
354
355 assert(s);
6c12b52e
LP
356
357 if (s->state == SCOPE_STOP_SIGTERM ||
358 s->state == SCOPE_STOP_SIGKILL)
359 return 0;
360
a911bb9a
LP
361 assert(s->state == SCOPE_RUNNING ||
362 s->state == SCOPE_ABANDONED);
6c12b52e
LP
363
364 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
82a2b6bb 365 return 1;
6c12b52e
LP
366}
367
8bcca7e2
LP
368static void scope_reset_failed(Unit *u) {
369 Scope *s = SCOPE(u);
370
371 assert(s);
372
373 if (s->state == SCOPE_FAILED)
374 scope_set_state(s, SCOPE_DEAD);
375
376 s->result = SCOPE_SUCCESS;
377}
378
718db961 379static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
6c12b52e
LP
380 return unit_kill_common(u, who, signo, -1, -1, error);
381}
382
7a7821c8 383static int scope_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 384 Scope *s = SCOPE(u);
7a7821c8 385 usec_t t;
68db7a3b
ZJS
386 int r;
387
388 if (!s->timer_event_source)
389 return 0;
390
7a7821c8 391 r = sd_event_source_get_time(s->timer_event_source, &t);
68db7a3b
ZJS
392 if (r < 0)
393 return r;
7a7821c8
LP
394 if (t == USEC_INFINITY)
395 return 0;
68db7a3b 396
7a7821c8 397 *timeout = t;
68db7a3b
ZJS
398 return 1;
399}
400
6c12b52e
LP
401static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
402 Scope *s = SCOPE(u);
403
404 assert(s);
405 assert(f);
406 assert(fds);
407
408 unit_serialize_item(u, f, "state", scope_state_to_string(s->state));
3862e809 409 unit_serialize_item(u, f, "was-abandoned", yes_no(s->was_abandoned));
6c12b52e
LP
410 return 0;
411}
412
413static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
414 Scope *s = SCOPE(u);
415
416 assert(u);
417 assert(key);
418 assert(value);
419 assert(fds);
420
421 if (streq(key, "state")) {
422 ScopeState state;
423
424 state = scope_state_from_string(value);
425 if (state < 0)
f2341e0a 426 log_unit_debug(u, "Failed to parse state value: %s", value);
6c12b52e
LP
427 else
428 s->deserialized_state = state;
429
3862e809
LP
430 } else if (streq(key, "was-abandoned")) {
431 int k;
432
433 k = parse_boolean(value);
434 if (k < 0)
435 log_unit_debug(u, "Failed to parse boolean value: %s", value);
436 else
437 s->was_abandoned = k;
6c12b52e 438 } else
f2341e0a 439 log_unit_debug(u, "Unknown serialization key: %s", key);
6c12b52e
LP
440
441 return 0;
442}
443
444static bool scope_check_gc(Unit *u) {
4e2744fc 445 assert(u);
6c12b52e
LP
446
447 /* Never clean up scopes that still have a process around,
448 * even if the scope is formally dead. */
449
700e2d63
LP
450 if (!u->cgroup_path)
451 return false;
4e2744fc 452
700e2d63 453 return cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path) <= 0;
6c12b52e
LP
454}
455
a911bb9a
LP
456static void scope_notify_cgroup_empty_event(Unit *u) {
457 Scope *s = SCOPE(u);
458 assert(u);
459
f2341e0a 460 log_unit_debug(u, "cgroup is empty");
a911bb9a
LP
461
462 if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
463 scope_enter_dead(s, SCOPE_SUCCESS);
464}
465
466static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
467
468 /* If we get a SIGCHLD event for one of the processes we were
469 interested in, then we look for others to watch, under the
470 assumption that we'll sooner or later get a SIGCHLD for
471 them, as the original process we watched was probably the
472 parent of them, and they are hence now our children. */
473
474 unit_tidy_watch_pids(u, 0, 0);
475 unit_watch_all_pids(u);
476
bbc85a16
EV
477 /* If the PID set is empty now, then let's finish this off
478 (On unified we use proper notifications) */
5da38d07 479 if (cg_unified(SYSTEMD_CGROUP_CONTROLLER) <= 0 && set_isempty(u->pids))
a911bb9a
LP
480 scope_notify_cgroup_empty_event(u);
481}
482
718db961
LP
483static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
484 Scope *s = SCOPE(userdata);
6c12b52e
LP
485
486 assert(s);
718db961 487 assert(s->timer_event_source == source);
6c12b52e
LP
488
489 switch (s->state) {
490
491 case SCOPE_STOP_SIGTERM:
492 if (s->kill_context.send_sigkill) {
f2341e0a 493 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
6c12b52e
LP
494 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
495 } else {
f2341e0a 496 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
6c12b52e
LP
497 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
498 }
499
500 break;
501
502 case SCOPE_STOP_SIGKILL:
f2341e0a 503 log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
6c12b52e
LP
504 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
505 break;
506
507 default:
508 assert_not_reached("Timeout at wrong time.");
509 }
718db961
LP
510
511 return 0;
6c12b52e
LP
512}
513
a911bb9a
LP
514int scope_abandon(Scope *s) {
515 assert(s);
6c12b52e 516
efdb0237
LP
517 if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
518 return -EPERM;
519
a911bb9a
LP
520 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
521 return -ESTALE;
6c12b52e 522
3862e809 523 s->was_abandoned = true;
a1e58e8e 524 s->controller = mfree(s->controller);
6c12b52e 525
a911bb9a
LP
526 /* The client is no longer watching the remaining processes,
527 * so let's step in here, under the assumption that the
528 * remaining processes will be sooner or later reassigned to
529 * us as parent. */
6c12b52e 530
a911bb9a
LP
531 unit_tidy_watch_pids(UNIT(s), 0, 0);
532 unit_watch_all_pids(UNIT(s));
6c12b52e 533
a911bb9a
LP
534 /* If the PID set is empty now, then let's finish this off */
535 if (set_isempty(UNIT(s)->pids))
536 scope_notify_cgroup_empty_event(UNIT(s));
537 else
538 scope_set_state(s, SCOPE_ABANDONED);
539
540 return 0;
6c12b52e
LP
541}
542
543_pure_ static UnitActiveState scope_active_state(Unit *u) {
544 assert(u);
545
546 return state_translation_table[SCOPE(u)->state];
547}
548
549_pure_ static const char *scope_sub_state_to_string(Unit *u) {
550 assert(u);
551
552 return scope_state_to_string(SCOPE(u)->state);
553}
554
ba64af90 555static void scope_enumerate(Manager *m) {
efdb0237
LP
556 Unit *u;
557 int r;
558
559 assert(m);
560
561 /* Let's unconditionally add the "init.scope" special unit
562 * that encapsulates PID 1. Note that PID 1 already is in the
563 * cgroup for this, we hence just need to allocate the object
564 * for it and that's it. */
565
566 u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
567 if (!u) {
568 u = unit_new(m, sizeof(Scope));
ba64af90
LP
569 if (!u) {
570 log_oom();
571 return;
572 }
efdb0237
LP
573
574 r = unit_add_name(u, SPECIAL_INIT_SCOPE);
575 if (r < 0) {
576 unit_free(u);
8e4e851f 577 log_error_errno(r, "Failed to add the " SPECIAL_INIT_SCOPE " name: %m");
ba64af90 578 return;
efdb0237
LP
579 }
580 }
581
582 u->transient = true;
efdb0237
LP
583 u->no_gc = true;
584 SCOPE(u)->deserialized_state = SCOPE_RUNNING;
efdb0237
LP
585
586 unit_add_to_load_queue(u);
587 unit_add_to_dbus_queue(u);
efdb0237
LP
588}
589
6c12b52e
LP
590static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
591 [SCOPE_SUCCESS] = "success",
592 [SCOPE_FAILURE_RESOURCES] = "resources",
593 [SCOPE_FAILURE_TIMEOUT] = "timeout",
594};
595
596DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
597
598const UnitVTable scope_vtable = {
599 .object_size = sizeof(Scope),
718db961
LP
600 .cgroup_context_offset = offsetof(Scope, cgroup_context),
601 .kill_context_offset = offsetof(Scope, kill_context),
602
6c12b52e
LP
603 .sections =
604 "Unit\0"
605 "Scope\0"
606 "Install\0",
6c12b52e 607 .private_section = "Scope",
6c12b52e 608
700e2d63 609 .can_transient = true,
6c12b52e
LP
610
611 .init = scope_init,
612 .load = scope_load,
613 .done = scope_done,
614
615 .coldplug = scope_coldplug,
616
617 .dump = scope_dump,
618
619 .start = scope_start,
620 .stop = scope_stop,
621
622 .kill = scope_kill,
623
68db7a3b
ZJS
624 .get_timeout = scope_get_timeout,
625
6c12b52e
LP
626 .serialize = scope_serialize,
627 .deserialize_item = scope_deserialize_item,
628
629 .active_state = scope_active_state,
630 .sub_state_to_string = scope_sub_state_to_string,
631
632 .check_gc = scope_check_gc,
633
a911bb9a
LP
634 .sigchld_event = scope_sigchld_event,
635
8bcca7e2
LP
636 .reset_failed = scope_reset_failed,
637
6c12b52e
LP
638 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
639
718db961 640 .bus_vtable = bus_scope_vtable,
6c12b52e
LP
641 .bus_set_property = bus_scope_set_property,
642 .bus_commit_properties = bus_scope_commit_properties,
643
efdb0237 644 .enumerate = scope_enumerate,
6c12b52e 645};