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

systemd / systemd / 15986406979

30 Jun 2025 05:03PM UTC coverage: 72.045% (-0.09%) from 72.13%
15986406979

push

github

bluca
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options

ephemeral/ephemeral-import are described as possible '--mutable' options but
not present in the list. Note, "systemd-sysext --help" lists them correctly.

300514 of 417119 relevant lines covered (72.05%)

708586.28 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
#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 "process-util.h"
33
#include "random-util.h"
34
#include "rlimit-util.h"
35
#include "strv.h"
36
#include "tests.h"
37
#include "tmpfile-util.h"
38
#include "uid-range.h"
39

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

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

47
        return p;
9✔
48
}
49

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

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

60
        assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
15✔
61
        assert_se(path_extract_directory(s, &d) >= 0);
15✔
62
        assert_se(envpath = path_join(d, "systemd-runtest.env"));
15✔
63

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

70
        STRV_FOREACH_PAIR(k, v, pairs)
×
71
                assert_se(setenv(*k, *v, 0) >= 0);
×
72
}
73

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

78
        load_testdata_env();
29✔
79

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

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

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

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

98
        load_testdata_env();
1✔
99

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

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

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

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

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

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

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

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

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

146
bool have_namespaces(void) {
4✔
147
        siginfo_t si = {};
4✔
148
        pid_t pid;
4✔
149

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

153
        pid = fork();
4✔
154
        assert_se(pid >= 0);
7✔
155

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

161
                _exit(EXIT_SUCCESS);
3✔
162
        }
163

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

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

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

173
        assert_not_reached();
×
174
}
175

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

289
        return 0;
290
}
291

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

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

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

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

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

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

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

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

325
        return 0;
326
}
327

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

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

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

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

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

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

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

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

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

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

387
        if (!start)
249✔
388
                return r;
389

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

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

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

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

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

417
                ran = true;
418
        }
419

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

423
        return r;
424
}
425

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

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

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

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

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

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

452
        return siginfo.si_status;
54✔
453
}
454

455

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

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

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