• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14161316195

30 Mar 2025 08:18AM UTC coverage: 71.892% (-0.08%) from 71.976%
14161316195

push

github

DaanDeMeyer
test: skip networkd tests if networkd/resolved are disabled at build time

296399 of 412283 relevant lines covered (71.89%)

653258.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.79
/src/core/scope.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <unistd.h>
5

6
#include "alloc-util.h"
7
#include "cgroup-setup.h"
8
#include "dbus-scope.h"
9
#include "dbus-unit.h"
10
#include "exit-status.h"
11
#include "load-dropin.h"
12
#include "log.h"
13
#include "process-util.h"
14
#include "random-util.h"
15
#include "scope.h"
16
#include "serialize.h"
17
#include "special.h"
18
#include "string-table.h"
19
#include "string-util.h"
20
#include "strv.h"
21
#include "unit-name.h"
22
#include "unit.h"
23
#include "user-util.h"
24

25
static const UnitActiveState state_translation_table[_SCOPE_STATE_MAX] = {
26
        [SCOPE_DEAD]         = UNIT_INACTIVE,
27
        [SCOPE_START_CHOWN]  = UNIT_ACTIVATING,
28
        [SCOPE_RUNNING]      = UNIT_ACTIVE,
29
        [SCOPE_ABANDONED]    = UNIT_ACTIVE,
30
        [SCOPE_STOP_SIGTERM] = UNIT_DEACTIVATING,
31
        [SCOPE_STOP_SIGKILL] = UNIT_DEACTIVATING,
32
        [SCOPE_FAILED]       = UNIT_FAILED,
33
};
34

35
static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
36

37
static void scope_init(Unit *u) {
289✔
38
        Scope *s = ASSERT_PTR(SCOPE(u));
289✔
39

40
        assert(u->load_state == UNIT_STUB);
289✔
41

42
        s->runtime_max_usec = USEC_INFINITY;
289✔
43
        s->timeout_stop_usec = u->manager->defaults.timeout_stop_usec;
289✔
44
        u->ignore_on_isolate = true;
289✔
45
        s->user = s->group = NULL;
289✔
46
        s->oom_policy = _OOM_POLICY_INVALID;
289✔
47
}
289✔
48

49
static void scope_done(Unit *u) {
289✔
50
        Scope *s = ASSERT_PTR(SCOPE(u));
289✔
51

52
        s->controller = mfree(s->controller);
289✔
53
        s->controller_track = sd_bus_track_unref(s->controller_track);
289✔
54

55
        s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
289✔
56

57
        s->user = mfree(s->user);
289✔
58
        s->group = mfree(s->group);
289✔
59
}
289✔
60

61
static usec_t scope_running_timeout(Scope *s) {
276✔
62
        usec_t delta = 0;
276✔
63

64
        assert(s);
276✔
65

66
        if (s->runtime_rand_extra_usec != 0) {
276✔
67
                delta = random_u64_range(s->runtime_rand_extra_usec);
×
68
                log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC));
×
69
        }
70

71
        return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic,
276✔
72
                                 s->runtime_max_usec),
73
                        delta);
74
}
75

76
static int scope_arm_timer(Scope *s, bool relative, usec_t usec) {
277✔
77
        assert(s);
277✔
78

79
        return unit_arm_timer(UNIT(s), &s->timer_event_source, relative, usec, scope_dispatch_timer);
277✔
80
}
81

82
static void scope_set_state(Scope *s, ScopeState state) {
291✔
83
        ScopeState old_state;
291✔
84

85
        assert(s);
291✔
86

87
        if (s->state != state)
291✔
88
                bus_unit_send_pending_change_signal(UNIT(s), false);
291✔
89

90
        old_state = s->state;
291✔
91
        s->state = state;
291✔
92

93
        if (!IN_SET(state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL, SCOPE_START_CHOWN, SCOPE_RUNNING))
291✔
94
                s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
14✔
95

96
        if (!IN_SET(old_state, SCOPE_DEAD, SCOPE_FAILED) && IN_SET(state, SCOPE_DEAD, SCOPE_FAILED)) {
291✔
97
                unit_unwatch_all_pids(UNIT(s));
9✔
98
                unit_dequeue_rewatch_pids(UNIT(s));
9✔
99
        }
100

101
        if (state != old_state)
291✔
102
                log_unit_debug(UNIT(s), "Changed %s -> %s",
568✔
103
                               scope_state_to_string(old_state), scope_state_to_string(state));
104

105
        unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
291✔
106
}
291✔
107

108
static int scope_add_default_dependencies(Scope *s) {
277✔
109
        int r;
277✔
110

111
        assert(s);
277✔
112

113
        if (!UNIT(s)->default_dependencies)
277✔
114
                return 0;
115

116
        /* Make sure scopes are unloaded on shutdown */
117
        r = unit_add_two_dependencies_by_name(
126✔
118
                        UNIT(s),
84✔
119
                        UNIT_BEFORE, UNIT_CONFLICTS,
120
                        SPECIAL_SHUTDOWN_TARGET, true,
121
                        UNIT_DEPENDENCY_DEFAULT);
122
        if (r < 0)
42✔
123
                return r;
×
124

125
        return 0;
126
}
127

128
static int scope_verify(Scope *s) {
277✔
129
        assert(s);
277✔
130
        assert(UNIT(s)->load_state == UNIT_LOADED);
277✔
131

132
        if (set_isempty(UNIT(s)->pids) &&
554✔
133
            !MANAGER_IS_RELOADING(UNIT(s)->manager) &&
442✔
134
            !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
180✔
135
                return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOENT), "Scope has no PIDs. Refusing.");
×
136

137
        return 0;
138
}
139

140
static int scope_load_init_scope(Unit *u) {
277✔
141
        assert(u);
277✔
142

143
        if (!unit_has_name(u, SPECIAL_INIT_SCOPE))
277✔
144
                return 0;
145

146
        u->transient = true;
235✔
147
        u->perpetual = true;
235✔
148

149
        /* init.scope is a bit special, as it has to stick around forever. Because of its special semantics we
150
         * synthesize it here, instead of relying on the unit file on disk. */
151

152
        u->default_dependencies = false;
235✔
153

154
        /* Prettify things, if we can. */
155
        if (!u->description)
235✔
156
                u->description = strdup("System and Service Manager");
235✔
157
        if (!u->documentation)
235✔
158
                (void) strv_extend(&u->documentation, "man:systemd(1)");
235✔
159

160
        return 1;
161
}
162

163
static int scope_add_extras(Scope *s) {
277✔
164
        int r;
277✔
165

166
        r = unit_patch_contexts(UNIT(s));
277✔
167
        if (r < 0)
277✔
168
                return r;
169

170
        r = unit_set_default_slice(UNIT(s));
277✔
171
        if (r < 0)
277✔
172
                return r;
173

174
        if (s->oom_policy < 0)
277✔
175
                s->oom_policy = s->cgroup_context.delegate ? OOM_CONTINUE : UNIT(s)->manager->defaults.oom_policy;
239✔
176

177
        s->cgroup_context.memory_oom_group = s->oom_policy == OOM_KILL;
277✔
178

179
        return scope_add_default_dependencies(s);
277✔
180
}
181

182
static int scope_load(Unit *u) {
304✔
183
        Scope *s = ASSERT_PTR(SCOPE(u));
304✔
184
        int r;
304✔
185

186
        assert(u->load_state == UNIT_STUB);
304✔
187

188
        if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
304✔
189
                /* Refuse to load non-transient scope units, but allow them while reloading. */
190
                return -ENOENT;
191

192
        r = scope_load_init_scope(u);
277✔
193
        if (r < 0)
277✔
194
                return r;
195

196
        r = unit_load_fragment_and_dropin(u, false);
277✔
197
        if (r < 0)
277✔
198
                return r;
199

200
        if (u->load_state != UNIT_LOADED)
277✔
201
                return 0;
202

203
        r = scope_add_extras(s);
277✔
204
        if (r < 0)
277✔
205
                return r;
206

207
        return scope_verify(s);
277✔
208
}
209

210
static usec_t scope_coldplug_timeout(Scope *s) {
261✔
211
        assert(s);
261✔
212

213
        switch (s->deserialized_state) {
261✔
214

215
        case SCOPE_RUNNING:
261✔
216
                return scope_running_timeout(s);
261✔
217

218
        case SCOPE_STOP_SIGKILL:
219
        case SCOPE_STOP_SIGTERM:
220
                return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec);
×
221

222
        default:
223
                return USEC_INFINITY;
224
        }
225
}
226

227
static int scope_coldplug(Unit *u) {
262✔
228
        Scope *s = ASSERT_PTR(SCOPE(u));
262✔
229
        int r;
262✔
230

231
        assert(s->state == SCOPE_DEAD);
262✔
232

233
        if (s->deserialized_state == s->state)
262✔
234
                return 0;
235

236
        r = scope_arm_timer(s, /* relative= */ false, scope_coldplug_timeout(s));
261✔
237
        if (r < 0)
261✔
238
                return r;
239

240
        if (!IN_SET(s->deserialized_state, SCOPE_DEAD, SCOPE_FAILED)) {
261✔
241
                if (u->pids) {
261✔
242
                        PidRef *pid;
×
243

244
                        SET_FOREACH(pid, u->pids) {
×
245
                                r = unit_watch_pidref(u, pid, /* exclusive= */ false);
×
246
                                if (r < 0 && r != -EEXIST)
×
247
                                        return r;
×
248
                        }
249
                } else
250
                        (void) unit_enqueue_rewatch_pids(u);
261✔
251
        }
252

253
        bus_scope_track_controller(s);
261✔
254

255
        scope_set_state(s, s->deserialized_state);
261✔
256
        return 0;
261✔
257
}
258

259
static void scope_dump(Unit *u, FILE *f, const char *prefix) {
6✔
260
        Scope *s = ASSERT_PTR(SCOPE(u));
6✔
261

262
        assert(f);
6✔
263
        assert(prefix);
6✔
264

265
        fprintf(f,
12✔
266
                "%sScope State: %s\n"
267
                "%sResult: %s\n"
268
                "%sRuntimeMaxSec: %s\n"
269
                "%sRuntimeRandomizedExtraSec: %s\n"
270
                "%sOOMPolicy: %s\n",
271
                prefix, scope_state_to_string(s->state),
272
                prefix, scope_result_to_string(s->result),
273
                prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC),
6✔
274
                prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC),
6✔
275
                prefix, oom_policy_to_string(s->oom_policy));
276

277
        cgroup_context_dump(u, f, prefix);
6✔
278
        kill_context_dump(&s->kill_context, f, prefix);
6✔
279
}
6✔
280

281
static void scope_enter_dead(Scope *s, ScopeResult f) {
9✔
282
        assert(s);
9✔
283

284
        if (s->result == SCOPE_SUCCESS)
9✔
285
                s->result = f;
9✔
286

287
        unit_log_result(UNIT(s), s->result == SCOPE_SUCCESS, scope_result_to_string(s->result));
9✔
288
        scope_set_state(s, s->result != SCOPE_SUCCESS ? SCOPE_FAILED : SCOPE_DEAD);
18✔
289
}
9✔
290

291
static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
1✔
292
        bool skip_signal = false;
1✔
293
        int r;
1✔
294

295
        assert(s);
1✔
296

297
        if (s->result == SCOPE_SUCCESS)
1✔
298
                s->result = f;
1✔
299

300
        /* Before sending any signal, make sure we track all members of this cgroup */
301
        (void) unit_watch_all_pids(UNIT(s));
1✔
302

303
        /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have
304
         * died now */
305
        (void) unit_enqueue_rewatch_pids(UNIT(s));
2✔
306

307
        /* If we have a controller set let's ask the controller nicely to terminate the scope, instead of us going
308
         * directly into SIGTERM berserk mode */
309
        if (state == SCOPE_STOP_SIGTERM)
1✔
310
                skip_signal = bus_scope_send_request_stop(s) > 0;
1✔
311

312
        if (skip_signal)
1✔
313
                r = 1; /* wait */
314
        else {
315
                r = unit_kill_context(
1✔
316
                                UNIT(s),
1✔
317
                                state != SCOPE_STOP_SIGTERM ? KILL_KILL :
318
                                s->was_abandoned            ? KILL_TERMINATE_AND_LOG :
1✔
319
                                                              KILL_TERMINATE);
320
                if (r < 0) {
1✔
321
                        log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
×
322
                        goto fail;
×
323
                }
324
        }
325

326
        if (r > 0) {
1✔
327
                r = scope_arm_timer(s, /* relative= */ true, s->timeout_stop_usec);
1✔
328
                if (r < 0) {
1✔
329
                        log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m");
×
330
                        goto fail;
×
331
                }
332

333
                scope_set_state(s, state);
1✔
334
        } else if (state == SCOPE_STOP_SIGTERM)
×
335
                scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_SUCCESS);
×
336
        else
337
                scope_enter_dead(s, SCOPE_SUCCESS);
×
338

339
        return;
340

341
fail:
×
342
        scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
×
343
}
344

345
static int scope_enter_start_chown(Scope *s) {
×
346
        Unit *u = UNIT(ASSERT_PTR(s));
×
347
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
×
348
        int r;
×
349

350
        assert(s->user);
×
351

352
        if (!s->cgroup_runtime)
×
353
                return -EINVAL;
354

355
        r = scope_arm_timer(s, /* relative= */ true, u->manager->defaults.timeout_start_usec);
×
356
        if (r < 0)
×
357
                return r;
358

359
        r = unit_fork_helper_process(u, "(sd-chown-cgroup)", /* into_cgroup= */ true, &pidref);
×
360
        if (r < 0)
1✔
361
                goto fail;
×
362

363
        if (r == 0) {
1✔
364
                uid_t uid = UID_INVALID;
1✔
365
                gid_t gid = GID_INVALID;
1✔
366

367
                if (!isempty(s->user)) {
1✔
368
                        const char *user = s->user;
1✔
369

370
                        r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
1✔
371
                        if (r < 0) {
1✔
372
                                log_unit_error_errno(UNIT(s), r, "Failed to resolve user \"%s\": %m", user);
×
373
                                _exit(EXIT_USER);
×
374
                        }
375
                }
376

377
                if (!isempty(s->group)) {
1✔
378
                        const char *group = s->group;
×
379

380
                        r = get_group_creds(&group, &gid, 0);
×
381
                        if (r < 0) {
×
382
                                log_unit_error_errno(UNIT(s), r, "Failed to resolve group \"%s\": %m", group);
×
383
                                _exit(EXIT_GROUP);
×
384
                        }
385
                }
386

387
                r = cg_set_access(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_runtime->cgroup_path, uid, gid);
1✔
388
                if (r < 0) {
1✔
389
                        log_unit_error_errno(UNIT(s), r, "Failed to adjust control group access: %m");
×
390
                        _exit(EXIT_CGROUP);
×
391
                }
392

393
                _exit(EXIT_SUCCESS);
1✔
394
        }
395

396
        r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true);
×
397
        if (r < 0)
×
398
                goto fail;
×
399

400
        scope_set_state(s, SCOPE_START_CHOWN);
×
401

402
        return 1;
403
fail:
×
404
        s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source);
×
405
        return r;
×
406
}
407

408
static int scope_enter_running(Scope *s) {
15✔
409
        Unit *u = UNIT(ASSERT_PTR(s));
15✔
410
        int r;
15✔
411

412
        (void) bus_scope_track_controller(s);
15✔
413

414
        r = unit_acquire_invocation_id(u);
15✔
415
        if (r < 0)
15✔
416
                return r;
417

418
        unit_export_state_files(u);
15✔
419

420
        r = unit_attach_pids_to_cgroup(u, u->pids, NULL);
15✔
421
        if (r < 0) {
15✔
422
                log_unit_warning_errno(u, r, "Failed to add PIDs to scope's control group: %m");
×
423
                goto fail;
×
424
        }
425
        if (r == 0) {
15✔
426
                r = log_unit_warning_errno(u, SYNTHETIC_ERRNO(ECHILD), "No PIDs left to attach to the scope's control group, refusing.");
×
427
                goto fail;
×
428
        }
429
        log_unit_debug(u, "%i %s added to scope's control group.", r, r == 1 ? "process" : "processes");
31✔
430

431
        s->result = SCOPE_SUCCESS;
15✔
432

433
        scope_set_state(s, SCOPE_RUNNING);
15✔
434

435
        /* Set the maximum runtime timeout. */
436
        scope_arm_timer(s, /* relative= */ false, scope_running_timeout(s));
15✔
437

438
        /* On unified we use proper notifications hence we can unwatch the PIDs
439
         * we just attached to the scope. This can also be done on legacy as
440
         * we're going to update the list of the processes we watch with the
441
         * PIDs currently in the scope anyway. */
442
        unit_unwatch_all_pids(u);
15✔
443

444
        /* Start watching the PIDs currently in the scope (legacy hierarchy only) */
445
        (void) unit_enqueue_rewatch_pids(u);
15✔
446
        return 1;
15✔
447

448
fail:
×
449
        scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
×
450
        return r;
×
451
}
452

453
static int scope_start(Unit *u) {
15✔
454
        Scope *s = ASSERT_PTR(SCOPE(u));
15✔
455

456
        if (unit_has_name(u, SPECIAL_INIT_SCOPE))
15✔
457
                return -EPERM;
458

459
        if (s->state == SCOPE_FAILED)
15✔
460
                return -EPERM;
461

462
        /* We can't fulfill this right now, please try again later */
463
        if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
15✔
464
                return -EAGAIN;
465

466
        assert(s->state == SCOPE_DEAD);
15✔
467

468
        if (!u->transient && !MANAGER_IS_RELOADING(u->manager))
15✔
469
                return -ENOENT;
470

471
        (void) unit_realize_cgroup(u);
15✔
472
        (void) unit_reset_accounting(u);
15✔
473

474
        /* We check only for User= option to keep behavior consistent with logic for service units,
475
         * i.e. having 'Delegate=true Group=foo' w/o specifying User= has no effect. */
476
        if (s->user && unit_cgroup_delegate(u))
15✔
477
                return scope_enter_start_chown(s);
×
478

479
        return scope_enter_running(s);
15✔
480
}
481

482
static int scope_stop(Unit *u) {
1✔
483
        Scope *s = ASSERT_PTR(SCOPE(u));
1✔
484

485
        if (IN_SET(s->state, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
1✔
486
                return 0;
487

488
        assert(IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED));
1✔
489

490
        scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_SUCCESS);
1✔
491
        return 1;
1✔
492
}
493

494
static void scope_reset_failed(Unit *u) {
×
495
        Scope *s = ASSERT_PTR(SCOPE(u));
×
496

497
        if (s->state == SCOPE_FAILED)
×
498
                scope_set_state(s, SCOPE_DEAD);
×
499

500
        s->result = SCOPE_SUCCESS;
×
501
}
×
502

503
static int scope_get_timeout(Unit *u, usec_t *timeout) {
×
504
        Scope *s = ASSERT_PTR(SCOPE(u));
×
505
        usec_t t;
×
506
        int r;
×
507

508
        if (!s->timer_event_source)
×
509
                return 0;
×
510

511
        r = sd_event_source_get_time(s->timer_event_source, &t);
×
512
        if (r < 0)
×
513
                return r;
514
        if (t == USEC_INFINITY)
×
515
                return 0;
516

517
        *timeout = t;
×
518
        return 1;
×
519
}
520

521
static int scope_serialize(Unit *u, FILE *f, FDSet *fds) {
105✔
522
        Scope *s = ASSERT_PTR(SCOPE(u));
105✔
523
        PidRef *pid;
105✔
524

525
        assert(f);
105✔
526
        assert(fds);
105✔
527

528
        (void) serialize_item(f, "state", scope_state_to_string(s->state));
105✔
529
        (void) serialize_bool(f, "was-abandoned", s->was_abandoned);
105✔
530

531
        if (s->controller)
105✔
532
                (void) serialize_item(f, "controller", s->controller);
×
533

534
        SET_FOREACH(pid, u->pids)
106✔
535
                serialize_pidref(f, fds, "pids", pid);
1✔
536

537
        return 0;
105✔
538
}
539

540
static int scope_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
166✔
541
        Scope *s = ASSERT_PTR(SCOPE(u));
166✔
542
        int r;
166✔
543

544
        assert(key);
166✔
545
        assert(value);
166✔
546
        assert(fds);
166✔
547

548
        if (streq(key, "state")) {
166✔
549
                ScopeState state;
82✔
550

551
                state = scope_state_from_string(value);
82✔
552
                if (state < 0)
82✔
553
                        log_unit_debug(u, "Failed to parse state value: %s", value);
×
554
                else
555
                        s->deserialized_state = state;
82✔
556

557
        } else if (streq(key, "was-abandoned")) {
84✔
558
                int k;
82✔
559

560
                k = parse_boolean(value);
82✔
561
                if (k < 0)
82✔
562
                        log_unit_debug(u, "Failed to parse boolean value: %s", value);
×
563
                else
564
                        s->was_abandoned = k;
82✔
565
        } else if (streq(key, "controller")) {
2✔
566

567
                r = free_and_strdup(&s->controller, value);
×
568
                if (r < 0)
×
569
                        return log_oom();
×
570

571
        } else if (streq(key, "pids")) {
2✔
572
                _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
2✔
573

574
                /* We don't check if we already received the pid before here because unit_watch_pidref()
575
                 * does this check internally and discards the new pidref if we already received it before. */
576
                if (deserialize_pidref(fds, value, &pidref) >= 0) {
2✔
577
                        r = unit_watch_pidref(u, &pidref, /* exclusive= */ false);
2✔
578
                        if (r < 0)
2✔
579
                                log_unit_debug(u, "Failed to watch PID, ignoring: %s", value);
×
580
                }
581
        } else
582
                log_unit_debug(u, "Unknown serialization key: %s", key);
×
583

584
        return 0;
585
}
586

587
static void scope_notify_cgroup_empty_event(Unit *u) {
9✔
588
        Scope *s = ASSERT_PTR(SCOPE(u));
9✔
589

590
        log_unit_debug(u, "cgroup is empty");
18✔
591

592
        if (IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED, SCOPE_STOP_SIGTERM, SCOPE_STOP_SIGKILL))
9✔
593
                scope_enter_dead(s, SCOPE_SUCCESS);
9✔
594
}
9✔
595

596
static void scope_notify_cgroup_oom_event(Unit *u, bool managed_oom) {
×
597
        Scope *s = ASSERT_PTR(SCOPE(u));
×
598

599
        if (managed_oom)
×
600
                log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd.");
×
601
        else
602
                log_unit_debug(u, "Process of control group was killed by the OOM killer.");
×
603

604
        if (s->oom_policy == OOM_CONTINUE)
×
605
                return;
606

607
        switch (s->state) {
×
608

609
        case SCOPE_START_CHOWN:
×
610
        case SCOPE_RUNNING:
611
                scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_OOM_KILL);
×
612
                break;
×
613

614
        case SCOPE_STOP_SIGTERM:
×
615
                scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_OOM_KILL);
×
616
                break;
×
617

618
        case SCOPE_STOP_SIGKILL:
×
619
                if (s->result == SCOPE_SUCCESS)
×
620
                        s->result = SCOPE_FAILURE_OOM_KILL;
×
621
                break;
622
        /* SCOPE_DEAD, SCOPE_ABANDONED, and SCOPE_FAILED end up in default */
623
        default:
×
624
                ;
×
625
        }
626
}
627

628
static void scope_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2,544✔
629
        Scope *s = ASSERT_PTR(SCOPE(u));
2,544✔
630

631
        if (s->state == SCOPE_START_CHOWN) {
2,544✔
632
                if (!is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
×
633
                        scope_enter_dead(s, SCOPE_FAILURE_RESOURCES);
×
634
                else
635
                        scope_enter_running(s);
×
636
                return;
×
637
        }
638

639
        /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to
640
         * watch, under the assumption that we'll sooner or later get a SIGCHLD for them, as the original
641
         * process we watched was probably the parent of them, and they are hence now our children. */
642

643
        (void) unit_enqueue_rewatch_pids(u);
2,544✔
644
}
645

646
static int scope_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
×
647
        Scope *s = ASSERT_PTR(SCOPE(userdata));
×
648

649
        assert(s->timer_event_source == source);
×
650

651
        switch (s->state) {
×
652

653
        case SCOPE_RUNNING:
654
                log_unit_warning(UNIT(s), "Scope reached runtime time limit. Stopping.");
×
655
                scope_enter_signal(s, SCOPE_STOP_SIGTERM, SCOPE_FAILURE_TIMEOUT);
×
656
                break;
×
657

658
        case SCOPE_STOP_SIGTERM:
×
659
                if (s->kill_context.send_sigkill) {
×
660
                        log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
×
661
                        scope_enter_signal(s, SCOPE_STOP_SIGKILL, SCOPE_FAILURE_TIMEOUT);
×
662
                } else {
663
                        log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL.");
×
664
                        scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
×
665
                }
666

667
                break;
668

669
        case SCOPE_STOP_SIGKILL:
670
                log_unit_warning(UNIT(s), "Still around after SIGKILL. Ignoring.");
×
671
                scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
×
672
                break;
×
673

674
        case SCOPE_START_CHOWN:
675
                log_unit_warning(UNIT(s), "User lookup timed out. Entering failed state.");
×
676
                scope_enter_dead(s, SCOPE_FAILURE_TIMEOUT);
×
677
                break;
×
678

679
        default:
×
680
                assert_not_reached();
×
681
        }
682

683
        return 0;
×
684
}
685

686
int scope_abandon(Scope *s) {
7✔
687
        assert(s);
7✔
688

689
        if (unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE))
7✔
690
                return -EPERM;
691

692
        if (!IN_SET(s->state, SCOPE_RUNNING, SCOPE_ABANDONED))
7✔
693
                return -ESTALE;
694

695
        s->was_abandoned = true;
5✔
696

697
        s->controller = mfree(s->controller);
5✔
698
        s->controller_track = sd_bus_track_unref(s->controller_track);
5✔
699

700
        scope_set_state(s, SCOPE_ABANDONED);
5✔
701

702
        /* The client is no longer watching the remaining processes, so let's step in here, under the assumption that
703
         * the remaining processes will be sooner or later reassigned to us as parent. */
704
        (void) unit_enqueue_rewatch_pids(UNIT(s));
5✔
705

706
        return 0;
5✔
707
}
708

709
static UnitActiveState scope_active_state(Unit *u) {
12,321✔
710
        Scope *s = ASSERT_PTR(SCOPE(u));
12,321✔
711

712
        return state_translation_table[s->state];
12,321✔
713
}
714

715
static const char *scope_sub_state_to_string(Unit *u) {
155✔
716
        Scope *s = ASSERT_PTR(SCOPE(u));
155✔
717

718
        return scope_state_to_string(s->state);
155✔
719
}
720

721
static void scope_enumerate_perpetual(Manager *m) {
235✔
722
        Unit *u;
235✔
723
        int r;
235✔
724

725
        assert(m);
235✔
726

727
        /* Let's unconditionally add the "init.scope" special unit
728
         * that encapsulates PID 1. Note that PID 1 already is in the
729
         * cgroup for this, we hence just need to allocate the object
730
         * for it and that's it. */
731

732
        u = manager_get_unit(m, SPECIAL_INIT_SCOPE);
235✔
733
        if (!u) {
235✔
734
                r = unit_new_for_name(m, sizeof(Scope), SPECIAL_INIT_SCOPE, &u);
235✔
735
                if (r < 0)  {
235✔
736
                        log_error_errno(r, "Failed to allocate the special " SPECIAL_INIT_SCOPE " unit: %m");
×
737
                        return;
×
738
                }
739
        }
740

741
        u->transient = true;
235✔
742
        u->perpetual = true;
235✔
743
        SCOPE(u)->deserialized_state = SCOPE_RUNNING;
235✔
744

745
        unit_add_to_load_queue(u);
235✔
746
        unit_add_to_dbus_queue(u);
235✔
747
        /* Enqueue an explicit cgroup realization here. Unlike other cgroups this one already exists and is
748
         * populated (by us, after all!) already, even when we are not in a reload cycle. Hence we cannot
749
         * apply the settings at creation time anymore, but let's at least apply them asynchronously. */
750
        unit_add_to_cgroup_realize_queue(u);
235✔
751
}
752

753
static const char* const scope_result_table[_SCOPE_RESULT_MAX] = {
754
        [SCOPE_SUCCESS]           = "success",
755
        [SCOPE_FAILURE_RESOURCES] = "resources",
756
        [SCOPE_FAILURE_TIMEOUT]   = "timeout",
757
        [SCOPE_FAILURE_OOM_KILL]  = "oom-kill",
758
};
759

760
DEFINE_STRING_TABLE_LOOKUP(scope_result, ScopeResult);
200✔
761

762
const UnitVTable scope_vtable = {
763
        .object_size = sizeof(Scope),
764
        .cgroup_context_offset = offsetof(Scope, cgroup_context),
765
        .kill_context_offset = offsetof(Scope, kill_context),
766
        .cgroup_runtime_offset = offsetof(Scope, cgroup_runtime),
767

768
        .sections =
769
                "Unit\0"
770
                "Scope\0"
771
                "Install\0",
772
        .private_section = "Scope",
773

774
        .can_transient = true,
775
        .can_delegate = true,
776
        .can_fail = true,
777
        .once_only = true,
778
        .can_set_managed_oom = true,
779

780
        .init = scope_init,
781
        .load = scope_load,
782
        .done = scope_done,
783

784
        .coldplug = scope_coldplug,
785

786
        .dump = scope_dump,
787

788
        .start = scope_start,
789
        .stop = scope_stop,
790

791
        .freezer_action = unit_cgroup_freezer_action,
792

793
        .get_timeout = scope_get_timeout,
794

795
        .serialize = scope_serialize,
796
        .deserialize_item = scope_deserialize_item,
797

798
        .active_state = scope_active_state,
799
        .sub_state_to_string = scope_sub_state_to_string,
800

801
        .sigchld_event = scope_sigchld_event,
802

803
        .reset_failed = scope_reset_failed,
804

805
        .notify_cgroup_empty = scope_notify_cgroup_empty_event,
806
        .notify_cgroup_oom = scope_notify_cgroup_oom_event,
807

808
        .bus_set_property = bus_scope_set_property,
809
        .bus_commit_properties = bus_scope_commit_properties,
810

811
        .enumerate_perpetual = scope_enumerate_perpetual,
812
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc