• 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

93.15
/src/test/test-path.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

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

7
#include "all-units.h"
8
#include "alloc-util.h"
9
#include "fd-util.h"
10
#include "fs-util.h"
11
#include "manager.h"
12
#include "mkdir.h"
13
#include "rm-rf.h"
14
#include "string-util.h"
15
#include "strv.h"
16
#include "tests.h"
17
#include "unit.h"
18

19
typedef void (*test_function_t)(Manager *m);
20

21
static int setup_test(Manager **m) {
7✔
22
        char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
7✔
23
                                      "directorynotempty", "makedirectory");
24
        Manager *tmp = NULL;
7✔
25
        int r;
7✔
26

27
        assert_se(m);
7✔
28

29
        r = enter_cgroup_subroot(NULL);
7✔
30
        if (r == -ENOMEDIUM)
7✔
31
                return log_tests_skipped("cgroupfs not available");
7✔
32

33
        r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &tmp);
7✔
34
        if (manager_errno_skip_test(r))
7✔
35
                return log_tests_skipped_errno(r, "manager_new");
×
36
        assert_se(r >= 0);
7✔
37
        assert_se(manager_startup(tmp, NULL, NULL, NULL) >= 0);
7✔
38

39
        STRV_FOREACH(test_path, tests_path) {
56✔
40
                _cleanup_free_ char *p = NULL;
49✔
41

42
                p = strjoin("/tmp/test-path_", *test_path);
49✔
43
                assert_se(p);
49✔
44

45
                (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
49✔
46
        }
47

48
        *m = tmp;
7✔
49

50
        return 0;
7✔
51
}
52

53
static void shutdown_test(Manager *m) {
7✔
54
        assert_se(m);
7✔
55

56
        manager_free(m);
7✔
57
}
7✔
58

59
static Service *service_for_path(Manager *m, Path *path, const char *service_name) {
6✔
60
        _cleanup_free_ char *tmp = NULL;
6✔
61
        Unit *service_unit = NULL;
6✔
62

63
        assert_se(m);
6✔
64
        assert_se(path);
6✔
65

66
        if (!service_name) {
6✔
67
                assert_se(tmp = strreplace(UNIT(path)->id, ".path", ".service"));
5✔
68
                service_unit = manager_get_unit(m, tmp);
5✔
69
        } else
70
                service_unit = manager_get_unit(m, service_name);
1✔
71
        assert_se(service_unit);
6✔
72

73
        return SERVICE(service_unit);
6✔
74
}
75

76
static int _check_states(unsigned line,
25✔
77
                         Manager *m, Path *path, Service *service, PathState path_state, ServiceState service_state) {
78
        assert_se(m);
25✔
79
        assert_se(service);
25✔
80

81
        usec_t end = now(CLOCK_MONOTONIC) + 30 * USEC_PER_SEC;
25✔
82

83
        while (path->state != path_state || service->state != service_state ||
148✔
84
               path->result != PATH_SUCCESS || service->result != SERVICE_SUCCESS) {
179✔
85

86
                assert_se(sd_event_run(m->event, 100 * USEC_PER_MSEC) >= 0);
129✔
87

88
                usec_t n = now(CLOCK_MONOTONIC);
129✔
89
                log_info("line %u: %s: state = %s; result = %s (left: %" PRIi64 ")",
129✔
90
                         line,
91
                         UNIT(path)->id,
92
                         path_state_to_string(path->state),
93
                         path_result_to_string(path->result),
94
                         (int64_t) (end - n));
95
                log_info("line %u: %s: state = %s; result = %s",
129✔
96
                         line,
97
                         UNIT(service)->id,
98
                         service_state_to_string(service->state),
99
                         service_result_to_string(service->result));
100

101
                if (service->state == SERVICE_FAILED &&
129✔
102
                    (service->main_exec_status.status == EXIT_CGROUP || service->result == SERVICE_FAILURE_RESOURCES)) {
×
103
                        const char *ci = ci_environment();
×
104

105
                        /* On a general purpose system we may fail to start the service for reasons which are
106
                         * not under our control: permission limits, resource exhaustion, etc. Let's skip the
107
                         * test in those cases. On developer machines we require proper setup. */
108
                        if (!ci)
×
109
                                return log_notice_errno(SYNTHETIC_ERRNO(ECANCELED),
×
110
                                                        "Failed to start service %s, aborting test: %s/%s",
111
                                                        UNIT(service)->id,
112
                                                        service_state_to_string(service->state),
113
                                                        service_result_to_string(service->result));
114

115
                        /* On Salsa we can't setup cgroups so the unit always fails. The test checks if it
116
                         * can but continues if it cannot at the beginning, but on Salsa it fails here. */
117
                        if (streq(ci, "salsa-ci"))
×
118
                                exit(EXIT_TEST_SKIP);
×
119
                }
120

121
                if (n >= end) {
129✔
122
                        log_error("Test timeout when testing %s", UNIT(path)->id);
×
123
                        exit(EXIT_FAILURE);
×
124
                }
125
        }
126

127
        return 0;
128
}
129
#define check_states(...) _check_states(__LINE__, __VA_ARGS__)
130

131
static void test_path_exists(Manager *m) {
1✔
132
        const char *test_path = "/tmp/test-path_exists";
1✔
133
        Unit *unit = NULL;
1✔
134
        Path *path = NULL;
1✔
135
        Service *service = NULL;
1✔
136

137
        assert_se(m);
1✔
138

139
        assert_se(manager_load_startable_unit_or_warn(m, "path-exists.path", NULL, &unit) >= 0);
1✔
140

141
        path = PATH(unit);
1✔
142
        service = service_for_path(m, path, NULL);
1✔
143

144
        assert_se(unit_start(unit, NULL) >= 0);
1✔
145
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
146
                return;
×
147

148
        assert_se(touch(test_path) >= 0);
1✔
149
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
150
                return;
151

152
        /* Service restarts if file still exists */
153
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
154
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
155
                return;
156

157
        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
1✔
158
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
159
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
160
                return;
161

162
        assert_se(unit_stop(unit) >= 0);
1✔
163
}
164

165
static void test_path_existsglob(Manager *m) {
1✔
166
        const char *test_path = "/tmp/test-path_existsglobFOOBAR";
1✔
167
        Unit *unit = NULL;
1✔
168
        Path *path = NULL;
1✔
169
        Service *service = NULL;
1✔
170

171
        assert_se(m);
1✔
172

173
        assert_se(manager_load_startable_unit_or_warn(m, "path-existsglob.path", NULL, &unit) >= 0);
1✔
174

175
        path = PATH(unit);
1✔
176
        service = service_for_path(m, path, NULL);
1✔
177

178
        assert_se(unit_start(unit, NULL) >= 0);
1✔
179
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
180
                return;
×
181

182
        assert_se(touch(test_path) >= 0);
1✔
183
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
184
                return;
185

186
        /* Service restarts if file still exists */
187
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
188
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
189
                return;
190

191
        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
1✔
192
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
193
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
194
                return;
195

196
        assert_se(unit_stop(unit) >= 0);
1✔
197
}
198

199
static void test_path_changed(Manager *m) {
1✔
200
        const char *test_path = "/tmp/test-path_changed";
1✔
201
        FILE *f;
1✔
202
        Unit *unit = NULL;
1✔
203
        Path *path = NULL;
1✔
204
        Service *service = NULL;
1✔
205

206
        assert_se(m);
1✔
207

208
        assert_se(manager_load_startable_unit_or_warn(m, "path-changed.path", NULL, &unit) >= 0);
1✔
209

210
        path = PATH(unit);
1✔
211
        service = service_for_path(m, path, NULL);
1✔
212

213
        assert_se(unit_start(unit, NULL) >= 0);
1✔
214
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
215
                return;
×
216

217
        assert_se(touch(test_path) >= 0);
1✔
218
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
219
                return;
220

221
        /* Service does not restart if file still exists */
222
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
223
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
224
                return;
225

226
        f = fopen(test_path, "w");
1✔
227
        assert_se(f);
1✔
228
        fclose(f);
1✔
229

230
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
231
                return;
232

233
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
234
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
235
                return;
236

237
        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
1✔
238
        assert_se(unit_stop(unit) >= 0);
1✔
239
}
240

241
static void test_path_modified(Manager *m) {
1✔
242
        _cleanup_fclose_ FILE *f = NULL;
1✔
243
        const char *test_path = "/tmp/test-path_modified";
1✔
244
        Unit *unit = NULL;
1✔
245
        Path *path = NULL;
1✔
246
        Service *service = NULL;
1✔
247

248
        assert_se(m);
1✔
249

250
        assert_se(manager_load_startable_unit_or_warn(m, "path-modified.path", NULL, &unit) >= 0);
1✔
251

252
        path = PATH(unit);
1✔
253
        service = service_for_path(m, path, NULL);
1✔
254

255
        assert_se(unit_start(unit, NULL) >= 0);
1✔
256
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
257
                return;
258

259
        assert_se(touch(test_path) >= 0);
1✔
260
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
261
                return;
262

263
        /* Service does not restart if file still exists */
264
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
265
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
266
                return;
267

268
        f = fopen(test_path, "w");
1✔
269
        assert_se(f);
1✔
270
        fputs("test", f);
1✔
271

272
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
273
                return;
274

275
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
276
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
277
                return;
278

279
        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
1✔
280
        assert_se(unit_stop(unit) >= 0);
1✔
281
}
282

283
static void test_path_unit(Manager *m) {
1✔
284
        const char *test_path = "/tmp/test-path_unit";
1✔
285
        Unit *unit = NULL;
1✔
286
        Path *path = NULL;
1✔
287
        Service *service = NULL;
1✔
288

289
        assert_se(m);
1✔
290

291
        assert_se(manager_load_startable_unit_or_warn(m, "path-unit.path", NULL, &unit) >= 0);
1✔
292

293
        path = PATH(unit);
1✔
294
        service = service_for_path(m, path, "path-mycustomunit.service");
1✔
295

296
        assert_se(unit_start(unit, NULL) >= 0);
1✔
297
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
298
                return;
×
299

300
        assert_se(touch(test_path) >= 0);
1✔
301
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
302
                return;
303

304
        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
1✔
305
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
306
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
307
                return;
308

309
        assert_se(unit_stop(unit) >= 0);
1✔
310
}
311

312
static void test_path_directorynotempty(Manager *m) {
1✔
313
        const char *test_file, *test_path = "/tmp/test-path_directorynotempty/";
1✔
314
        Unit *unit = NULL;
1✔
315
        Path *path = NULL;
1✔
316
        Service *service = NULL;
1✔
317

318
        assert_se(m);
1✔
319

320
        assert_se(manager_load_startable_unit_or_warn(m, "path-directorynotempty.path", NULL, &unit) >= 0);
1✔
321

322
        path = PATH(unit);
1✔
323
        service = service_for_path(m, path, NULL);
1✔
324

325
        assert_se(access(test_path, F_OK) < 0);
1✔
326

327
        assert_se(unit_start(unit, NULL) >= 0);
1✔
328
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
329
                return;
×
330

331
        /* MakeDirectory default to no */
332
        assert_se(access(test_path, F_OK) < 0);
1✔
333

334
        assert_se(mkdir_p(test_path, 0755) >= 0);
1✔
335
        test_file = strjoina(test_path, "test_file");
5✔
336
        assert_se(touch(test_file) >= 0);
1✔
337
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
338
                return;
339

340
        /* Service restarts if directory is still not empty */
341
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
342
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
343
                return;
344

345
        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
1✔
346
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
347
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
348
                return;
349

350
        assert_se(unit_stop(unit) >= 0);
1✔
351
}
352

353
static void test_path_makedirectory_directorymode(Manager *m) {
1✔
354
        const char *test_path = "/tmp/test-path_makedirectory/";
1✔
355
        Unit *unit = NULL;
1✔
356
        struct stat s;
1✔
357

358
        assert_se(m);
1✔
359

360
        assert_se(manager_load_startable_unit_or_warn(m, "path-makedirectory.path", NULL, &unit) >= 0);
1✔
361

362
        assert_se(access(test_path, F_OK) < 0);
1✔
363

364
        assert_se(unit_start(unit, NULL) >= 0);
1✔
365

366
        /* Check if the directory has been created */
367
        assert_se(access(test_path, F_OK) >= 0);
1✔
368

369
        /* Check the mode we specified with DirectoryMode=0744 */
370
        assert_se(stat(test_path, &s) >= 0);
1✔
371
        assert_se((s.st_mode & S_IRWXU) == 0700);
1✔
372
        assert_se((s.st_mode & S_IRWXG) == 0040);
1✔
373
        assert_se((s.st_mode & S_IRWXO) == 0004);
1✔
374

375
        assert_se(unit_stop(unit) >= 0);
1✔
376
        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
1✔
377
}
1✔
378

379
int main(int argc, char *argv[]) {
1✔
380
        static const test_function_t tests[] = {
1✔
381
                test_path_exists,
382
                test_path_existsglob,
383
                test_path_changed,
384
                test_path_modified,
385
                test_path_unit,
386
                test_path_directorynotempty,
387
                test_path_makedirectory_directorymode,
388
                NULL,
389
        };
390

391
        _cleanup_free_ char *test_path = NULL;
1✔
392
        _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
1✔
393

394
        umask(022);
1✔
395

396
        test_setup_logging(LOG_INFO);
1✔
397

398
        ASSERT_OK(get_testdata_dir("test-path", &test_path));
1✔
399
        ASSERT_OK(setenv_unit_path(test_path));
1✔
400
        assert_se(runtime_dir = setup_fake_runtime_dir());
1✔
401

402
        for (const test_function_t *test = tests; *test; test++) {
8✔
403
                Manager *m = NULL;
7✔
404
                int r;
7✔
405

406
                /* We create a clean environment for each test */
407
                r = setup_test(&m);
7✔
408
                if (r != 0)
7✔
409
                        return r;
×
410

411
                (*test)(m);
7✔
412

413
                shutdown_test(m);
7✔
414
        }
415

416
        return 0;
417
}
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