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

systemd / systemd / 15221274416

23 May 2025 06:04PM UTC coverage: 72.07% (+0.009%) from 72.061%
15221274416

push

github

yuwata
linux: update kernel headers from v6.15-rc7

299231 of 415193 relevant lines covered (72.07%)

701304.83 hits per line

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

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

3
#include <errno.h>
4
#include <fnmatch.h>
5
#include <limits.h>
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <unistd.h>
9

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

25
bool is_path(const char *p) {
118,720✔
26
        if (!p) /* A NULL pointer is definitely not a path */
118,720✔
27
                return false;
28

29
        return strchr(p, '/');
118,720✔
30
}
31

32
int path_split_and_make_absolute(const char *p, char ***ret) {
1,102✔
33
        _cleanup_strv_free_ char **l = NULL;
1,102✔
34
        int r;
1,102✔
35

36
        assert(p);
1,102✔
37
        assert(ret);
1,102✔
38

39
        l = strv_split(p, ":");
1,102✔
40
        if (!l)
1,102✔
41
                return -ENOMEM;
42

43
        r = path_strv_make_absolute_cwd(l);
1,102✔
44
        if (r < 0)
1,102✔
45
                return r;
46

47
        *ret = TAKE_PTR(l);
1,102✔
48
        return r;
1,102✔
49
}
50

51
char* path_make_absolute(const char *p, const char *prefix) {
2,284✔
52
        assert(p);
2,284✔
53

54
        /* Makes every item in the list an absolute path by prepending
55
         * the prefix, if specified and necessary */
56

57
        if (path_is_absolute(p) || isempty(prefix))
2,284✔
58
                return strdup(p);
312✔
59

60
        return path_join(prefix, p);
1,972✔
61
}
62

63
int safe_getcwd(char **ret) {
5,473✔
64
        _cleanup_free_ char *cwd = NULL;
5,473✔
65

66
        cwd = get_current_dir_name();
5,473✔
67
        if (!cwd)
5,473✔
68
                return negative_errno();
×
69

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

75
        if (ret)
5,473✔
76
                *ret = TAKE_PTR(cwd);
5,473✔
77

78
        return 0;
79
}
80

81
int path_make_absolute_cwd(const char *p, char **ret) {
3,226,590✔
82
        char *c;
3,226,590✔
83
        int r;
3,226,590✔
84

85
        assert(p);
3,226,590✔
86
        assert(ret);
3,226,590✔
87

88
        /* Similar to path_make_absolute(), but prefixes with the
89
         * current working directory. */
90

91
        if (path_is_absolute(p))
3,226,590✔
92
                c = strdup(p);
3,226,307✔
93
        else {
94
                _cleanup_free_ char *cwd = NULL;
283✔
95

96
                r = safe_getcwd(&cwd);
283✔
97
                if (r < 0)
283✔
98
                        return r;
×
99

100
                c = path_join(cwd, p);
283✔
101
        }
102
        if (!c)
3,226,590✔
103
                return -ENOMEM;
104

105
        *ret = c;
3,226,590✔
106
        return 0;
3,226,590✔
107
}
108

109
int path_make_relative(const char *from, const char *to, char **ret) {
864,104✔
110
        _cleanup_free_ char *result = NULL;
864,104✔
111
        unsigned n_parents;
864,104✔
112
        const char *f, *t;
864,104✔
113
        int r, k;
864,104✔
114
        char *p;
864,104✔
115

116
        assert(from);
864,104✔
117
        assert(to);
864,104✔
118
        assert(ret);
864,104✔
119

120
        /* Strips the common part, and adds ".." elements as necessary. */
121

122
        if (!path_is_absolute(from) || !path_is_absolute(to))
1,728,204✔
123
                return -EINVAL;
124

125
        for (;;) {
6,165,191✔
126
                r = path_find_first_component(&from, true, &f);
6,165,191✔
127
                if (r < 0)
6,165,191✔
128
                        return r;
129

130
                k = path_find_first_component(&to, true, &t);
6,165,191✔
131
                if (k < 0)
6,165,191✔
132
                        return k;
133

134
                if (r == 0) {
6,165,191✔
135
                        /* end of 'from' */
136
                        if (k == 0) {
706,949✔
137
                                /* from and to are equivalent. */
138
                                result = strdup(".");
4✔
139
                                if (!result)
4✔
140
                                        return -ENOMEM;
141
                        } else {
142
                                /* 'to' is inside of 'from'. */
143
                                r = path_simplify_alloc(t, &result);
706,945✔
144
                                if (r < 0)
706,945✔
145
                                        return r;
146

147
                                if (!path_is_valid(result))
706,945✔
148
                                        return -EINVAL;
149
                        }
150

151
                        *ret = TAKE_PTR(result);
706,949✔
152
                        return 0;
706,949✔
153
                }
154

155
                if (r != k || !strneq(f, t, r))
5,458,242✔
156
                        break;
157
        }
158

159
        /* If we're here, then "from_dir" has one or more elements that need to
160
         * be replaced with "..". */
161

162
        for (n_parents = 1;; n_parents++) {
195,257✔
163
                /* If this includes ".." we can't do a simple series of "..". */
164
                r = path_find_first_component(&from, false, &f);
352,408✔
165
                if (r < 0)
352,408✔
166
                        return r;
167
                if (r == 0)
352,406✔
168
                        break;
169
        }
170

171
        if (isempty(t) && n_parents * 3 > PATH_MAX)
157,149✔
172
                /* PATH_MAX is counted *with* the trailing NUL byte */
173
                return -EINVAL;
174

175
        result = new(char, n_parents * 3 + !isempty(t) + strlen_ptr(t));
471,443✔
176
        if (!result)
157,149✔
177
                return -ENOMEM;
178

179
        for (p = result; n_parents > 0; n_parents--)
509,555✔
180
                p = mempcpy(p, "../", 3);
352,406✔
181

182
        if (isempty(t)) {
157,149✔
183
                /* Remove trailing slash and terminate string. */
184
                *(--p) = '\0';
2✔
185
                *ret = TAKE_PTR(result);
2✔
186
                return 0;
2✔
187
        }
188

189
        strcpy(p, t);
157,147✔
190

191
        path_simplify(result);
157,147✔
192

193
        if (!path_is_valid(result))
157,147✔
194
                return -EINVAL;
195

196
        *ret = TAKE_PTR(result);
157,147✔
197
        return 0;
157,147✔
198
}
199

200
int path_make_relative_parent(const char *from_child, const char *to, char **ret) {
157,232✔
201
        _cleanup_free_ char *from = NULL;
157,232✔
202
        int r;
157,232✔
203

204
        assert(from_child);
157,232✔
205
        assert(to);
157,232✔
206
        assert(ret);
157,232✔
207

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

221
        r = path_extract_directory(from_child, &from);
157,232✔
222
        if (r < 0)
157,232✔
223
                return r;
224

225
        return path_make_relative(from, to, ret);
157,231✔
226
}
227

228
char* path_startswith_strv(const char *p, char * const *strv) {
148,202✔
229
        assert(p);
148,202✔
230

231
        STRV_FOREACH(s, strv) {
554,106✔
232
                char *t;
430,501✔
233

234
                t = path_startswith(p, *s);
430,501✔
235
                if (t)
430,501✔
236
                        return t;
237
        }
238

239
        return NULL;
240
}
241

242
int path_strv_make_absolute_cwd(char **l) {
1,472✔
243
        int r;
1,472✔
244

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

249
        STRV_FOREACH(s, l) {
8,436✔
250
                char *t;
6,964✔
251

252
                r = path_make_absolute_cwd(*s, &t);
6,964✔
253
                if (r < 0)
6,964✔
254
                        return r;
×
255

256
                path_simplify(t);
6,964✔
257
                free_and_replace(*s, t);
6,964✔
258
        }
259

260
        return 0;
261
}
262

263
char** path_strv_resolve(char **l, const char *root) {
22,643✔
264
        unsigned k = 0;
22,643✔
265
        bool enomem = false;
22,643✔
266
        int r;
22,643✔
267

268
        if (strv_isempty(l))
45,286✔
269
                return l;
270

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

275
        STRV_FOREACH(s, l) {
135,239✔
276
                _cleanup_free_ char *orig = NULL;
112,596✔
277
                char *t, *u;
112,596✔
278

279
                if (!path_is_absolute(*s)) {
112,596✔
280
                        free(*s);
×
281
                        continue;
×
282
                }
283

284
                if (root) {
112,596✔
285
                        orig = *s;
7✔
286
                        t = path_join(root, orig);
7✔
287
                        if (!t) {
7✔
288
                                enomem = true;
×
289
                                continue;
×
290
                        }
291
                } else
292
                        t = *s;
293

294
                r = chase(t, root, 0, &u, NULL);
112,596✔
295
                if (r == -ENOENT) {
112,596✔
296
                        if (root) {
94,951✔
297
                                u = TAKE_PTR(orig);
2✔
298
                                free(t);
2✔
299
                        } else
300
                                u = t;
94,949✔
301
                } else if (r < 0) {
17,645✔
302
                        free(t);
×
303

304
                        if (r == -ENOMEM)
×
305
                                enomem = true;
×
306

307
                        continue;
×
308
                } else if (root) {
17,645✔
309
                        char *x;
5✔
310

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

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

333
                l[k++] = u;
112,596✔
334
        }
335

336
        l[k] = NULL;
22,643✔
337

338
        if (enomem)
22,643✔
339
                return NULL;
×
340

341
        return l;
342
}
343

344
char** path_strv_resolve_uniq(char **l, const char *root) {
22,642✔
345

346
        if (strv_isempty(l))
22,642✔
347
                return l;
348

349
        if (!path_strv_resolve(l, root))
22,642✔
350
                return NULL;
351

352
        return strv_uniq(l);
22,642✔
353
}
354

355
char* skip_leading_slash(const char *p) {
12✔
356
        return skip_leading_chars(p, "/");
12✔
357
}
358

359
char* path_simplify_full(char *path, PathSimplifyFlags flags) {
8,375,764✔
360
        bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
8,375,764✔
361
        char *f = path;
8,375,764✔
362
        int r;
8,375,764✔
363

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

373
        if (isempty(path))
8,375,764✔
374
                return path;
375

376
        keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && endswith(path, "/");
8,350,426✔
377

378
        absolute = path_is_absolute(path);
8,350,426✔
379
        f += absolute;  /* Keep leading /, if present. */
8,350,426✔
380

381
        for (const char *p = f;;) {
8,350,426✔
382
                const char *e;
33,879,718✔
383

384
                r = path_find_first_component(&p, true, &e);
33,879,718✔
385
                if (r == 0)
33,879,718✔
386
                        break;
387

388
                if (r > 0 && absolute && beginning && path_startswith(e, ".."))
25,529,296✔
389
                        /* If we're at the beginning of an absolute path, we can safely skip ".." */
390
                        continue;
79✔
391

392
                beginning = false;
25,529,217✔
393

394
                if (add_slash)
25,529,217✔
395
                        *f++ = '/';
17,186,782✔
396

397
                if (r < 0) {
25,529,217✔
398
                        /* if path is invalid, then refuse to simplify the remaining part. */
399
                        memmove(f, p, strlen(p) + 1);
4✔
400
                        return path;
4✔
401
                }
402

403
                memmove(f, e, r);
25,529,213✔
404
                f += r;
25,529,213✔
405

406
                add_slash = true;
25,529,213✔
407
        }
408

409
        /* Special rule, if we stripped everything, we need a "." for the current directory. */
410
        if (f == path)
8,350,422✔
411
                *f++ = '.';
7✔
412

413
        if (*(f-1) != '/' && keep_trailing_slash)
8,350,422✔
414
                *f++ = '/';
12✔
415

416
        *f = '\0';
8,350,422✔
417
        return path;
8,350,422✔
418
}
419

420
int path_simplify_alloc(const char *path, char **ret) {
1,665,291✔
421
        assert(ret);
1,665,291✔
422

423
        if (!path) {
1,665,291✔
424
                *ret = NULL;
13,435✔
425
                return 0;
13,435✔
426
        }
427

428
        char *t = strdup(path);
1,651,856✔
429
        if (!t)
1,651,856✔
430
                return -ENOMEM;
431

432
        *ret = path_simplify(t);
1,651,856✔
433
        return 0;
1,651,856✔
434
}
435

436
char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) {
51,434,934✔
437
        assert(original_path);
51,434,934✔
438
        assert(prefix);
51,434,934✔
439

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

450
        const char *path = original_path;
51,434,934✔
451

452
        if ((path[0] == '/') != (prefix[0] == '/'))
51,434,934✔
453
                return NULL;
51,434,934✔
454

455
        for (;;) {
44,840,168✔
456
                const char *p, *q;
96,202,645✔
457
                int m, n;
96,202,645✔
458

459
                m = path_find_first_component(&path, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &p);
96,202,645✔
460
                if (m < 0)
96,202,645✔
461
                        return NULL;
51,362,477✔
462

463
                n = path_find_first_component(&prefix, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &q);
96,202,645✔
464
                if (n < 0)
96,202,645✔
465
                        return NULL;
466

467
                if (n == 0) {
96,202,645✔
468
                        if (!p)
37,410,443✔
469
                                p = path;
190,434✔
470

471
                        if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) {
37,410,443✔
472

473
                                if (p <= original_path)
7✔
474
                                        return NULL;
475

476
                                p--;
7✔
477

478
                                if (*p != '/')
7✔
479
                                        return NULL;
480
                        }
481

482
                        return (char*) p;
37,410,442✔
483
                }
484

485
                if (m != n)
58,792,202✔
486
                        return NULL;
487

488
                if (!strneq(p, q, m))
47,689,360✔
489
                        return NULL;
490
        }
491
}
492

493
int path_compare(const char *a, const char *b) {
21,272,997✔
494
        int r;
21,272,997✔
495

496
        /* Order NULL before non-NULL */
497
        r = CMP(!!a, !!b);
21,272,997✔
498
        if (r != 0)
21,252,167✔
499
                return r;
20,833✔
500

501
        /* A relative path and an absolute path must not compare as equal.
502
         * Which one is sorted before the other does not really matter.
503
         * Here a relative path is ordered before an absolute path. */
504
        r = CMP(path_is_absolute(a), path_is_absolute(b));
42,504,326✔
505
        if (r != 0)
20,989,569✔
506
                return r;
304,274✔
507

508
        for (;;) {
17,778,910✔
509
                const char *aa, *bb;
38,726,800✔
510
                int j, k;
38,726,800✔
511

512
                j = path_find_first_component(&a, true, &aa);
38,726,800✔
513
                k = path_find_first_component(&b, true, &bb);
38,726,800✔
514

515
                if (j < 0 || k < 0) {
38,726,800✔
516
                        /* When one of paths is invalid, order invalid path after valid one. */
517
                        r = CMP(j < 0, k < 0);
4✔
518
                        if (r != 0)
×
519
                                return r;
20,947,890✔
520

521
                        /* fallback to use strcmp() if both paths are invalid. */
522
                        return strcmp(a, b);
×
523
                }
524

525
                /* Order prefixes first: "/foo" before "/foo/bar" */
526
                if (j == 0) {
38,726,796✔
527
                        if (k == 0)
3,741,664✔
528
                                return 0;
529
                        return -1;
121,601✔
530
                }
531
                if (k == 0)
34,985,132✔
532
                        return 1;
533

534
                /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
535
                r = memcmp(aa, bb, MIN(j, k));
32,483,837✔
536
                if (r != 0)
32,483,837✔
537
                        return r;
538

539
                /* Sort "/foo/a" before "/foo/aaa" */
540
                r = CMP(j, k);
17,863,735✔
541
                if (r != 0)
17,808,188✔
542
                        return r;
84,825✔
543
        }
544
}
545

546
int path_compare_filename(const char *a, const char *b) {
115,676✔
547
        _cleanup_free_ char *fa = NULL, *fb = NULL;
115,676✔
548
        int r, j, k;
115,676✔
549

550
        /* Order NULL before non-NULL */
551
        r = CMP(!!a, !!b);
115,676✔
552
        if (r != 0)
115,676✔
553
                return r;
×
554

555
        j = path_extract_filename(a, &fa);
115,676✔
556
        k = path_extract_filename(b, &fb);
115,676✔
557

558
        /* When one of paths is "." or root, then order it earlier. */
559
        r = CMP(j != -EADDRNOTAVAIL, k != -EADDRNOTAVAIL);
115,676✔
560
        if (r != 0)
115,672✔
561
                return r;
8✔
562

563
        /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
564
        r = CMP(j < 0, k < 0);
115,668✔
565
        if (r != 0)
115,668✔
566
                return r;
×
567

568
        /* fallback to use strcmp() if both paths are invalid. */
569
        if (j < 0)
115,668✔
570
                return strcmp(a, b);
30✔
571

572
        return strcmp(fa, fb);
115,638✔
573
}
574

575
int path_equal_or_inode_same_full(const char *a, const char *b, int flags) {
51,816✔
576
        /* Returns true if paths are of the same entry, false if not, <0 on error. */
577

578
        if (path_equal(a, b))
51,816✔
579
                return 1;
580

581
        if (!a || !b)
469✔
582
                return 0;
583

584
        return inode_same(a, b, flags);
469✔
585
}
586

587
char* path_extend_internal(char **x, ...) {
23,460,670✔
588
        size_t sz, old_sz;
23,460,670✔
589
        char *q, *nx;
23,460,670✔
590
        const char *p;
23,460,670✔
591
        va_list ap;
23,460,670✔
592
        bool slash;
23,460,670✔
593

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

612
        sz = old_sz = x ? strlen_ptr(*x) : 0;
23,460,670✔
613
        va_start(ap, x);
23,460,670✔
614
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
52,933,250✔
615
                size_t add;
29,472,580✔
616

617
                if (isempty(p))
29,472,580✔
618
                        continue;
614,599✔
619

620
                add = 1 + strlen(p);
28,857,981✔
621
                if (sz > SIZE_MAX - add) { /* overflow check */
28,857,981✔
622
                        va_end(ap);
×
623
                        return NULL;
×
624
                }
625

626
                sz += add;
28,857,981✔
627
        }
628
        va_end(ap);
23,460,670✔
629

630
        nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1));
23,460,670✔
631
        if (!nx)
23,460,670✔
632
                return NULL;
633
        if (x)
23,460,670✔
634
                *x = nx;
17,790,438✔
635

636
        if (old_sz > 0)
23,460,670✔
637
                slash = nx[old_sz-1] == '/';
17,719,060✔
638
        else {
639
                nx[old_sz] = 0;
5,741,610✔
640
                slash = true; /* no need to generate a slash anymore */
5,741,610✔
641
        }
642

643
        q = nx + old_sz;
23,460,670✔
644

645
        va_start(ap, x);
23,460,670✔
646
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
52,933,250✔
647
                if (isempty(p))
29,472,580✔
648
                        continue;
614,599✔
649

650
                if (!slash && p[0] != '/')
28,857,981✔
651
                        *(q++) = '/';
17,907,593✔
652

653
                q = stpcpy(q, p);
28,857,981✔
654
                slash = endswith(p, "/");
28,857,981✔
655
        }
656
        va_end(ap);
23,460,670✔
657

658
        return nx;
23,460,670✔
659
}
660

661
int open_and_check_executable(const char *name, const char *root, char **ret_path, int *ret_fd) {
27,729✔
662
        _cleanup_close_ int fd = -EBADF;
27,729✔
663
        _cleanup_free_ char *resolved = NULL;
27,729✔
664
        int r;
27,729✔
665

666
        assert(name);
27,729✔
667

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

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

687
        r = fd_verify_regular(fd);
23,413✔
688
        if (r < 0)
23,413✔
689
                return r;
690

691
        r = access_fd(fd, X_OK);
23,410✔
692
        if (r == -ENOSYS)
23,410✔
693
                /* /proc/ is not mounted. Fall back to access(). */
694
                r = RET_NERRNO(access(name, X_OK));
×
695
        if (r < 0)
23,410✔
696
                return r;
697

698
        if (ret_path) {
23,409✔
699
                if (resolved)
23,053✔
700
                        *ret_path = TAKE_PTR(resolved);
×
701
                else {
702
                        r = path_make_absolute_cwd(name, ret_path);
23,053✔
703
                        if (r < 0)
23,053✔
704
                                return r;
705

706
                        path_simplify(*ret_path);
23,053✔
707
                }
708
        }
709

710
        if (ret_fd)
23,409✔
711
                *ret_fd = TAKE_FD(fd);
21,766✔
712

713
        return 0;
714
}
715

716
int find_executable_full(
23,419✔
717
                const char *name,
718
                const char *root,
719
                char * const *exec_search_path,
720
                bool use_path_envvar,
721
                char **ret_filename,
722
                int *ret_fd) {
723

724
        int last_error = -ENOENT, r = 0;
23,419✔
725

726
        assert(name);
23,419✔
727

728
        if (is_path(name))
23,419✔
729
                return open_and_check_executable(name, root, ret_filename, ret_fd);
23,419✔
730

731
        if (exec_search_path) {
3,367✔
732
                STRV_FOREACH(element, exec_search_path) {
6✔
733
                        _cleanup_free_ char *full_path = NULL;
5✔
734

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

740
                        full_path = path_join(*element, name);
5✔
741
                        if (!full_path)
5✔
742
                                return -ENOMEM;
743

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

753
        const char *p = NULL;
3,365✔
754

755
        if (use_path_envvar)
3,365✔
756
                /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
757
                 * binary. */
758
                p = getenv("PATH");
952✔
759
        if (!p)
3,365✔
760
                p = default_PATH();
2,423✔
761

762
        /* Resolve a single-component name to a full path */
763
        for (;;) {
7,676✔
764
                _cleanup_free_ char *element = NULL;
7,672✔
765

766
                r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
7,676✔
767
                if (r < 0)
7,676✔
768
                        return r;
769
                if (r == 0)
7,676✔
770
                        break;
771

772
                if (!path_is_absolute(element)) {
7,672✔
773
                        log_debug("Exec search path '%s' isn't absolute, ignoring.", element);
×
774
                        continue;
×
775
                }
776

777
                if (!path_extend(&element, name))
7,672✔
778
                        return -ENOMEM;
779

780
                r = open_and_check_executable(element, root, ret_filename, ret_fd);
7,672✔
781
                if (r >= 0) /* Found it! */
7,672✔
782
                        return 0;
783
                /* PATH entries which we don't have access to are ignored, as per tradition. */
784
                if (r != -EACCES)
4,311✔
785
                        last_error = r;
4,311✔
786
        }
787

788
        return last_error;
4✔
789
}
790

791
bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
×
792
        bool changed = false, originally_unset;
×
793

794
        assert(timestamp);
×
795

796
        if (!paths)
×
797
                return false;
798

799
        originally_unset = *timestamp == 0;
×
800

801
        STRV_FOREACH(i, paths) {
×
802
                struct stat stats;
×
803
                usec_t u;
×
804

805
                if (stat(*i, &stats) < 0)
×
806
                        continue;
×
807

808
                u = timespec_load(&stats.st_mtim);
×
809

810
                /* check first */
811
                if (*timestamp >= u)
×
812
                        continue;
×
813

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

816
                /* update timestamp */
817
                if (update) {
×
818
                        *timestamp = u;
×
819
                        changed = true;
×
820
                } else
821
                        return true;
×
822
        }
823

824
        return changed;
825
}
826

827
static int executable_is_good(const char *executable) {
184✔
828
        _cleanup_free_ char *p = NULL, *d = NULL;
184✔
829
        int r;
184✔
830

831
        r = find_executable(executable, &p);
184✔
832
        if (r == -ENOENT)
184✔
833
                return 0;
834
        if (r < 0)
182✔
835
                return r;
836

837
        /* An fsck that is linked to /bin/true is a non-existent fsck */
838

839
        r = readlink_malloc(p, &d);
182✔
840
        if (r == -EINVAL) /* not a symlink */
182✔
841
                return 1;
842
        if (r < 0)
64✔
843
                return r;
844

845
        return !PATH_IN_SET(d, "true"
64✔
846
                               "/bin/true",
847
                               "/usr/bin/true",
848
                               "/dev/null");
849
}
850

851
int fsck_exists(void) {
99✔
852
        return executable_is_good("fsck");
99✔
853
}
854

855
int fsck_exists_for_fstype(const char *fstype) {
85✔
856
        const char *checker;
85✔
857
        int r;
85✔
858

859
        assert(fstype);
85✔
860

861
        if (streq(fstype, "auto"))
85✔
862
                return -EINVAL;
863

864
        r = fsck_exists();
85✔
865
        if (r <= 0)
85✔
866
                return r;
867

868
        checker = strjoina("fsck.", fstype);
425✔
869
        return executable_is_good(checker);
85✔
870
}
871

872
static const char* skip_slash_or_dot(const char *p) {
904,088,099✔
873
        for (; !isempty(p); p++) {
1,395,354,511✔
874
                if (*p == '/')
1,207,355,580✔
875
                        continue;
491,265,612✔
876
                if (startswith(p, "./")) {
716,089,968✔
877
                        p++;
800✔
878
                        continue;
800✔
879
                }
880
                break;
881
        }
882
        return p;
904,088,099✔
883
}
884

885
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret) {
492,807,176✔
886
        const char *q, *first, *end_first, *next;
492,807,176✔
887
        size_t len;
492,807,176✔
888

889
        assert(p);
492,807,176✔
890

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

924
        q = *p;
492,807,176✔
925

926
        first = skip_slash_or_dot(q);
492,807,176✔
927
        if (isempty(first)) {
492,807,176✔
928
                *p = first;
81,518,641✔
929
                if (ret)
81,518,641✔
930
                        *ret = NULL;
81,513,157✔
931
                return 0;
81,518,641✔
932
        }
933
        if (streq(first, ".")) {
411,288,535✔
934
                *p = first + 1;
7,421✔
935
                if (ret)
7,421✔
936
                        *ret = NULL;
7,363✔
937
                return 0;
7,421✔
938
        }
939

940
        end_first = strchrnul(first, '/');
411,281,114✔
941
        len = end_first - first;
411,281,114✔
942

943
        if (len > NAME_MAX)
411,281,114✔
944
                return -EINVAL;
945
        if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.')
411,280,960✔
946
                return -EINVAL;
947

948
        next = skip_slash_or_dot(end_first);
411,280,923✔
949

950
        *p = next + streq(next, ".");
411,280,923✔
951
        if (ret)
411,280,923✔
952
                *ret = first;
376,860,120✔
953
        return len;
411,280,923✔
954
}
955

956
static const char* skip_slash_or_dot_backward(const char *path, const char *q) {
14,528,602✔
957
        assert(path);
14,528,602✔
958
        assert(!q || q >= path);
14,528,602✔
959

960
        for (; q; q = PTR_SUB1(q, path)) {
29,632,711✔
961
                if (*q == '/')
21,869,391✔
962
                        continue;
7,582,295✔
963
                if (q > path && strneq(q - 1, "/.", 2))
14,287,096✔
964
                        continue;
264✔
965
                if (q == path && *q == '.')
14,286,832✔
966
                        continue;
81✔
967
                break;
968
        }
969
        return q;
14,528,602✔
970
}
971

972
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) {
7,272,087✔
973
        const char *q, *last_end, *last_begin;
7,272,087✔
974
        size_t len;
7,272,087✔
975

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

1010
        if (isempty(path)) {
7,272,087✔
1011
                if (next)
9✔
1012
                        *next = path;
9✔
1013
                if (ret)
9✔
1014
                        *ret = NULL;
9✔
1015
                return 0;
9✔
1016
        }
1017

1018
        if (next && *next) {
7,272,078✔
1019
                if (*next < path || *next > path + strlen(path))
35,038✔
1020
                        return -EINVAL;
1021
                if (*next == path) {
35,038✔
1022
                        if (ret)
9✔
1023
                                *ret = NULL;
9✔
1024
                        return 0;
9✔
1025
                }
1026
                if (!IN_SET(**next, '\0', '/'))
35,029✔
1027
                        return -EINVAL;
1028
                q = *next - 1;
35,029✔
1029
        } else
1030
                q = path + strlen(path) - 1;
7,237,040✔
1031

1032
        q = skip_slash_or_dot_backward(path, q);
7,272,069✔
1033
        if (!q || /* the root directory */
7,272,069✔
1034
            (q == path && *q == '.')) { /* path is "." or "./" */
34✔
1035
                if (next)
15,467✔
1036
                        *next = path;
15,467✔
1037
                if (ret)
15,467✔
1038
                        *ret = NULL;
15,464✔
1039
                return 0;
15,467✔
1040
        }
1041

1042
        last_end = q + 1;
7,256,602✔
1043

1044
        while (q && *q != '/')
118,346,061✔
1045
                q = PTR_SUB1(q, path);
103,832,857✔
1046

1047
        last_begin = q ? q + 1 : path;
7,256,602✔
1048
        len = last_end - last_begin;
7,256,602✔
1049

1050
        if (len > NAME_MAX)
7,256,602✔
1051
                return -EINVAL;
1052
        if (!accept_dot_dot && len == 2 && strneq(last_begin, "..", 2))
7,256,598✔
1053
                return -EINVAL;
1054

1055
        if (next) {
7,256,573✔
1056
                q = skip_slash_or_dot_backward(path, q);
7,256,533✔
1057
                *next = q ? q + 1 : path;
7,256,533✔
1058
        }
1059

1060
        if (ret)
7,256,573✔
1061
                *ret = last_begin;
6,306,840✔
1062
        return len;
7,256,573✔
1063
}
1064

1065
const char* last_path_component(const char *path) {
158,954✔
1066

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

1087
        unsigned l, k;
158,954✔
1088

1089
        if (!path)
158,954✔
1090
                return NULL;
1091

1092
        l = k = strlen(path);
158,953✔
1093
        if (l == 0) /* special case — an empty string */
158,953✔
1094
                return path;
1095

1096
        while (k > 0 && path[k-1] == '/')
158,965✔
1097
                k--;
1098

1099
        if (k == 0) /* the root directory */
158,952✔
1100
                return path + l - 1;
3✔
1101

1102
        while (k > 0 && path[k-1] != '/')
1,367,286✔
1103
                k--;
1104

1105
        return path + k;
158,949✔
1106
}
1107

1108
int path_extract_filename(const char *path, char **ret) {
2,098,417✔
1109
        _cleanup_free_ char *a = NULL;
4,196,834✔
1110
        const char *c, *next = NULL;
2,098,417✔
1111
        int r;
2,098,417✔
1112

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

1128
        if (!path_is_valid(path))
2,098,417✔
1129
                return -EINVAL;
1130

1131
        r = path_find_last_component(path, false, &next, &c);
2,098,414✔
1132
        if (r < 0)
2,098,414✔
1133
                return r;
1134
        if (r == 0) /* root directory */
2,098,400✔
1135
                return -EADDRNOTAVAIL;
1136

1137
        a = strndup(c, r);
2,098,316✔
1138
        if (!a)
2,098,316✔
1139
                return -ENOMEM;
1140

1141
        *ret = TAKE_PTR(a);
2,098,316✔
1142
        return strlen(c) > (size_t) r ? O_DIRECTORY : 0;
2,098,316✔
1143
}
1144

1145
int path_extract_directory(const char *path, char **ret) {
4,188,827✔
1146
        const char *c, *next = NULL;
4,188,827✔
1147
        int r;
4,188,827✔
1148

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

1160
        r = path_find_last_component(path, false, &next, &c);
4,188,827✔
1161
        if (r < 0)
4,188,827✔
1162
                return r;
4,188,827✔
1163
        if (r == 0) /* empty or root */
4,188,821✔
1164
                return isempty(path) ? -EINVAL : -EADDRNOTAVAIL;
30,749✔
1165
        if (next == path) {
4,173,446✔
1166
                if (*path != '/') /* filename only */
26,906✔
1167
                        return -EDESTADDRREQ;
1168

1169
                return strdup_to(ret, "/");
19,805✔
1170
        }
1171

1172
        _cleanup_free_ char *a = strndup(path, next - path);
4,146,540✔
1173
        if (!a)
4,146,540✔
1174
                return -ENOMEM;
1175

1176
        path_simplify(a);
4,146,540✔
1177

1178
        if (!path_is_valid(a))
4,146,540✔
1179
                return -EINVAL;
1180

1181
        if (ret)
4,146,540✔
1182
                *ret = TAKE_PTR(a);
4,146,540✔
1183

1184
        return 0;
1185
}
1186

1187
bool filename_part_is_valid(const char *p) {
1,005,461✔
1188
        const char *e;
1,005,461✔
1189

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

1193
        if (!p)
1,005,461✔
1194
                return false;
1195

1196
        e = strchrnul(p, '/');
1,005,461✔
1197
        if (*e != 0)
1,005,461✔
1198
                return false;
1199

1200
        if (e - p > NAME_MAX) /* NAME_MAX is counted *without* the trailing NUL byte */
992,741✔
1201
                return false;
3✔
1202

1203
        return true;
1204
}
1205

1206
bool filename_is_valid(const char *p) {
1,001,345✔
1207

1208
        if (isempty(p))
1,001,345✔
1209
                return false;
1210

1211
        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
1,001,334✔
1212
                return false;
1213

1214
        return filename_part_is_valid(p);
1,001,321✔
1215
}
1216

1217
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
11,012,199✔
1218
        if (isempty(p))
11,012,199✔
1219
                return false;
1220

1221
        for (const char *e = p;;) {
11,012,191✔
1222
                int r;
34,426,381✔
1223

1224
                r = path_find_first_component(&e, accept_dot_dot, NULL);
34,426,381✔
1225
                if (r < 0)
34,426,381✔
1226
                        return false;
11,012,191✔
1227

1228
                if (e - p >= PATH_MAX) /* Already reached the maximum length for a path? (PATH_MAX is counted
34,426,345✔
1229
                                        * *with* the trailing NUL byte) */
1230
                        return false;
1231
                if (*e == 0)           /* End of string? Yay! */
34,426,342✔
1232
                        return true;
1233
        }
1234
}
1235

1236
bool path_is_normalized(const char *p) {
705,467✔
1237
        if (!path_is_safe(p))
705,467✔
1238
                return false;
1239

1240
        if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
705,442✔
1241
                return false;
1242

1243
        if (strstr(p, "//"))
705,439✔
1244
                return false;
1✔
1245

1246
        return true;
1247
}
1248

1249
int file_in_same_dir(const char *path, const char *filename, char **ret) {
265✔
1250
        _cleanup_free_ char *b = NULL;
265✔
1251
        int r;
265✔
1252

1253
        assert(path);
265✔
1254
        assert(filename);
265✔
1255
        assert(ret);
265✔
1256

1257
        /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1258
         * or the former isn't */
1259

1260
        if (path_is_absolute(filename))
265✔
1261
                b = strdup(filename);
17✔
1262
        else {
1263
                _cleanup_free_ char *dn = NULL;
248✔
1264

1265
                r = path_extract_directory(path, &dn);
248✔
1266
                if (r == -EDESTADDRREQ) /* no path prefix */
248✔
1267
                        b = strdup(filename);
1✔
1268
                else if (r < 0)
247✔
1269
                        return r;
9✔
1270
                else
1271
                        b = path_join(dn, filename);
238✔
1272
        }
1273
        if (!b)
256✔
1274
                return -ENOMEM;
1275

1276
        *ret = TAKE_PTR(b);
256✔
1277
        return 0;
256✔
1278
}
1279

1280
bool hidden_or_backup_file(const char *filename) {
6,697,540✔
1281
        assert(filename);
6,697,540✔
1282

1283
        if (filename[0] == '.' ||
12,471,514✔
1284
            STR_IN_SET(filename,
5,773,974✔
1285
                       "lost+found",
1286
                       "aquota.user",
1287
                       "aquota.group") ||
5,773,971✔
1288
            endswith(filename, "~"))
5,773,971✔
1289
                return true;
923,571✔
1290

1291
        const char *dot = strrchr(filename, '.');
5,773,969✔
1292
        if (!dot)
5,773,969✔
1293
                return false;
1294

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

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

1329
bool is_device_path(const char *path) {
9,486✔
1330

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

1340
        const char *p = PATH_STARTSWITH_SET(ASSERT_PTR(path), "/dev/", "/sys/");
9,486✔
1341
        return !isempty(p);
9,486✔
1342
}
1343

1344
bool valid_device_node_path(const char *path) {
460✔
1345

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

1349
        if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
460✔
1350
                return false;
×
1351

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

1355
        return path_is_normalized(path);
460✔
1356
}
1357

1358
bool valid_device_allow_pattern(const char *path) {
12✔
1359
        assert(path);
12✔
1360

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

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

1367
        return valid_device_node_path(path);
8✔
1368
}
1369

1370
bool dot_or_dot_dot(const char *path) {
6,527,017✔
1371
        if (!path)
6,527,017✔
1372
                return false;
1373
        if (path[0] != '.')
6,527,016✔
1374
                return false;
1375
        if (path[1] == 0)
257,605✔
1376
                return true;
1377
        if (path[1] != '.')
129,302✔
1378
                return false;
1379

1380
        return path[2] == 0;
127,183✔
1381
}
1382

1383
bool path_implies_directory(const char *path) {
28,597✔
1384

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

1388
        if (!path)
28,597✔
1389
                return false;
28,597✔
1390

1391
        if (dot_or_dot_dot(path))
22,610✔
1392
                return true;
1393

1394
        return ENDSWITH_SET(path, "/", "/.", "/..");
22,608✔
1395
}
1396

1397
bool empty_or_root(const char *path) {
11,120,253✔
1398

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

1402
        if (isempty(path))
11,120,253✔
1403
                return true;
1404

1405
        return path_equal(path, "/");
3,640,154✔
1406
}
1407

1408
const char* empty_to_root(const char *path) {
40,546✔
1409
        return isempty(path) ? "/" : path;
43,632✔
1410
}
1411

1412
bool path_strv_contains(char * const *l, const char *path) {
88,779✔
1413
        assert(path);
88,779✔
1414

1415
        STRV_FOREACH(i, l)
1,313,276✔
1416
                if (path_equal(*i, path))
1,239,898✔
1417
                        return true;
1418

1419
        return false;
1420
}
1421

1422
bool prefixed_path_strv_contains(char * const *l, const char *path) {
70✔
1423
        assert(path);
70✔
1424

1425
        STRV_FOREACH(i, l) {
71✔
1426
                const char *j = *i;
1✔
1427

1428
                if (*j == '-')
1✔
1429
                        j++;
×
1430
                if (*j == '+')
1✔
1431
                        j++;
×
1432

1433
                if (path_equal(j, path))
1✔
1434
                        return true;
1435
        }
1436

1437
        return false;
1438
}
1439

1440
int path_glob_can_match(const char *pattern, const char *prefix, char **ret) {
11,027✔
1441
        assert(pattern);
11,027✔
1442
        assert(prefix);
11,027✔
1443

1444
        for (const char *a = pattern, *b = prefix;;) {
11,027✔
1445
                _cleanup_free_ char *g = NULL, *h = NULL;
24,825✔
1446
                const char *p, *q;
33,083✔
1447
                int r, s;
33,083✔
1448

1449
                r = path_find_first_component(&a, /* accept_dot_dot = */ false, &p);
33,083✔
1450
                if (r < 0)
33,083✔
1451
                        return r;
1452

1453
                s = path_find_first_component(&b, /* accept_dot_dot = */ false, &q);
33,083✔
1454
                if (s < 0)
33,083✔
1455
                        return s;
1456

1457
                if (s == 0) {
33,083✔
1458
                        /* The pattern matches the prefix. */
1459
                        if (ret) {
2,769✔
1460
                                char *t;
2,769✔
1461

1462
                                t = path_join(prefix, p);
2,769✔
1463
                                if (!t)
2,769✔
1464
                                        return -ENOMEM;
1465

1466
                                *ret = t;
2,769✔
1467
                        }
1468
                        return true;
2,769✔
1469
                }
1470

1471
                if (r == 0)
30,314✔
1472
                        break;
1473

1474
                if (r == s && strneq(p, q, r))
30,310✔
1475
                        continue; /* common component. Check next. */
19,288✔
1476

1477
                g = strndup(p, r);
11,022✔
1478
                if (!g)
11,022✔
1479
                        return -ENOMEM;
1480

1481
                if (!string_is_glob(g))
11,022✔
1482
                        break;
1483

1484
                /* We found a glob component. Check if the glob pattern matches the prefix component. */
1485

1486
                h = strndup(q, s);
2,768✔
1487
                if (!h)
2,768✔
1488
                        return -ENOMEM;
1489

1490
                r = fnmatch(g, h, 0);
2,768✔
1491
                if (r == FNM_NOMATCH)
2,768✔
1492
                        break;
1493
                if (r != 0) /* Failure to process pattern? */
2,768✔
1494
                        return -EINVAL;
1495
        }
1496

1497
        /* The pattern does not match the prefix. */
1498
        if (ret)
8,258✔
1499
                *ret = NULL;
8,258✔
1500
        return false;
1501
}
1502

1503
const char* default_PATH(void) {
3,390✔
1504
#if HAVE_SPLIT_BIN
1505
        static int split = -1;
1506
        int r;
1507

1508
        /* Check whether /usr/sbin is not a symlink and return the appropriate $PATH.
1509
         * On error fall back to the safe value with both directories as configured… */
1510

1511
        if (split < 0)
1512
                STRV_FOREACH_PAIR(bin, sbin, STRV_MAKE("/usr/bin", "/usr/sbin",
1513
                                                       "/usr/local/bin", "/usr/local/sbin")) {
1514
                        r = inode_same(*bin, *sbin, AT_NO_AUTOMOUNT);
1515
                        if (r > 0 || r == -ENOENT)
1516
                                continue;
1517
                        if (r < 0)
1518
                                log_debug_errno(r, "Failed to compare \"%s\" and \"%s\", using compat $PATH: %m",
1519
                                                *bin, *sbin);
1520
                        split = true;
1521
                        break;
1522
                }
1523
        if (split < 0)
1524
                split = false;
1525
        if (split)
1526
                return DEFAULT_PATH_WITH_SBIN;
1527
#endif
1528
        return DEFAULT_PATH_WITHOUT_SBIN;
3,390✔
1529
}
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