2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
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.
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.
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/>.
23 #include "alloc-util.h"
24 #include "dbus-scope.h"
25 #include "load-dropin.h"
29 #include "string-table.h"
30 #include "string-util.h"
32 #include "unit-name.h"
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
44 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
46 static void scope_init(Unit
*u
) {
50 assert(u
->load_state
== UNIT_STUB
);
52 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
53 u
->ignore_on_isolate
= true;
56 static void scope_done(Unit
*u
) {
63 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
66 static int scope_arm_timer(Scope
*s
, usec_t usec
) {
71 if (s
->timer_event_source
) {
72 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
76 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
79 if (usec
== USEC_INFINITY
)
82 r
= sd_event_add_time(
83 UNIT(s
)->manager
->event
,
84 &s
->timer_event_source
,
87 scope_dispatch_timer
, s
);
91 (void) sd_event_source_set_description(s
->timer_event_source
, "scope-timer");
96 static void scope_set_state(Scope
*s
, ScopeState state
) {
100 old_state
= s
->state
;
103 if (!IN_SET(state
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
104 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
106 if (IN_SET(state
, SCOPE_DEAD
, SCOPE_FAILED
))
107 unit_unwatch_all_pids(UNIT(s
));
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
));
112 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
115 static int scope_add_default_dependencies(Scope
*s
) {
120 if (!UNIT(s
)->default_dependencies
)
123 /* Make sure scopes are unloaded on shutdown */
124 r
= unit_add_two_dependencies_by_name(
126 UNIT_BEFORE
, UNIT_CONFLICTS
,
127 SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
134 static int scope_verify(Scope
*s
) {
137 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
140 if (set_isempty(UNIT(s
)->pids
) &&
141 !manager_is_reloading_or_reexecuting(UNIT(s
)->manager
) &&
142 !unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
)) {
143 log_unit_error(UNIT(s
), "Scope has no PIDs. Refusing.");
150 static int scope_load(Unit
*u
) {
155 assert(u
->load_state
== UNIT_STUB
);
157 if (!u
->transient
&& !manager_is_reloading_or_reexecuting(u
->manager
))
160 u
->load_state
= UNIT_LOADED
;
162 r
= unit_load_dropin(u
);
166 r
= unit_patch_contexts(u
);
170 r
= unit_set_default_slice(u
);
174 r
= scope_add_default_dependencies(s
);
178 return scope_verify(s
);
181 static int scope_coldplug(Unit
*u
) {
186 assert(s
->state
== SCOPE_DEAD
);
188 if (s
->deserialized_state
== s
->state
)
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
));
197 if (!IN_SET(s
->deserialized_state
, SCOPE_DEAD
, SCOPE_FAILED
))
198 unit_watch_all_pids(UNIT(s
));
200 scope_set_state(s
, s
->deserialized_state
);
204 static void scope_dump(Unit
*u
, FILE *f
, const char *prefix
) {
211 "%sScope State: %s\n"
213 prefix
, scope_state_to_string(s
->state
),
214 prefix
, scope_result_to_string(s
->result
));
216 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
217 kill_context_dump(&s
->kill_context
, f
, prefix
);
220 static void scope_enter_dead(Scope
*s
, ScopeResult f
) {
223 if (f
!= SCOPE_SUCCESS
)
226 scope_set_state(s
, s
->result
!= SCOPE_SUCCESS
? SCOPE_FAILED
: SCOPE_DEAD
);
229 static void scope_enter_signal(Scope
*s
, ScopeState state
, ScopeResult f
) {
230 bool skip_signal
= false;
235 if (f
!= SCOPE_SUCCESS
)
238 unit_watch_all_pids(UNIT(s
));
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;
247 r
= unit_kill_context(
250 state
!= SCOPE_STOP_SIGTERM
? KILL_KILL
: KILL_TERMINATE
,
258 r
= scope_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
262 scope_set_state(s
, state
);
263 } else if (state
== SCOPE_STOP_SIGTERM
)
264 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_SUCCESS
);
266 scope_enter_dead(s
, SCOPE_SUCCESS
);
271 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
273 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
276 static int scope_start(Unit
*u
) {
282 if (unit_has_name(u
, SPECIAL_INIT_SCOPE
))
285 if (s
->state
== SCOPE_FAILED
)
288 /* We can't fulfill this right now, please try again later */
289 if (s
->state
== SCOPE_STOP_SIGTERM
||
290 s
->state
== SCOPE_STOP_SIGKILL
)
293 assert(s
->state
== SCOPE_DEAD
);
295 if (!u
->transient
&& !manager_is_reloading_or_reexecuting(u
->manager
))
298 (void) unit_realize_cgroup(u
);
299 (void) unit_reset_cpu_usage(u
);
301 r
= unit_attach_pids_to_cgroup(u
);
303 log_unit_warning_errno(UNIT(s
), r
, "Failed to add PIDs to scope's control group: %m");
304 scope_enter_dead(s
, SCOPE_FAILURE_RESOURCES
);
308 s
->result
= SCOPE_SUCCESS
;
310 scope_set_state(s
, SCOPE_RUNNING
);
314 static int scope_stop(Unit
*u
) {
319 if (s
->state
== SCOPE_STOP_SIGTERM
||
320 s
->state
== SCOPE_STOP_SIGKILL
)
323 assert(s
->state
== SCOPE_RUNNING
||
324 s
->state
== SCOPE_ABANDONED
);
326 scope_enter_signal(s
, SCOPE_STOP_SIGTERM
, SCOPE_SUCCESS
);
330 static void scope_reset_failed(Unit
*u
) {
335 if (s
->state
== SCOPE_FAILED
)
336 scope_set_state(s
, SCOPE_DEAD
);
338 s
->result
= SCOPE_SUCCESS
;
341 static int scope_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
342 return unit_kill_common(u
, who
, signo
, -1, -1, error
);
345 static int scope_get_timeout(Unit
*u
, usec_t
*timeout
) {
350 if (!s
->timer_event_source
)
353 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
356 if (t
== USEC_INFINITY
)
363 static int scope_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
370 unit_serialize_item(u
, f
, "state", scope_state_to_string(s
->state
));
374 static int scope_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
382 if (streq(key
, "state")) {
385 state
= scope_state_from_string(value
);
387 log_unit_debug(u
, "Failed to parse state value: %s", value
);
389 s
->deserialized_state
= state
;
392 log_unit_debug(u
, "Unknown serialization key: %s", key
);
397 static bool scope_check_gc(Unit
*u
) {
400 /* Never clean up scopes that still have a process around,
401 * even if the scope is formally dead. */
406 return cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, u
->cgroup_path
) <= 0;
409 static void scope_notify_cgroup_empty_event(Unit
*u
) {
413 log_unit_debug(u
, "cgroup is empty");
415 if (IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
, SCOPE_STOP_SIGTERM
, SCOPE_STOP_SIGKILL
))
416 scope_enter_dead(s
, SCOPE_SUCCESS
);
419 static void scope_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
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. */
427 unit_tidy_watch_pids(u
, 0, 0);
428 unit_watch_all_pids(u
);
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
);
435 static int scope_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
436 Scope
*s
= SCOPE(userdata
);
439 assert(s
->timer_event_source
== source
);
443 case SCOPE_STOP_SIGTERM
:
444 if (s
->kill_context
.send_sigkill
) {
445 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
446 scope_enter_signal(s
, SCOPE_STOP_SIGKILL
, SCOPE_FAILURE_TIMEOUT
);
448 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL.");
449 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
454 case SCOPE_STOP_SIGKILL
:
455 log_unit_warning(UNIT(s
), "Still around after SIGKILL. Ignoring.");
456 scope_enter_dead(s
, SCOPE_FAILURE_TIMEOUT
);
460 assert_not_reached("Timeout at wrong time.");
466 int scope_abandon(Scope
*s
) {
469 if (unit_has_name(UNIT(s
), SPECIAL_INIT_SCOPE
))
472 if (!IN_SET(s
->state
, SCOPE_RUNNING
, SCOPE_ABANDONED
))
475 s
->controller
= mfree(s
->controller
);
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
482 unit_tidy_watch_pids(UNIT(s
), 0, 0);
483 unit_watch_all_pids(UNIT(s
));
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
));
489 scope_set_state(s
, SCOPE_ABANDONED
);
494 _pure_
static UnitActiveState
scope_active_state(Unit
*u
) {
497 return state_translation_table
[SCOPE(u
)->state
];
500 _pure_
static const char *scope_sub_state_to_string(Unit
*u
) {
503 return scope_state_to_string(SCOPE(u
)->state
);
506 static void scope_enumerate(Manager
*m
) {
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. */
517 u
= manager_get_unit(m
, SPECIAL_INIT_SCOPE
);
519 u
= unit_new(m
, sizeof(Scope
));
525 r
= unit_add_name(u
, SPECIAL_INIT_SCOPE
);
528 log_error_errno(r
, "Failed to add init.scope name");
534 u
->default_dependencies
= false;
536 u
->ignore_on_isolate
= true;
537 u
->refuse_manual_start
= true;
538 u
->refuse_manual_stop
= true;
539 SCOPE(u
)->deserialized_state
= SCOPE_RUNNING
;
540 SCOPE(u
)->kill_context
.kill_signal
= SIGRTMIN
+14;
542 /* Prettify things, if we can. */
544 u
->description
= strdup("System and Service Manager");
545 if (!u
->documentation
)
546 (void) strv_extend(&u
->documentation
, "man:systemd(1)");
548 unit_add_to_load_queue(u
);
549 unit_add_to_dbus_queue(u
);
552 static const char* const scope_result_table
[_SCOPE_RESULT_MAX
] = {
553 [SCOPE_SUCCESS
] = "success",
554 [SCOPE_FAILURE_RESOURCES
] = "resources",
555 [SCOPE_FAILURE_TIMEOUT
] = "timeout",
558 DEFINE_STRING_TABLE_LOOKUP(scope_result
, ScopeResult
);
560 const UnitVTable scope_vtable
= {
561 .object_size
= sizeof(Scope
),
562 .cgroup_context_offset
= offsetof(Scope
, cgroup_context
),
563 .kill_context_offset
= offsetof(Scope
, kill_context
),
569 .private_section
= "Scope",
572 .no_instances
= true,
573 .can_transient
= true,
579 .coldplug
= scope_coldplug
,
583 .start
= scope_start
,
588 .get_timeout
= scope_get_timeout
,
590 .serialize
= scope_serialize
,
591 .deserialize_item
= scope_deserialize_item
,
593 .active_state
= scope_active_state
,
594 .sub_state_to_string
= scope_sub_state_to_string
,
596 .check_gc
= scope_check_gc
,
598 .sigchld_event
= scope_sigchld_event
,
600 .reset_failed
= scope_reset_failed
,
602 .notify_cgroup_empty
= scope_notify_cgroup_empty_event
,
604 .bus_vtable
= bus_scope_vtable
,
605 .bus_set_property
= bus_scope_set_property
,
606 .bus_commit_properties
= bus_scope_commit_properties
,
608 .enumerate
= scope_enumerate
,