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