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

systemd / systemd / 19603132989

22 Nov 2025 10:41PM UTC coverage: 72.453% (-0.07%) from 72.518%
19603132989

push

github

YHNdnzj
docs: Document cast formatting rules

308192 of 425371 relevant lines covered (72.45%)

1182701.2 hits per line

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

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

3
#include <stdlib.h>
4
#include <sys/mman.h>
5
#include <unistd.h>
6

7
#include "sd-bus.h"
8
#include "sd-daemon.h"
9

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

38
char* setup_fake_runtime_dir(void) {
9✔
39
        char *t;
9✔
40

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

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

52
        if (called)
30✔
53
                return;
54
        called = true;
15✔
55

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

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

66
        STRV_FOREACH_PAIR(k, v, pairs)
×
67
                ASSERT_OK(setenv(*k, *v, /* overwrite = */ false));
×
68
}
69

70
int get_testdata_dir(const char *suffix, char **ret) {
29✔
71
        const char *dir;
29✔
72
        char *p;
29✔
73

74
        load_testdata_env();
29✔
75

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

83
        p = path_join(dir, suffix);
29✔
84
        if (!p)
29✔
85
                return log_oom();
×
86

87
        *ret = p;
29✔
88
        return 0;
29✔
89
}
90

91
const char* get_catalog_dir(void) {
1✔
92
        const char *env;
1✔
93

94
        load_testdata_env();
1✔
95

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

107
bool slow_tests_enabled(void) {
17✔
108
        int r;
17✔
109

110
        r = getenv_bool("SYSTEMD_SLOW_TESTS");
17✔
111
        if (r >= 0)
17✔
112
                return r;
×
113

114
        if (r != -ENXIO)
17✔
115
                log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
×
116
        return SYSTEMD_SLOW_TESTS_DEFAULT;
117
}
118

119
void test_setup_logging(int level) {
408✔
120
        log_set_assert_return_is_critical(true);
408✔
121
        log_set_max_level(level);
408✔
122
        log_setup();
408✔
123
}
408✔
124

125
int write_tmpfile(char *pattern, const char *contents) {
17✔
126
        _cleanup_close_ int fd = -EBADF;
17✔
127

128
        assert(pattern);
17✔
129
        assert(contents);
17✔
130

131
        fd = mkostemp_safe(pattern);
17✔
132
        if (fd < 0)
17✔
133
                return fd;
134

135
        ssize_t l = strlen(contents);
17✔
136
        errno = 0;
17✔
137
        if (write(fd, contents, l) != l)
17✔
138
                return errno_or_else(EIO);
×
139
        return 0;
140
}
141

142
bool have_namespaces(void) {
4✔
143
        _cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
4✔
144
        int r;
4✔
145

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

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

155
                _exit(EXIT_SUCCESS);
3✔
156
        }
157

158
        ASSERT_OK(r = pidref_wait_for_terminate_and_check("(have_namespace)", &pid, /* flags = */ 0));
4✔
159
        if (r == EXIT_SUCCESS)
4✔
160
                return true;
161

162
        if (r == EXIT_FAILURE)
×
163
                return false;
164

165
        assert_not_reached();
×
166
}
167

168
bool userns_has_single_user(void) {
9✔
169
        _cleanup_(uid_range_freep) UIDRange *uidrange = NULL, *gidrange = NULL;
9✔
170

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

176
        if (uid_range_load_userns(NULL, UID_RANGE_USERNS_INSIDE, &uidrange) < 0)
9✔
177
                return false;
178

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

182
        return uidrange->n_entries == 1 && uidrange->entries[0].nr == 1 &&
9✔
183
                gidrange->n_entries == 1 && gidrange->entries[0].nr == 1;
18✔
184
}
185

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

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

196
        bool b = mlock(p, CAN_MEMLOCK_SIZE) >= 0;
2✔
197
        if (b)
2✔
198
                ASSERT_OK(munlock(p, CAN_MEMLOCK_SIZE));
×
199

200
        ASSERT_OK(munmap(p, CAN_MEMLOCK_SIZE));
2✔
201
        return b;
202
}
203

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

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

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

227
        r = bus_wait_for_jobs_new(bus, &w);
6✔
228
        if (r < 0)
6✔
229
                return log_error_errno(r, "Could not watch jobs: %m");
×
230

231
        if (asprintf(&scope, "%s-%" PRIx64 ".scope", program_invocation_short_name, random_u64()) < 0)
6✔
232
                return log_oom();
×
233

234
        r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
6✔
235
        if (r < 0)
6✔
236
                return bus_log_create_error(r);
×
237

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

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

248
        r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
6✔
249
        if (r < 0)
6✔
250
                return bus_log_create_error(r);
×
251

252
        r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1);
6✔
253
        if (r < 0)
6✔
254
                return bus_log_create_error(r);
×
255

256
        r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
6✔
257
        if (r < 0)
6✔
258
                return bus_log_create_error(r);
×
259

260
        r = sd_bus_message_close_container(m);
6✔
261
        if (r < 0)
6✔
262
                return bus_log_create_error(r);
×
263

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

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

273
        r = sd_bus_message_read(reply, "o", &object);
6✔
274
        if (r < 0)
6✔
275
                return bus_log_parse_error(r);
×
276

277
        r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
6✔
278
        if (r < 0)
6✔
279
                return r;
×
280

281
        return 0;
282
}
283

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

289
        r = cg_is_available();
17✔
290
        if (r < 0)
17✔
291
                return r;
292
        if (r == 0)
17✔
293
                return log_warning_errno(SYNTHETIC_ERRNO(ENOMEDIUM), "cgroupfs v2 is not mounted.");
×
294

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

299
        r = cg_pid_get_path(0, &cgroup_root);
17✔
300
        if (IN_SET(r, -ENOMEDIUM, -ENOENT))
17✔
301
                return log_warning_errno(r, "cg_pid_get_path(0, ...) failed: %m");
×
302
        ASSERT_OK(r);
17✔
303

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

309
        ASSERT_OK(cg_mask_supported(&supported));
17✔
310

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

314
        r = cg_create_and_attach(cgroup_subroot, 0);
17✔
315
        if (r < 0)
17✔
316
                return r;
317

318
        if (ret_cgroup)
17✔
319
                *ret_cgroup = TAKE_PTR(cgroup_subroot);
1✔
320

321
        return 0;
322
}
323

324
int enter_cgroup_subroot(char **ret_cgroup) {
16✔
325
        return enter_cgroup(ret_cgroup, true);
16✔
326
}
327

328
int enter_cgroup_root(char **ret_cgroup) {
1✔
329
        return enter_cgroup(ret_cgroup, false);
1✔
330
}
331

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

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

343
        if (ans != POINTER_MAX)
1✔
344
                return ans;
345

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

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

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

374
        return (ans = NULL);
1✔
375
}
376

377
int run_test_table(const TestFunc *start, const TestFunc *end) {
264✔
378
        _cleanup_strv_free_ char **tests = NULL;
247✔
379
        int r = EXIT_SUCCESS;
264✔
380
        bool ran = false;
264✔
381
        const char *e;
264✔
382

383
        if (!start)
264✔
384
                return r;
385

386
        e = getenv("TESTFUNCS");
263✔
387
        if (e) {
263✔
388
                r = strv_split_full(&tests, e, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
×
389
                if (r < 0)
×
390
                        return log_error_errno(r, "Failed to parse $TESTFUNCS: %m");
×
391
        }
392

393
        for (const TestFunc *t = ALIGN_PTR(start); t + 1 <= end; t = ALIGN_PTR(t + 1)) {
2,256✔
394

395
                if (tests && !strv_contains(tests, t->name))
2,010✔
396
                        continue;
×
397

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

405
                        if (t->has_ret) {
2,010✔
406
                                int r2 = t->f.int_func();
17✔
407
                                if (r == EXIT_SUCCESS)
17✔
408
                                        r = r2;
14✔
409
                        } else
410
                                t->f.void_func();
1,993✔
411
                }
412

413
                ran = true;
414
        }
415

416
        if (!ran)
246✔
417
                return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No matching tests found.");
×
418

419
        return r;
420
}
421

422
void test_prepare(int argc, char *argv[], int log_level) {
269✔
423
        save_argc_argv(argc, argv);
269✔
424
        test_setup_logging(log_level);
269✔
425
}
269✔
426

427
/* Returns:
428
 * ASSERT_SIGNAL_FORK_CHILD  = We are in the child process
429
 * ASSERT_SIGNAL_FORK_PARENT = We are in the parent process (signal/status stored in *ret_signal)
430
 * <0                        = Error (negative errno)
431
 */
432
int assert_signal_internal(int *ret_signal) {
72✔
433
        siginfo_t siginfo = {};
72✔
434
        int r;
72✔
435

436
        assert(ret_signal);
72✔
437

438
        r = fork();
72✔
439
        if (r < 0)
78✔
440
                return -errno;
×
441

442
        if (r == 0) {
78✔
443
                /* Speed things up by never even attempting to generate a coredump */
444
                (void) set_dumpable(SUID_DUMP_DISABLE);
6✔
445

446
                /* But still set an rlimit just in case */
447
                (void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(0));
6✔
448
                return ASSERT_SIGNAL_FORK_CHILD;
6✔
449
        }
450

451
        r = wait_for_terminate(r, &siginfo);
72✔
452
        if (r < 0)
72✔
453
                return r;
454

455
        /* si_status means different things depending on si_code:
456
         * - CLD_EXITED: si_status is the exit code
457
         * - CLD_KILLED/CLD_DUMPED: si_status is the signal number that killed the process
458
         * We need to return the signal number only if the child was killed by a signal. */
459
        if (IN_SET(siginfo.si_code, CLD_KILLED, CLD_DUMPED))
72✔
460
                *ret_signal = siginfo.si_status;
72✔
461
        else
462
                *ret_signal = 0;
×
463

464
        return ASSERT_SIGNAL_FORK_PARENT;
465
}
466

467

468
void log_test_failed_internal(const char *file, int line, const char *func, const char *format, ...) {
×
469
        va_list ap;
×
470

471
        va_start(ap, format);
×
472
        DISABLE_WARNING_FORMAT_NONLITERAL;
×
473
        log_internalv(LOG_ERR, 0, file, line, func, format, ap);
×
474
        REENABLE_WARNING;
×
475
        va_end(ap);
×
476

477
        abort();
×
478
}
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