]>
Commit | Line | Data |
---|---|---|
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, 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 | ||
362 | static 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 | ||
373 | static 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) | |
386 | log_unit_debug(u, "Failed to parse state value: %s", value); | |
387 | else | |
388 | s->deserialized_state = state; | |
389 | ||
390 | } else | |
391 | log_unit_debug(u, "Unknown serialization key: %s", key); | |
392 | ||
393 | return 0; | |
394 | } | |
395 | ||
396 | static bool scope_check_gc(Unit *u) { | |
397 | assert(u); | |
398 | ||
399 | /* Never clean up scopes that still have a process around, | |
400 | * even if the scope is formally dead. */ | |
401 | ||
402 | if (!u->cgroup_path) | |
403 | return false; | |
404 | ||
405 | return cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path) <= 0; | |
406 | } | |
407 | ||
408 | static void scope_notify_cgroup_empty_event(Unit *u) { | |
409 | Scope *s = SCOPE(u); | |
410 | assert(u); | |
411 | ||
412 | log_unit_debug(u, "cgroup is empty"); | |
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 | ||
418 | static 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 | ||
434 | static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { | |
435 | Scope *s = SCOPE(userdata); | |
436 | ||
437 | assert(s); | |
438 | assert(s->timer_event_source == source); | |
439 | ||
440 | switch (s->state) { | |
441 | ||
442 | case SCOPE_STOP_SIGTERM: | |
443 | if (s->kill_context.send_sigkill) { | |
444 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); | |
445 | scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT); | |
446 | } else { | |
447 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); | |
448 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); | |
449 | } | |
450 | ||
451 | break; | |
452 | ||
453 | case SCOPE_STOP_SIGKILL: | |
454 | log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring."); | |
455 | scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); | |
456 | break; | |
457 | ||
458 | default: | |
459 | assert_not_reached("Timeout at wrong time."); | |
460 | } | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | int scope_abandon(Scope *s) { | |
466 | assert(s); | |
467 | ||
468 | if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) | |
469 | return -EPERM; | |
470 | ||
471 | if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)) | |
472 | return -ESTALE; | |
473 | ||
474 | s->controller = mfree(s->controller); | |
475 | ||
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. */ | |
480 | ||
481 | unit_tidy_watch_pids(UNIT(s), 0, 0); | |
482 | unit_watch_all_pids(UNIT(s)); | |
483 | ||
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; | |
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 | ||
505 | static void scope_enumerate(Manager *m) { | |
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)); | |
519 | if (!u) { | |
520 | log_oom(); | |
521 | return; | |
522 | } | |
523 | ||
524 | r = unit_add_name(u, SPECIAL_INIT_SCOPE); | |
525 | if (r < 0) { | |
526 | unit_free(u); | |
527 | log_error_errno(r, "Failed to add init.scope name"); | |
528 | return; | |
529 | } | |
530 | } | |
531 | ||
532 | u->transient = true; | |
533 | u->default_dependencies = false; | |
534 | u->no_gc = true; | |
535 | u->ignore_on_isolate = true; | |
536 | u->refuse_manual_start = true; | |
537 | u->refuse_manual_stop = true; | |
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); | |
549 | } | |
550 | ||
551 | static 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 | ||
557 | DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult); | |
558 | ||
559 | const UnitVTable scope_vtable = { | |
560 | .object_size = sizeof(Scope), | |
561 | .cgroup_context_offset = offsetof(Scope, cgroup_context), | |
562 | .kill_context_offset = offsetof(Scope, kill_context), | |
563 | ||
564 | .sections = | |
565 | "Unit\0" | |
566 | "Scope\0" | |
567 | "Install\0", | |
568 | .private_section = "Scope", | |
569 | ||
570 | .no_alias = true, | |
571 | .no_instances = true, | |
572 | .can_transient = true, | |
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 | ||
587 | .get_timeout = scope_get_timeout, | |
588 | ||
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 | ||
597 | .sigchld_event = scope_sigchld_event, | |
598 | ||
599 | .reset_failed = scope_reset_failed, | |
600 | ||
601 | .notify_cgroup_empty = scope_notify_cgroup_empty_event, | |
602 | ||
603 | .bus_vtable = bus_scope_vtable, | |
604 | .bus_set_property = bus_scope_set_property, | |
605 | .bus_commit_properties = bus_scope_commit_properties, | |
606 | ||
607 | .enumerate = scope_enumerate, | |
608 | }; |