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