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

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 hits per line

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

92.79
/src/basic/path-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fnmatch.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "chase.h"
10
#include "errno-util.h"
11
#include "extract-word.h"
12
#include "fd-util.h"
13
#include "fs-util.h"
14
#include "glob-util.h"
15
#include "log.h"
16
#include "path-util.h"
17
#include "stat-util.h"
18
#include "string-util.h"
19
#include "strv.h"
20
#include "time-util.h"
21

22
bool is_path(const char *p) {
141,891✔
23
        if (!p) /* A NULL pointer is definitely not a path */
141,891✔
24
                return false;
25

26
        return strchr(p, '/');
141,891✔
27
}
28

29
int path_split_and_make_absolute(const char *p, char ***ret) {
1,159✔
30
        _cleanup_strv_free_ char **l = NULL;
1,159✔
31
        int r;
1,159✔
32

33
        assert(p);
1,159✔
34
        assert(ret);
1,159✔
35

36
        l = strv_split(p, ":");
1,159✔
37
        if (!l)
1,159✔
38
                return -ENOMEM;
39

40
        r = path_strv_make_absolute_cwd(l);
1,159✔
41
        if (r < 0)
1,159✔
42
                return r;
43

44
        *ret = TAKE_PTR(l);
1,159✔
45
        return r;
1,159✔
46
}
47

48
char* path_make_absolute(const char *p, const char *prefix) {
2,335✔
49
        assert(p);
2,335✔
50

51
        /* Makes every item in the list an absolute path by prepending
52
         * the prefix, if specified and necessary */
53

54
        if (path_is_absolute(p) || isempty(prefix))
2,335✔
55
                return strdup(p);
313✔
56

57
        return path_join(prefix, p);
2,022✔
58
}
59

60
int safe_getcwd(char **ret) {
381✔
61
        _cleanup_free_ char *cwd = NULL;
381✔
62

63
        cwd = get_current_dir_name();
381✔
64
        if (!cwd)
381✔
65
                return negative_errno();
×
66

67
        /* Let's make sure the directory is really absolute, to protect us from the logic behind
68
         * CVE-2018-1000001 */
69
        if (cwd[0] != '/')
381✔
70
                return -ENOMEDIUM;
71

72
        if (ret)
381✔
73
                *ret = TAKE_PTR(cwd);
381✔
74

75
        return 0;
76
}
77

78
int path_make_absolute_cwd(const char *p, char **ret) {
3,155,440✔
79
        char *c;
3,155,440✔
80
        int r;
3,155,440✔
81

82
        assert(p);
3,155,440✔
83
        assert(ret);
3,155,440✔
84

85
        /* Similar to path_make_absolute(), but prefixes with the
86
         * current working directory. */
87

88
        if (path_is_absolute(p))
3,155,440✔
89
                c = strdup(p);
3,155,151✔
90
        else {
91
                _cleanup_free_ char *cwd = NULL;
289✔
92

93
                r = safe_getcwd(&cwd);
289✔
94
                if (r < 0)
289✔
95
                        return r;
×
96

97
                c = path_join(cwd, p);
289✔
98
        }
99
        if (!c)
3,155,440✔
100
                return -ENOMEM;
101

102
        *ret = c;
3,155,440✔
103
        return 0;
3,155,440✔
104
}
105

106
int path_make_relative(const char *from, const char *to, char **ret) {
846,596✔
107
        _cleanup_free_ char *result = NULL;
846,596✔
108
        unsigned n_parents;
846,596✔
109
        const char *f, *t;
846,596✔
110
        int r, k;
846,596✔
111
        char *p;
846,596✔
112

113
        assert(from);
846,596✔
114
        assert(to);
846,596✔
115
        assert(ret);
846,596✔
116

117
        /* Strips the common part, and adds ".." elements as necessary. */
118

119
        if (!path_is_absolute(from) || !path_is_absolute(to))
1,693,188✔
120
                return -EINVAL;
121

122
        for (;;) {
6,013,143✔
123
                r = path_find_first_component(&from, true, &f);
6,013,143✔
124
                if (r < 0)
6,013,143✔
125
                        return r;
126

127
                k = path_find_first_component(&to, true, &t);
6,013,143✔
128
                if (k < 0)
6,013,143✔
129
                        return k;
130

131
                if (r == 0) {
6,013,143✔
132
                        /* end of 'from' */
133
                        if (k == 0) {
713,594✔
134
                                /* from and to are equivalent. */
135
                                result = strdup(".");
4✔
136
                                if (!result)
4✔
137
                                        return -ENOMEM;
138
                        } else {
139
                                /* 'to' is inside of 'from'. */
140
                                r = path_simplify_alloc(t, &result);
713,590✔
141
                                if (r < 0)
713,590✔
142
                                        return r;
143

144
                                if (!path_is_valid(result))
713,590✔
145
                                        return -EINVAL;
146
                        }
147

148
                        *ret = TAKE_PTR(result);
713,594✔
149
                        return 0;
713,594✔
150
                }
151

152
                if (r != k || !strneq(f, t, r))
5,299,549✔
153
                        break;
154
        }
155

156
        /* If we're here, then "from_dir" has one or more elements that need to
157
         * be replaced with "..". */
158

159
        for (n_parents = 1;; n_parents++) {
139,870✔
160
                /* If this includes ".." we can't do a simple series of "..". */
161
                r = path_find_first_component(&from, false, &f);
272,868✔
162
                if (r < 0)
272,868✔
163
                        return r;
164
                if (r == 0)
272,866✔
165
                        break;
166
        }
167

168
        if (isempty(t) && n_parents * 3 > PATH_MAX)
132,996✔
169
                /* PATH_MAX is counted *with* the trailing NUL byte */
170
                return -EINVAL;
171

172
        result = new(char, n_parents * 3 + !isempty(t) + strlen_ptr(t));
398,984✔
173
        if (!result)
132,996✔
174
                return -ENOMEM;
175

176
        for (p = result; n_parents > 0; n_parents--)
405,862✔
177
                p = mempcpy(p, "../", 3);
272,866✔
178

179
        if (isempty(t)) {
132,996✔
180
                /* Remove trailing slash and terminate string. */
181
                *(--p) = '\0';
2✔
182
                *ret = TAKE_PTR(result);
2✔
183
                return 0;
2✔
184
        }
185

186
        strcpy(p, t);
132,994✔
187

188
        path_simplify(result);
132,994✔
189

190
        if (!path_is_valid(result))
132,994✔
191
                return -EINVAL;
192

193
        *ret = TAKE_PTR(result);
132,994✔
194
        return 0;
132,994✔
195
}
196

197
int path_make_relative_parent(const char *from_child, const char *to, char **ret) {
133,069✔
198
        _cleanup_free_ char *from = NULL;
133,069✔
199
        int r;
133,069✔
200

201
        assert(from_child);
133,069✔
202
        assert(to);
133,069✔
203
        assert(ret);
133,069✔
204

205
        /* Similar to path_make_relative(), but provides the relative path from the parent directory of
206
         * 'from_child'. This may be useful when creating relative symlink.
207
         *
208
         * E.g.
209
         * - from = "/path/to/aaa", to = "/path/to/bbb"
210
         *      path_make_relative(from, to) = "../bbb"
211
         *      path_make_relative_parent(from, to) = "bbb"
212
         *
213
         * - from = "/path/to/aaa/bbb", to = "/path/to/ccc/ddd"
214
         *      path_make_relative(from, to) = "../../ccc/ddd"
215
         *      path_make_relative_parent(from, to) = "../ccc/ddd"
216
         */
217

218
        r = path_extract_directory(from_child, &from);
133,069✔
219
        if (r < 0)
133,069✔
220
                return r;
221

222
        return path_make_relative(from, to, ret);
133,068✔
223
}
224

225
char* path_startswith_strv(const char *p, char * const *strv) {
182,369✔
226
        assert(p);
182,369✔
227

228
        STRV_FOREACH(s, strv) {
692,246✔
229
                char *t;
540,768✔
230

231
                t = path_startswith(p, *s);
540,768✔
232
                if (t)
540,768✔
233
                        return t;
234
        }
235

236
        return NULL;
237
}
238

239
int path_strv_make_absolute_cwd(char **l) {
61,848✔
240
        int r;
61,848✔
241

242
        /* Goes through every item in the string list and makes it
243
         * absolute. This works in place and won't rollback any
244
         * changes on failure. */
245

246
        STRV_FOREACH(s, l) {
493,665✔
247
                char *t;
431,817✔
248

249
                r = path_make_absolute_cwd(*s, &t);
431,817✔
250
                if (r < 0)
431,817✔
251
                        return r;
×
252

253
                path_simplify(t);
431,817✔
254
                free_and_replace(*s, t);
431,817✔
255
        }
256

257
        return 0;
258
}
259

260
char** path_strv_resolve(char **l, const char *root) {
26,525✔
261
        unsigned k = 0;
26,525✔
262
        bool enomem = false;
26,525✔
263
        int r;
26,525✔
264

265
        if (strv_isempty(l))
53,050✔
266
                return l;
267

268
        /* Goes through every item in the string list and canonicalize
269
         * the path. This works in place and won't rollback any
270
         * changes on failure. */
271

272
        STRV_FOREACH(s, l) {
158,334✔
273
                _cleanup_free_ char *orig = NULL;
131,809✔
274
                char *t, *u;
131,809✔
275

276
                if (!path_is_absolute(*s)) {
131,809✔
277
                        free(*s);
×
278
                        continue;
×
279
                }
280

281
                if (root) {
131,809✔
282
                        orig = *s;
7✔
283
                        t = path_join(root, orig);
7✔
284
                        if (!t) {
7✔
285
                                enomem = true;
×
286
                                continue;
×
287
                        }
288
                } else
289
                        t = *s;
290

291
                r = chase(t, root, 0, &u, NULL);
131,809✔
292
                if (r == -ENOENT) {
131,809✔
293
                        if (root) {
110,473✔
294
                                u = TAKE_PTR(orig);
2✔
295
                                free(t);
2✔
296
                        } else
297
                                u = t;
110,471✔
298
                } else if (r < 0) {
21,336✔
299
                        free(t);
×
300

301
                        if (r == -ENOMEM)
×
302
                                enomem = true;
×
303

304
                        continue;
×
305
                } else if (root) {
21,336✔
306
                        char *x;
5✔
307

308
                        free(t);
5✔
309
                        x = path_startswith(u, root);
5✔
310
                        if (x) {
5✔
311
                                /* restore the slash if it was lost */
312
                                if (!startswith(x, "/"))
5✔
313
                                        *(--x) = '/';
5✔
314

315
                                t = strdup(x);
5✔
316
                                free(u);
5✔
317
                                if (!t) {
5✔
318
                                        enomem = true;
×
319
                                        continue;
×
320
                                }
321
                                u = t;
5✔
322
                        } else {
323
                                /* canonicalized path goes outside of
324
                                 * prefix, keep the original path instead */
325
                                free_and_replace(u, orig);
×
326
                        }
327
                } else
328
                        free(t);
21,331✔
329

330
                l[k++] = u;
131,809✔
331
        }
332

333
        l[k] = NULL;
26,525✔
334

335
        if (enomem)
26,525✔
336
                return NULL;
×
337

338
        return l;
339
}
340

341
char** path_strv_resolve_uniq(char **l, const char *root) {
26,524✔
342

343
        if (strv_isempty(l))
26,524✔
344
                return l;
345

346
        if (!path_strv_resolve(l, root))
26,524✔
347
                return NULL;
348

349
        return strv_uniq(l);
26,524✔
350
}
351

352
char* skip_leading_slash(const char *p) {
103,179✔
353
        return skip_leading_chars(p, "/");
103,179✔
354
}
355

356
char* path_simplify_full(char *path, PathSimplifyFlags flags) {
9,238,298✔
357
        bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
9,238,298✔
358
        char *f = path;
9,238,298✔
359
        int r;
9,238,298✔
360

361
        /* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
362
         * Modifies the passed string in-place.
363
         *
364
         * ///foo//./bar/.   becomes /foo/bar
365
         * .//./foo//./bar/. becomes foo/bar
366
         * /../foo/bar       becomes /foo/bar
367
         * /../foo/bar/..    becomes /foo/bar/..
368
         */
369

370
        if (isempty(path))
9,238,298✔
371
                return path;
372

373
        keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && endswith(path, "/");
9,208,112✔
374

375
        absolute = path_is_absolute(path);
9,208,112✔
376
        f += absolute;  /* Keep leading /, if present. */
9,208,112✔
377

378
        for (const char *p = f;;) {
9,208,112✔
379
                const char *e;
37,886,869✔
380

381
                r = path_find_first_component(&p, true, &e);
37,886,869✔
382
                if (r == 0)
37,886,869✔
383
                        break;
384

385
                if (r > 0 && absolute && beginning && path_startswith(e, ".."))
28,678,761✔
386
                        /* If we're at the beginning of an absolute path, we can safely skip ".." */
387
                        continue;
79✔
388

389
                beginning = false;
28,678,682✔
390

391
                if (add_slash)
28,678,682✔
392
                        *f++ = '/';
19,479,961✔
393

394
                if (r < 0) {
28,678,682✔
395
                        /* if path is invalid, then refuse to simplify the remaining part. */
396
                        memmove(f, p, strlen(p) + 1);
4✔
397
                        return path;
4✔
398
                }
399

400
                memmove(f, e, r);
28,678,678✔
401
                f += r;
28,678,678✔
402

403
                add_slash = true;
28,678,678✔
404
        }
405

406
        /* Special rule, if we stripped everything, we need a "." for the current directory. */
407
        if (f == path)
9,208,108✔
408
                *f++ = '.';
7✔
409

410
        if (*(f-1) != '/' && keep_trailing_slash)
9,208,108✔
411
                *f++ = '/';
37✔
412

413
        *f = '\0';
9,208,108✔
414
        return path;
9,208,108✔
415
}
416

417
int path_simplify_alloc(const char *path, char **ret) {
1,732,431✔
418
        assert(ret);
1,732,431✔
419

420
        if (!path) {
1,732,431✔
421
                *ret = NULL;
13,693✔
422
                return 0;
13,693✔
423
        }
424

425
        char *t = strdup(path);
1,718,738✔
426
        if (!t)
1,718,738✔
427
                return -ENOMEM;
428

429
        *ret = path_simplify(t);
1,718,738✔
430
        return 0;
1,718,738✔
431
}
432

433
char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) {
54,111,583✔
434
        assert(original_path);
54,111,583✔
435
        assert(prefix);
54,111,583✔
436

437
        /* Returns a pointer to the start of the first component after the parts matched by
438
         * the prefix, iff
439
         * - both paths are absolute or both paths are relative,
440
         * and
441
         * - each component in prefix in turn matches a component in path at the same position.
442
         * An empty string will be returned when the prefix and path are equivalent.
443
         *
444
         * Returns NULL otherwise.
445
         */
446

447
        const char *path = original_path;
54,111,583✔
448

449
        if ((path[0] == '/') != (prefix[0] == '/'))
54,111,583✔
450
                return NULL;
54,111,583✔
451

452
        for (;;) {
46,861,703✔
453
                const char *p, *q;
100,828,495✔
454
                int m, n;
100,828,495✔
455

456
                m = path_find_first_component(&path, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &p);
100,828,495✔
457
                if (m < 0)
100,828,495✔
458
                        return NULL;
53,966,792✔
459

460
                n = path_find_first_component(&prefix, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &q);
100,828,495✔
461
                if (n < 0)
100,828,495✔
462
                        return NULL;
463

464
                if (n == 0) {
100,828,495✔
465
                        if (!p)
40,123,240✔
466
                                p = path;
201,433✔
467

468
                        if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) {
40,123,240✔
469

470
                                if (p <= original_path)
7✔
471
                                        return NULL;
472

473
                                p--;
7✔
474

475
                                if (*p != '/')
7✔
476
                                        return NULL;
477
                        }
478

479
                        return (char*) p;
40,123,239✔
480
                }
481

482
                if (m != n)
60,705,255✔
483
                        return NULL;
484

485
                if (!strneq(p, q, m))
49,258,178✔
486
                        return NULL;
487
        }
488
}
489

490
int path_compare(const char *a, const char *b) {
30,921,828✔
491
        int r;
30,921,828✔
492

493
        /* Order NULL before non-NULL */
494
        r = CMP(!!a, !!b);
30,921,828✔
495
        if (r != 0)
30,882,158✔
496
                return r;
39,673✔
497

498
        /* A relative path and an absolute path must not compare as equal.
499
         * Which one is sorted before the other does not really matter.
500
         * Here a relative path is ordered before an absolute path. */
501
        r = CMP(path_is_absolute(a), path_is_absolute(b));
61,764,308✔
502
        if (r != 0)
30,587,092✔
503
                return r;
341,769✔
504

505
        for (;;) {
24,047,039✔
506
                const char *aa, *bb;
54,587,425✔
507
                int j, k;
54,587,425✔
508

509
                j = path_find_first_component(&a, true, &aa);
54,587,425✔
510
                k = path_find_first_component(&b, true, &bb);
54,587,425✔
511

512
                if (j < 0 || k < 0) {
54,587,425✔
513
                        /* When one of paths is invalid, order invalid path after valid one. */
514
                        r = CMP(j < 0, k < 0);
4✔
515
                        if (r != 0)
×
516
                                return r;
30,540,386✔
517

518
                        /* fallback to use strcmp() if both paths are invalid. */
519
                        return strcmp(a, b);
×
520
                }
521

522
                /* Order prefixes first: "/foo" before "/foo/bar" */
523
                if (j == 0) {
54,587,421✔
524
                        if (k == 0)
6,588,441✔
525
                                return 0;
526
                        return -1;
131,637✔
527
                }
528
                if (k == 0)
47,998,980✔
529
                        return 1;
530

531
                /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
532
                r = memcmp(aa, bb, MIN(j, k));
45,526,755✔
533
                if (r != 0)
45,526,755✔
534
                        return r;
535

536
                /* Sort "/foo/a" before "/foo/aaa" */
537
                r = CMP(j, k);
24,134,803✔
538
                if (r != 0)
24,079,611✔
539
                        return r;
87,764✔
540
        }
541
}
542

543
int path_compare_filename(const char *a, const char *b) {
138,368✔
544
        _cleanup_free_ char *fa = NULL, *fb = NULL;
138,368✔
545
        int r, j, k;
138,368✔
546

547
        /* Order NULL before non-NULL */
548
        r = CMP(!!a, !!b);
138,368✔
549
        if (r != 0)
138,368✔
550
                return r;
×
551

552
        j = path_extract_filename(a, &fa);
138,368✔
553
        k = path_extract_filename(b, &fb);
138,368✔
554

555
        /* When one of paths is "." or root, then order it earlier. */
556
        r = CMP(j != -EADDRNOTAVAIL, k != -EADDRNOTAVAIL);
138,368✔
557
        if (r != 0)
138,364✔
558
                return r;
8✔
559

560
        /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
561
        r = CMP(j < 0, k < 0);
138,360✔
562
        if (r != 0)
138,360✔
563
                return r;
×
564

565
        /* fallback to use strcmp() if both paths are invalid. */
566
        if (j < 0)
138,360✔
567
                return strcmp(a, b);
30✔
568

569
        return strcmp(fa, fb);
138,330✔
570
}
571

572
int path_equal_or_inode_same_full(const char *a, const char *b, int flags) {
52,120✔
573
        /* Returns true if paths are of the same entry, false if not, <0 on error. */
574

575
        if (path_equal(a, b))
52,120✔
576
                return 1;
577

578
        if (!a || !b)
297✔
579
                return 0;
580

581
        return inode_same(a, b, flags);
297✔
582
}
583

584
char* path_extend_internal(char **x, ...) {
24,827,396✔
585
        size_t sz, old_sz;
24,827,396✔
586
        char *q, *nx;
24,827,396✔
587
        const char *p;
24,827,396✔
588
        va_list ap;
24,827,396✔
589
        bool slash;
24,827,396✔
590

591
        /* Joins all listed strings until the sentinel and places a "/" between them unless the strings
592
         * end/begin already with one so that it is unnecessary. Note that slashes which are already
593
         * duplicate won't be removed. The string returned is hence always equal to or longer than the sum of
594
         * the lengths of the individual strings.
595
         *
596
         * The first argument may be an already allocated string that is extended via realloc() if
597
         * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
598
         * first parameter to distinguish the two operations.
599
         *
600
         * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of
601
         * which some are optional.
602
         *
603
         * Examples:
604
         *
605
         * path_join("foo", "bar") → "foo/bar"
606
         * path_join("foo/", "bar") → "foo/bar"
607
         * path_join("", "foo", "", "bar", "") → "foo/bar" */
608

609
        sz = old_sz = x ? strlen_ptr(*x) : 0;
24,827,396✔
610
        va_start(ap, x);
24,827,396✔
611
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
56,311,400✔
612
                size_t add;
31,484,004✔
613

614
                if (isempty(p))
31,484,004✔
615
                        continue;
710,338✔
616

617
                add = 1 + strlen(p);
30,773,666✔
618
                if (sz > SIZE_MAX - add) { /* overflow check */
30,773,666✔
619
                        va_end(ap);
×
620
                        return NULL;
×
621
                }
622

623
                sz += add;
30,773,666✔
624
        }
625
        va_end(ap);
24,827,396✔
626

627
        nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1));
24,827,396✔
628
        if (!nx)
24,827,396✔
629
                return NULL;
630
        if (x)
24,827,396✔
631
                *x = nx;
18,684,489✔
632

633
        if (old_sz > 0)
24,827,396✔
634
                slash = nx[old_sz-1] == '/';
18,610,932✔
635
        else {
636
                nx[old_sz] = 0;
6,216,464✔
637
                slash = true; /* no need to generate a slash anymore */
6,216,464✔
638
        }
639

640
        q = nx + old_sz;
24,827,396✔
641

642
        va_start(ap, x);
24,827,396✔
643
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
56,311,400✔
644
                if (isempty(p))
31,484,004✔
645
                        continue;
710,338✔
646

647
                if (!slash && p[0] != '/')
30,773,666✔
648
                        *(q++) = '/';
19,048,544✔
649

650
                q = stpcpy(q, p);
30,773,666✔
651
                slash = endswith(p, "/");
30,773,666✔
652
        }
653
        va_end(ap);
24,827,396✔
654

655
        return nx;
24,827,396✔
656
}
657

658
int open_and_check_executable(const char *name, const char *root, char **ret_path, int *ret_fd) {
30,256✔
659
        _cleanup_close_ int fd = -EBADF;
30,256✔
660
        _cleanup_free_ char *resolved = NULL;
30,256✔
661
        int r;
30,256✔
662

663
        assert(name);
30,256✔
664

665
        /* Function chase() is invoked only when root is not NULL, as using it regardless of
666
         * root value would alter the behavior of existing callers for example: /bin/sleep would become
667
         * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
668
         * needed to avoid unforeseen regression or other complicated changes. */
669
        if (root) {
30,256✔
670
                /* prefix root to name in case full paths are not specified */
671
                r = chase(name, root, CHASE_PREFIX_ROOT, &resolved, &fd);
4✔
672
                if (r < 0)
4✔
673
                        return r;
674

675
                name = resolved;
4✔
676
        } else {
677
                /* We need to use O_PATH because there may be executables for which we have only exec permissions,
678
                 * but not read (usually suid executables). */
679
                fd = open(name, O_PATH|O_CLOEXEC);
30,252✔
680
                if (fd < 0)
30,252✔
681
                        return -errno;
5,122✔
682
        }
683

684
        r = fd_verify_regular(fd);
25,134✔
685
        if (r < 0)
25,134✔
686
                return r;
687

688
        r = access_fd(fd, X_OK);
25,131✔
689
        if (r == -ENOSYS)
25,131✔
690
                /* /proc/ is not mounted. Fall back to access(). */
691
                r = RET_NERRNO(access(name, X_OK));
×
692
        if (r < 0)
25,131✔
693
                return r;
694

695
        if (ret_path) {
25,130✔
696
                if (resolved)
24,723✔
697
                        *ret_path = TAKE_PTR(resolved);
×
698
                else {
699
                        r = path_make_absolute_cwd(name, ret_path);
24,723✔
700
                        if (r < 0)
24,723✔
701
                                return r;
702

703
                        path_simplify(*ret_path);
24,723✔
704
                }
705
        }
706

707
        if (ret_fd)
25,130✔
708
                *ret_fd = TAKE_FD(fd);
23,222✔
709

710
        return 0;
711
}
712

713
int find_executable_full(
25,140✔
714
                const char *name,
715
                const char *root,
716
                char * const *exec_search_path,
717
                bool use_path_envvar,
718
                char **ret_filename,
719
                int *ret_fd) {
720

721
        int last_error = -ENOENT, r = 0;
25,140✔
722

723
        assert(name);
25,140✔
724

725
        if (is_path(name))
25,140✔
726
                return open_and_check_executable(name, root, ret_filename, ret_fd);
25,140✔
727

728
        if (exec_search_path) {
3,872✔
729
                STRV_FOREACH(element, exec_search_path) {
6✔
730
                        _cleanup_free_ char *full_path = NULL;
5✔
731

732
                        if (!path_is_absolute(*element)) {
5✔
733
                                log_debug("Exec search path '%s' isn't absolute, ignoring.", *element);
×
734
                                continue;
×
735
                        }
736

737
                        full_path = path_join(*element, name);
5✔
738
                        if (!full_path)
5✔
739
                                return -ENOMEM;
740

741
                        r = open_and_check_executable(full_path, root, ret_filename, ret_fd);
5✔
742
                        if (r >= 0)
5✔
743
                                return 0;
744
                        if (r != -EACCES)
4✔
745
                                last_error = r;
4✔
746
                }
747
                return last_error;
748
        }
749

750
        const char *p = NULL;
3,870✔
751

752
        if (use_path_envvar)
3,870✔
753
                /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
754
                 * binary. */
755
                p = getenv("PATH");
1,253✔
756
        if (!p)
3,870✔
757
                p = default_PATH();
2,627✔
758

759
        /* Resolve a single-component name to a full path */
760
        for (;;) {
8,987✔
761
                _cleanup_free_ char *element = NULL;
8,983✔
762

763
                r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
8,987✔
764
                if (r < 0)
8,987✔
765
                        return r;
766
                if (r == 0)
8,987✔
767
                        break;
768

769
                if (!path_is_absolute(element)) {
8,983✔
770
                        log_debug("Exec search path '%s' isn't absolute, ignoring.", element);
×
771
                        continue;
×
772
                }
773

774
                if (!path_extend(&element, name))
8,983✔
775
                        return -ENOMEM;
776

777
                r = open_and_check_executable(element, root, ret_filename, ret_fd);
8,983✔
778
                if (r >= 0) /* Found it! */
8,983✔
779
                        return 0;
780
                /* PATH entries which we don't have access to are ignored, as per tradition. */
781
                if (r != -EACCES)
5,117✔
782
                        last_error = r;
5,117✔
783
        }
784

785
        return last_error;
4✔
786
}
787

788
bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
×
789
        bool changed = false, originally_unset;
×
790

791
        assert(timestamp);
×
792

793
        if (!paths)
×
794
                return false;
795

796
        originally_unset = *timestamp == 0;
×
797

798
        STRV_FOREACH(i, paths) {
×
799
                struct stat stats;
×
800
                usec_t u;
×
801

802
                if (stat(*i, &stats) < 0)
×
803
                        continue;
×
804

805
                u = timespec_load(&stats.st_mtim);
×
806

807
                /* check first */
808
                if (*timestamp >= u)
×
809
                        continue;
×
810

811
                log_debug(originally_unset ? "Loaded timestamp for '%s'." : "Timestamp of '%s' changed.", *i);
×
812

813
                /* update timestamp */
814
                if (update) {
×
815
                        *timestamp = u;
×
816
                        changed = true;
×
817
                } else
818
                        return true;
×
819
        }
820

821
        return changed;
822
}
823

824
static int executable_is_good(const char *executable) {
190✔
825
        _cleanup_free_ char *p = NULL, *d = NULL;
190✔
826
        int r;
190✔
827

828
        r = find_executable(executable, &p);
190✔
829
        if (r == -ENOENT)
190✔
830
                return 0;
831
        if (r < 0)
188✔
832
                return r;
833

834
        /* An fsck that is linked to /bin/true is a non-existent fsck */
835

836
        r = readlink_malloc(p, &d);
188✔
837
        if (r == -EINVAL) /* not a symlink */
188✔
838
                return 1;
839
        if (r < 0)
71✔
840
                return r;
841

842
        return !PATH_IN_SET(d, "true"
71✔
843
                               "/bin/true",
844
                               "/usr/bin/true",
845
                               "/dev/null");
846
}
847

848
int fsck_exists(void) {
102✔
849
        return executable_is_good("fsck");
102✔
850
}
851

852
int fsck_exists_for_fstype(const char *fstype) {
88✔
853
        const char *checker;
88✔
854
        int r;
88✔
855

856
        assert(fstype);
88✔
857

858
        if (streq(fstype, "auto"))
88✔
859
                return -EINVAL;
860

861
        r = fsck_exists();
88✔
862
        if (r <= 0)
88✔
863
                return r;
864

865
        checker = strjoina("fsck.", fstype);
440✔
866
        return executable_is_good(checker);
88✔
867
}
868

869
static const char* skip_slash_or_dot(const char *p) {
1,078,715,909✔
870
        for (; !isempty(p); p++) {
1,668,643,267✔
871
                if (*p == '/')
1,449,884,904✔
872
                        continue;
589,926,591✔
873
                if (startswith(p, "./")) {
859,958,313✔
874
                        p++;
767✔
875
                        continue;
767✔
876
                }
877
                break;
878
        }
879
        return p;
1,078,715,909✔
880
}
881

882
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret) {
588,451,839✔
883
        const char *q, *first, *end_first, *next;
588,451,839✔
884
        size_t len;
588,451,839✔
885

886
        assert(p);
588,451,839✔
887

888
        /* When a path is input, then returns the pointer to the first component and its length, and
889
         * move the input pointer to the next component or nul. This skips both over any '/'
890
         * immediately *before* and *after* the first component before returning.
891
         *
892
         * Examples
893
         *   Input:  p: "//.//aaa///bbbbb/cc"
894
         *   Output: p: "bbbbb///cc"
895
         *           ret: "aaa///bbbbb/cc"
896
         *           return value: 3 (== strlen("aaa"))
897
         *
898
         *   Input:  p: "aaa//"
899
         *   Output: p: (pointer to NUL)
900
         *           ret: "aaa//"
901
         *           return value: 3 (== strlen("aaa"))
902
         *
903
         *   Input:  p: "/", ".", ""
904
         *   Output: p: (pointer to NUL)
905
         *           ret: NULL
906
         *           return value: 0
907
         *
908
         *   Input:  p: NULL
909
         *   Output: p: NULL
910
         *           ret: NULL
911
         *           return value: 0
912
         *
913
         *   Input:  p: "(too long component)"
914
         *   Output: return value: -EINVAL
915
         *
916
         *   (when accept_dot_dot is false)
917
         *   Input:  p: "//..//aaa///bbbbb/cc"
918
         *   Output: return value: -EINVAL
919
         */
920

921
        q = *p;
588,451,839✔
922

923
        first = skip_slash_or_dot(q);
588,451,839✔
924
        if (isempty(first)) {
588,451,839✔
925
                *p = first;
98,179,284✔
926
                if (ret)
98,179,284✔
927
                        *ret = NULL;
98,174,090✔
928
                return 0;
98,179,284✔
929
        }
930
        if (streq(first, ".")) {
490,272,555✔
931
                *p = first + 1;
8,278✔
932
                if (ret)
8,278✔
933
                        *ret = NULL;
8,272✔
934
                return 0;
8,278✔
935
        }
936

937
        end_first = strchrnul(first, '/');
490,264,277✔
938
        len = end_first - first;
490,264,277✔
939

940
        if (len > NAME_MAX)
490,264,277✔
941
                return -EINVAL;
942
        if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.')
490,264,107✔
943
                return -EINVAL;
944

945
        next = skip_slash_or_dot(end_first);
490,264,070✔
946

947
        *p = next + streq(next, ".");
490,264,070✔
948
        if (ret)
490,264,070✔
949
                *ret = first;
453,259,695✔
950
        return len;
490,264,070✔
951
}
952

953
static const char* skip_slash_or_dot_backward(const char *path, const char *q) {
15,656,264✔
954
        assert(path);
15,656,264✔
955
        assert(!q || q >= path);
15,656,264✔
956

957
        for (; q; q = PTR_SUB1(q, path)) {
31,619,987✔
958
                if (*q == '/')
23,349,267✔
959
                        continue;
8,021,554✔
960
                if (q > path && strneq(q - 1, "/.", 2))
15,327,713✔
961
                        continue;
296✔
962
                if (q == path && *q == '.')
15,327,417✔
963
                        continue;
84✔
964
                break;
965
        }
966
        return q;
15,656,264✔
967
}
968

969
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) {
7,835,900✔
970
        const char *q, *last_end, *last_begin;
7,835,900✔
971
        size_t len;
7,835,900✔
972

973
        /* Similar to path_find_first_component(), but search components from the end.
974
        *
975
        * Examples
976
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
977
        *           next: NULL
978
        *   Output: next: "/cc//././"
979
        *           ret: "cc//././"
980
        *           return value: 2 (== strlen("cc"))
981
        *
982
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
983
        *           next: "/cc//././"
984
        *   Output: next: "///bbbbb/cc//././"
985
        *           ret: "bbbbb/cc//././"
986
        *           return value: 5 (== strlen("bbbbb"))
987
        *
988
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
989
        *           next: "///bbbbb/cc//././"
990
        *   Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
991
        *           ret: "aaa///bbbbb/cc//././"
992
        *           return value: 3 (== strlen("aaa"))
993
        *
994
        *   Input:  path: "/", ".", "", or NULL
995
        *   Output: next: equivalent to path
996
        *           ret: NULL
997
        *           return value: 0
998
        *
999
        *   Input:  path: "(too long component)"
1000
        *   Output: return value: -EINVAL
1001
        *
1002
        *   (when accept_dot_dot is false)
1003
        *   Input:  path: "//..//aaa///bbbbb/cc/..//"
1004
        *   Output: return value: -EINVAL
1005
        */
1006

1007
        if (isempty(path)) {
7,835,900✔
1008
                if (next)
4✔
1009
                        *next = path;
4✔
1010
                if (ret)
4✔
1011
                        *ret = NULL;
4✔
1012
                return 0;
4✔
1013
        }
1014

1015
        if (next && *next) {
7,835,896✔
1016
                if (*next < path || *next > path + strlen(path))
35,881✔
1017
                        return -EINVAL;
1018
                if (*next == path) {
35,881✔
1019
                        if (ret)
9✔
1020
                                *ret = NULL;
9✔
1021
                        return 0;
9✔
1022
                }
1023
                if (!IN_SET(**next, '\0', '/'))
35,872✔
1024
                        return -EINVAL;
1025
                q = *next - 1;
35,872✔
1026
        } else
1027
                q = path + strlen(path) - 1;
7,800,015✔
1028

1029
        q = skip_slash_or_dot_backward(path, q);
7,835,887✔
1030
        if (!q) { /* the root directory, "." or "./" */
7,835,887✔
1031
                if (next)
15,430✔
1032
                        *next = path;
15,430✔
1033
                if (ret)
15,430✔
1034
                        *ret = NULL;
15,427✔
1035
                return 0;
15,430✔
1036
        }
1037

1038
        last_end = q + 1;
7,820,457✔
1039

1040
        while (q && *q != '/')
124,085,256✔
1041
                q = PTR_SUB1(q, path);
108,444,342✔
1042

1043
        last_begin = q ? q + 1 : path;
7,820,457✔
1044
        len = last_end - last_begin;
7,820,457✔
1045

1046
        if (len > NAME_MAX)
7,820,457✔
1047
                return -EINVAL;
1048
        if (!accept_dot_dot && len == 2 && strneq(last_begin, "..", 2))
7,820,453✔
1049
                return -EINVAL;
1050

1051
        if (next) {
7,820,420✔
1052
                q = skip_slash_or_dot_backward(path, q);
7,820,377✔
1053
                *next = q ? q + 1 : path;
7,820,377✔
1054
        }
1055

1056
        if (ret)
7,820,420✔
1057
                *ret = last_begin;
6,954,502✔
1058
        return len;
7,820,420✔
1059
}
1060

1061
const char* last_path_component(const char *path) {
128,108✔
1062

1063
        /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1064
         *
1065
         *    a/b/c → c
1066
         *    a/b/c/ → c/
1067
         *    x → x
1068
         *    x/ → x/
1069
         *    /y → y
1070
         *    /y/ → y/
1071
         *    / → /
1072
         *    // → /
1073
         *    /foo/a → a
1074
         *    /foo/a/ → a/
1075
         *
1076
         *    Also, the empty string is mapped to itself.
1077
         *
1078
         * This is different than basename(), which returns "" when a trailing slash is present.
1079
         *
1080
         * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1081
         */
1082

1083
        unsigned l, k;
128,108✔
1084

1085
        if (!path)
128,108✔
1086
                return NULL;
1087

1088
        l = k = strlen(path);
128,107✔
1089
        if (l == 0) /* special case — an empty string */
128,107✔
1090
                return path;
1091

1092
        while (k > 0 && path[k-1] == '/')
128,119✔
1093
                k--;
1094

1095
        if (k == 0) /* the root directory */
128,106✔
1096
                return path + l - 1;
3✔
1097

1098
        while (k > 0 && path[k-1] != '/')
1,047,992✔
1099
                k--;
1100

1101
        return path + k;
128,103✔
1102
}
1103

1104
int path_split_prefix_filename(const char *path, char **ret_dir, char **ret_filename) {
6,934,036✔
1105
        _cleanup_free_ char *d = NULL;
6,934,036✔
1106
        const char *c, *next = NULL;
6,934,036✔
1107
        int r;
6,934,036✔
1108

1109
        /* Split the path into dir prefix/filename pair. Returns:
1110
         *
1111
         * -EINVAL        → if the path is not valid
1112
         * -EADDRNOTAVAIL → if the path refers to the uppermost directory in hierarchy (i.e. has neither
1113
         *                  dir prefix nor filename - the root dir itself or ".")
1114
         * -EDESTADDRREQ  → if only a filename was passed, and caller only specifies ret_dir
1115
         * -ENOMEM        → no memory
1116
         *
1117
         * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1118
         * indicate the referenced file must be a directory.
1119
         *
1120
         * This function guarantees to return a fully valid filename, i.e. one that passes
1121
         * filename_is_valid() – this means "." and ".." are not accepted. */
1122

1123
        if (isempty(path))
13,868,068✔
1124
                return -EINVAL;
1125

1126
        r = path_find_last_component(path, /* accept_dot_dot = */ false, &next, &c);
6,934,023✔
1127
        if (r < 0)
6,934,023✔
1128
                return r;
1129
        if (r == 0) /* root directory or "." */
6,933,995✔
1130
                return -EADDRNOTAVAIL;
1131

1132
        if (ret_dir) {
6,918,578✔
1133
                if (next == path) {
4,458,990✔
1134
                        if (*path != '/') { /* filename only */
48,790✔
1135
                                if (!ret_filename)
28,544✔
1136
                                        return -EDESTADDRREQ;
1137
                        } else {
1138
                                d = strdup("/");
20,246✔
1139
                                if (!d)
20,246✔
1140
                                        return -ENOMEM;
1141
                        }
1142
                } else {
1143
                        d = strndup(path, next - path);
4,410,200✔
1144
                        if (!d)
4,410,200✔
1145
                                return -ENOMEM;
1146

1147
                        path_simplify(d);
4,410,200✔
1148

1149
                        if (!path_is_valid(d))
4,410,200✔
1150
                                return -EINVAL;
1151
                }
1152

1153
        } else if (!path_is_valid(path))
2,459,588✔
1154
                /* We didn't validate the dir prefix, hence check if the whole path is valid now */
1155
                return -EINVAL;
1156

1157
        if (ret_filename) {
6,890,034✔
1158
                char *fn = strndup(c, r);
2,796,309✔
1159
                if (!fn)
2,796,309✔
1160
                        return -ENOMEM;
1161

1162
                *ret_filename = fn;
2,796,309✔
1163
        }
1164

1165
        if (ret_dir)
6,895,594✔
1166
                *ret_dir = TAKE_PTR(d);
4,436,006✔
1167

1168
        return strlen(c) > (size_t) r ? O_DIRECTORY : 0;
6,895,594✔
1169
}
1170

1171
bool filename_part_is_valid(const char *p) {
1,008,713✔
1172
        const char *e;
1,008,713✔
1173

1174
        /* Checks f the specified string is OK to be *part* of a filename. This is different from
1175
         * filename_is_valid() as "." and ".." and "" are OK by this call, but not by filename_is_valid(). */
1176

1177
        if (!p)
1,008,713✔
1178
                return false;
1179

1180
        e = strchrnul(p, '/');
1,008,713✔
1181
        if (*e != 0)
1,008,713✔
1182
                return false;
1183

1184
        if (e - p > NAME_MAX) /* NAME_MAX is counted *without* the trailing NUL byte */
990,222✔
1185
                return false;
3✔
1186

1187
        return true;
1188
}
1189

1190
bool filename_is_valid(const char *p) {
1,003,879✔
1191

1192
        if (isempty(p))
1,003,879✔
1193
                return false;
1194

1195
        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
1,003,868✔
1196
                return false;
1197

1198
        return filename_part_is_valid(p);
1,003,857✔
1199
}
1200

1201
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
11,725,883✔
1202
        if (isempty(p))
11,725,883✔
1203
                return false;
1204

1205
        for (const char *e = p;;) {
11,725,878✔
1206
                int r;
37,009,611✔
1207

1208
                r = path_find_first_component(&e, accept_dot_dot, NULL);
37,009,611✔
1209
                if (r < 0)
37,009,611✔
1210
                        return false;
11,725,878✔
1211

1212
                if (e - p >= PATH_MAX) /* Already reached the maximum length for a path? (PATH_MAX is counted
37,009,575✔
1213
                                        * *with* the trailing NUL byte) */
1214
                        return false;
1215
                if (*e == 0)           /* End of string? Yay! */
37,009,572✔
1216
                        return true;
1217
        }
1218
}
1219

1220
bool path_is_normalized(const char *p) {
769,079✔
1221
        if (!path_is_safe(p))
769,079✔
1222
                return false;
1223

1224
        if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
769,054✔
1225
                return false;
1226

1227
        if (strstr(p, "//"))
769,051✔
1228
                return false;
1✔
1229

1230
        return true;
1231
}
1232

1233
int file_in_same_dir(const char *path, const char *filename, char **ret) {
798✔
1234
        _cleanup_free_ char *b = NULL;
798✔
1235
        int r;
798✔
1236

1237
        assert(path);
798✔
1238
        assert(filename);
798✔
1239
        assert(ret);
798✔
1240

1241
        /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1242
         * or the former isn't */
1243

1244
        if (path_is_absolute(filename))
798✔
1245
                b = strdup(filename);
16✔
1246
        else {
1247
                _cleanup_free_ char *dn = NULL;
782✔
1248

1249
                r = path_extract_directory(path, &dn);
782✔
1250
                if (r == -EDESTADDRREQ) /* no path prefix */
782✔
1251
                        b = strdup(filename);
1✔
1252
                else if (r < 0)
781✔
1253
                        return r;
11✔
1254
                else
1255
                        b = path_join(dn, filename);
770✔
1256
        }
1257
        if (!b)
787✔
1258
                return -ENOMEM;
1259

1260
        *ret = TAKE_PTR(b);
787✔
1261
        return 0;
787✔
1262
}
1263

1264
bool hidden_or_backup_file(const char *filename) {
6,822,266✔
1265
        assert(filename);
6,822,266✔
1266

1267
        if (filename[0] == '.' ||
12,811,990✔
1268
            STR_IN_SET(filename,
5,989,724✔
1269
                       "lost+found",
1270
                       "aquota.user",
1271
                       "aquota.group") ||
5,989,721✔
1272
            endswith(filename, "~"))
5,989,721✔
1273
                return true;
832,547✔
1274

1275
        const char *dot = strrchr(filename, '.');
5,989,719✔
1276
        if (!dot)
5,989,719✔
1277
                return false;
1278

1279
        /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1280
         * to come up with always new suffixes and that everybody else should just adjust to that, then it
1281
         * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1282
         * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1283
         * possibly augmented with an additional string. Specifically: there's now:
1284
         *
1285
         *    The generic suffixes "~" and ".bak" for backup files
1286
         *    The generic prefix "." for hidden files
1287
         *
1288
         * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1289
         * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1290
         * ".foopkg.old" or ".foopkg~" instead.
1291
         */
1292

1293
        return STR_IN_SET(dot + 1,
5,383,253✔
1294
                          "ignore",
1295
                          "rpmnew",
1296
                          "rpmsave",
1297
                          "rpmorig",
1298
                          "dpkg-old",
1299
                          "dpkg-new",
1300
                          "dpkg-tmp",
1301
                          "dpkg-dist",
1302
                          "dpkg-bak",
1303
                          "dpkg-backup",
1304
                          "dpkg-remove",
1305
                          "ucf-new",
1306
                          "ucf-old",
1307
                          "ucf-dist",
1308
                          "swp",
1309
                          "bak",
1310
                          "old",
1311
                          "new");
1312
}
1313

1314
bool is_device_path(const char *path) {
12,345✔
1315

1316
        /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1317
         * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1318
         * It does not actually resolve the path.
1319
         *
1320
         * Examples:
1321
         * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1322
         * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1323
         */
1324

1325
        const char *p = PATH_STARTSWITH_SET(ASSERT_PTR(path), "/dev/", "/sys/");
12,345✔
1326
        return !isempty(p);
12,345✔
1327
}
1328

1329
bool valid_device_node_path(const char *path) {
555✔
1330

1331
        /* Some superficial checks whether the specified path is a valid device node path, all without
1332
         * looking at the actual device node. */
1333

1334
        if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
555✔
1335
                return false;
×
1336

1337
        if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
555✔
1338
                return false;
1339

1340
        return path_is_normalized(path);
555✔
1341
}
1342

1343
bool valid_device_allow_pattern(const char *path) {
12✔
1344
        assert(path);
12✔
1345

1346
        /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1347
         * DeviceAllow= and DeviceDeny=. */
1348

1349
        if (STARTSWITH_SET(path, "block-", "char-"))
12✔
1350
                return true;
4✔
1351

1352
        return valid_device_node_path(path);
8✔
1353
}
1354

1355
bool dot_or_dot_dot(const char *path) {
7,150,371✔
1356
        if (!path)
7,150,371✔
1357
                return false;
1358
        if (path[0] != '.')
7,150,370✔
1359
                return false;
1360
        if (path[1] == 0)
291,983✔
1361
                return true;
1362
        if (path[1] != '.')
146,473✔
1363
                return false;
1364

1365
        return path[2] == 0;
144,381✔
1366
}
1367

1368
bool path_implies_directory(const char *path) {
60,285✔
1369

1370
        /* Sometimes, if we look at a path we already know it must refer to a directory, because it is
1371
         * suffixed with a slash, or its last component is "." or ".." */
1372

1373
        if (!path)
60,285✔
1374
                return false;
60,285✔
1375

1376
        if (dot_or_dot_dot(path))
39,391✔
1377
                return true;
1378

1379
        return ENDSWITH_SET(path, "/", "/.", "/..");
39,389✔
1380
}
1381

1382
bool empty_or_root(const char *path) {
12,063,357✔
1383

1384
        /* For operations relative to some root directory, returns true if the specified root directory is
1385
         * redundant, i.e. either / or NULL or the empty string or any equivalent. */
1386

1387
        if (isempty(path))
12,063,357✔
1388
                return true;
1389

1390
        return path_equal(path, "/");
6,081,620✔
1391
}
1392

1393
const char* empty_to_root(const char *path) {
386,235✔
1394
        return isempty(path) ? "/" : path;
392,108✔
1395
}
1396

1397
int empty_or_root_harder_to_null(const char **path) {
2,691,486✔
1398
        int r;
2,691,486✔
1399

1400
        assert(path);
2,691,486✔
1401

1402
        /* This nullifies the input path when the path is empty or points to "/". */
1403

1404
        if (empty_or_root(*path)) {
2,691,486✔
1405
                *path = NULL;
2,637,173✔
1406
                return 0;
2,637,173✔
1407
        }
1408

1409
        r = path_is_root(*path);
54,313✔
1410
        if (r < 0)
54,313✔
1411
                return r;
1412
        if (r > 0)
54,305✔
1413
                *path = NULL;
9✔
1414

1415
        return 0;
1416
}
1417

1418
bool path_strv_contains(char * const *l, const char *path) {
77,856✔
1419
        assert(path);
77,856✔
1420

1421
        STRV_FOREACH(i, l)
1,362,117✔
1422
                if (path_equal(*i, path))
1,296,614✔
1423
                        return true;
1424

1425
        return false;
1426
}
1427

1428
bool prefixed_path_strv_contains(char * const *l, const char *path) {
79✔
1429
        assert(path);
79✔
1430

1431
        STRV_FOREACH(i, l) {
80✔
1432
                const char *j = *i;
1✔
1433

1434
                if (*j == '-')
1✔
1435
                        j++;
×
1436
                if (*j == '+')
1✔
1437
                        j++;
×
1438

1439
                if (path_equal(j, path))
1✔
1440
                        return true;
1441
        }
1442

1443
        return false;
1444
}
1445

1446
int path_glob_can_match(const char *pattern, const char *prefix, char **ret) {
11,768✔
1447
        assert(pattern);
11,768✔
1448
        assert(prefix);
11,768✔
1449

1450
        for (const char *a = pattern, *b = prefix;;) {
11,768✔
1451
                _cleanup_free_ char *g = NULL, *h = NULL;
26,508✔
1452
                const char *p, *q;
35,318✔
1453
                int r, s;
35,318✔
1454

1455
                r = path_find_first_component(&a, /* accept_dot_dot= */ false, &p);
35,318✔
1456
                if (r < 0)
35,318✔
1457
                        return r;
1458

1459
                s = path_find_first_component(&b, /* accept_dot_dot= */ false, &q);
35,318✔
1460
                if (s < 0)
35,318✔
1461
                        return s;
1462

1463
                if (s == 0) {
35,318✔
1464
                        /* The pattern matches the prefix. */
1465
                        if (ret) {
2,958✔
1466
                                char *t;
2,958✔
1467

1468
                                t = path_join(prefix, p);
2,958✔
1469
                                if (!t)
2,958✔
1470
                                        return -ENOMEM;
1471

1472
                                *ret = t;
2,958✔
1473
                        }
1474
                        return true;
2,958✔
1475
                }
1476

1477
                if (r == 0)
32,360✔
1478
                        break;
1479

1480
                if (r == s && strneq(p, q, r))
32,356✔
1481
                        continue; /* common component. Check next. */
20,590✔
1482

1483
                g = strndup(p, r);
11,766✔
1484
                if (!g)
11,766✔
1485
                        return -ENOMEM;
1486

1487
                if (!string_is_glob(g))
11,766✔
1488
                        break;
1489

1490
                /* We found a glob component. Check if the glob pattern matches the prefix component. */
1491

1492
                h = strndup(q, s);
2,960✔
1493
                if (!h)
2,960✔
1494
                        return -ENOMEM;
1495

1496
                r = fnmatch(g, h, 0);
2,960✔
1497
                if (r == FNM_NOMATCH)
2,960✔
1498
                        break;
1499
                if (r != 0) /* Failure to process pattern? */
2,960✔
1500
                        return -EINVAL;
1501
        }
1502

1503
        /* The pattern does not match the prefix. */
1504
        if (ret)
8,810✔
1505
                *ret = NULL;
8,810✔
1506
        return false;
1507
}
1508

1509
#if HAVE_SPLIT_BIN
1510
static bool dir_is_split(const char *a, const char *b) {
1511
        int r;
1512

1513
        r = inode_same(a, b, AT_NO_AUTOMOUNT);
1514
        if (r < 0 && r != -ENOENT) {
1515
                log_debug_errno(r, "Failed to compare \"%s\" and \"%s\", assuming split directories: %m", a, b);
1516
                return true;
1517
        }
1518
        return r == 0;
1519
}
1520
#endif
1521

1522
const char* default_PATH(void) {
3,683✔
1523
#if HAVE_SPLIT_BIN
1524
        static const char *default_path = NULL;
1525

1526
        /* Return one of the three sets of paths:
1527
         * a) split /usr/s?bin, /usr/local/sbin doesn't matter.
1528
         * b) merged /usr/s?bin, /usr/sbin is a symlink, but /usr/local/sbin is not,
1529
         * c) fully merged, neither /usr/sbin nor /usr/local/sbin are symlinks,
1530
         *
1531
         * On error the fallback to the safe value with both directories as configured is returned.
1532
         */
1533

1534
        if (default_path)
1535
                return default_path;
1536

1537
        if (dir_is_split("/usr/sbin", "/usr/bin"))
1538
                return (default_path = DEFAULT_PATH_WITH_FULL_SBIN);  /* a */
1539
        if (dir_is_split("/usr/local/sbin", "/usr/local/bin"))
1540
                return (default_path = DEFAULT_PATH_WITH_LOCAL_SBIN); /* b */
1541
        return (default_path = DEFAULT_PATH_WITHOUT_SBIN);            /* c */
1542
#else
1543
        return DEFAULT_PATH_WITHOUT_SBIN;
3,683✔
1544
#endif
1545
}
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