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

systemd / systemd / 24373971178

14 Apr 2026 12:08AM UTC coverage: 72.291% (+0.2%) from 72.107%
24373971178

push

github

web-flow
hwdb: Add extended SteelSeries Arctis headset device support (#41628)

Add USB device IDs for additional SteelSeries Arctis headset models to
the sound card hardware database.

Newly added device IDs:

- Arctis Nova 7x v2 (22AD)
- Arctis Nova 7 Diablo IV (22A9)
- Arctis Nova 7X (22A4)
- Arctis Nova 7X (22A5)
- Arctis Nova 7P V2 (22A7)

321138 of 444232 relevant lines covered (72.29%)

1277257.29 hits per line

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

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

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

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

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

25
        return strchr(p, '/');
163,029✔
26
}
27

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

32
        assert(p);
1,322✔
33
        assert(ret);
1,322✔
34

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

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

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

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

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

53
        if (path_is_absolute(p) || isempty(prefix))
2,568✔
54
                return strdup(p);
388✔
55

56
        return path_join(prefix, p);
2,180✔
57
}
58

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

62
        cwd = get_current_dir_name();
382✔
63
        if (!cwd)
382✔
64
                return negative_errno();
×
65

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

71
        if (ret)
382✔
72
                *ret = TAKE_PTR(cwd);
382✔
73

74
        return 0;
75
}
76

77
int path_make_absolute_cwd(const char *p, char **ret) {
3,340,088✔
78
        char *c;
3,340,088✔
79
        int r;
3,340,088✔
80

81
        assert(p);
3,340,088✔
82
        assert(ret);
3,340,088✔
83

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

87
        if (path_is_absolute(p))
3,340,088✔
88
                c = strdup(p);
3,339,799✔
89
        else {
90
                _cleanup_free_ char *cwd = NULL;
289✔
91

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

96
                c = path_join(cwd, p);
289✔
97
        }
98
        if (!c)
3,340,088✔
99
                return -ENOMEM;
100

101
        *ret = c;
3,340,088✔
102
        return 0;
3,340,088✔
103
}
104

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

112
        assert(from);
866,157✔
113
        assert(to);
866,157✔
114
        assert(ret);
866,157✔
115

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

118
        if (!path_is_absolute(from) || !path_is_absolute(to))
1,732,310✔
119
                return -EINVAL;
120

121
        for (;;) {
6,108,687✔
122
                r = path_find_first_component(&from, true, &f);
6,108,687✔
123
                if (r < 0)
6,108,687✔
124
                        return r;
125

126
                k = path_find_first_component(&to, true, &t);
6,108,687✔
127
                if (k < 0)
6,108,687✔
128
                        return k;
129

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

143
                                if (!path_is_valid(result))
731,371✔
144
                                        return -EINVAL;
145
                        }
146

147
                        *ret = TAKE_PTR(result);
731,375✔
148
                        return 0;
731,375✔
149
                }
150

151
                if (r != k || !strneq(f, t, r))
5,377,312✔
152
                        break;
153
        }
154

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

158
        for (n_parents = 1;; n_parents++) {
141,327✔
159
                /* If this includes ".." we can't do a simple series of "..". */
160
                r = path_find_first_component(&from, false, &f);
276,105✔
161
                if (r < 0)
276,105✔
162
                        return r;
163
                if (r == 0)
276,103✔
164
                        break;
165
        }
166

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

171
        result = new(char, n_parents * 3 + !isempty(t) + strlen_ptr(t));
404,324✔
172
        if (!result)
134,776✔
173
                return -ENOMEM;
174

175
        for (p = result; n_parents > 0; n_parents--)
410,879✔
176
                p = mempcpy(p, "../", 3);
276,103✔
177

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

185
        strcpy(p, t);
134,774✔
186

187
        path_simplify(result);
134,774✔
188

189
        if (!path_is_valid(result))
134,774✔
190
                return -EINVAL;
191

192
        *ret = TAKE_PTR(result);
134,774✔
193
        return 0;
134,774✔
194
}
195

196
int path_make_relative_parent(const char *from_child, const char *to, char **ret) {
134,851✔
197
        _cleanup_free_ char *from = NULL;
134,851✔
198
        int r;
134,851✔
199

200
        assert(from_child);
134,851✔
201
        assert(to);
134,851✔
202
        assert(ret);
134,851✔
203

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

217
        r = path_extract_directory(from_child, &from);
134,851✔
218
        if (r < 0)
134,851✔
219
                return r;
220

221
        return path_make_relative(from, to, ret);
134,850✔
222
}
223

224
char* path_startswith_strv(const char *p, char * const *strv) {
199,407✔
225
        assert(p);
199,407✔
226

227
        STRV_FOREACH(s, strv) {
770,495✔
228
                char *t;
606,239✔
229

230
                t = path_startswith(p, *s);
606,239✔
231
                if (t)
606,239✔
232
                        return t;
233
        }
234

235
        return NULL;
236
}
237

238
int path_strv_make_absolute_cwd(char **l) {
68,937✔
239
        int r;
68,937✔
240

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

245
        STRV_FOREACH(s, l) {
525,847✔
246
                char *t;
456,910✔
247

248
                r = path_make_absolute_cwd(*s, &t);
456,910✔
249
                if (r < 0)
456,910✔
250
                        return r;
×
251

252
                path_simplify(t);
456,910✔
253
                free_and_replace(*s, t);
456,910✔
254
        }
255

256
        return 0;
257
}
258

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

264
        if (strv_isempty(l))
55,362✔
265
                return l;
266

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

271
        STRV_FOREACH(s, l) {
165,293✔
272
                _cleanup_free_ char *orig = NULL;
137,612✔
273
                char *t, *u;
137,612✔
274

275
                if (!path_is_absolute(*s)) {
137,612✔
276
                        free(*s);
×
277
                        continue;
×
278
                }
279

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

290
                r = chase(t, root, 0, &u, NULL);
137,612✔
291
                if (r == -ENOENT) {
137,612✔
292
                        if (root) {
115,839✔
293
                                u = TAKE_PTR(orig);
2✔
294
                                free(t);
2✔
295
                        } else
296
                                u = t;
115,837✔
297
                } else if (r < 0) {
21,773✔
298
                        free(t);
×
299

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

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

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

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

329
                l[k++] = u;
137,612✔
330
        }
331

332
        l[k] = NULL;
27,681✔
333

334
        if (enomem)
27,681✔
335
                return NULL;
×
336

337
        return l;
338
}
339

340
char** path_strv_resolve_uniq(char **l, const char *root) {
27,680✔
341

342
        if (strv_isempty(l))
27,680✔
343
                return l;
344

345
        if (!path_strv_resolve(l, root))
27,680✔
346
                return NULL;
347

348
        return strv_uniq(l);
27,680✔
349
}
350

351
char* skip_leading_slash(const char *p) {
118,284✔
352
        return skip_leading_chars(p, "/");
118,284✔
353
}
354

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

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

369
        if (isempty(path))
9,336,304✔
370
                return path;
371

372
        keep_trailing_slash = FLAGS_SET(flags, PATH_SIMPLIFY_KEEP_TRAILING_SLASH) && endswith(path, "/");
9,300,709✔
373

374
        absolute = path_is_absolute(path);
9,300,709✔
375
        f += absolute;  /* Keep leading /, if present. */
9,300,709✔
376

377
        for (const char *p = f;;) {
9,300,709✔
378
                const char *e;
38,404,041✔
379

380
                r = path_find_first_component(&p, true, &e);
38,404,041✔
381
                if (r == 0)
38,404,041✔
382
                        break;
383

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

388
                beginning = false;
29,103,257✔
389

390
                if (add_slash)
29,103,257✔
391
                        *f++ = '/';
19,813,267✔
392

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

399
                memmove(f, e, r);
29,103,253✔
400
                f += r;
29,103,253✔
401

402
                add_slash = true;
29,103,253✔
403
        }
404

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

409
        if (*(f-1) != '/' && keep_trailing_slash)
9,300,705✔
410
                *f++ = '/';
41✔
411

412
        *f = '\0';
9,300,705✔
413
        return path;
9,300,705✔
414
}
415

416
int path_simplify_alloc(const char *path, char **ret) {
1,827,493✔
417
        assert(ret);
1,827,493✔
418

419
        if (!path) {
1,827,493✔
420
                *ret = NULL;
17,121✔
421
                return 0;
17,121✔
422
        }
423

424
        char *t = strdup(path);
1,810,372✔
425
        if (!t)
1,810,372✔
426
                return -ENOMEM;
427

428
        *ret = path_simplify(t);
1,810,372✔
429
        return 0;
1,810,372✔
430
}
431

432
char* path_startswith_full(const char *original_path, const char *prefix, PathStartWithFlags flags) {
55,631,609✔
433
        assert(original_path);
55,631,609✔
434
        assert(prefix);
55,631,609✔
435

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

446
        const char *path = original_path;
55,631,609✔
447

448
        if ((path[0] == '/') != (prefix[0] == '/'))
55,631,609✔
449
                return NULL;
55,631,609✔
450

451
        for (;;) {
48,491,803✔
452
                const char *p, *q;
103,967,179✔
453
                int m, n;
103,967,179✔
454

455
                m = path_find_first_component(&path, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &p);
103,967,179✔
456
                if (m < 0)
103,967,179✔
457
                        return NULL;
55,475,376✔
458

459
                n = path_find_first_component(&prefix, !FLAGS_SET(flags, PATH_STARTSWITH_REFUSE_DOT_DOT), &q);
103,967,179✔
460
                if (n < 0)
103,967,179✔
461
                        return NULL;
462

463
                if (n == 0) {
103,967,179✔
464
                        if (!p)
41,077,277✔
465
                                p = path;
209,471✔
466

467
                        if (FLAGS_SET(flags, PATH_STARTSWITH_RETURN_LEADING_SLASH)) {
41,077,277✔
468

469
                                if (p <= original_path)
32✔
470
                                        return NULL;
471

472
                                p--;
32✔
473

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

478
                        return (char*) p;
41,077,276✔
479
                }
480

481
                if (m != n)
62,889,902✔
482
                        return NULL;
483

484
                if (!strneq(p, q, m))
50,975,573✔
485
                        return NULL;
486
        }
487
}
488

489
int path_compare(const char *a, const char *b) {
34,174,279✔
490
        int r;
34,174,279✔
491

492
        /* Order NULL before non-NULL */
493
        r = CMP(!!a, !!b);
34,174,279✔
494
        if (r != 0)
34,131,797✔
495
                return r;
42,488✔
496

497
        /* A relative path and an absolute path must not compare as equal.
498
         * Which one is sorted before the other does not really matter.
499
         * Here a relative path is ordered before an absolute path. */
500
        r = CMP(path_is_absolute(a), path_is_absolute(b));
68,263,508✔
501
        if (r != 0)
33,825,879✔
502
                return r;
362,297✔
503

504
        for (;;) {
26,776,708✔
505
                const char *aa, *bb;
60,546,202✔
506
                int j, k;
60,546,202✔
507

508
                j = path_find_first_component(&a, true, &aa);
60,546,202✔
509
                k = path_find_first_component(&b, true, &bb);
60,546,202✔
510

511
                if (j < 0 || k < 0) {
60,546,202✔
512
                        /* When one of paths is invalid, order invalid path after valid one. */
513
                        r = CMP(j < 0, k < 0);
8✔
514
                        if (r != 0)
×
515
                                return r;
33,769,494✔
516

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

521
                /* Order prefixes first: "/foo" before "/foo/bar" */
522
                if (j == 0) {
60,546,194✔
523
                        if (k == 0)
6,958,205✔
524
                                return 0;
525
                        return -1;
142,848✔
526
                }
527
                if (k == 0)
53,587,989✔
528
                        return 1;
529

530
                /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
531
                r = memcmp(aa, bb, MIN(j, k));
51,912,847✔
532
                if (r != 0)
51,912,847✔
533
                        return r;
534

535
                /* Sort "/foo/a" before "/foo/aaa" */
536
                r = CMP(j, k);
26,881,491✔
537
                if (r != 0)
26,818,217✔
538
                        return r;
104,783✔
539
        }
540
}
541

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

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

551
        j = path_extract_filename(a, &fa);
176,047✔
552
        k = path_extract_filename(b, &fb);
176,047✔
553

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

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

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

568
        return strcmp(fa, fb);
176,009✔
569
}
570

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

574
        if (path_equal(a, b))
53,107✔
575
                return 1;
576

577
        if (!a || !b)
363✔
578
                return 0;
579

580
        return inode_same(a, b, flags);
363✔
581
}
582

583
char* path_extend_internal(char **x, ...) {
26,172,600✔
584
        size_t sz, old_sz;
26,172,600✔
585
        char *q, *nx;
26,172,600✔
586
        const char *p;
26,172,600✔
587
        va_list ap;
26,172,600✔
588
        bool slash;
26,172,600✔
589

590
        POINTER_MAY_BE_NULL(x);
26,172,600✔
591

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

610
        sz = old_sz = x ? strlen_ptr(*x) : 0;
26,172,600✔
611
        va_start(ap, x);
26,172,600✔
612
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
59,641,279✔
613
                size_t add;
33,468,679✔
614

615
                if (isempty(p))
33,468,679✔
616
                        continue;
871,534✔
617

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

624
                sz += add;
32,597,145✔
625
        }
626
        va_end(ap);
26,172,600✔
627

628
        nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1));
26,172,600✔
629
        if (!nx)
26,172,600✔
630
                return NULL;
631
        if (x)
26,172,600✔
632
                *x = nx;
19,450,613✔
633

634
        if (old_sz > 0)
26,172,600✔
635
                slash = nx[old_sz-1] == '/';
19,370,876✔
636
        else {
637
                nx[old_sz] = 0;
6,801,724✔
638
                slash = true; /* no need to generate a slash anymore */
6,801,724✔
639
        }
640

641
        q = nx + old_sz;
26,172,600✔
642

643
        va_start(ap, x);
26,172,600✔
644
        while ((p = va_arg(ap, char*)) != POINTER_MAX) {
59,641,279✔
645
                if (isempty(p))
33,468,679✔
646
                        continue;
871,534✔
647

648
                if (!slash && p[0] != '/')
32,597,145✔
649
                        *(q++) = '/';
19,954,608✔
650

651
                q = stpcpy(q, p);
32,597,145✔
652
                slash = endswith(p, "/");
32,597,145✔
653
        }
654
        va_end(ap);
26,172,600✔
655

656
        return nx;
26,172,600✔
657
}
658

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

664
        assert(name);
31,094✔
665

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

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

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

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

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

704
                        path_simplify(*ret_path);
25,315✔
705
                }
706
        }
707

708
        if (ret_fd)
25,754✔
709
                *ret_fd = TAKE_FD(fd);
23,767✔
710

711
        return 0;
712
}
713

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

722
        int last_error = -ENOENT, r = 0;
25,764✔
723

724
        assert(name);
25,764✔
725

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

729
        if (exec_search_path) {
4,030✔
730
                STRV_FOREACH(element, exec_search_path) {
6✔
731
                        _cleanup_free_ char *full_path = NULL;
5✔
732

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

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

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

751
        const char *p = NULL;
4,028✔
752

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

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

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

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

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

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

786
        return last_error;
4✔
787
}
788

789
static int executable_is_good(const char *executable) {
192✔
790
        _cleanup_free_ char *p = NULL, *d = NULL;
192✔
791
        int r;
192✔
792

793
        r = find_executable(executable, &p);
192✔
794
        if (r == -ENOENT)
192✔
795
                return 0;
796
        if (r < 0)
190✔
797
                return r;
798

799
        /* An fsck that is linked to /bin/true is a non-existent fsck */
800

801
        r = readlink_malloc(p, &d);
190✔
802
        if (r == -EINVAL) /* not a symlink */
190✔
803
                return 1;
804
        if (r < 0)
72✔
805
                return r;
806

807
        return !PATH_IN_SET(d, "true"
72✔
808
                               "/bin/true",
809
                               "/usr/bin/true",
810
                               "/dev/null");
811
}
812

813
int fsck_exists(void) {
103✔
814
        return executable_is_good("fsck");
103✔
815
}
816

817
int fsck_exists_for_fstype(const char *fstype) {
89✔
818
        const char *checker;
89✔
819
        int r;
89✔
820

821
        assert(fstype);
89✔
822

823
        if (streq(fstype, "auto"))
89✔
824
                return -EINVAL;
825

826
        r = fsck_exists();
89✔
827
        if (r <= 0)
89✔
828
                return r;
829

830
        checker = strjoina("fsck.", fstype);
445✔
831
        return executable_is_good(checker);
89✔
832
}
833

834
static const char* skip_slash_or_dot(const char *p) {
1,156,933,632✔
835
        POINTER_MAY_BE_NULL(p);
1,156,933,632✔
836

837
        for (; !isempty(p); p++) {
1,787,987,333✔
838
                if (*p == '/')
1,556,926,337✔
839
                        continue;
631,052,933✔
840
                if (startswith(p, "./")) {
925,873,404✔
841
                        p++;
768✔
842
                        continue;
768✔
843
                }
844
                break;
845
        }
846
        return p;
1,156,933,632✔
847
}
848

849
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret) {
629,885,940✔
850
        const char *q, *first, *end_first, *next;
629,885,940✔
851
        size_t len;
629,885,940✔
852

853
        assert(p);
629,885,940✔
854

855
        /* When a path is input, then returns the pointer to the first component and its length, and
856
         * move the input pointer to the next component or nul. This skips both over any '/'
857
         * immediately *before* and *after* the first component before returning.
858
         *
859
         * Examples
860
         *   Input:  p: "//.//aaa///bbbbb/cc"
861
         *   Output: p: "bbbbb///cc"
862
         *           ret: "aaa///bbbbb/cc"
863
         *           return value: 3 (== strlen("aaa"))
864
         *
865
         *   Input:  p: "aaa//"
866
         *   Output: p: (pointer to NUL)
867
         *           ret: "aaa//"
868
         *           return value: 3 (== strlen("aaa"))
869
         *
870
         *   Input:  p: "/", ".", ""
871
         *   Output: p: (pointer to NUL)
872
         *           ret: NULL
873
         *           return value: 0
874
         *
875
         *   Input:  p: NULL
876
         *   Output: p: NULL
877
         *           ret: NULL
878
         *           return value: 0
879
         *
880
         *   Input:  p: "(too long component)"
881
         *   Output: return value: -EINVAL
882
         *
883
         *   (when accept_dot_dot is false)
884
         *   Input:  p: "//..//aaa///bbbbb/cc"
885
         *   Output: return value: -EINVAL
886
         */
887

888
        q = *p;
629,885,940✔
889

890
        first = skip_slash_or_dot(q);
629,885,940✔
891
        if (isempty(first)) {
629,885,940✔
892
                *p = first;
102,830,011✔
893
                if (ret)
102,830,011✔
894
                        *ret = NULL;
102,824,475✔
895
                return 0;
102,830,011✔
896
        }
897
        if (streq(first, ".")) {
527,055,929✔
898
                *p = first + 1;
8,026✔
899
                if (ret)
8,026✔
900
                        *ret = NULL;
8,020✔
901
                return 0;
8,026✔
902
        }
903

904
        end_first = strchrnul(first, '/');
527,047,903✔
905
        len = end_first - first;
527,047,903✔
906

907
        if (len > NAME_MAX)
527,047,903✔
908
                return -EINVAL;
909
        if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.')
527,047,729✔
910
                return -EINVAL;
911

912
        next = skip_slash_or_dot(end_first);
527,047,692✔
913

914
        *p = next + streq(next, ".");
527,047,692✔
915
        if (ret)
527,047,692✔
916
                *ret = first;
489,490,248✔
917
        return len;
527,047,692✔
918
}
919

920
static const char* skip_slash_or_dot_backward(const char *path, const char *q) {
15,649,588✔
921
        assert(path);
15,649,588✔
922
        assert(!q || q >= path);
15,649,588✔
923

924
        for (; q; q = PTR_SUB1(q, path)) {
31,506,705✔
925
                if (*q == '/')
23,230,651✔
926
                        continue;
7,967,944✔
927
                if (q > path && strneq(q - 1, "/.", 2))
15,262,707✔
928
                        continue;
296✔
929
                if (q == path && *q == '.')
15,262,411✔
930
                        continue;
85✔
931
                break;
932
        }
933
        return q;
15,649,588✔
934
}
935

936
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) {
7,832,709✔
937
        const char *q, *last_end, *last_begin;
7,832,709✔
938
        size_t len;
7,832,709✔
939

940
        POINTER_MAY_BE_NULL(next);
7,832,709✔
941
        POINTER_MAY_BE_NULL(ret);
7,832,709✔
942

943
        /* Similar to path_find_first_component(), but search components from the end.
944
        *
945
        * Examples
946
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
947
        *           next: NULL
948
        *   Output: next: "/cc//././"
949
        *           ret: "cc//././"
950
        *           return value: 2 (== strlen("cc"))
951
        *
952
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
953
        *           next: "/cc//././"
954
        *   Output: next: "///bbbbb/cc//././"
955
        *           ret: "bbbbb/cc//././"
956
        *           return value: 5 (== strlen("bbbbb"))
957
        *
958
        *   Input:  path: "//.//aaa///bbbbb/cc//././"
959
        *           next: "///bbbbb/cc//././"
960
        *   Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
961
        *           ret: "aaa///bbbbb/cc//././"
962
        *           return value: 3 (== strlen("aaa"))
963
        *
964
        *   Input:  path: "/", ".", "", or NULL
965
        *   Output: next: equivalent to path
966
        *           ret: NULL
967
        *           return value: 0
968
        *
969
        *   Input:  path: "(too long component)"
970
        *   Output: return value: -EINVAL
971
        *
972
        *   (when accept_dot_dot is false)
973
        *   Input:  path: "//..//aaa///bbbbb/cc/..//"
974
        *   Output: return value: -EINVAL
975
        */
976

977
        if (isempty(path)) {
7,832,709✔
978
                if (next)
4✔
979
                        *next = path;
4✔
980
                if (ret)
4✔
981
                        *ret = NULL;
4✔
982
                return 0;
4✔
983
        }
984

985
        if (next && *next) {
7,832,705✔
986
                if (*next < path || *next > path + strlen(path))
35,435✔
987
                        return -EINVAL;
988
                if (*next == path) {
35,435✔
989
                        if (ret)
9✔
990
                                *ret = NULL;
9✔
991
                        return 0;
9✔
992
                }
993
                if (!IN_SET(**next, '\0', '/'))
35,426✔
994
                        return -EINVAL;
995
                q = *next - 1;
35,426✔
996
        } else
997
                q = path + strlen(path) - 1;
7,797,270✔
998

999
        q = skip_slash_or_dot_backward(path, q);
7,832,696✔
1000
        if (!q) { /* the root directory, "." or "./" */
7,832,696✔
1001
                if (next)
15,720✔
1002
                        *next = path;
15,720✔
1003
                if (ret)
15,720✔
1004
                        *ret = NULL;
15,715✔
1005
                return 0;
15,720✔
1006
        }
1007

1008
        last_end = q + 1;
7,816,976✔
1009

1010
        while (q && *q != '/')
124,464,167✔
1011
                q = PTR_SUB1(q, path);
108,830,215✔
1012

1013
        last_begin = q ? q + 1 : path;
7,816,976✔
1014
        len = last_end - last_begin;
7,816,976✔
1015

1016
        if (len > NAME_MAX)
7,816,976✔
1017
                return -EINVAL;
1018
        if (!accept_dot_dot && len == 2 && strneq(last_begin, "..", 2))
7,816,972✔
1019
                return -EINVAL;
1020

1021
        if (next) {
7,816,939✔
1022
                q = skip_slash_or_dot_backward(path, q);
7,816,892✔
1023
                *next = q ? q + 1 : path;
7,816,892✔
1024
        }
1025

1026
        if (ret)
7,816,939✔
1027
                *ret = last_begin;
6,931,729✔
1028
        return len;
7,816,939✔
1029
}
1030

1031
const char* last_path_component(const char *path) {
137,448✔
1032

1033
        /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1034
         *
1035
         *    a/b/c → c
1036
         *    a/b/c/ → c/
1037
         *    x → x
1038
         *    x/ → x/
1039
         *    /y → y
1040
         *    /y/ → y/
1041
         *    / → /
1042
         *    // → /
1043
         *    /foo/a → a
1044
         *    /foo/a/ → a/
1045
         *
1046
         *    Also, the empty string is mapped to itself.
1047
         *
1048
         * This is different than basename(), which returns "" when a trailing slash is present.
1049
         *
1050
         * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1051
         */
1052

1053
        unsigned l, k;
137,448✔
1054

1055
        if (!path)
137,448✔
1056
                return NULL;
1057

1058
        l = k = strlen(path);
137,447✔
1059
        if (l == 0) /* special case — an empty string */
137,447✔
1060
                return path;
1061

1062
        while (k > 0 && path[k-1] == '/')
137,459✔
1063
                k--;
1064

1065
        if (k == 0) /* the root directory */
137,446✔
1066
                return path + l - 1;
3✔
1067

1068
        while (k > 0 && path[k-1] != '/')
1,136,941✔
1069
                k--;
1070

1071
        return path + k;
137,443✔
1072
}
1073

1074
int path_split_prefix_filename(const char *path, char **ret_dir, char **ret_filename) {
6,911,995✔
1075
        _cleanup_free_ char *d = NULL;
6,911,995✔
1076
        const char *c, *next = NULL;
6,911,995✔
1077
        int r;
6,911,995✔
1078

1079
        POINTER_MAY_BE_NULL(path);
6,911,995✔
1080

1081
        /* Split the path into dir prefix/filename pair. Returns:
1082
         *
1083
         * -EINVAL        → if the path is not valid
1084
         * -EADDRNOTAVAIL → if the path refers to the uppermost directory in hierarchy (i.e. has neither
1085
         *                  dir prefix nor filename - the root dir itself or ".")
1086
         * -EDESTADDRREQ  → if only a filename was passed, and caller only specifies ret_dir
1087
         * -ENOMEM        → no memory
1088
         *
1089
         * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1090
         * indicate the referenced file must be a directory.
1091
         *
1092
         * This function guarantees to return a fully valid filename, i.e. one that passes
1093
         * filename_is_valid() – this means "." and ".." are not accepted. */
1094

1095
        if (isempty(path))
13,823,985✔
1096
                return -EINVAL;
1097

1098
        r = path_find_last_component(path, /* accept_dot_dot = */ false, &next, &c);
6,911,980✔
1099
        if (r < 0)
6,911,980✔
1100
                return r;
1101
        if (r == 0) /* root directory or "." */
6,911,952✔
1102
                return -EADDRNOTAVAIL;
1103

1104
        if (ret_dir) {
6,896,247✔
1105
                if (next == path) {
4,268,962✔
1106
                        if (*path != '/') { /* filename only */
53,029✔
1107
                                if (!ret_filename)
32,311✔
1108
                                        return -EDESTADDRREQ;
1109
                        } else {
1110
                                d = strdup("/");
20,718✔
1111
                                if (!d)
20,718✔
1112
                                        return -ENOMEM;
1113
                        }
1114
                } else {
1115
                        d = strndup(path, next - path);
4,215,933✔
1116
                        if (!d)
4,215,933✔
1117
                                return -ENOMEM;
1118

1119
                        path_simplify(d);
4,215,933✔
1120

1121
                        if (!path_is_valid(d))
4,215,933✔
1122
                                return -EINVAL;
1123
                }
1124

1125
        } else if (!path_is_valid(path))
2,627,285✔
1126
                /* We didn't validate the dir prefix, hence check if the whole path is valid now */
1127
                return -EINVAL;
1128

1129
        if (ret_filename) {
6,863,936✔
1130
                char *fn = strndup(c, r);
2,972,016✔
1131
                if (!fn)
2,972,016✔
1132
                        return -ENOMEM;
1133

1134
                *ret_filename = fn;
2,972,016✔
1135
        }
1136

1137
        if (ret_dir)
6,870,761✔
1138
                *ret_dir = TAKE_PTR(d);
4,243,476✔
1139

1140
        return strlen(c) > (size_t) r ? O_DIRECTORY : 0;
6,870,761✔
1141
}
1142

1143
bool filename_part_is_valid(const char *p) {
1,087,591✔
1144
        const char *e;
1,087,591✔
1145

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

1149
        if (!p)
1,087,591✔
1150
                return false;
1151

1152
        e = strchrnul(p, '/');
1,087,591✔
1153
        if (*e != 0)
1,087,591✔
1154
                return false;
1155

1156
        if (e - p > NAME_MAX) /* NAME_MAX is counted *without* the trailing NUL byte */
1,066,222✔
1157
                return false;
3✔
1158

1159
        return true;
1160
}
1161

1162
bool filename_is_valid(const char *p) {
1,068,377✔
1163

1164
        if (isempty(p))
1,068,377✔
1165
                return false;
1166

1167
        if (dot_or_dot_dot(p)) /* Yes, in this context we consider "." and ".." invalid */
1,068,366✔
1168
                return false;
1169

1170
        return filename_part_is_valid(p);
1,068,355✔
1171
}
1172

1173
bool path_is_valid_full(const char *p, bool accept_dot_dot) {
11,966,786✔
1174
        if (isempty(p))
11,966,786✔
1175
                return false;
1176

1177
        for (const char *e = p;;) {
11,966,781✔
1178
                int r;
37,563,022✔
1179

1180
                r = path_find_first_component(&e, accept_dot_dot, NULL);
37,563,022✔
1181
                if (r < 0)
37,563,022✔
1182
                        return false;
11,966,781✔
1183

1184
                if (e - p >= PATH_MAX) /* Already reached the maximum length for a path? (PATH_MAX is counted
37,562,986✔
1185
                                        * *with* the trailing NUL byte) */
1186
                        return false;
1187
                if (*e == 0)           /* End of string? Yay! */
37,562,983✔
1188
                        return true;
1189
        }
1190
}
1191

1192
bool path_is_normalized(const char *p) {
848,954✔
1193
        if (!path_is_safe(p))
848,954✔
1194
                return false;
1195

1196
        if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
848,929✔
1197
                return false;
1198

1199
        if (strstr(p, "//"))
848,926✔
1200
                return false;
1✔
1201

1202
        return true;
1203
}
1204

1205
int file_in_same_dir(const char *path, const char *filename, char **ret) {
1,031✔
1206
        _cleanup_free_ char *b = NULL;
1,031✔
1207
        int r;
1,031✔
1208

1209
        assert(path);
1,031✔
1210
        assert(filename);
1,031✔
1211
        assert(ret);
1,031✔
1212

1213
        /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1214
         * or the former isn't */
1215

1216
        if (path_is_absolute(filename))
1,031✔
1217
                b = strdup(filename);
16✔
1218
        else {
1219
                _cleanup_free_ char *dn = NULL;
1,015✔
1220

1221
                r = path_extract_directory(path, &dn);
1,015✔
1222
                if (r == -EDESTADDRREQ) /* no path prefix */
1,015✔
1223
                        b = strdup(filename);
1✔
1224
                else if (r < 0)
1,014✔
1225
                        return r;
11✔
1226
                else
1227
                        b = path_join(dn, filename);
1,003✔
1228
        }
1229
        if (!b)
1,020✔
1230
                return -ENOMEM;
1231

1232
        *ret = TAKE_PTR(b);
1,020✔
1233
        return 0;
1,020✔
1234
}
1235

1236
bool hidden_or_backup_file(const char *filename) {
8,631,453✔
1237
        assert(filename);
8,631,453✔
1238

1239
        if (filename[0] == '.' ||
16,316,394✔
1240
            STR_IN_SET(filename,
7,684,941✔
1241
                       "lost+found",
1242
                       "aquota.user",
1243
                       "aquota.group") ||
7,684,938✔
1244
            endswith(filename, "~"))
7,684,938✔
1245
                return true;
946,517✔
1246

1247
        const char *dot = strrchr(filename, '.');
7,684,936✔
1248
        if (!dot)
7,684,936✔
1249
                return false;
1250

1251
        /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1252
         * to come up with always new suffixes and that everybody else should just adjust to that, then it
1253
         * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1254
         * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1255
         * possibly augmented with an additional string. Specifically: there's now:
1256
         *
1257
         *    The generic suffixes "~" and ".bak" for backup files
1258
         *    The generic prefix "." for hidden files
1259
         *
1260
         * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1261
         * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1262
         * ".foopkg.old" or ".foopkg~" instead.
1263
         */
1264

1265
        return STR_IN_SET(dot + 1,
7,059,683✔
1266
                          "ignore",
1267
                          "rpmnew",
1268
                          "rpmsave",
1269
                          "rpmorig",
1270
                          "dpkg-old",
1271
                          "dpkg-new",
1272
                          "dpkg-tmp",
1273
                          "dpkg-dist",
1274
                          "dpkg-bak",
1275
                          "dpkg-backup",
1276
                          "dpkg-remove",
1277
                          "ucf-new",
1278
                          "ucf-old",
1279
                          "ucf-dist",
1280
                          "swp",
1281
                          "bak",
1282
                          "old",
1283
                          "new");
1284
}
1285

1286
bool is_device_path(const char *path) {
14,837✔
1287

1288
        /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1289
         * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1290
         * It does not actually resolve the path.
1291
         *
1292
         * Examples:
1293
         * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1294
         * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1295
         */
1296

1297
        const char *p = PATH_STARTSWITH_SET(ASSERT_PTR(path), "/dev/", "/sys/");
14,837✔
1298
        return !isempty(p);
14,837✔
1299
}
1300

1301
bool valid_device_node_path(const char *path) {
684✔
1302

1303
        /* Some superficial checks whether the specified path is a valid device node path, all without
1304
         * looking at the actual device node. */
1305

1306
        if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
684✔
1307
                return false;
×
1308

1309
        if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
684✔
1310
                return false;
1311

1312
        return path_is_normalized(path);
684✔
1313
}
1314

1315
bool valid_device_allow_pattern(const char *path) {
18✔
1316
        assert(path);
18✔
1317

1318
        /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1319
         * DeviceAllow= and DeviceDeny=. */
1320

1321
        if (STARTSWITH_SET(path, "block-", "char-"))
18✔
1322
                return true;
6✔
1323

1324
        return valid_device_node_path(path);
12✔
1325
}
1326

1327
bool dot_or_dot_dot(const char *path) {
7,480,650✔
1328
        if (!path)
7,480,650✔
1329
                return false;
1330
        if (path[0] != '.')
7,480,649✔
1331
                return false;
1332
        if (path[1] == 0)
312,956✔
1333
                return true;
1334
        if (path[1] != '.')
156,982✔
1335
                return false;
1336

1337
        return path[2] == 0;
154,383✔
1338
}
1339

1340
bool path_implies_directory(const char *path) {
68,704✔
1341

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

1345
        if (!path)
68,704✔
1346
                return false;
68,704✔
1347

1348
        if (dot_or_dot_dot(path))
45,706✔
1349
                return true;
1350

1351
        return ENDSWITH_SET(path, "/", "/.", "/..");
45,704✔
1352
}
1353

1354
bool empty_or_root(const char *path) {
11,834,135✔
1355

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

1359
        if (isempty(path))
11,834,135✔
1360
                return true;
1361

1362
        return path_equal(path, "/");
5,442,289✔
1363
}
1364

1365
const char* empty_to_root(const char *path) {
445,626✔
1366
        return isempty(path) ? "/" : path;
452,102✔
1367
}
1368

1369
int empty_or_root_harder_to_null(const char **path) {
2,855,206✔
1370
        int r;
2,855,206✔
1371

1372
        assert(path);
2,855,206✔
1373

1374
        /* This nullifies the input path when the path is empty or points to "/". */
1375

1376
        if (empty_or_root(*path)) {
2,855,206✔
1377
                *path = NULL;
2,797,722✔
1378
                return 0;
2,797,722✔
1379
        }
1380

1381
        r = path_is_root(*path);
57,484✔
1382
        if (r < 0)
57,484✔
1383
                return r;
1384
        if (r > 0)
57,476✔
1385
                *path = NULL;
9✔
1386

1387
        return 0;
1388
}
1389

1390
bool path_strv_contains(char * const *l, const char *path) {
73,928✔
1391
        assert(path);
73,928✔
1392

1393
        STRV_FOREACH(i, l)
1,238,415✔
1394
                if (path_equal(*i, path))
1,176,079✔
1395
                        return true;
1396

1397
        return false;
1398
}
1399

1400
bool prefixed_path_strv_contains(char * const *l, const char *path) {
2,205✔
1401
        assert(path);
2,205✔
1402

1403
        STRV_FOREACH(i, l) {
2,210✔
1404
                const char *j = *i;
6✔
1405

1406
                if (*j == '-')
6✔
1407
                        j++;
×
1408
                if (*j == '+')
6✔
1409
                        j++;
×
1410

1411
                if (path_equal(j, path))
6✔
1412
                        return true;
1413
        }
1414

1415
        return false;
1416
}
1417

1418
int path_glob_can_match(const char *pattern, const char *prefix, char **ret) {
11,972✔
1419
        assert(pattern);
11,972✔
1420
        assert(prefix);
11,972✔
1421

1422
        for (const char *a = pattern, *b = prefix;;) {
11,972✔
1423
                _cleanup_free_ char *g = NULL, *h = NULL;
26,967✔
1424
                const char *p, *q;
35,930✔
1425
                int r, s;
35,930✔
1426

1427
                r = path_find_first_component(&a, /* accept_dot_dot= */ false, &p);
35,930✔
1428
                if (r < 0)
35,930✔
1429
                        return r;
1430

1431
                s = path_find_first_component(&b, /* accept_dot_dot= */ false, &q);
35,930✔
1432
                if (s < 0)
35,930✔
1433
                        return s;
1434

1435
                if (s == 0) {
35,930✔
1436
                        /* The pattern matches the prefix. */
1437
                        if (ret) {
3,009✔
1438
                                char *t;
3,009✔
1439

1440
                                t = path_join(prefix, p);
3,009✔
1441
                                if (!t)
3,009✔
1442
                                        return -ENOMEM;
1443

1444
                                *ret = t;
3,009✔
1445
                        }
1446
                        return true;
3,009✔
1447
                }
1448

1449
                if (r == 0)
32,921✔
1450
                        break;
1451

1452
                if (r == s && strneq(p, q, r))
32,917✔
1453
                        continue; /* common component. Check next. */
20,947✔
1454

1455
                g = strndup(p, r);
11,970✔
1456
                if (!g)
11,970✔
1457
                        return -ENOMEM;
1458

1459
                if (!string_is_glob(g))
11,970✔
1460
                        break;
1461

1462
                /* We found a glob component. Check if the glob pattern matches the prefix component. */
1463

1464
                h = strndup(q, s);
3,011✔
1465
                if (!h)
3,011✔
1466
                        return -ENOMEM;
1467

1468
                r = fnmatch(g, h, 0);
3,011✔
1469
                if (r == FNM_NOMATCH)
3,011✔
1470
                        break;
1471
                if (r != 0) /* Failure to process pattern? */
3,011✔
1472
                        return -EINVAL;
1473
        }
1474

1475
        /* The pattern does not match the prefix. */
1476
        if (ret)
8,963✔
1477
                *ret = NULL;
8,963✔
1478
        return false;
1479
}
1480

1481
#if HAVE_SPLIT_BIN
1482
static bool dir_is_split(const char *a, const char *b) {
1483
        int r;
1484

1485
        r = inode_same(a, b, AT_NO_AUTOMOUNT);
1486
        if (r < 0 && r != -ENOENT) {
1487
                log_debug_errno(r, "Failed to compare \"%s\" and \"%s\", assuming split directories: %m", a, b);
1488
                return true;
1489
        }
1490
        return r == 0;
1491
}
1492
#endif
1493

1494
const char* default_PATH(void) {
3,839✔
1495
#if HAVE_SPLIT_BIN
1496
        static const char *default_path = NULL;
1497

1498
        /* Return one of the three sets of paths:
1499
         * a) split /usr/s?bin, /usr/local/sbin doesn't matter.
1500
         * b) merged /usr/s?bin, /usr/sbin is a symlink, but /usr/local/sbin is not,
1501
         * c) fully merged, neither /usr/sbin nor /usr/local/sbin are symlinks,
1502
         *
1503
         * On error the fallback to the safe value with both directories as configured is returned.
1504
         */
1505

1506
        if (default_path)
1507
                return default_path;
1508

1509
        if (dir_is_split("/usr/sbin", "/usr/bin"))
1510
                return (default_path = DEFAULT_PATH_WITH_FULL_SBIN);  /* a */
1511
        if (dir_is_split("/usr/local/sbin", "/usr/local/bin"))
1512
                return (default_path = DEFAULT_PATH_WITH_LOCAL_SBIN); /* b */
1513
        return (default_path = DEFAULT_PATH_WITHOUT_SBIN);            /* c */
1514
#else
1515
        return DEFAULT_PATH_WITHOUT_SBIN;
3,839✔
1516
#endif
1517
}
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