]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/scope.c
a7c3c302eef213cf33948f75262cbc0f68a8c51f
[thirdparty/systemd.git] / src / core / scope.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2013 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "dbus-scope.h"
11 #include "load-dropin.h"
12 #include "log.h"
13 #include "scope.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
21 static 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
30 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
31
32 static void scope_init(Unit *u) {
33 Scope *s = SCOPE(u);
34
35 assert(u);
36 assert(u->load_state == UNIT_STUB);
37
38 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
39 u->ignore_on_isolate = true;
40 }
41
42 static void scope_done(Unit *u) {
43 Scope *s = SCOPE(u);
44
45 assert(u);
46
47 s->controller = mfree(s->controller);
48 s->controller_track = sd_bus_track_unref(s->controller_track);
49
50 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
51 }
52
53 static int scope_arm_timer(Scope *s, usec_t usec) {
54 int r;
55
56 assert(s);
57
58 if (s->timer_event_source) {
59 r = sd_event_source_set_time(s->timer_event_source, usec);
60 if (r < 0)
61 return r;
62
63 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
64 }
65
66 if (usec == USEC_INFINITY)
67 return 0;
68
69 r = sd_event_add_time(
70 UNIT(s)->manager->event,
71 &s->timer_event_source,
72 CLOCK_MONOTONIC,
73 usec, 0,
74 scope_dispatch_timer, s);
75 if (r < 0)
76 return r;
77
78 (void) sd_event_source_set_description(s->timer_event_source, "scope-timer");
79
80 return 0;
81 }
82
83 static void scope_set_state(Scope *s, ScopeState state) {
84 ScopeState old_state;
85 assert(s);
86
87 old_state = s->state;
88 s->state = state;
89
90 if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
91 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
92
93 if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) {
94 unit_unwatch_all_pids(UNIT(s));
95 unit_dequeue_rewatch_pids(UNIT(s));
96 }
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], 0);
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 -ENOENT;
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 (void) unit_enqueue_rewatch_pids(u);
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 /* Before sending any signal, make sure we track all members of this cgroup */
261 (void) unit_watch_all_pids(UNIT(s));
262
263 /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
264 * died now */
265 (void) unit_enqueue_rewatch_pids(UNIT(s));
266
267 /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going
268 * directly into SIGTERM berserk mode */
269 if (state == SCOPE_STOP_SIGTERM)
270 skip_signal = bus_scope_send_request_stop(s) > 0;
271
272 if (skip_signal)
273 r = 1; /* wait */
274 else {
275 r = unit_kill_context(
276 UNIT(s),
277 &s->kill_context,
278 state != SCOPE_STOP_SIGTERM ? KILL_KILL :
279 s->was_abandoned ? KILL_TERMINATE_AND_LOG :
280 KILL_TERMINATE,
281 -1, -1, false);
282 if (r < 0)
283 goto fail;
284 }
285
286 if (r > 0) {
287 r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
288 if (r < 0)
289 goto fail;
290
291 scope_set_state(s, state);
292 } else if (state == SCOPE_STOP_SIGTERM)
293 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
294 else
295 scope_enter_dead(s, SCOPE_SUCCESS);
296
297 return;
298
299 fail:
300 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
301
302 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
303 }
304
305 static int scope_start(Unit *u) {
306 Scope *s = SCOPE(u);
307 int r;
308
309 assert(s);
310
311 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
312 return -EPERM;
313
314 if (s->state == SCOPE_FAILED)
315 return -EPERM;
316
317 /* We can't fulfill this right now, please try again later */
318 if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
319 return -EAGAIN;
320
321 assert(s->state == SCOPE_DEAD);
322
323 if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
324 return -ENOENT;
325
326 (void) bus_scope_track_controller(s);
327
328 r = unit_acquire_invocation_id(u);
329 if (r < 0)
330 return r;
331
332 (void) unit_realize_cgroup(u);
333 (void) unit_reset_cpu_accounting(u);
334 (void) unit_reset_ip_accounting(u);
335
336 unit_export_state_files(UNIT(s));
337
338 r = unit_attach_pids_to_cgroup(u, UNIT(s)->pids, NULL);
339 if (r < 0) {
340 log_unit_warning_errno(UNIT(s), r, "Failed to add PIDs to scope's control group: %m");
341 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
342 return r;
343 }
344
345 s->result = SCOPE_SUCCESS;
346
347 scope_set_state(s, SCOPE_RUNNING);
348
349 /* Start watching the PIDs currently in the scope */
350 (void) unit_enqueue_rewatch_pids(UNIT(s));
351 return 1;
352 }
353
354 static int scope_stop(Unit *u) {
355 Scope *s = SCOPE(u);
356
357 assert(s);
358
359 if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
360 return 0;
361
362 assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED));
363
364 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
365 return 1;
366 }
367
368 static 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
379 static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
380 return unit_kill_common(u, who, signo, -1, -1, error);
381 }
382
383 static int scope_get_timeout(Unit *u, usec_t *timeout) {
384 Scope *s = SCOPE(u);
385 usec_t t;
386 int r;
387
388 if (!s->timer_event_source)
389 return 0;
390
391 r = sd_event_source_get_time(s->timer_event_source, &t);
392 if (r < 0)
393 return r;
394 if (t == USEC_INFINITY)
395 return 0;
396
397 *timeout = t;
398 return 1;
399 }
400
401 static 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));
409 unit_serialize_item(u, f, "was-abandoned", yes_no(s->was_abandoned));
410
411 if (s->controller)
412 unit_serialize_item(u, f, "controller", s->controller);
413
414 return 0;
415 }
416
417 static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
418 Scope *s = SCOPE(u);
419 int r;
420
421 assert(u);
422 assert(key);
423 assert(value);
424 assert(fds);
425
426 if (streq(key, "state")) {
427 ScopeState state;
428
429 state = scope_state_from_string(value);
430 if (state < 0)
431 log_unit_debug(u, "Failed to parse state value: %s", value);
432 else
433 s->deserialized_state = state;
434
435 } else if (streq(key, "was-abandoned")) {
436 int k;
437
438 k = parse_boolean(value);
439 if (k < 0)
440 log_unit_debug(u, "Failed to parse boolean value: %s", value);
441 else
442 s->was_abandoned = k;
443 } else if (streq(key, "controller")) {
444
445 r = free_and_strdup(&s->controller, value);
446 if (r < 0)
447 log_oom();
448
449 } else
450 log_unit_debug(u, "Unknown serialization key: %s", key);
451
452 return 0;
453 }
454
455 static void scope_notify_cgroup_empty_event(Unit *u) {
456 Scope *s = SCOPE(u);
457 assert(u);
458
459 log_unit_debug(u, "cgroup is empty");
460
461 if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
462 scope_enter_dead(s, SCOPE_SUCCESS);
463 }
464
465 static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
466 assert(u);
467
468 /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
469 * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
470 * process we watched was probably the parent of them, and they are hence now our children. */
471
472 (void) unit_enqueue_rewatch_pids(u);
473 }
474
475 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
476 Scope *s = SCOPE(userdata);
477
478 assert(s);
479 assert(s->timer_event_source == source);
480
481 switch (s->state) {
482
483 case SCOPE_STOP_SIGTERM:
484 if (s->kill_context.send_sigkill) {
485 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
486 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
487 } else {
488 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
489 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
490 }
491
492 break;
493
494 case SCOPE_STOP_SIGKILL:
495 log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
496 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
497 break;
498
499 default:
500 assert_not_reached("Timeout at wrong time.");
501 }
502
503 return 0;
504 }
505
506 int scope_abandon(Scope *s) {
507 assert(s);
508
509 if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
510 return -EPERM;
511
512 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
513 return -ESTALE;
514
515 s->was_abandoned = true;
516
517 s->controller = mfree(s->controller);
518 s->controller_track = sd_bus_track_unref(s->controller_track);
519
520 scope_set_state(s, SCOPE_ABANDONED);
521
522 /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
523 * the remaining processes will be sooner or later reassigned to us as parent. */
524 (void) unit_enqueue_rewatch_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_perpetual(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 .once_only = true,
591
592 .init = scope_init,
593 .load = scope_load,
594 .done = scope_done,
595
596 .coldplug = scope_coldplug,
597
598 .dump = scope_dump,
599
600 .start = scope_start,
601 .stop = scope_stop,
602
603 .kill = scope_kill,
604
605 .get_timeout = scope_get_timeout,
606
607 .serialize = scope_serialize,
608 .deserialize_item = scope_deserialize_item,
609
610 .active_state = scope_active_state,
611 .sub_state_to_string = scope_sub_state_to_string,
612
613 .sigchld_event = scope_sigchld_event,
614
615 .reset_failed = scope_reset_failed,
616
617 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
618
619 .bus_vtable = bus_scope_vtable,
620 .bus_set_property = bus_scope_set_property,
621 .bus_commit_properties = bus_scope_commit_properties,
622
623 .enumerate_perpetual = scope_enumerate_perpetual,
624 };