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