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

systemd / systemd / 19397511130

15 Nov 2025 09:47PM UTC coverage: 72.518% (+0.1%) from 72.37%
19397511130

push

github

web-flow
sd-event: several follow-ups for recent change (#39743)

3 of 4 new or added lines in 2 files covered. (75.0%)

1869 existing lines in 55 files now uncovered.

308519 of 425439 relevant lines covered (72.52%)

1258617.71 hits per line

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

92.84
/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) {
139,852✔
23
        if (!p) /* A NULL pointer is definitely not a path */
139,852✔
24
                return false;
25

26
        return strchr(p, '/');
139,852✔
27
}
28

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

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

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

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

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

48
char* path_make_absolute(const char *p, const char *prefix) {
2,374✔
49
        assert(p);
2,374✔
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,374✔
55
                return strdup(p);
358✔
56

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

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

63
        cwd = get_current_dir_name();
5,475✔
64
        if (!cwd)
5,475✔
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] != '/')
5,475✔
70
                return -ENOMEDIUM;
71

72
        if (ret)
5,475✔
73
                *ret = TAKE_PTR(cwd);
5,475✔
74

75
        return 0;
76
}
77

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

82
        assert(p);
3,728,640✔
83
        assert(ret);
3,728,640✔
84

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

88
        if (path_is_absolute(p))
3,728,640✔
89
                c = strdup(p);
3,728,351✔
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,728,640✔
100
                return -ENOMEM;
101

102
        *ret = c;
3,728,640✔
103
        return 0;
3,728,640✔
104
}
105

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

113
        assert(from);
937,701✔
114
        assert(to);
937,701✔
115
        assert(ret);
937,701✔
116

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

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

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

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

131
                if (r == 0) {
6,607,536✔
132
                        /* end of 'from' */
133
                        if (k == 0) {
804,246✔
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);
804,242✔
141
                                if (r < 0)
804,242✔
142
                                        return r;
143

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

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

152
                if (r != k || !strneq(f, t, r))
5,803,290✔
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++) {
135,680✔
160
                /* If this includes ".." we can't do a simple series of "..". */
161
                r = path_find_first_component(&from, false, &f);
269,131✔
162
                if (r < 0)
269,131✔
163
                        return r;
164
                if (r == 0)
269,129✔
165
                        break;
166
        }
167

168
        if (isempty(t) && n_parents * 3 > PATH_MAX)
133,449✔
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));
400,343✔
173
        if (!result)
133,449✔
174
                return -ENOMEM;
175

176
        for (p = result; n_parents > 0; n_parents--)
402,578✔
177
                p = mempcpy(p, "../", 3);
269,129✔
178

179
        if (isempty(t)) {
133,449✔
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);
133,447✔
187

188
        path_simplify(result);
133,447✔
189

190
        if (!path_is_valid(result))
133,447✔
191
                return -EINVAL;
192

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

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

201
        assert(from_child);
133,522✔
202
        assert(to);
133,522✔
203
        assert(ret);
133,522✔
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,522✔
219
        if (r < 0)
133,522✔
220
                return r;
221

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

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

228
        STRV_FOREACH(s, strv) {
667,757✔
229
                char *t;
515,390✔
230

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

236
        return NULL;
237
}
238

239
int path_strv_make_absolute_cwd(char **l) {
117,518✔
240
        int r;
117,518✔
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) {
825,280✔
247
                char *t;
707,762✔
248

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

253
                path_simplify(t);
707,762✔
254
                free_and_replace(*s, t);
707,762✔
255
        }
256

257
        return 0;
258
}
259

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

265
        if (strv_isempty(l))
54,884✔
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) {
163,905✔
273
                _cleanup_free_ char *orig = NULL;
136,463✔
274
                char *t, *u;
136,463✔
275

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

281
                if (root) {
136,463✔
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);
136,463✔
292
                if (r == -ENOENT) {
136,463✔
293
                        if (root) {
113,856✔
294
                                u = TAKE_PTR(orig);
2✔
295
                                free(t);
2✔
296
                        } else
297
                                u = t;
113,854✔
298
                } else if (r < 0) {
22,607✔
299
                        free(t);
×
300

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

304
                        continue;
×
305
                } else if (root) {
22,607✔
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);
22,602✔
329

330
                l[k++] = u;
136,463✔
331
        }
332

333
        l[k] = NULL;
27,442✔
334

335
        if (enomem)
27,442✔
336
                return NULL;
×
337

338
        return l;
339
}
340

341
char** path_strv_resolve_uniq(char **l, const char *root) {
27,441✔
342

343
        if (strv_isempty(l))
27,441✔
344
                return l;
345

346
        if (!path_strv_resolve(l, root))
27,441✔
347
                return NULL;
348

349
        return strv_uniq(l);
27,441✔
350
}
351

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

356
char* path_simplify_full(char *path, PathSimplifyFlags flags) {
9,634,339✔
357
        bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
9,634,339✔
358
        char *f = path;
9,634,339✔
359
        int r;
9,634,339✔
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,634,339✔
371
                return path;
372

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

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

378
        for (const char *p = f;;) {
9,604,657✔
379
                const char *e;
39,868,070✔
380

381
                r = path_find_first_component(&p, true, &e);
39,868,070✔
382
                if (r == 0)
39,868,070✔
383
                        break;
384

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

389
                beginning = false;
30,263,338✔
390

391
                if (add_slash)
30,263,338✔
392
                        *f++ = '/';
20,668,074✔
393

394
                if (r < 0) {
30,263,338✔
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);
30,263,334✔
401
                f += r;
30,263,334✔
402

403
                add_slash = true;
30,263,334✔
404
        }
405

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

410
        if (*(f-1) != '/' && keep_trailing_slash)
9,604,653✔
411
                *f++ = '/';
39✔
412

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

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

420
        if (!path) {
1,851,380✔
421
                *ret = NULL;
13,482✔
422
                return 0;
13,482✔
423
        }
424

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

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

433
char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) {
60,081,140✔
434
        assert(original_path);
60,081,140✔
435
        assert(prefix);
60,081,140✔
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;
60,081,140✔
448

449
        if ((path[0] == '/') != (prefix[0] == '/'))
60,081,140✔
450
                return NULL;
60,081,140✔
451

452
        for (;;) {
52,445,692✔
453
                const char *p, *q;
112,382,085✔
454
                int m, n;
112,382,085✔
455

456
                m = path_find_first_component(&path, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &p);
112,382,085✔
457
                if (m < 0)
112,382,085✔
458
                        return NULL;
59,936,393✔
459

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

464
                if (n == 0) {
112,382,085✔
465
                        if (!p)
45,629,722✔
466
                                p = path;
226,825✔
467

468
                        if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) {
45,629,722✔
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;
45,629,721✔
480
                }
481

482
                if (m != n)
66,752,363✔
483
                        return NULL;
484

485
                if (!strneq(p, q, m))
54,896,546✔
486
                        return NULL;
487
        }
488
}
489

490
int path_compare(const char *a, const char *b) {
27,034,981✔
491
        int r;
27,034,981✔
492

493
        /* Order NULL before non-NULL */
494
        r = CMP(!!a, !!b);
27,034,981✔
495
        if (r != 0)
27,007,636✔
496
                return r;
27,348✔
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));
54,015,264✔
502
        if (r != 0)
26,771,533✔
503
                return r;
281,998✔
504

505
        for (;;) {
23,103,461✔
506
                const char *aa, *bb;
49,829,096✔
507
                int j, k;
49,829,096✔
508

509
                j = path_find_first_component(&a, true, &aa);
49,829,096✔
510
                k = path_find_first_component(&b, true, &bb);
49,829,096✔
511

512
                if (j < 0 || k < 0) {
49,829,096✔
513
                        /* When one of paths is invalid, order invalid path after valid one. */
514
                        r = CMP(j < 0, k < 0);
3✔
515
                        if (r != 0)
×
516
                                return r;
26,725,635✔
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) {
49,829,093✔
524
                        if (k == 0)
4,328,740✔
525
                                return 0;
526
                        return -1;
136,370✔
527
                }
528
                if (k == 0)
45,500,353✔
529
                        return 1;
530

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

536
                /* Sort "/foo/a" before "/foo/aaa" */
537
                r = CMP(j, k);
23,200,946✔
538
                if (r != 0)
23,137,528✔
539
                        return r;
97,485✔
540
        }
541
}
542

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

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

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

555
        /* When one of paths is "." or root, then order it earlier. */
556
        r = CMP(j != -EADDRNOTAVAIL, k != -EADDRNOTAVAIL);
106,100✔
557
        if (r != 0)
106,096✔
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);
106,092✔
562
        if (r != 0)
106,092✔
563
                return r;
×
564

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

569
        return strcmp(fa, fb);
106,062✔
570
}
571

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

575
        if (path_equal(a, b))
51,951✔
576
                return 1;
577

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

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

584
char* path_extend_internal(char **x, ...) {
26,947,542✔
585
        size_t sz, old_sz;
26,947,542✔
586
        char *q, *nx;
26,947,542✔
587
        const char *p;
26,947,542✔
588
        va_list ap;
26,947,542✔
589
        bool slash;
26,947,542✔
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;
26,947,542✔
610
        va_start(ap, x);
26,947,542✔
611
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
61,108,793✔
612
                size_t add;
34,161,251✔
613

614
                if (isempty(p))
34,161,251✔
615
                        continue;
610,243✔
616

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

623
                sz += add;
33,551,008✔
624
        }
625
        va_end(ap);
26,947,542✔
626

627
        nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1));
26,947,542✔
628
        if (!nx)
26,947,542✔
629
                return NULL;
630
        if (x)
26,947,542✔
631
                *x = nx;
20,158,026✔
632

633
        if (old_sz > 0)
26,947,542✔
634
                slash = nx[old_sz-1] == '/';
20,086,627✔
635
        else {
636
                nx[old_sz] = 0;
6,860,915✔
637
                slash = true; /* no need to generate a slash anymore */
6,860,915✔
638
        }
639

640
        q = nx + old_sz;
26,947,542✔
641

642
        va_start(ap, x);
26,947,542✔
643
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
61,108,793✔
644
                if (isempty(p))
34,161,251✔
645
                        continue;
610,243✔
646

647
                if (!slash && p[0] != '/')
33,551,008✔
648
                        *(q++) = '/';
20,621,627✔
649

650
                q = stpcpy(q, p);
33,551,008✔
651
                slash = endswith(p, "/");
33,551,008✔
652
        }
653
        va_end(ap);
26,947,542✔
654

655
        return nx;
26,947,542✔
656
}
657

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

663
        assert(name);
32,053✔
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) {
32,053✔
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);
32,049✔
680
                if (fd < 0)
32,049✔
681
                        return -errno;
5,245✔
682
        }
683

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

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

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

703
                        path_simplify(*ret_path);
26,432✔
704
                }
705
        }
706

707
        if (ret_fd)
26,804✔
708
                *ret_fd = TAKE_FD(fd);
24,921✔
709

710
        return 0;
711
}
712

713
int find_executable_full(
26,814✔
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;
26,814✔
722

723
        assert(name);
26,814✔
724

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

728
        if (exec_search_path) {
4,015✔
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;
4,013✔
751

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

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

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

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

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

777
                r = open_and_check_executable(element, root, ret_filename, ret_fd);
9,249✔
778
                if (r >= 0) /* Found it! */
9,249✔
779
                        return 0;
780
                /* PATH entries which we don't have access to are ignored, as per tradition. */
781
                if (r != -EACCES)
5,240✔
782
                        last_error = r;
5,240✔
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) {
216✔
825
        _cleanup_free_ char *p = NULL, *d = NULL;
216✔
826
        int r;
216✔
827

828
        r = find_executable(executable, &p);
216✔
829
        if (r == -ENOENT)
216✔
830
                return 0;
831
        if (r < 0)
214✔
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);
214✔
837
        if (r == -EINVAL) /* not a symlink */
214✔
838
                return 1;
839
        if (r < 0)
86✔
840
                return r;
841

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

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

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

856
        assert(fstype);
101✔
857

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

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

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

869
static const char* skip_slash_or_dot(const char *p) {
1,076,805,981✔
870
        for (; !isempty(p); p++) {
1,664,930,452✔
871
                if (*p == '/')
1,440,549,085✔
872
                        continue;
588,123,674✔
873
                if (startswith(p, "./")) {
852,425,411✔
874
                        p++;
797✔
875
                        continue;
797✔
876
                }
877
                break;
878
        }
879
        return p;
1,076,805,981✔
880
}
881

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

886
        assert(p);
587,158,630✔
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;
587,158,630✔
922

923
        first = skip_slash_or_dot(q);
587,158,630✔
924
        if (isempty(first)) {
587,158,630✔
925
                *p = first;
97,502,074✔
926
                if (ret)
97,502,074✔
927
                        *ret = NULL;
97,496,593✔
928
                return 0;
97,502,074✔
929
        }
930
        if (streq(first, ".")) {
489,656,556✔
931
                *p = first + 1;
9,015✔
932
                if (ret)
9,015✔
933
                        *ret = NULL;
8,956✔
934
                return 0;
9,015✔
935
        }
936

937
        end_first = strchrnul(first, '/');
489,647,541✔
938
        len = end_first - first;
489,647,541✔
939

940
        if (len > NAME_MAX)
489,647,541✔
941
                return -EINVAL;
942
        if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.')
489,647,388✔
943
                return -EINVAL;
944

945
        next = skip_slash_or_dot(end_first);
489,647,351✔
946

947
        *p = next + streq(next, ".");
489,647,351✔
948
        if (ret)
489,647,351✔
949
                *ret = first;
450,802,941✔
950
        return len;
489,647,351✔
951
}
952

953
static const char* skip_slash_or_dot_backward(const char *path, const char *q) {
16,516,987✔
954
        assert(path);
16,516,987✔
955
        assert(!q || q >= path);
16,516,987✔
956

957
        for (; q; q = PTR_SUB1(q, path)) {
33,420,878✔
958
                if (*q == '/')
24,719,143✔
959
                        continue;
8,504,326✔
960
                if (q > path && strneq(q - 1, "/.", 2))
16,214,817✔
961
                        continue;
268✔
962
                if (q == path && *q == '.')
16,214,549✔
963
                        continue;
82✔
964
                break;
965
        }
966
        return q;
16,516,987✔
967
}
968

969
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) {
8,268,308✔
970
        const char *q, *last_end, *last_begin;
8,268,308✔
971
        size_t len;
8,268,308✔
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)) {
8,268,308✔
1008
                if (next)
9✔
1009
                        *next = path;
9✔
1010
                if (ret)
9✔
1011
                        *ret = NULL;
9✔
1012
                return 0;
9✔
1013
        }
1014

1015
        if (next && *next) {
8,268,299✔
1016
                if (*next < path || *next > path + strlen(path))
48,734✔
1017
                        return -EINVAL;
1018
                if (*next == path) {
48,734✔
1019
                        if (ret)
9✔
1020
                                *ret = NULL;
9✔
1021
                        return 0;
9✔
1022
                }
1023
                if (!IN_SET(**next, '\0', '/'))
48,725✔
1024
                        return -EINVAL;
1025
                q = *next - 1;
48,725✔
1026
        } else
1027
                q = path + strlen(path) - 1;
8,219,565✔
1028

1029
        q = skip_slash_or_dot_backward(path, q);
8,268,290✔
1030
        if (!q || /* the root directory */
8,268,290✔
1031
            (q == path && *q == '.')) { /* path is "." or "./" */
42✔
1032
                if (next)
19,517✔
1033
                        *next = path;
19,517✔
1034
                if (ret)
19,517✔
1035
                        *ret = NULL;
19,514✔
1036
                return 0;
19,517✔
1037
        }
1038

1039
        last_end = q + 1;
8,248,773✔
1040

1041
        while (q && *q != '/')
127,314,972✔
1042
                q = PTR_SUB1(q, path);
110,817,426✔
1043

1044
        last_begin = q ? q + 1 : path;
8,248,773✔
1045
        len = last_end - last_begin;
8,248,773✔
1046

1047
        if (len > NAME_MAX)
8,248,773✔
1048
                return -EINVAL;
1049
        if (!accept_dot_dot && len == 2 && strneq(last_begin, "..", 2))
8,248,769✔
1050
                return -EINVAL;
1051

1052
        if (next) {
8,248,744✔
1053
                q = skip_slash_or_dot_backward(path, q);
8,248,697✔
1054
                *next = q ? q + 1 : path;
8,248,697✔
1055
        }
1056

1057
        if (ret)
8,248,744✔
1058
                *ret = last_begin;
7,373,441✔
1059
        return len;
8,248,744✔
1060
}
1061

1062
const char* last_path_component(const char *path) {
145,523✔
1063

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

1084
        unsigned l, k;
145,523✔
1085

1086
        if (!path)
145,523✔
1087
                return NULL;
1088

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

1093
        while (k > 0 && path[k-1] == '/')
145,534✔
1094
                k--;
1095

1096
        if (k == 0) /* the root directory */
145,521✔
1097
                return path + l - 1;
3✔
1098

1099
        while (k > 0 && path[k-1] != '/')
1,174,112✔
1100
                k--;
1101

1102
        return path + k;
145,518✔
1103
}
1104

1105
int path_extract_filename(const char *path, char **ret) {
2,905,716✔
1106
        _cleanup_free_ char *a = NULL;
5,811,432✔
1107
        const char *c, *next = NULL;
2,905,716✔
1108
        int r;
2,905,716✔
1109

1110
        /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
1111
         * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing
1112
         * slashes. Returns:
1113
         *
1114
         * -EINVAL        → if the path is not valid
1115
         * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir
1116
         *                  itself or "." is specified
1117
         * -ENOMEM        → no memory
1118
         *
1119
         * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1120
         * indicate the referenced file must be a directory.
1121
         *
1122
         * This function guarantees to return a fully valid filename, i.e. one that passes
1123
         * filename_is_valid() – this means "." and ".." are not accepted. */
1124

1125
        if (!path_is_valid(path))
2,905,716✔
1126
                return -EINVAL;
1127

1128
        r = path_find_last_component(path, false, &next, &c);
2,905,712✔
1129
        if (r < 0)
2,905,712✔
1130
                return r;
1131
        if (r == 0) /* root directory */
2,905,698✔
1132
                return -EADDRNOTAVAIL;
1133

1134
        a = strndup(c, r);
2,905,610✔
1135
        if (!a)
2,905,610✔
1136
                return -ENOMEM;
1137

1138
        *ret = TAKE_PTR(a);
2,905,610✔
1139
        return strlen(c) > (size_t) r ? O_DIRECTORY : 0;
2,905,610✔
1140
}
1141

1142
int path_extract_directory(const char *path, char **ret) {
4,438,477✔
1143
        const char *c, *next = NULL;
4,438,477✔
1144
        int r;
4,438,477✔
1145

1146
        /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns:
1147
         *
1148
         * -EINVAL        → if the path is not valid
1149
         * -EDESTADDRREQ  → if no directory was specified in the passed in path, i.e. only a filename was passed
1150
         * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e.
1151
         *                   the root dir itself or "." was specified
1152
         * -ENOMEM        → no memory (surprise!)
1153
         *
1154
         * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid().
1155
         */
1156

1157
        r = path_find_last_component(path, false, &next, &c);
4,438,477✔
1158
        if (r < 0)
4,438,477✔
1159
                return r;
4,438,477✔
1160
        if (r == 0) /* empty or root */
4,438,471✔
1161
                return isempty(path) ? -EINVAL : -EADDRNOTAVAIL;
38,841✔
1162
        if (next == path) {
4,419,050✔
1163
                if (*path != '/') /* filename only */
52,904✔
1164
                        return -EDESTADDRREQ;
1165

1166
                return strdup_to(ret, "/");
24,724✔
1167
        }
1168

1169
        _cleanup_free_ char *a = strndup(path, next - path);
4,366,146✔
1170
        if (!a)
4,366,146✔
1171
                return -ENOMEM;
1172

1173
        path_simplify(a);
4,366,146✔
1174

1175
        if (!path_is_valid(a))
4,366,146✔
1176
                return -EINVAL;
1177

1178
        if (ret)
4,366,146✔
1179
                *ret = TAKE_PTR(a);
4,366,146✔
1180

1181
        return 0;
1182
}
1183

1184
bool filename_part_is_valid(const char *p) {
1,113,074✔
1185
        const char *e;
1,113,074✔
1186

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

1190
        if (!p)
1,113,074✔
1191
                return false;
1192

1193
        e = strchrnul(p, '/');
1,113,074✔
1194
        if (*e != 0)
1,113,074✔
1195
                return false;
1196

1197
        if (e - p > NAME_MAX) /* NAME_MAX is counted *without* the trailing NUL byte */
1,096,248✔
1198
                return false;
3✔
1199

1200
        return true;
1201
}
1202

1203
bool filename_is_valid(const char *p) {
1,108,929✔
1204

1205
        if (isempty(p))
1,108,929✔
1206
                return false;
1207

1208
        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
1,108,917✔
1209
                return false;
1210

1211
        return filename_part_is_valid(p);
1,108,904✔
1212
}
1213

1214
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
12,209,328✔
1215
        if (isempty(p))
12,209,328✔
1216
                return false;
1217

1218
        for (const char *e = p;;) {
12,209,319✔
1219
                int r;
38,849,986✔
1220

1221
                r = path_find_first_component(&e, accept_dot_dot, NULL);
38,849,986✔
1222
                if (r < 0)
38,849,986✔
1223
                        return false;
12,209,319✔
1224

1225
                if (e - p >= PATH_MAX) /* Already reached the maximum length for a path? (PATH_MAX is counted
38,849,950✔
1226
                                        * *with* the trailing NUL byte) */
1227
                        return false;
1228
                if (*e == 0)           /* End of string? Yay! */
38,849,947✔
1229
                        return true;
1230
        }
1231
}
1232

1233
bool path_is_normalized(const char *p) {
742,620✔
1234
        if (!path_is_safe(p))
742,620✔
1235
                return false;
1236

1237
        if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
742,595✔
1238
                return false;
1239

1240
        if (strstr(p, "//"))
742,592✔
1241
                return false;
1✔
1242

1243
        return true;
1244
}
1245

1246
int file_in_same_dir(const char *path, const char *filename, char **ret) {
314✔
1247
        _cleanup_free_ char *b = NULL;
314✔
1248
        int r;
314✔
1249

1250
        assert(path);
314✔
1251
        assert(filename);
314✔
1252
        assert(ret);
314✔
1253

1254
        /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1255
         * or the former isn't */
1256

1257
        if (path_is_absolute(filename))
314✔
1258
                b = strdup(filename);
17✔
1259
        else {
1260
                _cleanup_free_ char *dn = NULL;
297✔
1261

1262
                r = path_extract_directory(path, &dn);
297✔
1263
                if (r == -EDESTADDRREQ) /* no path prefix */
297✔
1264
                        b = strdup(filename);
1✔
1265
                else if (r < 0)
296✔
1266
                        return r;
11✔
1267
                else
1268
                        b = path_join(dn, filename);
285✔
1269
        }
1270
        if (!b)
303✔
1271
                return -ENOMEM;
1272

1273
        *ret = TAKE_PTR(b);
303✔
1274
        return 0;
303✔
1275
}
1276

1277
bool hidden_or_backup_file(const char *filename) {
6,667,432✔
1278
        assert(filename);
6,667,432✔
1279

1280
        if (filename[0] == '.' ||
12,506,895✔
1281
            STR_IN_SET(filename,
5,839,463✔
1282
                       "lost+found",
1283
                       "aquota.user",
1284
                       "aquota.group") ||
5,839,460✔
1285
            endswith(filename, "~"))
5,839,460✔
1286
                return true;
827,974✔
1287

1288
        const char *dot = strrchr(filename, '.');
5,839,458✔
1289
        if (!dot)
5,839,458✔
1290
                return false;
1291

1292
        /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1293
         * to come up with always new suffixes and that everybody else should just adjust to that, then it
1294
         * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1295
         * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1296
         * possibly augmented with an additional string. Specifically: there's now:
1297
         *
1298
         *    The generic suffixes "~" and ".bak" for backup files
1299
         *    The generic prefix "." for hidden files
1300
         *
1301
         * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1302
         * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1303
         * ".foopkg.old" or ".foopkg~" instead.
1304
         */
1305

1306
        return STR_IN_SET(dot + 1,
5,259,462✔
1307
                          "ignore",
1308
                          "rpmnew",
1309
                          "rpmsave",
1310
                          "rpmorig",
1311
                          "dpkg-old",
1312
                          "dpkg-new",
1313
                          "dpkg-tmp",
1314
                          "dpkg-dist",
1315
                          "dpkg-bak",
1316
                          "dpkg-backup",
1317
                          "dpkg-remove",
1318
                          "ucf-new",
1319
                          "ucf-old",
1320
                          "ucf-dist",
1321
                          "swp",
1322
                          "bak",
1323
                          "old",
1324
                          "new");
1325
}
1326

1327
bool is_device_path(const char *path) {
11,255✔
1328

1329
        /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1330
         * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1331
         * It does not actually resolve the path.
1332
         *
1333
         * Examples:
1334
         * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1335
         * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1336
         */
1337

1338
        const char *p = PATH_STARTSWITH_SET(ASSERT_PTR(path), "/dev/", "/sys/");
11,255✔
1339
        return !isempty(p);
11,255✔
1340
}
1341

1342
bool valid_device_node_path(const char *path) {
605✔
1343

1344
        /* Some superficial checks whether the specified path is a valid device node path, all without
1345
         * looking at the actual device node. */
1346

1347
        if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
605✔
UNCOV
1348
                return false;
×
1349

1350
        if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
605✔
1351
                return false;
1352

1353
        return path_is_normalized(path);
605✔
1354
}
1355

1356
bool valid_device_allow_pattern(const char *path) {
12✔
1357
        assert(path);
12✔
1358

1359
        /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1360
         * DeviceAllow= and DeviceDeny=. */
1361

1362
        if (STARTSWITH_SET(path, "block-", "char-"))
12✔
1363
                return true;
4✔
1364

1365
        return valid_device_node_path(path);
8✔
1366
}
1367

1368
bool dot_or_dot_dot(const char *path) {
7,751,790✔
1369
        if (!path)
7,751,790✔
1370
                return false;
1371
        if (path[0] != '.')
7,751,789✔
1372
                return false;
1373
        if (path[1] == 0)
313,803✔
1374
                return true;
1375
        if (path[1] != '.')
157,449✔
1376
                return false;
1377

1378
        return path[2] == 0;
155,107✔
1379
}
1380

1381
bool path_implies_directory(const char *path) {
60,920✔
1382

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

1386
        if (!path)
60,920✔
1387
                return false;
60,920✔
1388

1389
        if (dot_or_dot_dot(path))
40,270✔
1390
                return true;
1391

1392
        return ENDSWITH_SET(path, "/", "/.", "/..");
40,268✔
1393
}
1394

1395
bool empty_or_root(const char *path) {
10,315,791✔
1396

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

1400
        if (isempty(path))
10,315,791✔
1401
                return true;
1402

1403
        return path_equal(path, "/");
3,775,592✔
1404
}
1405

1406
const char* empty_to_root(const char *path) {
300,392✔
1407
        return isempty(path) ? "/" : path;
308,491✔
1408
}
1409

1410
int empty_or_root_harder_to_null(const char **path) {
3,015,103✔
1411
        int r;
3,015,103✔
1412

1413
        assert(path);
3,015,103✔
1414

1415
        /* This nullifies the input path when the path is empty or points to "/". */
1416

1417
        if (empty_or_root(*path)) {
3,015,103✔
1418
                *path = NULL;
2,933,997✔
1419
                return 0;
2,933,997✔
1420
        }
1421

1422
        r = path_is_root(*path);
81,106✔
1423
        if (r < 0)
81,106✔
1424
                return r;
1425
        if (r > 0)
81,098✔
1426
                *path = NULL;
9✔
1427

1428
        return 0;
1429
}
1430

1431
bool path_strv_contains(char * const *l, const char *path) {
84,200✔
1432
        assert(path);
84,200✔
1433

1434
        STRV_FOREACH(i, l)
1,477,613✔
1435
                if (path_equal(*i, path))
1,407,357✔
1436
                        return true;
1437

1438
        return false;
1439
}
1440

1441
bool prefixed_path_strv_contains(char * const *l, const char *path) {
92✔
1442
        assert(path);
92✔
1443

1444
        STRV_FOREACH(i, l) {
93✔
1445
                const char *j = *i;
1✔
1446

1447
                if (*j == '-')
1✔
UNCOV
1448
                        j++;
×
1449
                if (*j == '+')
1✔
UNCOV
1450
                        j++;
×
1451

1452
                if (path_equal(j, path))
1✔
1453
                        return true;
1454
        }
1455

1456
        return false;
1457
}
1458

1459
int path_glob_can_match(const char *pattern, const char *prefix, char **ret) {
11,636✔
1460
        assert(pattern);
11,636✔
1461
        assert(prefix);
11,636✔
1462

1463
        for (const char *a = pattern, *b = prefix;;) {
11,636✔
1464
                _cleanup_free_ char *g = NULL, *h = NULL;
26,211✔
1465
                const char *p, *q;
34,922✔
1466
                int r, s;
34,922✔
1467

1468
                r = path_find_first_component(&a, /* accept_dot_dot = */ false, &p);
34,922✔
1469
                if (r < 0)
34,922✔
1470
                        return r;
1471

1472
                s = path_find_first_component(&b, /* accept_dot_dot = */ false, &q);
34,922✔
1473
                if (s < 0)
34,922✔
1474
                        return s;
1475

1476
                if (s == 0) {
34,922✔
1477
                        /* The pattern matches the prefix. */
1478
                        if (ret) {
2,925✔
1479
                                char *t;
2,925✔
1480

1481
                                t = path_join(prefix, p);
2,925✔
1482
                                if (!t)
2,925✔
1483
                                        return -ENOMEM;
1484

1485
                                *ret = t;
2,925✔
1486
                        }
1487
                        return true;
2,925✔
1488
                }
1489

1490
                if (r == 0)
31,997✔
1491
                        break;
1492

1493
                if (r == s && strneq(p, q, r))
31,993✔
1494
                        continue; /* common component. Check next. */
20,359✔
1495

1496
                g = strndup(p, r);
11,634✔
1497
                if (!g)
11,634✔
1498
                        return -ENOMEM;
1499

1500
                if (!string_is_glob(g))
11,634✔
1501
                        break;
1502

1503
                /* We found a glob component. Check if the glob pattern matches the prefix component. */
1504

1505
                h = strndup(q, s);
2,927✔
1506
                if (!h)
2,927✔
1507
                        return -ENOMEM;
1508

1509
                r = fnmatch(g, h, 0);
2,927✔
1510
                if (r == FNM_NOMATCH)
2,927✔
1511
                        break;
1512
                if (r != 0) /* Failure to process pattern? */
2,927✔
1513
                        return -EINVAL;
1514
        }
1515

1516
        /* The pattern does not match the prefix. */
1517
        if (ret)
8,711✔
1518
                *ret = NULL;
8,711✔
1519
        return false;
1520
}
1521

1522
#if HAVE_SPLIT_BIN
1523
static bool dir_is_split(const char *a, const char *b) {
1524
        int r;
1525

1526
        r = inode_same(a, b, AT_NO_AUTOMOUNT);
1527
        if (r < 0 && r != -ENOENT) {
1528
                log_debug_errno(r, "Failed to compare \"%s\" and \"%s\", assuming split directories: %m", a, b);
1529
                return true;
1530
        }
1531
        return r == 0;
1532
}
1533
#endif
1534

1535
const char* default_PATH(void) {
3,835✔
1536
#if HAVE_SPLIT_BIN
1537
        static const char *default_path = NULL;
1538

1539
        /* Return one of the three sets of paths:
1540
         * a) split /usr/s?bin, /usr/local/sbin doesn't matter.
1541
         * b) merged /usr/s?bin, /usr/sbin is a symlink, but /usr/local/sbin is not,
1542
         * c) fully merged, neither /usr/sbin nor /usr/local/sbin are symlinks,
1543
         *
1544
         * On error the fallback to the safe value with both directories as configured is returned.
1545
         */
1546

1547
        if (default_path)
1548
                return default_path;
1549

1550
        if (dir_is_split("/usr/sbin", "/usr/bin"))
1551
                return (default_path = DEFAULT_PATH_WITH_FULL_SBIN);  /* a */
1552
        if (dir_is_split("/usr/local/sbin", "/usr/local/bin"))
1553
                return (default_path = DEFAULT_PATH_WITH_LOCAL_SBIN); /* b */
1554
        return (default_path = DEFAULT_PATH_WITHOUT_SBIN);            /* c */
1555
#else
1556
        return DEFAULT_PATH_WITHOUT_SBIN;
3,835✔
1557
#endif
1558
}
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