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

systemd / systemd / 15336122756

29 May 2025 09:26PM UTC coverage: 72.064% (+0.02%) from 72.04%
15336122756

push

github

yuwata
NEWS: fix typos

299688 of 415863 relevant lines covered (72.06%)

699722.54 hits per line

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

69.72
/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

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

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

39
char* setup_fake_runtime_dir(void) {
9✔
40
        char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
9✔
41

42
        assert_se(mkdtemp(t));
9✔
43
        assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
9✔
44
        assert_se(p = strdup(t));
9✔
45

46
        return p;
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_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
15✔
60
        assert_se(path_extract_directory(s, &d) >= 0);
15✔
61
        assert_se(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: %m", envpath);
15✔
66
                return;
15✔
67
        }
68

69
        STRV_FOREACH_PAIR(k, v, pairs)
×
70
                assert_se(setenv(*k, *v, 0) >= 0);
×
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✔
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✔
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✔
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) {
15✔
111
        int r;
15✔
112

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

117
        if (r != -ENXIO)
15✔
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) {
394✔
123
        log_set_assert_return_is_critical(true);
394✔
124
        log_set_max_level(level);
394✔
125
        log_setup();
394✔
126
}
394✔
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✔
141
                return errno_or_else(EIO);
×
142
        return 0;
143
}
144

145
bool have_namespaces(void) {
4✔
146
        siginfo_t si = {};
4✔
147
        pid_t pid;
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
        pid = fork();
4✔
153
        assert_se(pid >= 0);
7✔
154

155
        if (pid == 0) {
7✔
156
                /* child */
157
                if (detach_mount_namespace() < 0)
3✔
158
                        _exit(EXIT_FAILURE);
×
159

160
                _exit(EXIT_SUCCESS);
3✔
161
        }
162

163
        assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0);
4✔
164
        assert_se(si.si_code == CLD_EXITED);
4✔
165

166
        if (si.si_status == EXIT_SUCCESS)
4✔
167
                return true;
4✔
168

169
        if (si.si_status == EXIT_FAILURE)
×
170
                return false;
171

172
        assert_not_reached();
×
173
}
174

175
bool userns_has_single_user(void) {
9✔
176
        _cleanup_(uid_range_freep) UIDRange *uidrange = NULL, *gidrange = NULL;
9✔
177

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

183
        if (uid_range_load_userns(NULL, UID_RANGE_USERNS_INSIDE, &uidrange) < 0)
9✔
184
                return false;
185

186
        if (uid_range_load_userns(NULL, GID_RANGE_USERNS_INSIDE, &gidrange) < 0)
9✔
187
                return false;
188

189
        return uidrange->n_entries == 1 && uidrange->entries[0].nr == 1 &&
9✔
190
                gidrange->n_entries == 1 && gidrange->entries[0].nr == 1;
18✔
191
}
192

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

199
        void *p = mmap(NULL, CAN_MEMLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
1✔
200
        if (p == MAP_FAILED)
1✔
201
                return false;
202

203
        bool b = mlock(p, CAN_MEMLOCK_SIZE) >= 0;
1✔
204
        if (b)
1✔
205
                assert_se(munlock(p, CAN_MEMLOCK_SIZE) >= 0);
×
206

207
        assert_se(munmap(p, CAN_MEMLOCK_SIZE) >= 0);
1✔
208
        return b;
209
}
210

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

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

227
        if (geteuid() == 0)
6✔
228
                r = sd_bus_default_system(&bus);
6✔
229
        else
230
                r = sd_bus_default_user(&bus);
×
231
        if (r < 0)
6✔
232
                return log_error_errno(r, "Failed to connect to system bus: %m");
×
233

234
        r = bus_wait_for_jobs_new(bus, &w);
6✔
235
        if (r < 0)
6✔
236
                return log_error_errno(r, "Could not watch jobs: %m");
×
237

238
        if (asprintf(&scope, "%s-%" PRIx64 ".scope", program_invocation_short_name, random_u64()) < 0)
6✔
239
                return log_oom();
×
240

241
        r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
6✔
242
        if (r < 0)
6✔
243
                return bus_log_create_error(r);
×
244

245
        /* Name and Mode */
246
        r = sd_bus_message_append(m, "ss", scope, "fail");
6✔
247
        if (r < 0)
6✔
248
                return bus_log_create_error(r);
×
249

250
        /* Properties */
251
        r = sd_bus_message_open_container(m, 'a', "(sv)");
6✔
252
        if (r < 0)
6✔
253
                return bus_log_create_error(r);
×
254

255
        r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
6✔
256
        if (r < 0)
6✔
257
                return bus_log_create_error(r);
×
258

259
        r = sd_bus_message_append(m, "(sv)", "Delegate", "b", 1);
6✔
260
        if (r < 0)
6✔
261
                return bus_log_create_error(r);
×
262

263
        r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
6✔
264
        if (r < 0)
6✔
265
                return bus_log_create_error(r);
×
266

267
        r = sd_bus_message_close_container(m);
6✔
268
        if (r < 0)
6✔
269
                return bus_log_create_error(r);
×
270

271
        /* Auxiliary units */
272
        r = sd_bus_message_append(m, "a(sa(sv))", 0);
6✔
273
        if (r < 0)
6✔
274
                return bus_log_create_error(r);
×
275

276
        r = sd_bus_call(bus, m, 0, &error, &reply);
6✔
277
        if (r < 0)
6✔
278
                return log_error_errno(r, "Failed to start transient scope unit: %s", bus_error_message(&error, r));
×
279

280
        r = sd_bus_message_read(reply, "o", &object);
6✔
281
        if (r < 0)
6✔
282
                return bus_log_parse_error(r);
×
283

284
        r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
6✔
285
        if (r < 0)
6✔
286
                return r;
×
287

288
        return 0;
289
}
290

291
static int enter_cgroup(char **ret_cgroup, bool enter_subroot) {
17✔
292
        _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL;
17✔
293
        CGroupMask supported;
17✔
294
        int r;
17✔
295

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

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

305
        if (enter_subroot)
17✔
306
                assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0);
16✔
307
        else {
308
                cgroup_subroot = strdup(cgroup_root);
1✔
309
                assert_se(cgroup_subroot != NULL);
1✔
310
        }
311

312
        assert_se(cg_mask_supported(&supported) >= 0);
17✔
313

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

317
        r = cg_create_and_attach(cgroup_subroot, 0);
17✔
318
        if (r < 0)
17✔
319
                return r;
320

321
        if (ret_cgroup)
17✔
322
                *ret_cgroup = TAKE_PTR(cgroup_subroot);
×
323

324
        return 0;
325
}
326

327
int enter_cgroup_subroot(char **ret_cgroup) {
16✔
328
        return enter_cgroup(ret_cgroup, true);
16✔
329
}
330

331
int enter_cgroup_root(char **ret_cgroup) {
1✔
332
        return enter_cgroup(ret_cgroup, false);
1✔
333
}
334

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

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

346
        if (ans != POINTER_MAX)
×
347
                return ans;
348

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

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

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

377
        return (ans = NULL);
×
378
}
379

380
int run_test_table(const TestFunc *start, const TestFunc *end) {
248✔
381
        _cleanup_strv_free_ char **tests = NULL;
238✔
382
        int r = EXIT_SUCCESS;
248✔
383
        bool ran = false;
248✔
384
        const char *e;
248✔
385

386
        if (!start)
248✔
387
                return r;
388

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

396
        for (const TestFunc *t = ALIGN_PTR(start); t + 1 <= end; t = ALIGN_PTR(t + 1)) {
2,211✔
397

398
                if (tests && !strv_contains(tests, t->name))
1,973✔
399
                        continue;
×
400

401
                if (t->sd_booted && sd_booted() <= 0) {
1,973✔
402
                        log_info("/* systemd not booted, skipping %s */", t->name);
×
403
                        if (t->has_ret && r == EXIT_SUCCESS)
×
404
                                r = EXIT_TEST_SKIP;
×
405
                } else {
406
                        log_info("/* %s */", t->name);
1,973✔
407

408
                        if (t->has_ret) {
1,973✔
409
                                int r2 = t->f.int_func();
17✔
410
                                if (r == EXIT_SUCCESS)
17✔
411
                                        r = r2;
14✔
412
                        } else
413
                                t->f.void_func();
1,956✔
414
                }
415

416
                ran = true;
417
        }
418

419
        if (!ran)
238✔
420
                return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No matching tests found.");
×
421

422
        return r;
423
}
424

425
void test_prepare(int argc, char *argv[], int log_level) {
251✔
426
        save_argc_argv(argc, argv);
251✔
427
        test_setup_logging(log_level);
251✔
428
}
251✔
429

430
int assert_signal_internal(void) {
54✔
431
        siginfo_t siginfo = {};
54✔
432
        int r;
54✔
433

434
        r = fork();
54✔
435
        if (r < 0)
54✔
436
                return -errno;
×
437

438
        if (r == 0) {
54✔
439
                /* Speed things up by never even attempting to generate a coredump */
440
                (void) set_dumpable(SUID_DUMP_DISABLE);
×
441

442
                /* But still set an rlimit just in case */
443
                (void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(0));
×
444
                return 0;
×
445
        }
446

447
        r = wait_for_terminate(r, &siginfo);
54✔
448
        if (r < 0)
54✔
449
                return r;
450

451
        return siginfo.si_status;
54✔
452
}
453

454

455
void log_test_failed_internal(const char *file, int line, const char *func, const char *format, ...) {
×
456
        va_list ap;
×
457

458
        va_start(ap, format);
×
459
        DISABLE_WARNING_FORMAT_NONLITERAL;
×
460
        log_internalv(LOG_ERR, 0, file, line, func, format, ap);
×
461
        REENABLE_WARNING;
×
462
        va_end(ap);
×
463

464
        abort();
×
465
}
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