]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/scope.c
Merge pull request #2525 from chaloulo/journal-remote-microhttp-max-memory-usage
[thirdparty/systemd.git] / src / core / scope.c
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>
23 #include <unistd.h>
24
25 #include "alloc-util.h"
26 #include "dbus-scope.h"
27 #include "load-dropin.h"
28 #include "log.h"
29 #include "scope.h"
30 #include "special.h"
31 #include "string-table.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "unit-name.h"
35 #include "unit.h"
36
37 static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
38 [SCOPE_DEAD] = UNIT_INACTIVE,
39 [SCOPE_RUNNING] = UNIT_ACTIVE,
40 [SCOPE_ABANDONED] = UNIT_ACTIVE,
41 [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
42 [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
43 [SCOPE_FAILED] = UNIT_FAILED
44 };
45
46 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
47
48 static void scope_init(Unit *u) {
49 Scope *s = SCOPE(u);
50
51 assert(u);
52 assert(u->load_state == UNIT_STUB);
53
54 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
55
56 UNIT(s)->ignore_on_isolate = true;
57 }
58
59 static void scope_done(Unit *u) {
60 Scope *s = SCOPE(u);
61
62 assert(u);
63
64 free(s->controller);
65
66 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
67 }
68
69 static int scope_arm_timer(Scope *s, usec_t usec) {
70 int r;
71
72 assert(s);
73
74 if (s->timer_event_source) {
75 r = sd_event_source_set_time(s->timer_event_source, usec);
76 if (r < 0)
77 return r;
78
79 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
80 }
81
82 if (usec == USEC_INFINITY)
83 return 0;
84
85 r = sd_event_add_time(
86 UNIT(s)->manager->event,
87 &s->timer_event_source,
88 CLOCK_MONOTONIC,
89 usec, 0,
90 scope_dispatch_timer, s);
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;
97 }
98
99 static 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
106 if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
107 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
108
109 if (IN_SET(state, SCOPE_DEAD, SCOPE_FAILED))
110 unit_unwatch_all_pids(UNIT(s));
111
112 if (state != old_state)
113 log_debug("%s changed %s -> %s", UNIT(s)->id, scope_state_to_string(old_state), scope_state_to_string(state));
114
115 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
116 }
117
118 static int scope_add_default_dependencies(Scope *s) {
119 int r;
120
121 assert(s);
122
123 if (!UNIT(s)->default_dependencies)
124 return 0;
125
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
137 static int scope_verify(Scope *s) {
138 assert(s);
139
140 if (UNIT(s)->load_state != UNIT_LOADED)
141 return 0;
142
143 if (set_isempty(UNIT(s)->pids) &&
144 !manager_is_reloading_or_reexecuting(UNIT(s)->manager) &&
145 !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) {
146 log_unit_error(UNIT(s), "Scope has no PIDs. Refusing.");
147 return -EINVAL;
148 }
149
150 return 0;
151 }
152
153 static 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
160 if (!u->transient && !manager_is_reloading_or_reexecuting(u->manager))
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
169 r = unit_patch_contexts(u);
170 if (r < 0)
171 return r;
172
173 r = unit_set_default_slice(u);
174 if (r < 0)
175 return r;
176
177 r = scope_add_default_dependencies(s);
178 if (r < 0)
179 return r;
180
181 return scope_verify(s);
182 }
183
184 static int scope_coldplug(Unit *u) {
185 Scope *s = SCOPE(u);
186 int r;
187
188 assert(s);
189 assert(s->state == SCOPE_DEAD);
190
191 if (s->deserialized_state == s->state)
192 return 0;
193
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;
198 }
199
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);
204 return 0;
205 }
206
207 static 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
223 static 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
232 static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
233 bool skip_signal = false;
234 int r;
235
236 assert(s);
237
238 if (f != SCOPE_SUCCESS)
239 s->result = f;
240
241 unit_watch_all_pids(UNIT(s));
242
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,
253 state != SCOPE_STOP_SIGTERM ? KILL_KILL : KILL_TERMINATE,
254 -1, -1, false);
255 if (r < 0)
256 goto fail;
257 } else
258 r = 1;
259
260 if (r > 0) {
261 r = scope_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
262 if (r < 0)
263 goto fail;
264
265 scope_set_state(s, state);
266 } else if (state == SCOPE_STOP_SIGTERM)
267 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
268 else
269 scope_enter_dead(s, SCOPE_SUCCESS);
270
271 return;
272
273 fail:
274 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
275
276 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
277 }
278
279 static int scope_start(Unit *u) {
280 Scope *s = SCOPE(u);
281 int r;
282
283 assert(s);
284
285 if (unit_has_name(u, SPECIAL_INIT_SCOPE))
286 return -EPERM;
287
288 if (s->state == SCOPE_FAILED)
289 return -EPERM;
290
291 /* We can't fulfill this right now, please try again later */
292 if (s->state == SCOPE_STOP_SIGTERM ||
293 s->state == SCOPE_STOP_SIGKILL)
294 return -EAGAIN;
295
296 assert(s->state == SCOPE_DEAD);
297
298 if (!u->transient && !manager_is_reloading_or_reexecuting(u->manager))
299 return -ENOENT;
300
301 (void) unit_realize_cgroup(u);
302 (void) unit_reset_cpu_usage(u);
303
304 r = unit_attach_pids_to_cgroup(u);
305 if (r < 0) {
306 log_unit_warning_errno(UNIT(s), r, "Failed to add PIDs to scope's control group: %m");
307 scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
308 return r;
309 }
310
311 s->result = SCOPE_SUCCESS;
312
313 scope_set_state(s, SCOPE_RUNNING);
314 return 1;
315 }
316
317 static int scope_stop(Unit *u) {
318 Scope *s = SCOPE(u);
319
320 assert(s);
321
322 if (s->state == SCOPE_STOP_SIGTERM ||
323 s->state == SCOPE_STOP_SIGKILL)
324 return 0;
325
326 assert(s->state == SCOPE_RUNNING ||
327 s->state == SCOPE_ABANDONED);
328
329 scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
330 return 1;
331 }
332
333 static 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
344 static int scope_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
345 return unit_kill_common(u, who, signo, -1, -1, error);
346 }
347
348 static int scope_get_timeout(Unit *u, usec_t *timeout) {
349 Scope *s = SCOPE(u);
350 usec_t t;
351 int r;
352
353 if (!s->timer_event_source)
354 return 0;
355
356 r = sd_event_source_get_time(s->timer_event_source, &t);
357 if (r < 0)
358 return r;
359 if (t == USEC_INFINITY)
360 return 0;
361
362 *timeout = t;
363 return 1;
364 }
365
366 static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
367 Scope *s = SCOPE(u);
368
369 assert(s);
370 assert(f);
371 assert(fds);
372
373 unit_serialize_item(u, f, "state", scope_state_to_string(s->state));
374 return 0;
375 }
376
377 static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
378 Scope *s = SCOPE(u);
379
380 assert(u);
381 assert(key);
382 assert(value);
383 assert(fds);
384
385 if (streq(key, "state")) {
386 ScopeState state;
387
388 state = scope_state_from_string(value);
389 if (state < 0)
390 log_unit_debug(u, "Failed to parse state value: %s", value);
391 else
392 s->deserialized_state = state;
393
394 } else
395 log_unit_debug(u, "Unknown serialization key: %s", key);
396
397 return 0;
398 }
399
400 static bool scope_check_gc(Unit *u) {
401 assert(u);
402
403 /* Never clean up scopes that still have a process around,
404 * even if the scope is formally dead. */
405
406 if (!u->cgroup_path)
407 return false;
408
409 return cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path) <= 0;
410 }
411
412 static void scope_notify_cgroup_empty_event(Unit *u) {
413 Scope *s = SCOPE(u);
414 assert(u);
415
416 log_unit_debug(u, "cgroup is empty");
417
418 if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
419 scope_enter_dead(s, SCOPE_SUCCESS);
420 }
421
422 static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
423
424 /* If we get a SIGCHLD event for one of the processes we were
425 interested in, then we look for others to watch, under the
426 assumption that we'll sooner or later get a SIGCHLD for
427 them, as the original process we watched was probably the
428 parent of them, and they are hence now our children. */
429
430 unit_tidy_watch_pids(u, 0, 0);
431 unit_watch_all_pids(u);
432
433 /* If the PID set is empty now, then let's finish this off */
434 if (set_isempty(u->pids))
435 scope_notify_cgroup_empty_event(u);
436 }
437
438 static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
439 Scope *s = SCOPE(userdata);
440
441 assert(s);
442 assert(s->timer_event_source == source);
443
444 switch (s->state) {
445
446 case SCOPE_STOP_SIGTERM:
447 if (s->kill_context.send_sigkill) {
448 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
449 scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
450 } else {
451 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
452 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
453 }
454
455 break;
456
457 case SCOPE_STOP_SIGKILL:
458 log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
459 scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
460 break;
461
462 default:
463 assert_not_reached("Timeout at wrong time.");
464 }
465
466 return 0;
467 }
468
469 int scope_abandon(Scope *s) {
470 assert(s);
471
472 if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
473 return -EPERM;
474
475 if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
476 return -ESTALE;
477
478 s->controller = mfree(s->controller);
479
480 /* The client is no longer watching the remaining processes,
481 * so let's step in here, under the assumption that the
482 * remaining processes will be sooner or later reassigned to
483 * us as parent. */
484
485 unit_tidy_watch_pids(UNIT(s), 0, 0);
486 unit_watch_all_pids(UNIT(s));
487
488 /* If the PID set is empty now, then let's finish this off */
489 if (set_isempty(UNIT(s)->pids))
490 scope_notify_cgroup_empty_event(UNIT(s));
491 else
492 scope_set_state(s, SCOPE_ABANDONED);
493
494 return 0;
495 }
496
497 _pure_ static UnitActiveState scope_active_state(Unit *u) {
498 assert(u);
499
500 return state_translation_table[SCOPE(u)->state];
501 }
502
503 _pure_ static const char *scope_sub_state_to_string(Unit *u) {
504 assert(u);
505
506 return scope_state_to_string(SCOPE(u)->state);
507 }
508
509 static void scope_enumerate(Manager *m) {
510 Unit *u;
511 int r;
512
513 assert(m);
514
515 /* Let's unconditionally add the "init.scope" special unit
516 * that encapsulates PID 1. Note that PID 1 already is in the
517 * cgroup for this, we hence just need to allocate the object
518 * for it and that's it. */
519
520 u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
521 if (!u) {
522 u = unit_new(m, sizeof(Scope));
523 if (!u) {
524 log_oom();
525 return;
526 }
527
528 r = unit_add_name(u, SPECIAL_INIT_SCOPE);
529 if (r < 0) {
530 unit_free(u);
531 log_error_errno(r, "Failed to add init.scope name");
532 return;
533 }
534 }
535
536 u->transient = true;
537 u->default_dependencies = false;
538 u->no_gc = true;
539 u->ignore_on_isolate = true;
540 u->refuse_manual_start = true;
541 u->refuse_manual_stop = true;
542 SCOPE(u)->deserialized_state = SCOPE_RUNNING;
543 SCOPE(u)->kill_context.kill_signal = SIGRTMIN+14;
544
545 /* Prettify things, if we can. */
546 if (!u->description)
547 u->description = strdup("System and Service Manager");
548 if (!u->documentation)
549 (void) strv_extend(&u->documentation, "man:systemd(1)");
550
551 unit_add_to_load_queue(u);
552 unit_add_to_dbus_queue(u);
553 }
554
555 static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
556 [SCOPE_SUCCESS] = "success",
557 [SCOPE_FAILURE_RESOURCES] = "resources",
558 [SCOPE_FAILURE_TIMEOUT] = "timeout",
559 };
560
561 DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
562
563 const UnitVTable scope_vtable = {
564 .object_size = sizeof(Scope),
565 .cgroup_context_offset = offsetof(Scope, cgroup_context),
566 .kill_context_offset = offsetof(Scope, kill_context),
567
568 .sections =
569 "Unit\0"
570 "Scope\0"
571 "Install\0",
572 .private_section = "Scope",
573
574 .no_alias = true,
575 .no_instances = true,
576 .can_transient = true,
577
578 .init = scope_init,
579 .load = scope_load,
580 .done = scope_done,
581
582 .coldplug = scope_coldplug,
583
584 .dump = scope_dump,
585
586 .start = scope_start,
587 .stop = scope_stop,
588
589 .kill = scope_kill,
590
591 .get_timeout = scope_get_timeout,
592
593 .serialize = scope_serialize,
594 .deserialize_item = scope_deserialize_item,
595
596 .active_state = scope_active_state,
597 .sub_state_to_string = scope_sub_state_to_string,
598
599 .check_gc = scope_check_gc,
600
601 .sigchld_event = scope_sigchld_event,
602
603 .reset_failed = scope_reset_failed,
604
605 .notify_cgroup_empty = scope_notify_cgroup_empty_event,
606
607 .bus_vtable = bus_scope_vtable,
608 .bus_set_property = bus_scope_set_property,
609 .bus_commit_properties = bus_scope_commit_properties,
610
611 .enumerate = scope_enumerate,
612 };