]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/scope.c
core: remove ManagerRunningAs enum
[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) &&
09d2f5b1 141 !manager_is_reloading_or_reexecuting(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
150static int scope_load(Unit *u) {
151 Scope *s = SCOPE(u);
152 int r;
153
154 assert(s);
155 assert(u->load_state == UNIT_STUB);
156
efdb0237 157 if (!u->transient && !manager_is_reloading_or_reexecuting(u->manager))
6c12b52e
LP
158 return -ENOENT;
159
160 u->load_state = UNIT_LOADED;
161
162 r = unit_load_dropin(u);
163 if (r < 0)
164 return r;
165
598459ce
LP
166 r = unit_patch_contexts(u);
167 if (r < 0)
168 return r;
169
d79200e2 170 r = unit_set_default_slice(u);
6c12b52e
LP
171 if (r < 0)
172 return r;
173
4c9ea260
LP
174 r = scope_add_default_dependencies(s);
175 if (r < 0)
176 return r;
6c12b52e
LP
177
178 return scope_verify(s);
179}
180
be847e82 181static int scope_coldplug(Unit *u) {
6c12b52e
LP
182 Scope *s = SCOPE(u);
183 int r;
184
185 assert(s);
186 assert(s->state == SCOPE_DEAD);
187
36c16a7c
LP
188 if (s->deserialized_state == s->state)
189 return 0;
a911bb9a 190
36c16a7c
LP
191 if (IN_SET(s->deserialized_state, SCOPE_STOP_SIGKILL, SCOPE_STOP_SIGTERM)) {
192 r = scope_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_stop_usec));
193 if (r < 0)
194 return r;
6c12b52e
LP
195 }
196
36c16a7c
LP
197 if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED))
198 unit_watch_all_pids(UNIT(s));
199
200 scope_set_state(s, s->deserialized_state);
6c12b52e
LP
201 return 0;
202}
203
204static void scope_dump(Unit *u, FILE *f, const char *prefix) {
205 Scope *s = SCOPE(u);
206
207 assert(s);
208 assert(f);
209
210 fprintf(f,
211 "%sScope State: %s\n"
212 "%sResult: %s\n",
213 prefix, scope_state_to_string(s->state),
214 prefix, scope_result_to_string(s->result));
215
216 cgroup_context_dump(&s->cgroup_context, f, prefix);
217 kill_context_dump(&s->kill_context, f, prefix);
218}
219
220static void scope_enter_dead(Scope *s, ScopeResult f) {
221 assert(s);
222
223 if (f != SCOPE_SUCCESS)
224 s->result = f;
225
226 scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD);
227}
228
229static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
2d4a39e7 230 bool skip_signal = false;
6c12b52e
LP
231 int r;
232
233 assert(s);
234
235 if (f != SCOPE_SUCCESS)
236 s->result = f;
237
a911bb9a
LP
238 unit_watch_all_pids(UNIT(s));
239
2d4a39e7
LP
240 /* If we have a controller set let's ask the controller nicely
241 * to terminate the scope, instead of us going directly into
242 * SIGTERM beserk mode */
243 if (state == SCOPE_STOP_SIGTERM)
244 skip_signal = bus_scope_send_request_stop(s) > 0;
245
246 if (!skip_signal) {
247 r = unit_kill_context(
248 UNIT(s),
249 &s->kill_context,
db2cb23b 250 state != SCOPE_STOP_SIGTERM ? KILL_KILL : KILL_TERMINATE,
2d4a39e7
LP
251 -1, -1, false);
252 if (r < 0)
253 goto fail;
254 } else
255 r = 1;
6c12b52e
LP
256
257 if (r > 0) {
36c16a7c 258 r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
718db961
LP
259 if (r < 0)
260 goto fail;
6c12b52e
LP
261
262 scope_set_state(s, state);
ac84d1fb
LP
263 } else if (state == SCOPE_STOP_SIGTERM)
264 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
265 else
6c12b52e
LP
266 scope_enter_dead(s, SCOPE_SUCCESS);
267
268 return;
269
270fail:
f2341e0a 271 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
6c12b52e
LP
272
273 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
274}
275
276static int scope_start(Unit *u) {
277 Scope *s = SCOPE(u);
278 int r;
279
280 assert(s);
281
efdb0237
LP
282 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
283 return -EPERM;
284
7b617155
LP
285 if (s->state == SCOPE_FAILED)
286 return -EPERM;
287
dd305ec9 288 /* We can't fulfill this right now, please try again later */
6c12b52e
LP
289 if (s->state == SCOPE_STOP_SIGTERM ||
290 s->state == SCOPE_STOP_SIGKILL)
291 return -EAGAIN;
292
293 assert(s->state == SCOPE_DEAD);
294
efdb0237 295 if (!u->transient && !manager_is_reloading_or_reexecuting(u->manager))
6c12b52e
LP
296 return -ENOENT;
297
5ad096b3
LP
298 (void) unit_realize_cgroup(u);
299 (void) unit_reset_cpu_usage(u);
300
7b3fd631 301 r = unit_attach_pids_to_cgroup(u);
dd305ec9 302 if (r < 0) {
f2341e0a 303 log_unit_warning_errno(UNIT(s), r, "Failed to add PIDs to scope's control group: %m");
68a01fb6 304 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
6c12b52e 305 return r;
dd305ec9 306 }
6c12b52e 307
6c12b52e
LP
308 s->result = SCOPE_SUCCESS;
309
310 scope_set_state(s, SCOPE_RUNNING);
82a2b6bb 311 return 1;
6c12b52e
LP
312}
313
314static int scope_stop(Unit *u) {
315 Scope *s = SCOPE(u);
316
317 assert(s);
6c12b52e
LP
318
319 if (s->state == SCOPE_STOP_SIGTERM ||
320 s->state == SCOPE_STOP_SIGKILL)
321 return 0;
322
a911bb9a
LP
323 assert(s->state == SCOPE_RUNNING ||
324 s->state == SCOPE_ABANDONED);
6c12b52e
LP
325
326 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
82a2b6bb 327 return 1;
6c12b52e
LP
328}
329
8bcca7e2
LP
330static void scope_reset_failed(Unit *u) {
331 Scope *s = SCOPE(u);
332
333 assert(s);
334
335 if (s->state == SCOPE_FAILED)
336 scope_set_state(s, SCOPE_DEAD);
337
338 s->result = SCOPE_SUCCESS;
339}
340
718db961 341static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
6c12b52e
LP
342 return unit_kill_common(u, who, signo, -1, -1, error);
343}
344
7a7821c8 345static int scope_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 346 Scope *s = SCOPE(u);
7a7821c8 347 usec_t t;
68db7a3b
ZJS
348 int r;
349
350 if (!s->timer_event_source)
351 return 0;
352
7a7821c8 353 r = sd_event_source_get_time(s->timer_event_source, &t);
68db7a3b
ZJS
354 if (r < 0)
355 return r;
7a7821c8
LP
356 if (t == USEC_INFINITY)
357 return 0;
68db7a3b 358
7a7821c8 359 *timeout = t;
68db7a3b
ZJS
360 return 1;
361}
362
6c12b52e
LP
363static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
364 Scope *s = SCOPE(u);
365
366 assert(s);
367 assert(f);
368 assert(fds);
369
370 unit_serialize_item(u, f, "state", scope_state_to_string(s->state));
371 return 0;
372}
373
374static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
375 Scope *s = SCOPE(u);
376
377 assert(u);
378 assert(key);
379 assert(value);
380 assert(fds);
381
382 if (streq(key, "state")) {
383 ScopeState state;
384
385 state = scope_state_from_string(value);
386 if (state < 0)
f2341e0a 387 log_unit_debug(u, "Failed to parse state value: %s", value);
6c12b52e
LP
388 else
389 s->deserialized_state = state;
390
391 } else
f2341e0a 392 log_unit_debug(u, "Unknown serialization key: %s", key);
6c12b52e
LP
393
394 return 0;
395}
396
397static bool scope_check_gc(Unit *u) {
4e2744fc 398 assert(u);
6c12b52e
LP
399
400 /* Never clean up scopes that still have a process around,
401 * even if the scope is formally dead. */
402
700e2d63
LP
403 if (!u->cgroup_path)
404 return false;
4e2744fc 405
700e2d63 406 return cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path) <= 0;
6c12b52e
LP
407}
408
a911bb9a
LP
409static void scope_notify_cgroup_empty_event(Unit *u) {
410 Scope *s = SCOPE(u);
411 assert(u);
412
f2341e0a 413 log_unit_debug(u, "cgroup is empty");
a911bb9a
LP
414
415 if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
416 scope_enter_dead(s, SCOPE_SUCCESS);
417}
418
419static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
420
421 /* If we get a SIGCHLD event for one of the processes we were
422 interested in, then we look for others to watch, under the
423 assumption that we'll sooner or later get a SIGCHLD for
424 them, as the original process we watched was probably the
425 parent of them, and they are hence now our children. */
426
427 unit_tidy_watch_pids(u, 0, 0);
428 unit_watch_all_pids(u);
429
430 /* If the PID set is empty now, then let's finish this off */
431 if (set_isempty(u->pids))
432 scope_notify_cgroup_empty_event(u);
433}
434
718db961
LP
435static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
436 Scope *s = SCOPE(userdata);
6c12b52e
LP
437
438 assert(s);
718db961 439 assert(s->timer_event_source == source);
6c12b52e
LP
440
441 switch (s->state) {
442
443 case SCOPE_STOP_SIGTERM:
444 if (s->kill_context.send_sigkill) {
f2341e0a 445 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
6c12b52e
LP
446 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
447 } else {
f2341e0a 448 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
6c12b52e
LP
449 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
450 }
451
452 break;
453
454 case SCOPE_STOP_SIGKILL:
f2341e0a 455 log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
6c12b52e
LP
456 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
457 break;
458
459 default:
460 assert_not_reached("Timeout at wrong time.");
461 }
718db961
LP
462
463 return 0;
6c12b52e
LP
464}
465
a911bb9a
LP
466int scope_abandon(Scope *s) {
467 assert(s);
6c12b52e 468
efdb0237
LP
469 if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
470 return -EPERM;
471
a911bb9a
LP
472 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
473 return -ESTALE;
6c12b52e 474
a1e58e8e 475 s->controller = mfree(s->controller);
6c12b52e 476
a911bb9a
LP
477 /* The client is no longer watching the remaining processes,
478 * so let's step in here, under the assumption that the
479 * remaining processes will be sooner or later reassigned to
480 * us as parent. */
6c12b52e 481
a911bb9a
LP
482 unit_tidy_watch_pids(UNIT(s), 0, 0);
483 unit_watch_all_pids(UNIT(s));
6c12b52e 484
a911bb9a
LP
485 /* If the PID set is empty now, then let's finish this off */
486 if (set_isempty(UNIT(s)->pids))
487 scope_notify_cgroup_empty_event(UNIT(s));
488 else
489 scope_set_state(s, SCOPE_ABANDONED);
490
491 return 0;
6c12b52e
LP
492}
493
494_pure_ static UnitActiveState scope_active_state(Unit *u) {
495 assert(u);
496
497 return state_translation_table[SCOPE(u)->state];
498}
499
500_pure_ static const char *scope_sub_state_to_string(Unit *u) {
501 assert(u);
502
503 return scope_state_to_string(SCOPE(u)->state);
504}
505
ba64af90 506static void scope_enumerate(Manager *m) {
efdb0237
LP
507 Unit *u;
508 int r;
509
510 assert(m);
511
512 /* Let's unconditionally add the "init.scope" special unit
513 * that encapsulates PID 1. Note that PID 1 already is in the
514 * cgroup for this, we hence just need to allocate the object
515 * for it and that's it. */
516
517 u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
518 if (!u) {
519 u = unit_new(m, sizeof(Scope));
ba64af90
LP
520 if (!u) {
521 log_oom();
522 return;
523 }
efdb0237
LP
524
525 r = unit_add_name(u, SPECIAL_INIT_SCOPE);
526 if (r < 0) {
527 unit_free(u);
ba64af90
LP
528 log_error_errno(r, "Failed to add init.scope name");
529 return;
efdb0237
LP
530 }
531 }
532
533 u->transient = true;
534 u->default_dependencies = false;
535 u->no_gc = true;
41780022
LP
536 u->ignore_on_isolate = true;
537 u->refuse_manual_start = true;
538 u->refuse_manual_stop = true;
efdb0237
LP
539 SCOPE(u)->deserialized_state = SCOPE_RUNNING;
540 SCOPE(u)->kill_context.kill_signal = SIGRTMIN+14;
541
542 /* Prettify things, if we can. */
543 if (!u->description)
544 u->description = strdup("System and Service Manager");
545 if (!u->documentation)
546 (void) strv_extend(&u->documentation, "man:systemd(1)");
547
548 unit_add_to_load_queue(u);
549 unit_add_to_dbus_queue(u);
efdb0237
LP
550}
551
6c12b52e
LP
552static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
553 [SCOPE_SUCCESS] = "success",
554 [SCOPE_FAILURE_RESOURCES] = "resources",
555 [SCOPE_FAILURE_TIMEOUT] = "timeout",
556};
557
558DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
559
560const UnitVTable scope_vtable = {
561 .object_size = sizeof(Scope),
718db961
LP
562 .cgroup_context_offset = offsetof(Scope, cgroup_context),
563 .kill_context_offset = offsetof(Scope, kill_context),
564
6c12b52e
LP
565 .sections =
566 "Unit\0"
567 "Scope\0"
568 "Install\0",
6c12b52e 569 .private_section = "Scope",
6c12b52e
LP
570
571 .no_alias = true,
572 .no_instances = true,
700e2d63 573 .can_transient = true,
6c12b52e
LP
574
575 .init = scope_init,
576 .load = scope_load,
577 .done = scope_done,
578
579 .coldplug = scope_coldplug,
580
581 .dump = scope_dump,
582
583 .start = scope_start,
584 .stop = scope_stop,
585
586 .kill = scope_kill,
587
68db7a3b
ZJS
588 .get_timeout = scope_get_timeout,
589
6c12b52e
LP
590 .serialize = scope_serialize,
591 .deserialize_item = scope_deserialize_item,
592
593 .active_state = scope_active_state,
594 .sub_state_to_string = scope_sub_state_to_string,
595
596 .check_gc = scope_check_gc,
597
a911bb9a
LP
598 .sigchld_event = scope_sigchld_event,
599
8bcca7e2
LP
600 .reset_failed = scope_reset_failed,
601
6c12b52e
LP
602 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
603
718db961 604 .bus_vtable = bus_scope_vtable,
6c12b52e
LP
605 .bus_set_property = bus_scope_set_property,
606 .bus_commit_properties = bus_scope_commit_properties,
607
efdb0237 608 .enumerate = scope_enumerate,
6c12b52e 609};