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

systemd / systemd / 16533846256

25 Jul 2025 07:44PM UTC coverage: 72.199% (+0.03%) from 72.165%
16533846256

push

github

bluca
bootctl: automatically set --graceful when running in chroot

Installing stuff in a chroot should not fail because efivars are
not available. When running in a container touching efivars is
completely disabled, but there are some cases (recovery) where
it is needed to touch them in a chroot, so don't disable them but
avoid failing the run instead.

1 of 3 new or added lines in 1 file covered. (33.33%)

223 existing lines in 36 files now uncovered.

302658 of 419200 relevant lines covered (72.2%)

732672.88 hits per line

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

76.02
/src/shared/tests.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sched.h>
4
#include <stdlib.h>
5
#include <sys/mman.h>
6
#include <sys/prctl.h>
7
#include <sys/wait.h>
8
#include <unistd.h>
9

10
#include "sd-bus.h"
11
#include "sd-daemon.h"
12

13
#include "alloc-util.h"
14
#include "argv-util.h"
15
#include "bus-error.h"
16
#include "bus-locator.h"
17
#include "bus-util.h"
18
#include "bus-wait-for-jobs.h"
19
#include "cgroup-setup.h"
20
#include "cgroup-util.h"
21
#include "coredump-util.h"
22
#include "env-file.h"
23
#include "env-util.h"
24
#include "errno-util.h"
25
#include "extract-word.h"
26
#include "fd-util.h"
27
#include "fs-util.h"
28
#include "hexdecoct.h"
29
#include "log.h"
30
#include "namespace-util.h"
31
#include "path-util.h"
32
#include "pidref.h"
33
#include "process-util.h"
34
#include "random-util.h"
35
#include "rlimit-util.h"
36
#include "strv.h"
37
#include "tests.h"
38
#include "tmpfile-util.h"
39
#include "uid-range.h"
40

41
char* setup_fake_runtime_dir(void) {
9✔
42
        char *t;
9✔
43

44
        ASSERT_OK(mkdtemp_malloc("/tmp/fake-xdg-runtime-XXXXXX", &t));
9✔
45
        ASSERT_OK(setenv("XDG_RUNTIME_DIR", t, /* overwrite = */ true));
9✔
46
        return t;
9✔
47
}
48

49
static void load_testdata_env(void) {
30✔
50
        static bool called = false;
30✔
51
        _cleanup_free_ char *s = NULL, *d = NULL, *envpath = NULL;
30✔
52
        _cleanup_strv_free_ char **pairs = NULL;
30✔
53
        int r;
30✔
54

55
        if (called)
30✔
56
                return;
57
        called = true;
15✔
58

59
        ASSERT_OK(readlink_and_make_absolute("/proc/self/exe", &s));
15✔
60
        ASSERT_OK(path_extract_directory(s, &d));
15✔
61
        ASSERT_NOT_NULL(envpath = path_join(d, "systemd-runtest.env"));
15✔
62

63
        r = load_env_file_pairs(NULL, envpath, &pairs);
15✔
64
        if (r < 0) {
15✔
65
                log_debug_errno(r, "Reading %s failed, ignoring: %m", envpath);
15✔
66
                return;
15✔
67
        }
68

UNCOV
69
        STRV_FOREACH_PAIR(k, v, pairs)
×
70
                ASSERT_OK(setenv(*k, *v, /* overwrite = */ false));
×
71
}
72

73
int get_testdata_dir(const char *suffix, char **ret) {
29✔
74
        const char *dir;
29✔
75
        char *p;
29✔
76

77
        load_testdata_env();
29✔
78

79
        /* if the env var is set, use that */
80
        dir = getenv("SYSTEMD_TEST_DATA");
29✔
81
        if (!dir)
29✔
82
                dir = SYSTEMD_TEST_DATA;
29✔
83
        if (access(dir, F_OK) < 0)
29✔
UNCOV
84
                return log_error_errno(errno, "ERROR: $SYSTEMD_TEST_DATA directory [%s] not accessible: %m", dir);
×
85

86
        p = path_join(dir, suffix);
29✔
87
        if (!p)
29✔
UNCOV
88
                return log_oom();
×
89

90
        *ret = p;
29✔
91
        return 0;
29✔
92
}
93

94
const char* get_catalog_dir(void) {
1✔
95
        const char *env;
1✔
96

97
        load_testdata_env();
1✔
98

99
        /* if the env var is set, use that */
100
        env = getenv("SYSTEMD_CATALOG_DIR");
1✔
101
        if (!env)
1✔
102
                env = SYSTEMD_CATALOG_DIR;
1✔
103
        if (access(env, F_OK) < 0) {
1✔
UNCOV
104
                fprintf(stderr, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env);
×
105
                exit(EXIT_FAILURE);
×
106
        }
107
        return env;
1✔
108
}
109

110
bool slow_tests_enabled(void) {
17✔
111
        int r;
17✔
112

113
        r = getenv_bool("SYSTEMD_SLOW_TESTS");
17✔
114
        if (r >= 0)
17✔
UNCOV
115
                return r;
×
116

117
        if (r != -ENXIO)
17✔
UNCOV
118
                log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
×
119
        return SYSTEMD_SLOW_TESTS_DEFAULT;
120
}
121

122
void test_setup_logging(int level) {
400✔
123
        log_set_assert_return_is_critical(true);
400✔
124
        log_set_max_level(level);
400✔
125
        log_setup();
400✔
126
}
400✔
127

128
int write_tmpfile(char *pattern, const char *contents) {
17✔
129
        _cleanup_close_ int fd = -EBADF;
17✔
130

131
        assert(pattern);
17✔
132
        assert(contents);
17✔
133

134
        fd = mkostemp_safe(pattern);
17✔
135
        if (fd < 0)
17✔
136
                return fd;
137

138
        ssize_t l = strlen(contents);
17✔
139
        errno = 0;
17✔
140
        if (write(fd, contents, l) != l)
17✔
UNCOV
141
                return errno_or_else(EIO);
×
142
        return 0;
143
}
144

145
bool have_namespaces(void) {
4✔
146
        _cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
4✔
147
        int r;
4✔
148

149
        /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
150
         * do so in a child process in order not to affect our own process. */
151

152
        ASSERT_OK(r = pidref_safe_fork("(have_namespace)", /* flags = */ 0, &pid));
4✔
153
        if (r == 0) {
7✔
154
                /* child */
155
                if (detach_mount_namespace() < 0)
3✔
UNCOV
156
                        _exit(EXIT_FAILURE);
×
157

158
                _exit(EXIT_SUCCESS);
3✔
159
        }
160

161
        ASSERT_OK(r = pidref_wait_for_terminate_and_check("(have_namespace)", &pid, /* flags = */ 0));
4✔
162
        if (r == EXIT_SUCCESS)
4✔
163
                return true;
164

UNCOV
165
        if (r == EXIT_FAILURE)
×
166
                return false;
167

UNCOV
168
        assert_not_reached();
×
169
}
170

171
bool userns_has_single_user(void) {
9✔
172
        _cleanup_(uid_range_freep) UIDRange *uidrange = NULL, *gidrange = NULL;
9✔
173

174
        /* Check if we're in a user namespace with only a single user mapped in. We special case this
175
         * scenario in a few tests because it's the only kind of namespace that can be created unprivileged
176
         * and as such happens more often than not, so we make sure to deal with it so that all tests pass
177
         * in such environments. */
178

179
        if (uid_range_load_userns(NULL, UID_RANGE_USERNS_INSIDE, &uidrange) < 0)
9✔
180
                return false;
181

182
        if (uid_range_load_userns(NULL, GID_RANGE_USERNS_INSIDE, &gidrange) < 0)
9✔
183
                return false;
184

185
        return uidrange->n_entries == 1 && uidrange->entries[0].nr == 1 &&
9✔
186
                gidrange->n_entries == 1 && gidrange->entries[0].nr == 1;
18✔
187
}
188

189
bool can_memlock(void) {
2✔
190
        /* Let's see if we can mlock() a larger blob of memory. BPF programs are charged against
191
         * RLIMIT_MEMLOCK, hence let's first make sure we can lock memory at all, and skip the test if we
192
         * cannot. Why not check RLIMIT_MEMLOCK explicitly? Because in container environments the
193
         * RLIMIT_MEMLOCK value we see might not match the RLIMIT_MEMLOCK value actually in effect. */
194

195
        void *p = mmap(NULL, CAN_MEMLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
2✔
196
        if (p == MAP_FAILED)
2✔
197
                return false;
198

199
        bool b = mlock(p, CAN_MEMLOCK_SIZE) >= 0;
2✔
200
        if (b)
2✔
UNCOV
201
                ASSERT_OK(munlock(p, CAN_MEMLOCK_SIZE));
×
202

203
        ASSERT_OK(munmap(p, CAN_MEMLOCK_SIZE));
2✔
204
        return b;
205
}
206

207
static int allocate_scope(void) {
17✔
208
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
23✔
209
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
17✔
UNCOV
210
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
×
211
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
17✔
212
        _cleanup_free_ char *scope = NULL, *cgroup_root = NULL;
17✔
213
        const char *object;
17✔
214
        int r;
17✔
215

216
        /* Let's try to run this test in a scope of its own, with delegation turned on, so that PID 1 doesn't
217
         * interfere with our cgroup management. */
218
        if (cg_pid_get_path(NULL, 0, &cgroup_root) >= 0 && cg_is_delegated(cgroup_root) && stderr_is_journal()) {
17✔
219
                log_debug("Already running as a unit with delegated cgroup, not allocating a cgroup subroot.");
11✔
220
                return 0;
11✔
221
        }
222

223
        if (geteuid() == 0)
6✔
224
                r = sd_bus_default_system(&bus);
6✔
225
        else
UNCOV
226
                r = sd_bus_default_user(&bus);
×
227
        if (r < 0)
6✔
UNCOV
228
                return log_error_errno(r, "Failed to connect to system bus: %m");
×
229

230
        r = bus_wait_for_jobs_new(bus, &w);
6✔
231
        if (r < 0)
6✔
UNCOV
232
                return log_error_errno(r, "Could not watch jobs: %m");
×
233

234
        if (asprintf(&scope, "%s-%" PRIx64 ".scope", program_invocation_short_name, random_u64()) < 0)
6✔
UNCOV
235
                return log_oom();
×
236

237
        r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
6✔
238
        if (r < 0)
6✔
UNCOV
239
                return bus_log_create_error(r);
×
240

241
        /* Name and Mode */
242
        r = sd_bus_message_append(m, "ss", scope, "fail");
6✔
243
        if (r < 0)
6✔
244
                return bus_log_create_error(r);
×
245

246
        /* Properties */
247
        r = sd_bus_message_open_container(m, 'a', "(sv)");
6✔
248
        if (r < 0)
6✔
249
                return bus_log_create_error(r);
×
250

251
        r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
6✔
252
        if (r < 0)
6✔
UNCOV
253
                return bus_log_create_error(r);
×
254

255
        r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1);
6✔
256
        if (r < 0)
6✔
UNCOV
257
                return bus_log_create_error(r);
×
258

259
        r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
6✔
260
        if (r < 0)
6✔
UNCOV
261
                return bus_log_create_error(r);
×
262

263
        r = sd_bus_message_close_container(m);
6✔
264
        if (r < 0)
6✔
UNCOV
265
                return bus_log_create_error(r);
×
266

267
        /* Auxiliary units */
268
        r = sd_bus_message_append(m, "a(sa(sv))", 0);
6✔
269
        if (r < 0)
6✔
270
                return bus_log_create_error(r);
×
271

272
        r = sd_bus_call(bus, m, 0, &error, &reply);
6✔
273
        if (r < 0)
6✔
UNCOV
274
                return log_error_errno(r, "Failed to start transient scope unit: %s", bus_error_message(&error, r));
×
275

276
        r = sd_bus_message_read(reply, "o", &object);
6✔
277
        if (r < 0)
6✔
UNCOV
278
                return bus_log_parse_error(r);
×
279

280
        r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
6✔
281
        if (r < 0)
6✔
UNCOV
282
                return r;
×
283

284
        return 0;
285
}
286

287
static int enter_cgroup(char **ret_cgroup, bool enter_subroot) {
17✔
288
        _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL;
17✔
289
        CGroupMask supported;
17✔
290
        int r;
17✔
291

292
        r = allocate_scope();
17✔
293
        if (r < 0)
17✔
UNCOV
294
                log_warning_errno(r, "Couldn't allocate a scope unit for this test, proceeding without.");
×
295

296
        r = cg_pid_get_path(NULL, 0, &cgroup_root);
17✔
297
        if (IN_SET(r, -ENOMEDIUM, -ENOENT))
17✔
UNCOV
298
                return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m");
×
299
        ASSERT_OK(r);
17✔
300

301
        if (enter_subroot)
17✔
302
                ASSERT_OK(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()));
16✔
303
        else
304
                ASSERT_NOT_NULL(cgroup_subroot = strdup(cgroup_root));
1✔
305

306
        ASSERT_OK(cg_mask_supported(&supported));
17✔
307

308
        /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if
309
         * we handle any errors at that point. */
310

311
        r = cg_create_and_attach(cgroup_subroot, 0);
17✔
312
        if (r < 0)
17✔
313
                return r;
314

315
        if (ret_cgroup)
17✔
316
                *ret_cgroup = TAKE_PTR(cgroup_subroot);
1✔
317

318
        return 0;
319
}
320

321
int enter_cgroup_subroot(char **ret_cgroup) {
16✔
322
        return enter_cgroup(ret_cgroup, true);
16✔
323
}
324

325
int enter_cgroup_root(char **ret_cgroup) {
1✔
326
        return enter_cgroup(ret_cgroup, false);
1✔
327
}
328

329
int define_hex_ptr_internal(const char *hex, void **name, size_t *name_len) {
180✔
330
        return unhexmem_full(hex, strlen_ptr(hex), false, name, name_len);
352✔
331
}
332

333
const char* ci_environment(void) {
1✔
334
        /* We return a string because we might want to provide multiple bits of information later on: not
335
         * just the general CI environment type, but also whether we're sanitizing or not, etc. The caller is
336
         * expected to use strstr on the returned value. */
337
        static const char *ans = POINTER_MAX;
1✔
338
        int r;
1✔
339

340
        if (ans != POINTER_MAX)
1✔
341
                return ans;
342

343
        /* We allow specifying the environment with $CITYPE. Nobody uses this so far, but we are ready. */
344
        const char *citype = getenv("CITYPE");
1✔
345
        if (!isempty(citype))
1✔
UNCOV
346
                return (ans = citype);
×
347

348
        if (getenv_bool("TRAVIS") > 0)
1✔
UNCOV
349
                return (ans = "travis");
×
350
        if (getenv_bool("SEMAPHORE") > 0)
1✔
UNCOV
351
                return (ans = "semaphore");
×
352
        if (getenv_bool("GITHUB_ACTIONS") > 0)
1✔
353
                return (ans = "github-actions");
×
354
        if (getenv("AUTOPKGTEST_ARTIFACTS") || getenv("AUTOPKGTEST_TMP"))
1✔
UNCOV
355
                return (ans = "autopkgtest");
×
356
        if (getenv("SALSA_CI_IMAGES"))
1✔
UNCOV
357
                return (ans = "salsa-ci");
×
358

359
        FOREACH_STRING(var, "CI", "CONTINOUS_INTEGRATION") {
3✔
360
                /* Those vars are booleans according to Semaphore and Travis docs:
361
                 * https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
362
                 * https://docs.semaphoreci.com/ci-cd-environment/environment-variables/#ci
363
                 */
364
                r = getenv_bool(var);
2✔
365
                if (r > 0)
2✔
UNCOV
366
                        return (ans = "unknown"); /* Some other unknown thing */
×
367
                if (r == 0)
2✔
UNCOV
368
                        return (ans = NULL);
×
369
        }
370

371
        return (ans = NULL);
1✔
372
}
373

374
int run_test_table(const TestFunc *start, const TestFunc *end) {
253✔
375
        _cleanup_strv_free_ char **tests = NULL;
242✔
376
        int r = EXIT_SUCCESS;
253✔
377
        bool ran = false;
253✔
378
        const char *e;
253✔
379

380
        if (!start)
253✔
381
                return r;
382

383
        e = getenv("TESTFUNCS");
252✔
384
        if (e) {
252✔
UNCOV
385
                r = strv_split_full(&tests, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
×
UNCOV
386
                if (r < 0)
×
UNCOV
387
                        return log_error_errno(r, "Failed to parse $TESTFUNCS: %m");
×
388
        }
389

390
        for (const TestFunc *t = ALIGN_PTR(start); t + 1 <= end; t = ALIGN_PTR(t + 1)) {
2,254✔
391

392
                if (tests && !strv_contains(tests, t->name))
2,013✔
393
                        continue;
×
394

395
                if (t->sd_booted && sd_booted() <= 0) {
2,013✔
UNCOV
396
                        log_info("/* systemd not booted, skipping %s */", t->name);
×
UNCOV
397
                        if (t->has_ret && r == EXIT_SUCCESS)
×
UNCOV
398
                                r = EXIT_TEST_SKIP;
×
399
                } else {
400
                        log_info("/* %s */", t->name);
2,013✔
401

402
                        if (t->has_ret) {
2,013✔
403
                                int r2 = t->f.int_func();
17✔
404
                                if (r == EXIT_SUCCESS)
17✔
405
                                        r = r2;
14✔
406
                        } else
407
                                t->f.void_func();
1,996✔
408
                }
409

410
                ran = true;
411
        }
412

413
        if (!ran)
241✔
UNCOV
414
                return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No matching tests found.");
×
415

416
        return r;
417
}
418

419
void test_prepare(int argc, char *argv[], int log_level) {
258✔
420
        save_argc_argv(argc, argv);
258✔
421
        test_setup_logging(log_level);
258✔
422
}
258✔
423

424
int assert_signal_internal(void) {
58✔
425
        siginfo_t siginfo = {};
58✔
426
        int r;
58✔
427

428
        r = fork();
58✔
429
        if (r < 0)
58✔
UNCOV
430
                return -errno;
×
431

432
        if (r == 0) {
58✔
433
                /* Speed things up by never even attempting to generate a coredump */
UNCOV
434
                (void) set_dumpable(SUID_DUMP_DISABLE);
×
435

436
                /* But still set an rlimit just in case */
437
                (void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(0));
×
UNCOV
438
                return 0;
×
439
        }
440

441
        r = wait_for_terminate(r, &siginfo);
58✔
442
        if (r < 0)
58✔
443
                return r;
444

445
        return siginfo.si_status;
58✔
446
}
447

448

UNCOV
449
void log_test_failed_internal(const char *file, int line, const char *func, const char *format, ...) {
×
UNCOV
450
        va_list ap;
×
451

UNCOV
452
        va_start(ap, format);
×
UNCOV
453
        DISABLE_WARNING_FORMAT_NONLITERAL;
×
UNCOV
454
        log_internalv(LOG_ERR, 0, file, line, func, format, ap);
×
UNCOV
455
        REENABLE_WARNING;
×
456
        va_end(ap);
×
457

UNCOV
458
        abort();
×
459
}
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