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