| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
|
| 7 |
#include "cgroup-setup.h" |
| 8 |
#include "dbus-scope.h" |
| 9 |
#include "dbus-unit.h" |
| 10 |
#include "exit-status.h" |
| 11 |
#include "log.h" |
| 12 |
#include "manager.h" |
| 13 |
#include "parse-util.h" |
| 14 |
#include "pidref.h" |
| 15 |
#include "random-util.h" |
| 16 |
#include "scope.h" |
| 17 |
#include "serialize.h" |
| 18 |
#include "set.h" |
| 19 |
#include "special.h" |
| 20 |
#include "string-table.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "strv.h" |
| 23 |
#include "unit.h" |
| 24 |
#include "user-util.h" |
| 25 |
|
| 26 |
static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = { |
| 27 |
[SCOPE_DEAD] = UNIT_INACTIVE, |
| 28 |
[SCOPE_START_CHOWN] = UNIT_ACTIVATING, |
| 29 |
[SCOPE_RUNNING] = UNIT_ACTIVE, |
| 30 |
[SCOPE_ABANDONED] = UNIT_ACTIVE, |
| 31 |
[SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
| 32 |
[SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING, |
| 33 |
[SCOPE_FAILED] = UNIT_FAILED, |
| 34 |
}; |
| 35 |
|
| 36 |
static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
| 37 |
|
| 38 |
static void scope_init(Unit *u) { |
| 39 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 40 |
|
| 41 |
assert(u->load_state == UNIT_STUB); |
| 42 |
|
| 43 |
s->runtime_max_usec = USEC_INFINITY; |
| 44 |
s->timeout_stop_usec = u->manager->defaults.timeout_stop_usec; |
| 45 |
u->ignore_on_isolate = true; |
| 46 |
s->user = s->group = NULL; |
| 47 |
s->oom_policy = _OOM_POLICY_INVALID; |
| 48 |
} |
| 49 |
|
| 50 |
static void scope_done(Unit *u) { |
| 51 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 52 |
|
| 53 |
s->controller = mfree(s->controller); |
| 54 |
s->controller_track = sd_bus_track_unref(s->controller_track); |
| 55 |
|
| 56 |
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
| 57 |
|
| 58 |
s->user = mfree(s->user); |
| 59 |
s->group = mfree(s->group); |
| 60 |
} |
| 61 |
|
| 62 |
static usec_t scope_running_timeout(Scope *s) { |
| 63 |
usec_t delta = 0; |
| 64 |
|
| 65 |
assert(s); |
| 66 |
|
| 67 |
if (s->runtime_rand_extra_usec != 0) { |
| 68 |
delta = random_u64_range(s->runtime_rand_extra_usec); |
| 69 |
log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC)); |
| 70 |
} |
| 71 |
|
| 72 |
return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic, |
| 73 |
s->runtime_max_usec), |
| 74 |
delta); |
| 75 |
} |
| 76 |
|
| 77 |
static int scope_arm_timer(Scope *s, bool relative, usec_t usec) { |
| 78 |
assert(s); |
| 79 |
|
| 80 |
return unit_arm_timer(UNIT(s), &s->timer_event_source, relative, usec, scope_dispatch_timer); |
| 81 |
} |
| 82 |
|
| 83 |
static void scope_set_state(Scope *s, ScopeState state) { |
| 84 |
ScopeState old_state; |
| 85 |
|
| 86 |
assert(s); |
| 87 |
|
| 88 |
if (s->state != state) |
| 89 |
bus_unit_send_pending_change_signal(UNIT(s), false); |
| 90 |
|
| 91 |
old_state = s->state; |
| 92 |
s->state = state; |
| 93 |
|
| 94 |
if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL, SCOPE_START_CHOWN, SCOPE_RUNNING)) |
| 95 |
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
| 96 |
|
| 97 |
if (!IN_SET(old_state, SCOPE_DEAD, SCOPE_FAILED) && IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) |
| 98 |
unit_unwatch_all_pids(UNIT(s)); |
| 99 |
|
| 100 |
if (state != old_state) |
| 101 |
log_unit_debug(UNIT(s), "Changed %s -> %s", |
| 102 |
scope_state_to_string(old_state), scope_state_to_string(state)); |
| 103 |
|
| 104 |
unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], /* reload_success= */ true); |
| 105 |
} |
| 106 |
|
| 107 |
static int scope_add_default_dependencies(Scope *s) { |
| 108 |
int r; |
| 109 |
|
| 110 |
assert(s); |
| 111 |
|
| 112 |
if (!UNIT(s)->default_dependencies) |
| 113 |
return 0; |
| 114 |
|
| 115 |
/* Make sure scopes are unloaded on shutdown */ |
| 116 |
r = unit_add_two_dependencies_by_name( |
| 117 |
UNIT(s), |
| 118 |
UNIT_BEFORE, UNIT_CONFLICTS, |
| 119 |
SPECIAL_SHUTDOWN_TARGET, true, |
| 120 |
UNIT_DEPENDENCY_DEFAULT); |
| 121 |
if (r < 0) |
| 122 |
return r; |
| 123 |
|
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
static int scope_verify(Scope *s) { |
| 128 |
assert(s); |
| 129 |
assert(UNIT(s)->load_state == UNIT_LOADED); |
| 130 |
|
| 131 |
if (set_isempty(UNIT(s)->pids) && |
| 132 |
!MANAGER_IS_RELOADING(UNIT(s)->manager) && |
| 133 |
!unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) |
| 134 |
return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOENT), "Scope has no PIDs. Refusing."); |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
static int scope_load_init_scope(Unit *u) { |
| 140 |
assert(u); |
| 141 |
|
| 142 |
if (!unit_has_name(u, SPECIAL_INIT_SCOPE)) |
| 143 |
return 0; |
| 144 |
|
| 145 |
u->transient = true; |
| 146 |
u->perpetual = true; |
| 147 |
|
| 148 |
/* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we |
| 149 |
* synthesize it here, instead of relying on the unit file on disk. */ |
| 150 |
|
| 151 |
u->default_dependencies = false; |
| 152 |
|
| 153 |
/* Prettify things, if we can. */ |
| 154 |
if (!u->description) |
| 155 |
u->description = strdup("System and Service Manager"); |
| 156 |
if (!u->documentation) |
| 157 |
(void) strv_extend(&u->documentation, "man:systemd(1)"); |
| 158 |
|
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
static int scope_add_extras(Scope *s) { |
| 163 |
int r; |
| 164 |
|
| 165 |
r = unit_patch_contexts(UNIT(s)); |
| 166 |
if (r < 0) |
| 167 |
return r; |
| 168 |
|
| 169 |
r = unit_set_default_slice(UNIT(s)); |
| 170 |
if (r < 0) |
| 171 |
return r; |
| 172 |
|
| 173 |
if (s->oom_policy < 0) |
| 174 |
s->oom_policy = s->cgroup_context.delegate ? OOM_CONTINUE : UNIT(s)->manager->defaults.oom_policy; |
| 175 |
|
| 176 |
s->cgroup_context.memory_oom_group = s->oom_policy == OOM_KILL; |
| 177 |
|
| 178 |
return scope_add_default_dependencies(s); |
| 179 |
} |
| 180 |
|
| 181 |
static int scope_load(Unit *u) { |
| 182 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 183 |
int r; |
| 184 |
|
| 185 |
assert(u->load_state == UNIT_STUB); |
| 186 |
|
| 187 |
if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) |
| 188 |
/* Refuse to load non-transient scope units, but allow them while reloading. */ |
| 189 |
return -ENOENT; |
| 190 |
|
| 191 |
r = scope_load_init_scope(u); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
|
| 195 |
r = unit_load_fragment_and_dropin(u, false); |
| 196 |
if (r < 0) |
| 197 |
return r; |
| 198 |
|
| 199 |
if (u->load_state != UNIT_LOADED) |
| 200 |
return 0; |
| 201 |
|
| 202 |
r = scope_add_extras(s); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
|
| 206 |
return scope_verify(s); |
| 207 |
} |
| 208 |
|
| 209 |
static usec_t scope_coldplug_timeout(Scope *s) { |
| 210 |
assert(s); |
| 211 |
|
| 212 |
switch (s->deserialized_state) { |
| 213 |
|
| 214 |
case SCOPE_RUNNING: |
| 215 |
return scope_running_timeout(s); |
| 216 |
|
| 217 |
case SCOPE_STOP_SIGKILL: |
| 218 |
case SCOPE_STOP_SIGTERM: |
| 219 |
return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec); |
| 220 |
|
| 221 |
default: |
| 222 |
return USEC_INFINITY; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
static int scope_coldplug(Unit *u) { |
| 227 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 228 |
int r; |
| 229 |
|
| 230 |
assert(s->state == SCOPE_DEAD); |
| 231 |
|
| 232 |
if (s->deserialized_state == s->state) |
| 233 |
return 0; |
| 234 |
|
| 235 |
r = scope_arm_timer(s, /* relative= */ false, scope_coldplug_timeout(s)); |
| 236 |
if (r < 0) |
| 237 |
return r; |
| 238 |
|
| 239 |
bus_scope_track_controller(s); |
| 240 |
|
| 241 |
scope_set_state(s, s->deserialized_state); |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
static void scope_dump(Unit *u, FILE *f, const char *prefix) { |
| 246 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 247 |
|
| 248 |
assert(f); |
| 249 |
assert(prefix); |
| 250 |
|
| 251 |
fprintf(f, |
| 252 |
"%sScope State: %s\n" |
| 253 |
"%sResult: %s\n" |
| 254 |
"%sRuntimeMaxSec: %s\n" |
| 255 |
"%sRuntimeRandomizedExtraSec: %s\n" |
| 256 |
"%sOOMPolicy: %s\n", |
| 257 |
prefix, scope_state_to_string(s->state), |
| 258 |
prefix, scope_result_to_string(s->result), |
| 259 |
prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC), |
| 260 |
prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC), |
| 261 |
prefix, oom_policy_to_string(s->oom_policy)); |
| 262 |
|
| 263 |
cgroup_context_dump(u, f, prefix); |
| 264 |
kill_context_dump(&s->kill_context, f, prefix); |
| 265 |
} |
| 266 |
|
| 267 |
static void scope_enter_dead(Scope *s, ScopeResult f) { |
| 268 |
assert(s); |
| 269 |
|
| 270 |
if (s->result == SCOPE_SUCCESS) |
| 271 |
s->result = f; |
| 272 |
|
| 273 |
unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result)); |
| 274 |
scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD); |
| 275 |
} |
| 276 |
|
| 277 |
static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { |
| 278 |
bool skip_signal = false; |
| 279 |
int r; |
| 280 |
|
| 281 |
assert(s); |
| 282 |
|
| 283 |
if (s->result == SCOPE_SUCCESS) |
| 284 |
s->result = f; |
| 285 |
|
| 286 |
/* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going |
| 287 |
* directly into SIGTERM berserk mode */ |
| 288 |
if (state == SCOPE_STOP_SIGTERM) |
| 289 |
skip_signal = bus_scope_send_request_stop(s) > 0; |
| 290 |
|
| 291 |
if (skip_signal) |
| 292 |
r = 1; /* wait */ |
| 293 |
else { |
| 294 |
r = unit_kill_context( |
| 295 |
UNIT(s), |
| 296 |
state != SCOPE_STOP_SIGTERM ? KILL_KILL : |
| 297 |
s->was_abandoned ? KILL_TERMINATE_AND_LOG : |
| 298 |
KILL_TERMINATE); |
| 299 |
if (r < 0) { |
| 300 |
log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
| 301 |
goto fail; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if (r > 0) { |
| 306 |
r = scope_arm_timer(s, /* relative= */ true, s->timeout_stop_usec); |
| 307 |
if (r < 0) { |
| 308 |
log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); |
| 309 |
goto fail; |
| 310 |
} |
| 311 |
|
| 312 |
scope_set_state(s, state); |
| 313 |
} else if (state == SCOPE_STOP_SIGTERM) |
| 314 |
scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS); |
| 315 |
else |
| 316 |
scope_enter_dead(s, SCOPE_SUCCESS); |
| 317 |
|
| 318 |
return; |
| 319 |
|
| 320 |
fail: |
| 321 |
scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); |
| 322 |
} |
| 323 |
|
| 324 |
static int scope_enter_start_chown(Scope *s) { |
| 325 |
Unit *u = UNIT(ASSERT_PTR(s)); |
| 326 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 327 |
int r; |
| 328 |
|
| 329 |
assert(s->user); |
| 330 |
|
| 331 |
if (!s->cgroup_runtime) |
| 332 |
return -EINVAL; |
| 333 |
|
| 334 |
r = scope_arm_timer(s, /* relative= */ true, u->manager->defaults.timeout_start_usec); |
| 335 |
if (r < 0) |
| 336 |
return r; |
| 337 |
|
| 338 |
r = unit_fork_helper_process(u, "(sd-chown-cgroup)", /* into_cgroup= */ true, &pidref); |
| 339 |
if (r < 0) |
| 340 |
goto fail; |
| 341 |
|
| 342 |
if (r == 0) { |
| 343 |
uid_t uid = UID_INVALID; |
| 344 |
gid_t gid = GID_INVALID; |
| 345 |
|
| 346 |
if (!isempty(s->user)) { |
| 347 |
r = get_user_creds(s->user, /* flags= */ 0, NULL, &uid, &gid, NULL, NULL); |
| 348 |
if (r < 0) { |
| 349 |
log_unit_error_errno(UNIT(s), r, |
| 350 |
"Failed to resolve user '%s': %s", |
| 351 |
s->user, STRERROR_USER(r)); |
| 352 |
_exit(EXIT_USER); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
if (!isempty(s->group)) { |
| 357 |
r = get_group_creds(s->group, /* flags= */ 0, /* ret_name= */ NULL, &gid); |
| 358 |
if (r < 0) { |
| 359 |
log_unit_error_errno(UNIT(s), r, |
| 360 |
"Failed to resolve group '%s': %s", |
| 361 |
s->group, STRERROR_GROUP(r)); |
| 362 |
_exit(EXIT_GROUP); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
r = cg_set_access(s->cgroup_runtime->cgroup_path, uid, gid); |
| 367 |
if (r < 0) { |
| 368 |
log_unit_error_errno(UNIT(s), r, "Failed to adjust control group access: %m"); |
| 369 |
_exit(EXIT_CGROUP); |
| 370 |
} |
| 371 |
|
| 372 |
_exit(EXIT_SUCCESS); |
| 373 |
} |
| 374 |
|
| 375 |
r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true); |
| 376 |
if (r < 0) |
| 377 |
goto fail; |
| 378 |
|
| 379 |
scope_set_state(s, SCOPE_START_CHOWN); |
| 380 |
|
| 381 |
return 1; |
| 382 |
fail: |
| 383 |
s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
| 384 |
return r; |
| 385 |
} |
| 386 |
|
| 387 |
static int scope_enter_running(Scope *s) { |
| 388 |
Unit *u = UNIT(ASSERT_PTR(s)); |
| 389 |
int r; |
| 390 |
|
| 391 |
(void) bus_scope_track_controller(s); |
| 392 |
|
| 393 |
r = unit_acquire_invocation_id(u); |
| 394 |
if (r < 0) |
| 395 |
return r; |
| 396 |
|
| 397 |
unit_export_state_files(u); |
| 398 |
|
| 399 |
r = unit_attach_pids_to_cgroup(u, u->pids, NULL); |
| 400 |
if (r < 0) { |
| 401 |
log_unit_warning_errno(u, r, "Failed to add PIDs to scope's control group: %m"); |
| 402 |
goto fail; |
| 403 |
} |
| 404 |
if (r == 0) { |
| 405 |
r = log_unit_warning_errno(u, SYNTHETIC_ERRNO(ECHILD), "No PIDs left to attach to the scope's control group, refusing."); |
| 406 |
goto fail; |
| 407 |
} |
| 408 |
log_unit_debug(u, "%i %s added to scope's control group.", r, r == 1 ? "process" : "processes"); |
| 409 |
|
| 410 |
s->result = SCOPE_SUCCESS; |
| 411 |
|
| 412 |
scope_set_state(s, SCOPE_RUNNING); |
| 413 |
|
| 414 |
/* Set the maximum runtime timeout. */ |
| 415 |
scope_arm_timer(s, /* relative= */ false, scope_running_timeout(s)); |
| 416 |
|
| 417 |
/* Unwatch all pids we've just added to cgroup. We rely on empty notifications there. */ |
| 418 |
unit_unwatch_all_pids(u); |
| 419 |
|
| 420 |
return 1; |
| 421 |
|
| 422 |
fail: |
| 423 |
scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); |
| 424 |
return r; |
| 425 |
} |
| 426 |
|
| 427 |
static int scope_start(Unit *u) { |
| 428 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 429 |
|
| 430 |
if (unit_has_name(u, SPECIAL_INIT_SCOPE)) |
| 431 |
return -EPERM; |
| 432 |
|
| 433 |
if (s->state == SCOPE_FAILED) |
| 434 |
return -EPERM; |
| 435 |
|
| 436 |
/* We can't fulfill this right now, please try again later */ |
| 437 |
if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
| 438 |
return -EAGAIN; |
| 439 |
|
| 440 |
assert(s->state == SCOPE_DEAD); |
| 441 |
|
| 442 |
if (!u->transient && !MANAGER_IS_RELOADING(u->manager)) |
| 443 |
return -ENOENT; |
| 444 |
|
| 445 |
(void) unit_realize_cgroup(u); |
| 446 |
(void) unit_reset_accounting(u); |
| 447 |
|
| 448 |
/* We check only for User= option to keep behavior consistent with logic for service units, |
| 449 |
* i.e. having 'Delegate=true Group=foo' w/o specifying User= has no effect. */ |
| 450 |
if (s->user && unit_cgroup_delegate(u)) |
| 451 |
return scope_enter_start_chown(s); |
| 452 |
|
| 453 |
return scope_enter_running(s); |
| 454 |
} |
| 455 |
|
| 456 |
static int scope_stop(Unit *u) { |
| 457 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 458 |
|
| 459 |
if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
| 460 |
return 0; |
| 461 |
|
| 462 |
assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)); |
| 463 |
|
| 464 |
scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS); |
| 465 |
return 1; |
| 466 |
} |
| 467 |
|
| 468 |
static void scope_reset_failed(Unit *u) { |
| 469 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 470 |
|
| 471 |
if (s->state == SCOPE_FAILED) |
| 472 |
scope_set_state(s, SCOPE_DEAD); |
| 473 |
|
| 474 |
s->result = SCOPE_SUCCESS; |
| 475 |
} |
| 476 |
|
| 477 |
static int scope_get_timeout(Unit *u, usec_t *timeout) { |
| 478 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 479 |
usec_t t; |
| 480 |
int r; |
| 481 |
|
| 482 |
assert(timeout); |
| 483 |
|
| 484 |
if (!s->timer_event_source) |
| 485 |
return 0; |
| 486 |
|
| 487 |
r = sd_event_source_get_time(s->timer_event_source, &t); |
| 488 |
if (r < 0) |
| 489 |
return r; |
| 490 |
if (t == USEC_INFINITY) |
| 491 |
return 0; |
| 492 |
|
| 493 |
*timeout = t; |
| 494 |
return 1; |
| 495 |
} |
| 496 |
|
| 497 |
static int scope_serialize(Unit *u, FILE *f, FDSet *fds) { |
| 498 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 499 |
PidRef *pid; |
| 500 |
|
| 501 |
assert(f); |
| 502 |
assert(fds); |
| 503 |
|
| 504 |
(void) serialize_item(f, "state", scope_state_to_string(s->state)); |
| 505 |
(void) serialize_bool(f, "was-abandoned", s->was_abandoned); |
| 506 |
|
| 507 |
(void) serialize_item(f, "controller", s->controller); |
| 508 |
|
| 509 |
SET_FOREACH(pid, u->pids) |
| 510 |
serialize_pidref(f, fds, "pids", pid); |
| 511 |
|
| 512 |
return 0; |
| 513 |
} |
| 514 |
|
| 515 |
static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { |
| 516 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 517 |
int r; |
| 518 |
|
| 519 |
assert(key); |
| 520 |
assert(value); |
| 521 |
assert(fds); |
| 522 |
|
| 523 |
if (streq(key, "state")) { |
| 524 |
ScopeState state; |
| 525 |
|
| 526 |
state = scope_state_from_string(value); |
| 527 |
if (state < 0) |
| 528 |
log_unit_debug(u, "Failed to parse state value: %s", value); |
| 529 |
else |
| 530 |
s->deserialized_state = state; |
| 531 |
|
| 532 |
} else if (streq(key, "was-abandoned")) { |
| 533 |
int k; |
| 534 |
|
| 535 |
k = parse_boolean(value); |
| 536 |
if (k < 0) |
| 537 |
log_unit_debug(u, "Failed to parse boolean value: %s", value); |
| 538 |
else |
| 539 |
s->was_abandoned = k; |
| 540 |
} else if (streq(key, "controller")) { |
| 541 |
|
| 542 |
r = free_and_strdup(&s->controller, value); |
| 543 |
if (r < 0) |
| 544 |
return log_oom(); |
| 545 |
|
| 546 |
} else if (streq(key, "pids")) { |
| 547 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 548 |
|
| 549 |
/* We don't check if we already received the pid before here because unit_watch_pidref() |
| 550 |
* does this check internally and discards the new pidref if we already received it before. */ |
| 551 |
if (deserialize_pidref(fds, value, &pidref) >= 0) { |
| 552 |
r = unit_watch_pidref(u, &pidref, /* exclusive= */ false); |
| 553 |
if (r < 0) |
| 554 |
log_unit_debug(u, "Failed to watch PID, ignoring: %s", value); |
| 555 |
} |
| 556 |
} else |
| 557 |
log_unit_debug(u, "Unknown serialization key: %s", key); |
| 558 |
|
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
static void scope_notify_cgroup_empty_event(Unit *u) { |
| 563 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 564 |
|
| 565 |
log_unit_debug(u, "cgroup is empty"); |
| 566 |
|
| 567 |
if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL)) |
| 568 |
scope_enter_dead(s, SCOPE_SUCCESS); |
| 569 |
} |
| 570 |
|
| 571 |
static void scope_notify_cgroup_oom_event(Unit *u, bool managed_oom) { |
| 572 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 573 |
|
| 574 |
if (managed_oom) |
| 575 |
log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd."); |
| 576 |
else |
| 577 |
log_unit_debug(u, "Process of control group was killed by the OOM killer."); |
| 578 |
|
| 579 |
if (s->oom_policy == OOM_CONTINUE) |
| 580 |
return; |
| 581 |
|
| 582 |
switch (s->state) { |
| 583 |
|
| 584 |
case SCOPE_START_CHOWN: |
| 585 |
case SCOPE_RUNNING: |
| 586 |
scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_OOM_KILL); |
| 587 |
break; |
| 588 |
|
| 589 |
case SCOPE_STOP_SIGTERM: |
| 590 |
scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_OOM_KILL); |
| 591 |
break; |
| 592 |
|
| 593 |
case SCOPE_STOP_SIGKILL: |
| 594 |
if (s->result == SCOPE_SUCCESS) |
| 595 |
s->result = SCOPE_FAILURE_OOM_KILL; |
| 596 |
break; |
| 597 |
/* SCOPE_DEAD, SCOPE_ABANDONED, and SCOPE_FAILED end up in default */ |
| 598 |
default: |
| 599 |
; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
| 604 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 605 |
|
| 606 |
if (s->state == SCOPE_START_CHOWN) { |
| 607 |
if (!is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL)) |
| 608 |
scope_enter_dead(s, SCOPE_FAILURE_RESOURCES); |
| 609 |
else |
| 610 |
scope_enter_running(s); |
| 611 |
return; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
| 616 |
Scope *s = ASSERT_PTR(SCOPE(userdata)); |
| 617 |
|
| 618 |
assert(s->timer_event_source == source); |
| 619 |
|
| 620 |
switch (s->state) { |
| 621 |
|
| 622 |
case SCOPE_RUNNING: |
| 623 |
log_unit_warning(UNIT(s), "Scope reached runtime time limit. Stopping."); |
| 624 |
scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_TIMEOUT); |
| 625 |
break; |
| 626 |
|
| 627 |
case SCOPE_STOP_SIGTERM: |
| 628 |
if (s->kill_context.send_sigkill) { |
| 629 |
log_unit_warning(UNIT(s), "Stopping timed out. Killing."); |
| 630 |
scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT); |
| 631 |
} else { |
| 632 |
log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); |
| 633 |
scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
| 634 |
} |
| 635 |
|
| 636 |
break; |
| 637 |
|
| 638 |
case SCOPE_STOP_SIGKILL: |
| 639 |
log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring."); |
| 640 |
scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
| 641 |
break; |
| 642 |
|
| 643 |
case SCOPE_START_CHOWN: |
| 644 |
log_unit_warning(UNIT(s), "User lookup timed out. Entering failed state."); |
| 645 |
scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT); |
| 646 |
break; |
| 647 |
|
| 648 |
default: |
| 649 |
assert_not_reached(); |
| 650 |
} |
| 651 |
|
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
int scope_abandon(Scope *s) { |
| 656 |
assert(s); |
| 657 |
|
| 658 |
if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) |
| 659 |
return -EPERM; |
| 660 |
|
| 661 |
if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED)) |
| 662 |
return -ESTALE; |
| 663 |
|
| 664 |
s->was_abandoned = true; |
| 665 |
|
| 666 |
s->controller = mfree(s->controller); |
| 667 |
s->controller_track = sd_bus_track_unref(s->controller_track); |
| 668 |
|
| 669 |
scope_set_state(s, SCOPE_ABANDONED); |
| 670 |
|
| 671 |
return 0; |
| 672 |
} |
| 673 |
|
| 674 |
static UnitActiveState scope_active_state(Unit *u) { |
| 675 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 676 |
|
| 677 |
return state_translation_table[s->state]; |
| 678 |
} |
| 679 |
|
| 680 |
static const char *scope_sub_state_to_string(Unit *u) { |
| 681 |
Scope *s = ASSERT_PTR(SCOPE(u)); |
| 682 |
|
| 683 |
return scope_state_to_string(s->state); |
| 684 |
} |
| 685 |
|
| 686 |
static void scope_enumerate_perpetual(Manager *m) { |
| 687 |
Unit *u; |
| 688 |
int r; |
| 689 |
|
| 690 |
assert(m); |
| 691 |
|
| 692 |
/* Let's unconditionally add the "init.scope" special unit |
| 693 |
* that encapsulates PID 1. Note that PID 1 already is in the |
| 694 |
* cgroup for this, we hence just need to allocate the object |
| 695 |
* for it and that's it. */ |
| 696 |
|
| 697 |
u = manager_get_unit(m, SPECIAL_INIT_SCOPE); |
| 698 |
if (!u) { |
| 699 |
r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u); |
| 700 |
if (r < 0) |
| 701 |
return (void) log_error_errno(r, "Failed to allocate the special %s unit: %m", |
| 702 |
SPECIAL_INIT_SCOPE); |
| 703 |
} |
| 704 |
|
| 705 |
u->transient = true; |
| 706 |
u->perpetual = true; |
| 707 |
SCOPE(u)->deserialized_state = SCOPE_RUNNING; |
| 708 |
|
| 709 |
unit_add_to_load_queue(u); |
| 710 |
unit_add_to_dbus_queue(u); |
| 711 |
/* Enqueue an explicit cgroup realization here. Unlike other cgroups this one already exists and is |
| 712 |
* populated (by us, after all!) already, even when we are not in a reload cycle. Hence we cannot |
| 713 |
* apply the settings at creation time anymore, but let's at least apply them asynchronously. */ |
| 714 |
unit_add_to_cgroup_realize_queue(u); |
| 715 |
} |
| 716 |
|
| 717 |
static const char* const scope_result_table[_SCOPE_RESULT_MAX] = { |
| 718 |
[SCOPE_SUCCESS] = "success", |
| 719 |
[SCOPE_FAILURE_RESOURCES] = "resources", |
| 720 |
[SCOPE_FAILURE_TIMEOUT] = "timeout", |
| 721 |
[SCOPE_FAILURE_OOM_KILL] = "oom-kill", |
| 722 |
}; |
| 723 |
|
| 724 |
DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult); |
| 725 |
|
| 726 |
const UnitVTable scope_vtable = { |
| 727 |
.object_size = sizeof(Scope), |
| 728 |
.cgroup_context_offset = offsetof(Scope, cgroup_context), |
| 729 |
.kill_context_offset = offsetof(Scope, kill_context), |
| 730 |
.cgroup_runtime_offset = offsetof(Scope, cgroup_runtime), |
| 731 |
|
| 732 |
.sections = |
| 733 |
"Unit\0" |
| 734 |
"Scope\0" |
| 735 |
"Install\0", |
| 736 |
.private_section = "Scope", |
| 737 |
|
| 738 |
.can_transient = true, |
| 739 |
.can_delegate = true, |
| 740 |
.can_fail = true, |
| 741 |
.once_only = true, |
| 742 |
.can_set_managed_oom = true, |
| 743 |
.track_orphaned = true, |
| 744 |
|
| 745 |
.init = scope_init, |
| 746 |
.load = scope_load, |
| 747 |
.done = scope_done, |
| 748 |
|
| 749 |
.coldplug = scope_coldplug, |
| 750 |
|
| 751 |
.dump = scope_dump, |
| 752 |
|
| 753 |
.start = scope_start, |
| 754 |
.stop = scope_stop, |
| 755 |
|
| 756 |
.freezer_action = unit_cgroup_freezer_action, |
| 757 |
|
| 758 |
.get_timeout = scope_get_timeout, |
| 759 |
|
| 760 |
.serialize = scope_serialize, |
| 761 |
.deserialize_item = scope_deserialize_item, |
| 762 |
|
| 763 |
.active_state = scope_active_state, |
| 764 |
.sub_state_to_string = scope_sub_state_to_string, |
| 765 |
|
| 766 |
.sigchld_event = scope_sigchld_event, |
| 767 |
|
| 768 |
.reset_failed = scope_reset_failed, |
| 769 |
|
| 770 |
.notify_cgroup_empty = scope_notify_cgroup_empty_event, |
| 771 |
.notify_cgroup_oom = scope_notify_cgroup_oom_event, |
| 772 |
|
| 773 |
.bus_set_property = bus_scope_set_property, |
| 774 |
.bus_commit_properties = bus_scope_commit_properties, |
| 775 |
|
| 776 |
.enumerate_perpetual = scope_enumerate_perpetual, |
| 777 |
}; |