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

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

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

92.64
/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,379✔
26
        if (!p) /* A NULL pointer is definitely not a path */
118,379✔
27
                return false;
28

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

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

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

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

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

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

51
char* path_make_absolute(const char *p, const char *prefix) {
2,272✔
52
        assert(p);
2,272✔
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,272✔
58
                return strdup(p);
312✔
59

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

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

66
        cwd = get_current_dir_name();
5,465✔
67
        if (!cwd)
5,465✔
UNCOV
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,465✔
73
                return -ENOMEDIUM;
74

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

78
        return 0;
79
}
80

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

85
        assert(p);
3,164,842✔
86
        assert(ret);
3,164,842✔
87

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

91
        if (path_is_absolute(p))
3,164,842✔
92
                c = strdup(p);
3,164,565✔
93
        else {
94
                _cleanup_free_ char *cwd = NULL;
277✔
95

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

100
                c = path_join(cwd, p);
277✔
101
        }
102
        if (!c)
3,164,842✔
103
                return -ENOMEM;
104

105
        *ret = c;
3,164,842✔
106
        return 0;
3,164,842✔
107
}
108

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

116
        assert(from);
848,904✔
117
        assert(to);
848,904✔
118
        assert(ret);
848,904✔
119

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

122
        if (!path_is_absolute(from) || !path_is_absolute(to))
848,904✔
123
                return -EINVAL;
124

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

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

134
                if (r == 0) {
6,072,399✔
135
                        /* end of 'from' */
136
                        if (k == 0) {
694,074✔
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);
694,070✔
144
                                if (r < 0)
694,070✔
145
                                        return r;
146

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

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

155
                if (r != k || !strneq(f, t, r))
5,378,325✔
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++) {
192,212✔
163
                /* If this includes ".." we can't do a simple series of "..". */
164
                r = path_find_first_component(&from, false, &f);
347,038✔
165
                if (r < 0)
347,038✔
166
                        return r;
167
                if (r == 0)
347,036✔
168
                        break;
169
        }
170

171
        if (isempty(t) && n_parents * 3 > PATH_MAX)
154,824✔
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));
464,468✔
176
        if (!result)
154,824✔
177
                return -ENOMEM;
178

179
        for (p = result; n_parents > 0; n_parents--)
501,860✔
180
                p = mempcpy(p, "../", 3);
347,036✔
181

182
        if (isempty(t)) {
154,824✔
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);
154,822✔
190

191
        path_simplify(result);
154,822✔
192

193
        if (!path_is_valid(result))
154,822✔
194
                return -EINVAL;
195

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

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

204
        assert(from_child);
154,913✔
205
        assert(to);
154,913✔
206
        assert(ret);
154,913✔
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);
154,913✔
222
        if (r < 0)
154,913✔
223
                return r;
224

225
        return path_make_relative(from, to, ret);
154,912✔
226
}
227

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

231
        STRV_FOREACH(s, strv) {
569,053✔
232
                char *t;
441,605✔
233

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

239
        return NULL;
240
}
241

242
int path_strv_make_absolute_cwd(char **l) {
1,464✔
243
        int r;
1,464✔
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,381✔
250
                char *t;
6,917✔
251

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

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

260
        return 0;
261
}
262

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

268
        if (strv_isempty(l))
31,582✔
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) {
94,127✔
276
                _cleanup_free_ char *orig = NULL;
78,336✔
277
                char *t, *u;
78,336✔
278

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

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

294
                r = chase(t, root, 0, &u, NULL);
78,336✔
295
                if (r == -ENOENT) {
78,336✔
296
                        if (root) {
66,717✔
297
                                u = TAKE_PTR(orig);
2✔
298
                                free(t);
2✔
299
                        } else
300
                                u = t;
66,715✔
301
                } else if (r < 0) {
11,619✔
UNCOV
302
                        free(t);
×
303

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

UNCOV
307
                        continue;
×
308
                } else if (root) {
11,619✔
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✔
UNCOV
321
                                        enomem = true;
×
UNCOV
322
                                        continue;
×
323
                                }
324
                                u = t;
5✔
325
                        } else {
326
                                /* canonicalized path goes outside of
327
                                 * prefix, keep the original path instead */
UNCOV
328
                                free_and_replace(u, orig);
×
329
                        }
330
                } else
331
                        free(t);
11,614✔
332

333
                l[k++] = u;
78,336✔
334
        }
335

336
        l[k] = NULL;
15,791✔
337

338
        if (enomem)
15,791✔
UNCOV
339
                return NULL;
×
340

341
        return l;
342
}
343

344
char** path_strv_resolve_uniq(char **l, const char *root) {
15,790✔
345

346
        if (strv_isempty(l))
15,790✔
347
                return l;
348

349
        if (!path_strv_resolve(l, root))
15,790✔
350
                return NULL;
351

352
        return strv_uniq(l);
15,790✔
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,293,749✔
360
        bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
8,293,749✔
361
        char *f = path;
8,293,749✔
362
        int r;
8,293,749✔
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,293,749✔
374
                return path;
375

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

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

381
        for (const char *p = f;;) {
8,268,564✔
382
                const char *e;
33,590,216✔
383

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

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

392
                beginning = false;
25,321,577✔
393

394
                if (add_slash)
25,321,577✔
395
                        *f++ = '/';
17,060,984✔
396

397
                if (r < 0) {
25,321,577✔
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,321,573✔
404
                f += r;
25,321,573✔
405

406
                add_slash = true;
25,321,573✔
407
        }
408

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

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

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

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

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

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

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

436
char* path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) {
50,850,271✔
437
        assert(path);
50,850,271✔
438
        assert(prefix);
50,850,271✔
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
        if ((path[0] == '/') != (prefix[0] == '/'))
50,850,271✔
451
                return NULL;
452

453
        for (;;) {
44,441,736✔
454
                const char *p, *q;
95,208,191✔
455
                int r, k;
95,208,191✔
456

457
                r = path_find_first_component(&path, accept_dot_dot, &p);
95,208,191✔
458
                if (r < 0)
95,208,191✔
459
                        return NULL;
50,766,455✔
460

461
                k = path_find_first_component(&prefix, accept_dot_dot, &q);
95,208,191✔
462
                if (k < 0)
95,208,191✔
463
                        return NULL;
464

465
                if (k == 0)
95,208,191✔
466
                        return (char*) (p ?: path);
36,884,173✔
467

468
                if (r != k)
58,324,018✔
469
                        return NULL;
470

471
                if (!strneq(p, q, r))
47,265,877✔
472
                        return NULL;
473
        }
474
}
475

476
int path_compare(const char *a, const char *b) {
20,955,758✔
477
        int r;
20,955,758✔
478

479
        /* Order NULL before non-NULL */
480
        r = CMP(!!a, !!b);
20,955,758✔
481
        if (r != 0)
20,934,982✔
482
                return r;
20,779✔
483

484
        /* A relative path and an absolute path must not compare as equal.
485
         * Which one is sorted before the other does not really matter.
486
         * Here a relative path is ordered before an absolute path. */
487
        r = CMP(path_is_absolute(a), path_is_absolute(b));
41,869,956✔
488
        if (r != 0)
20,673,892✔
489
                return r;
302,744✔
490

491
        for (;;) {
17,722,235✔
492
                const char *aa, *bb;
38,354,470✔
493
                int j, k;
38,354,470✔
494

495
                j = path_find_first_component(&a, true, &aa);
38,354,470✔
496
                k = path_find_first_component(&b, true, &bb);
38,354,470✔
497

498
                if (j < 0 || k < 0) {
38,354,470✔
499
                        /* When one of paths is invalid, order invalid path after valid one. */
500
                        r = CMP(j < 0, k < 0);
2✔
UNCOV
501
                        if (r != 0)
×
502
                                return r;
20,632,235✔
503

504
                        /* fallback to use strcmp() if both paths are invalid. */
UNCOV
505
                        return strcmp(a, b);
×
506
                }
507

508
                /* Order prefixes first: "/foo" before "/foo/bar" */
509
                if (j == 0) {
38,354,468✔
510
                        if (k == 0)
3,704,553✔
511
                                return 0;
512
                        return -1;
123,150✔
513
                }
514
                if (k == 0)
34,649,915✔
515
                        return 1;
516

517
                /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
518
                r = memcmp(aa, bb, MIN(j, k));
32,143,509✔
519
                if (r != 0)
32,143,509✔
520
                        return r;
521

522
                /* Sort "/foo/a" before "/foo/aaa" */
523
                r = CMP(j, k);
17,804,730✔
524
                if (r != 0)
17,750,851✔
525
                        return r;
82,495✔
526
        }
527
}
528

529
int path_compare_filename(const char *a, const char *b) {
115,304✔
530
        _cleanup_free_ char *fa = NULL, *fb = NULL;
115,304✔
531
        int r, j, k;
115,304✔
532

533
        /* Order NULL before non-NULL */
534
        r = CMP(!!a, !!b);
115,304✔
535
        if (r != 0)
115,304✔
UNCOV
536
                return r;
×
537

538
        j = path_extract_filename(a, &fa);
115,304✔
539
        k = path_extract_filename(b, &fb);
115,304✔
540

541
        /* When one of paths is "." or root, then order it earlier. */
542
        r = CMP(j != -EADDRNOTAVAIL, k != -EADDRNOTAVAIL);
115,304✔
543
        if (r != 0)
115,300✔
544
                return r;
8✔
545

546
        /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
547
        r = CMP(j < 0, k < 0);
115,296✔
548
        if (r != 0)
115,296✔
UNCOV
549
                return r;
×
550

551
        /* fallback to use strcmp() if both paths are invalid. */
552
        if (j < 0)
115,296✔
553
                return strcmp(a, b);
30✔
554

555
        return strcmp(fa, fb);
115,266✔
556
}
557

558
int path_equal_or_inode_same_full(const char *a, const char *b, int flags) {
51,819✔
559
        /* Returns true if paths are of the same entry, false if not, <0 on error. */
560

561
        if (path_equal(a, b))
51,819✔
562
                return 1;
563

564
        if (!a || !b)
489✔
565
                return 0;
566

567
        return inode_same(a, b, flags);
489✔
568
}
569

570
char* path_extend_internal(char **x, ...) {
23,156,519✔
571
        size_t sz, old_sz;
23,156,519✔
572
        char *q, *nx;
23,156,519✔
573
        const char *p;
23,156,519✔
574
        va_list ap;
23,156,519✔
575
        bool slash;
23,156,519✔
576

577
        /* Joins all listed strings until the sentinel and places a "/" between them unless the strings
578
         * end/begin already with one so that it is unnecessary. Note that slashes which are already
579
         * duplicate won't be removed. The string returned is hence always equal to or longer than the sum of
580
         * the lengths of the individual strings.
581
         *
582
         * The first argument may be an already allocated string that is extended via realloc() if
583
         * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
584
         * first parameter to distinguish the two operations.
585
         *
586
         * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of
587
         * which some are optional.
588
         *
589
         * Examples:
590
         *
591
         * path_join("foo", "bar") → "foo/bar"
592
         * path_join("foo/", "bar") → "foo/bar"
593
         * path_join("", "foo", "", "bar", "") → "foo/bar" */
594

595
        sz = old_sz = x ? strlen_ptr(*x) : 0;
23,156,519✔
596
        va_start(ap, x);
23,156,519✔
597
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
52,226,543✔
598
                size_t add;
29,070,024✔
599

600
                if (isempty(p))
29,070,024✔
601
                        continue;
576,060✔
602

603
                add = 1 + strlen(p);
28,493,964✔
604
                if (sz > SIZE_MAX - add) { /* overflow check */
28,493,964✔
UNCOV
605
                        va_end(ap);
×
UNCOV
606
                        return NULL;
×
607
                }
608

609
                sz += add;
28,493,964✔
610
        }
611
        va_end(ap);
23,156,519✔
612

613
        nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1));
23,156,519✔
614
        if (!nx)
23,156,519✔
615
                return NULL;
616
        if (x)
23,156,519✔
617
                *x = nx;
17,553,733✔
618

619
        if (old_sz > 0)
23,156,519✔
620
                slash = nx[old_sz-1] == '/';
17,481,899✔
621
        else {
622
                nx[old_sz] = 0;
5,674,620✔
623
                slash = true; /* no need to generate a slash anymore */
5,674,620✔
624
        }
625

626
        q = nx + old_sz;
23,156,519✔
627

628
        va_start(ap, x);
23,156,519✔
629
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
52,226,543✔
630
                if (isempty(p))
29,070,024✔
631
                        continue;
576,060✔
632

633
                if (!slash && p[0] != '/')
28,493,964✔
634
                        *(q++) = '/';
17,693,232✔
635

636
                q = stpcpy(q, p);
28,493,964✔
637
                slash = endswith(p, "/");
28,493,964✔
638
        }
639
        va_end(ap);
23,156,519✔
640

641
        return nx;
23,156,519✔
642
}
643

644
int open_and_check_executable(const char *name, const char *root, char **ret_path, int *ret_fd) {
27,528✔
645
        _cleanup_close_ int fd = -EBADF;
27,528✔
646
        _cleanup_free_ char *resolved = NULL;
27,528✔
647
        int r;
27,528✔
648

649
        assert(name);
27,528✔
650

651
        /* Function chase() is invoked only when root is not NULL, as using it regardless of
652
         * root value would alter the behavior of existing callers for example: /bin/sleep would become
653
         * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
654
         * needed to avoid unforeseen regression or other complicated changes. */
655
        if (root) {
27,528✔
656
                /* prefix root to name in case full paths are not specified */
657
                r = chase(name, root, CHASE_PREFIX_ROOT, &resolved, &fd);
4✔
658
                if (r < 0)
4✔
659
                        return r;
660

661
                name = resolved;
4✔
662
        } else {
663
                /* We need to use O_PATH because there may be executables for which we have only exec permissions,
664
                 * but not read (usually suid executables). */
665
                fd = open(name, O_PATH|O_CLOEXEC);
27,524✔
666
                if (fd < 0)
27,524✔
667
                        return -errno;
4,291✔
668
        }
669

670
        r = fd_verify_regular(fd);
23,237✔
671
        if (r < 0)
23,237✔
672
                return r;
673

674
        r = access_fd(fd, X_OK);
23,234✔
675
        if (r == -ENOSYS)
23,234✔
676
                /* /proc/ is not mounted. Fall back to access(). */
UNCOV
677
                r = RET_NERRNO(access(name, X_OK));
×
678
        if (r < 0)
23,234✔
679
                return r;
680

681
        if (ret_path) {
23,233✔
682
                if (resolved)
22,879✔
UNCOV
683
                        *ret_path = TAKE_PTR(resolved);
×
684
                else {
685
                        r = path_make_absolute_cwd(name, ret_path);
22,879✔
686
                        if (r < 0)
22,879✔
687
                                return r;
688

689
                        path_simplify(*ret_path);
22,879✔
690
                }
691
        }
692

693
        if (ret_fd)
23,233✔
694
                *ret_fd = TAKE_FD(fd);
21,597✔
695

696
        return 0;
697
}
698

699
int find_executable_full(
23,243✔
700
                const char *name,
701
                const char *root,
702
                char * const *exec_search_path,
703
                bool use_path_envvar,
704
                char **ret_filename,
705
                int *ret_fd) {
706

707
        int last_error = -ENOENT, r = 0;
23,243✔
708

709
        assert(name);
23,243✔
710

711
        if (is_path(name))
23,243✔
712
                return open_and_check_executable(name, root, ret_filename, ret_fd);
23,243✔
713

714
        if (exec_search_path) {
3,347✔
715
                STRV_FOREACH(element, exec_search_path) {
6✔
716
                        _cleanup_free_ char *full_path = NULL;
5✔
717

718
                        if (!path_is_absolute(*element)) {
5✔
UNCOV
719
                                log_debug("Exec search path '%s' isn't absolute, ignoring.", *element);
×
UNCOV
720
                                continue;
×
721
                        }
722

723
                        full_path = path_join(*element, name);
5✔
724
                        if (!full_path)
5✔
725
                                return -ENOMEM;
726

727
                        r = open_and_check_executable(full_path, root, ret_filename, ret_fd);
5✔
728
                        if (r >= 0)
5✔
729
                                return 0;
730
                        if (r != -EACCES)
4✔
731
                                last_error = r;
4✔
732
                }
733
                return last_error;
734
        }
735

736
        const char *p = NULL;
3,345✔
737

738
        if (use_path_envvar)
3,345✔
739
                /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
740
                 * binary. */
741
                p = getenv("PATH");
947✔
742
        if (!p)
3,345✔
743
                p = default_PATH();
2,408✔
744

745
        /* Resolve a single-component name to a full path */
746
        for (;;) {
7,631✔
747
                _cleanup_free_ char *element = NULL;
7,627✔
748

749
                r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
7,631✔
750
                if (r < 0)
7,631✔
751
                        return r;
752
                if (r == 0)
7,631✔
753
                        break;
754

755
                if (!path_is_absolute(element)) {
7,627✔
756
                        log_debug("Exec search path '%s' isn't absolute, ignoring.", element);
×
757
                        continue;
×
758
                }
759

760
                if (!path_extend(&element, name))
7,627✔
761
                        return -ENOMEM;
762

763
                r = open_and_check_executable(element, root, ret_filename, ret_fd);
7,627✔
764
                if (r >= 0) /* Found it! */
7,627✔
765
                        return 0;
766
                /* PATH entries which we don't have access to are ignored, as per tradition. */
767
                if (r != -EACCES)
4,286✔
768
                        last_error = r;
4,286✔
769
        }
770

771
        return last_error;
4✔
772
}
773

774
bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
×
UNCOV
775
        bool changed = false, originally_unset;
×
776

UNCOV
777
        assert(timestamp);
×
778

UNCOV
779
        if (!paths)
×
780
                return false;
781

UNCOV
782
        originally_unset = *timestamp == 0;
×
783

UNCOV
784
        STRV_FOREACH(i, paths) {
×
UNCOV
785
                struct stat stats;
×
UNCOV
786
                usec_t u;
×
787

UNCOV
788
                if (stat(*i, &stats) < 0)
×
UNCOV
789
                        continue;
×
790

UNCOV
791
                u = timespec_load(&stats.st_mtim);
×
792

793
                /* check first */
UNCOV
794
                if (*timestamp >= u)
×
UNCOV
795
                        continue;
×
796

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

799
                /* update timestamp */
UNCOV
800
                if (update) {
×
UNCOV
801
                        *timestamp = u;
×
UNCOV
802
                        changed = true;
×
803
                } else
UNCOV
804
                        return true;
×
805
        }
806

807
        return changed;
808
}
809

810
static int executable_is_good(const char *executable) {
186✔
811
        _cleanup_free_ char *p = NULL, *d = NULL;
186✔
812
        int r;
186✔
813

814
        r = find_executable(executable, &p);
186✔
815
        if (r == -ENOENT)
186✔
816
                return 0;
817
        if (r < 0)
184✔
818
                return r;
819

820
        /* An fsck that is linked to /bin/true is a non-existent fsck */
821

822
        r = readlink_malloc(p, &d);
184✔
823
        if (r == -EINVAL) /* not a symlink */
184✔
824
                return 1;
825
        if (r < 0)
64✔
826
                return r;
827

828
        return !PATH_IN_SET(d, "true"
64✔
829
                               "/bin/true",
830
                               "/usr/bin/true",
831
                               "/dev/null");
832
}
833

834
int fsck_exists(void) {
100✔
835
        return executable_is_good("fsck");
100✔
836
}
837

838
int fsck_exists_for_fstype(const char *fstype) {
86✔
839
        const char *checker;
86✔
840
        int r;
86✔
841

842
        assert(fstype);
86✔
843

844
        if (streq(fstype, "auto"))
86✔
845
                return -EINVAL;
846

847
        r = fsck_exists();
86✔
848
        if (r <= 0)
86✔
849
                return r;
850

851
        checker = strjoina("fsck.", fstype);
430✔
852
        return executable_is_good(checker);
86✔
853
}
854

855
static const char* skip_slash_or_dot(const char *p) {
895,513,471✔
856
        for (; !isempty(p); p++) {
1,382,269,654✔
857
                if (*p == '/')
1,196,597,397✔
858
                        continue;
486,755,388✔
859
                if (startswith(p, "./")) {
709,842,009✔
860
                        p++;
795✔
861
                        continue;
795✔
862
                }
863
                break;
864
        }
865
        return p;
895,513,471✔
866
}
867

868
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret) {
488,082,999✔
869
        const char *q, *first, *end_first, *next;
488,082,999✔
870
        size_t len;
488,082,999✔
871

872
        assert(p);
488,082,999✔
873

874
        /* When a path is input, then returns the pointer to the first component and its length, and
875
         * move the input pointer to the next component or nul. This skips both over any '/'
876
         * immediately *before* and *after* the first component before returning.
877
         *
878
         * Examples
879
         *   Input:  p: "//.//aaa///bbbbb/cc"
880
         *   Output: p: "bbbbb///cc"
881
         *           ret: "aaa///bbbbb/cc"
882
         *           return value: 3 (== strlen("aaa"))
883
         *
884
         *   Input:  p: "aaa//"
885
         *   Output: p: (pointer to NUL)
886
         *           ret: "aaa//"
887
         *           return value: 3 (== strlen("aaa"))
888
         *
889
         *   Input:  p: "/", ".", ""
890
         *   Output: p: (pointer to NUL)
891
         *           ret: NULL
892
         *           return value: 0
893
         *
894
         *   Input:  p: NULL
895
         *   Output: p: NULL
896
         *           ret: NULL
897
         *           return value: 0
898
         *
899
         *   Input:  p: "(too long component)"
900
         *   Output: return value: -EINVAL
901
         *
902
         *   (when accept_dot_dot is false)
903
         *   Input:  p: "//..//aaa///bbbbb/cc"
904
         *   Output: return value: -EINVAL
905
         */
906

907
        q = *p;
488,082,999✔
908

909
        first = skip_slash_or_dot(q);
488,082,999✔
910
        if (isempty(first)) {
488,082,999✔
911
                *p = first;
80,644,266✔
912
                if (ret)
80,644,266✔
913
                        *ret = NULL;
80,639,070✔
914
                return 0;
80,644,266✔
915
        }
916
        if (streq(first, ".")) {
407,438,733✔
917
                *p = first + 1;
8,072✔
918
                if (ret)
8,072✔
919
                        *ret = NULL;
8,015✔
920
                return 0;
8,072✔
921
        }
922

923
        end_first = strchrnul(first, '/');
407,430,661✔
924
        len = end_first - first;
407,430,661✔
925

926
        if (len > NAME_MAX)
407,430,661✔
927
                return -EINVAL;
928
        if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.')
407,430,509✔
929
                return -EINVAL;
930

931
        next = skip_slash_or_dot(end_first);
407,430,472✔
932

933
        *p = next + streq(next, ".");
407,430,472✔
934
        if (ret)
407,430,472✔
935
                *ret = first;
373,540,921✔
936
        return len;
407,430,472✔
937
}
938

939
static const char* skip_slash_or_dot_backward(const char *path, const char *q) {
14,288,913✔
940
        assert(path);
14,288,913✔
941
        assert(!q || q >= path);
14,288,913✔
942

943
        for (; q; q = PTR_SUB1(q, path)) {
29,145,651✔
944
                if (*q == '/')
21,507,285✔
945
                        continue;
7,457,458✔
946
                if (q > path && strneq(q - 1, "/.", 2))
14,049,827✔
947
                        continue;
264✔
948
                if (q == path && *q == '.')
14,049,563✔
949
                        continue;
80✔
950
                break;
951
        }
952
        return q;
14,288,913✔
953
}
954

955
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) {
7,151,838✔
956
        const char *q, *last_end, *last_begin;
7,151,838✔
957
        size_t len;
7,151,838✔
958

959
        /* Similar to path_find_first_component(), but search components from the end.
960
        *
961
        * Examples
962
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
963
        *           next: NULL
964
        *   Output: next: "/cc//././"
965
        *           ret: "cc//././"
966
        *           return value: 2 (== strlen("cc"))
967
        *
968
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
969
        *           next: "/cc//././"
970
        *   Output: next: "///bbbbb/cc//././"
971
        *           ret: "bbbbb/cc//././"
972
        *           return value: 5 (== strlen("bbbbb"))
973
        *
974
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
975
        *           next: "///bbbbb/cc//././"
976
        *   Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
977
        *           ret: "aaa///bbbbb/cc//././"
978
        *           return value: 3 (== strlen("aaa"))
979
        *
980
        *   Input:  path: "/", ".", "", or NULL
981
        *   Output: next: equivalent to path
982
        *           ret: NULL
983
        *           return value: 0
984
        *
985
        *   Input:  path: "(too long component)"
986
        *   Output: return value: -EINVAL
987
        *
988
        *   (when accept_dot_dot is false)
989
        *   Input:  path: "//..//aaa///bbbbb/cc/..//"
990
        *   Output: return value: -EINVAL
991
        */
992

993
        if (isempty(path)) {
7,151,838✔
994
                if (next)
9✔
995
                        *next = path;
9✔
996
                if (ret)
9✔
997
                        *ret = NULL;
9✔
998
                return 0;
9✔
999
        }
1000

1001
        if (next && *next) {
7,151,829✔
1002
                if (*next < path || *next > path + strlen(path))
35,308✔
1003
                        return -EINVAL;
1004
                if (*next == path) {
35,308✔
1005
                        if (ret)
9✔
1006
                                *ret = NULL;
9✔
1007
                        return 0;
9✔
1008
                }
1009
                if (!IN_SET(**next, '\0', '/'))
35,299✔
1010
                        return -EINVAL;
1011
                q = *next - 1;
35,299✔
1012
        } else
1013
                q = path + strlen(path) - 1;
7,116,521✔
1014

1015
        q = skip_slash_or_dot_backward(path, q);
7,151,820✔
1016
        if (!q || /* the root directory */
7,151,820✔
1017
            (q == path && *q == '.')) { /* path is "." or "./" */
34✔
1018
                if (next)
14,656✔
1019
                        *next = path;
14,656✔
1020
                if (ret)
14,656✔
1021
                        *ret = NULL;
14,653✔
1022
                return 0;
14,656✔
1023
        }
1024

1025
        last_end = q + 1;
7,137,164✔
1026

1027
        while (q && *q != '/')
116,144,138✔
1028
                q = PTR_SUB1(q, path);
101,869,810✔
1029

1030
        last_begin = q ? q + 1 : path;
7,137,164✔
1031
        len = last_end - last_begin;
7,137,164✔
1032

1033
        if (len > NAME_MAX)
7,137,164✔
1034
                return -EINVAL;
1035
        if (!accept_dot_dot && len == 2 && strneq(last_begin, "..", 2))
7,137,160✔
1036
                return -EINVAL;
1037

1038
        if (next) {
7,137,135✔
1039
                q = skip_slash_or_dot_backward(path, q);
7,137,093✔
1040
                *next = q ? q + 1 : path;
7,137,093✔
1041
        }
1042

1043
        if (ret)
7,137,135✔
1044
                *ret = last_begin;
6,198,955✔
1045
        return len;
7,137,135✔
1046
}
1047

1048
const char* last_path_component(const char *path) {
156,479✔
1049

1050
        /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1051
         *
1052
         *    a/b/c → c
1053
         *    a/b/c/ → c/
1054
         *    x → x
1055
         *    x/ → x/
1056
         *    /y → y
1057
         *    /y/ → y/
1058
         *    / → /
1059
         *    // → /
1060
         *    /foo/a → a
1061
         *    /foo/a/ → a/
1062
         *
1063
         *    Also, the empty string is mapped to itself.
1064
         *
1065
         * This is different than basename(), which returns "" when a trailing slash is present.
1066
         *
1067
         * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1068
         */
1069

1070
        unsigned l, k;
156,479✔
1071

1072
        if (!path)
156,479✔
1073
                return NULL;
1074

1075
        l = k = strlen(path);
156,478✔
1076
        if (l == 0) /* special case — an empty string */
156,478✔
1077
                return path;
1078

1079
        while (k > 0 && path[k-1] == '/')
156,490✔
1080
                k--;
1081

1082
        if (k == 0) /* the root directory */
156,477✔
1083
                return path + l - 1;
3✔
1084

1085
        while (k > 0 && path[k-1] != '/')
1,348,544✔
1086
                k--;
1087

1088
        return path + k;
156,474✔
1089
}
1090

1091
int path_extract_filename(const char *path, char **ret) {
2,035,231✔
1092
        _cleanup_free_ char *a = NULL;
4,070,462✔
1093
        const char *c, *next = NULL;
2,035,231✔
1094
        int r;
2,035,231✔
1095

1096
        /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
1097
         * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing
1098
         * slashes. Returns:
1099
         *
1100
         * -EINVAL        → if the path is not valid
1101
         * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir
1102
         *                  itself or "." is specified
1103
         * -ENOMEM        → no memory
1104
         *
1105
         * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1106
         * indicate the referenced file must be a directory.
1107
         *
1108
         * This function guarantees to return a fully valid filename, i.e. one that passes
1109
         * filename_is_valid() – this means "." and ".." are not accepted. */
1110

1111
        if (!path_is_valid(path))
2,035,231✔
1112
                return -EINVAL;
1113

1114
        r = path_find_last_component(path, false, &next, &c);
2,035,229✔
1115
        if (r < 0)
2,035,229✔
1116
                return r;
1117
        if (r == 0) /* root directory */
2,035,215✔
1118
                return -EADDRNOTAVAIL;
1119

1120
        a = strndup(c, r);
2,035,133✔
1121
        if (!a)
2,035,133✔
1122
                return -ENOMEM;
1123

1124
        *ret = TAKE_PTR(a);
2,035,133✔
1125
        return strlen(c) > (size_t) r ? O_DIRECTORY : 0;
2,035,133✔
1126
}
1127

1128
int path_extract_directory(const char *path, char **ret) {
4,143,044✔
1129
        const char *c, *next = NULL;
4,143,044✔
1130
        int r;
4,143,044✔
1131

1132
        /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns:
1133
         *
1134
         * -EINVAL        → if the path is not valid
1135
         * -EDESTADDRREQ  → if no directory was specified in the passed in path, i.e. only a filename was passed
1136
         * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e.
1137
         *                   the root dir itself or "." was specified
1138
         * -ENOMEM        → no memory (surprise!)
1139
         *
1140
         * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid().
1141
         */
1142

1143
        r = path_find_last_component(path, false, &next, &c);
4,143,044✔
1144
        if (r < 0)
4,143,044✔
1145
                return r;
4,143,044✔
1146
        if (r == 0) /* empty or root */
4,143,038✔
1147
                return isempty(path) ? -EINVAL : -EADDRNOTAVAIL;
29,131✔
1148
        if (next == path) {
4,128,472✔
1149
                if (*path != '/') /* filename only */
26,662✔
1150
                        return -EDESTADDRREQ;
1151

1152
                return strdup_to(ret, "/");
19,606✔
1153
        }
1154

1155
        _cleanup_free_ char *a = strndup(path, next - path);
4,101,810✔
1156
        if (!a)
4,101,810✔
1157
                return -ENOMEM;
1158

1159
        path_simplify(a);
4,101,810✔
1160

1161
        if (!path_is_valid(a))
4,101,810✔
1162
                return -EINVAL;
1163

1164
        if (ret)
4,101,810✔
1165
                *ret = TAKE_PTR(a);
4,101,810✔
1166

1167
        return 0;
1168
}
1169

1170
bool filename_part_is_valid(const char *p) {
994,031✔
1171
        const char *e;
994,031✔
1172

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

1176
        if (!p)
994,031✔
1177
                return false;
1178

1179
        e = strchrnul(p, '/');
994,031✔
1180
        if (*e != 0)
994,031✔
1181
                return false;
1182

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

1186
        return true;
1187
}
1188

1189
bool filename_is_valid(const char *p) {
989,963✔
1190

1191
        if (isempty(p))
989,963✔
1192
                return false;
1193

1194
        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
989,952✔
1195
                return false;
1196

1197
        return filename_part_is_valid(p);
989,939✔
1198
}
1199

1200
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
10,841,236✔
1201
        if (isempty(p))
10,841,236✔
1202
                return false;
1203

1204
        for (const char *e = p;;) {
10,841,229✔
1205
                int r;
33,894,840✔
1206

1207
                r = path_find_first_component(&e, accept_dot_dot, NULL);
33,894,840✔
1208
                if (r < 0)
33,894,840✔
1209
                        return false;
10,841,229✔
1210

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

1219
bool path_is_normalized(const char *p) {
700,088✔
1220
        if (!path_is_safe(p))
700,088✔
1221
                return false;
1222

1223
        if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
700,063✔
1224
                return false;
1225

1226
        if (strstr(p, "//"))
700,060✔
1227
                return false;
1✔
1228

1229
        return true;
1230
}
1231

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

1236
        assert(path);
265✔
1237
        assert(filename);
265✔
1238
        assert(ret);
265✔
1239

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

1243
        if (path_is_absolute(filename))
265✔
1244
                b = strdup(filename);
17✔
1245
        else {
1246
                _cleanup_free_ char *dn = NULL;
248✔
1247

1248
                r = path_extract_directory(path, &dn);
248✔
1249
                if (r == -EDESTADDRREQ) /* no path prefix */
248✔
1250
                        b = strdup(filename);
1✔
1251
                else if (r < 0)
247✔
1252
                        return r;
9✔
1253
                else
1254
                        b = path_join(dn, filename);
238✔
1255
        }
1256
        if (!b)
256✔
1257
                return -ENOMEM;
1258

1259
        *ret = TAKE_PTR(b);
256✔
1260
        return 0;
256✔
1261
}
1262

1263
bool hidden_or_backup_file(const char *filename) {
6,754,230✔
1264
        assert(filename);
6,754,230✔
1265

1266
        if (filename[0] == '.' ||
12,594,371✔
1267
            STR_IN_SET(filename,
5,840,141✔
1268
                       "lost+found",
1269
                       "aquota.user",
1270
                       "aquota.group") ||
5,840,138✔
1271
            endswith(filename, "~"))
5,840,138✔
1272
                return true;
914,094✔
1273

1274
        const char *dot = strrchr(filename, '.');
5,840,136✔
1275
        if (!dot)
5,840,136✔
1276
                return false;
1277

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

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

1312
bool is_device_path(const char *path) {
15,941✔
1313

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

1323
        const char *p = PATH_STARTSWITH_SET(ASSERT_PTR(path), "/dev/", "/sys/");
15,941✔
1324
        return !isempty(p);
15,941✔
1325
}
1326

1327
bool valid_device_node_path(const char *path) {
460✔
1328

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

1332
        if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
460✔
UNCOV
1333
                return false;
×
1334

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

1338
        return path_is_normalized(path);
460✔
1339
}
1340

1341
bool valid_device_allow_pattern(const char *path) {
12✔
1342
        assert(path);
12✔
1343

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

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

1350
        return valid_device_node_path(path);
8✔
1351
}
1352

1353
bool dot_or_dot_dot(const char *path) {
6,447,234✔
1354
        if (!path)
6,447,234✔
1355
                return false;
1356
        if (path[0] != '.')
6,447,233✔
1357
                return false;
1358
        if (path[1] == 0)
264,741✔
1359
                return true;
1360
        if (path[1] != '.')
132,872✔
1361
                return false;
1362

1363
        return path[2] == 0;
130,743✔
1364
}
1365

1366
bool path_implies_directory(const char *path) {
19,972✔
1367

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

1371
        if (!path)
19,972✔
1372
                return false;
19,972✔
1373

1374
        if (dot_or_dot_dot(path))
18,188✔
1375
                return true;
1376

1377
        return ENDSWITH_SET(path, "/", "/.", "/..");
18,186✔
1378
}
1379

1380
bool empty_or_root(const char *path) {
10,949,553✔
1381

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

1385
        if (isempty(path))
10,949,553✔
1386
                return true;
1387

1388
        return path_equal(path, "/");
3,597,370✔
1389
}
1390

1391
const char* empty_to_root(const char *path) {
40,271✔
1392
        return isempty(path) ? "/" : path;
43,353✔
1393
}
1394

1395
bool path_strv_contains(char * const *l, const char *path) {
86,072✔
1396
        assert(path);
86,072✔
1397

1398
        STRV_FOREACH(i, l)
1,243,964✔
1399
                if (path_equal(*i, path))
1,172,715✔
1400
                        return true;
1401

1402
        return false;
1403
}
1404

1405
bool prefixed_path_strv_contains(char * const *l, const char *path) {
70✔
1406
        assert(path);
70✔
1407

1408
        STRV_FOREACH(i, l) {
71✔
1409
                const char *j = *i;
1✔
1410

1411
                if (*j == '-')
1✔
UNCOV
1412
                        j++;
×
1413
                if (*j == '+')
1✔
UNCOV
1414
                        j++;
×
1415

1416
                if (path_equal(j, path))
1✔
1417
                        return true;
1418
        }
1419

1420
        return false;
1421
}
1422

1423
int path_glob_can_match(const char *pattern, const char *prefix, char **ret) {
11,003✔
1424
        assert(pattern);
11,003✔
1425
        assert(prefix);
11,003✔
1426

1427
        for (const char *a = pattern, *b = prefix;;) {
11,003✔
1428
                _cleanup_free_ char *g = NULL, *h = NULL;
24,771✔
1429
                const char *p, *q;
33,011✔
1430
                int r, s;
33,011✔
1431

1432
                r = path_find_first_component(&a, /* accept_dot_dot = */ false, &p);
33,011✔
1433
                if (r < 0)
33,011✔
1434
                        return r;
1435

1436
                s = path_find_first_component(&b, /* accept_dot_dot = */ false, &q);
33,011✔
1437
                if (s < 0)
33,011✔
1438
                        return s;
1439

1440
                if (s == 0) {
33,011✔
1441
                        /* The pattern matches the prefix. */
1442
                        if (ret) {
2,763✔
1443
                                char *t;
2,763✔
1444

1445
                                t = path_join(prefix, p);
2,763✔
1446
                                if (!t)
2,763✔
1447
                                        return -ENOMEM;
1448

1449
                                *ret = t;
2,763✔
1450
                        }
1451
                        return true;
2,763✔
1452
                }
1453

1454
                if (r == 0)
30,248✔
1455
                        break;
1456

1457
                if (r == s && strneq(p, q, r))
30,244✔
1458
                        continue; /* common component. Check next. */
19,246✔
1459

1460
                g = strndup(p, r);
10,998✔
1461
                if (!g)
10,998✔
1462
                        return -ENOMEM;
1463

1464
                if (!string_is_glob(g))
10,998✔
1465
                        break;
1466

1467
                /* We found a glob component. Check if the glob pattern matches the prefix component. */
1468

1469
                h = strndup(q, s);
2,762✔
1470
                if (!h)
2,762✔
1471
                        return -ENOMEM;
1472

1473
                r = fnmatch(g, h, 0);
2,762✔
1474
                if (r == FNM_NOMATCH)
2,762✔
1475
                        break;
1476
                if (r != 0) /* Failure to process pattern? */
2,762✔
1477
                        return -EINVAL;
1478
        }
1479

1480
        /* The pattern does not match the prefix. */
1481
        if (ret)
8,240✔
1482
                *ret = NULL;
8,240✔
1483
        return false;
1484
}
1485

1486
const char* default_PATH(void) {
3,372✔
1487
#if HAVE_SPLIT_BIN
1488
        static int split = -1;
1489
        int r;
1490

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

1494
        if (split < 0)
1495
                STRV_FOREACH_PAIR(bin, sbin, STRV_MAKE("/usr/bin", "/usr/sbin",
1496
                                                       "/usr/local/bin", "/usr/local/sbin")) {
1497
                        r = inode_same(*bin, *sbin, AT_NO_AUTOMOUNT);
1498
                        if (r > 0 || r == -ENOENT)
1499
                                continue;
1500
                        if (r < 0)
1501
                                log_debug_errno(r, "Failed to compare \"%s\" and \"%s\", using compat $PATH: %m",
1502
                                                *bin, *sbin);
1503
                        split = true;
1504
                        break;
1505
                }
1506
        if (split < 0)
1507
                split = false;
1508
        if (split)
1509
                return DEFAULT_PATH_WITH_SBIN;
1510
#endif
1511
        return DEFAULT_PATH_WITHOUT_SBIN;
3,372✔
1512
}
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