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