]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/path-util.c
Merge pull request #13651 from cdown/find_binary_2
[thirdparty/systemd.git] / src / basic / path-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <limits.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10
11 /* When we include libgen.h because we need dirname() we immediately
12 * undefine basename() since libgen.h defines it as a macro to the
13 * POSIX version which is really broken. We prefer GNU basename(). */
14 #include <libgen.h>
15 #undef basename
16
17 #include "alloc-util.h"
18 #include "extract-word.h"
19 #include "fs-util.h"
20 #include "glob-util.h"
21 #include "log.h"
22 #include "macro.h"
23 #include "missing.h"
24 #include "nulstr-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "stat-util.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "time-util.h"
31 #include "utf8.h"
32
33 bool path_is_absolute(const char *p) {
34 return p[0] == '/';
35 }
36
37 bool is_path(const char *p) {
38 return !!strchr(p, '/');
39 }
40
41 int path_split_and_make_absolute(const char *p, char ***ret) {
42 char **l;
43 int r;
44
45 assert(p);
46 assert(ret);
47
48 l = strv_split(p, ":");
49 if (!l)
50 return -ENOMEM;
51
52 r = path_strv_make_absolute_cwd(l);
53 if (r < 0) {
54 strv_free(l);
55 return r;
56 }
57
58 *ret = l;
59 return r;
60 }
61
62 char *path_make_absolute(const char *p, const char *prefix) {
63 assert(p);
64
65 /* Makes every item in the list an absolute path by prepending
66 * the prefix, if specified and necessary */
67
68 if (path_is_absolute(p) || isempty(prefix))
69 return strdup(p);
70
71 return path_join(prefix, p);
72 }
73
74 int safe_getcwd(char **ret) {
75 char *cwd;
76
77 cwd = get_current_dir_name();
78 if (!cwd)
79 return negative_errno();
80
81 /* Let's make sure the directory is really absolute, to protect us from the logic behind
82 * CVE-2018-1000001 */
83 if (cwd[0] != '/') {
84 free(cwd);
85 return -ENOMEDIUM;
86 }
87
88 *ret = cwd;
89 return 0;
90 }
91
92 int path_make_absolute_cwd(const char *p, char **ret) {
93 char *c;
94 int r;
95
96 assert(p);
97 assert(ret);
98
99 /* Similar to path_make_absolute(), but prefixes with the
100 * current working directory. */
101
102 if (path_is_absolute(p))
103 c = strdup(p);
104 else {
105 _cleanup_free_ char *cwd = NULL;
106
107 r = safe_getcwd(&cwd);
108 if (r < 0)
109 return r;
110
111 c = path_join(cwd, p);
112 }
113 if (!c)
114 return -ENOMEM;
115
116 *ret = c;
117 return 0;
118 }
119
120 int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
121 char *f, *t, *r, *p;
122 unsigned n_parents = 0;
123
124 assert(from_dir);
125 assert(to_path);
126 assert(_r);
127
128 /* Strips the common part, and adds ".." elements as necessary. */
129
130 if (!path_is_absolute(from_dir) || !path_is_absolute(to_path))
131 return -EINVAL;
132
133 f = strdupa(from_dir);
134 t = strdupa(to_path);
135
136 path_simplify(f, true);
137 path_simplify(t, true);
138
139 /* Skip the common part. */
140 for (;;) {
141 size_t a, b;
142
143 f += *f == '/';
144 t += *t == '/';
145
146 if (!*f) {
147 if (!*t)
148 /* from_dir equals to_path. */
149 r = strdup(".");
150 else
151 /* from_dir is a parent directory of to_path. */
152 r = strdup(t);
153 if (!r)
154 return -ENOMEM;
155
156 *_r = r;
157 return 0;
158 }
159
160 if (!*t)
161 break;
162
163 a = strcspn(f, "/");
164 b = strcspn(t, "/");
165
166 if (a != b || memcmp(f, t, a) != 0)
167 break;
168
169 f += a;
170 t += b;
171 }
172
173 /* If we're here, then "from_dir" has one or more elements that need to
174 * be replaced with "..". */
175
176 /* Count the number of necessary ".." elements. */
177 for (; *f;) {
178 size_t w;
179
180 w = strcspn(f, "/");
181
182 /* If this includes ".." we can't do a simple series of "..", refuse */
183 if (w == 2 && f[0] == '.' && f[1] == '.')
184 return -EINVAL;
185
186 /* Count number of elements */
187 n_parents++;
188
189 f += w;
190 f += *f == '/';
191 }
192
193 r = new(char, n_parents * 3 + strlen(t) + 1);
194 if (!r)
195 return -ENOMEM;
196
197 for (p = r; n_parents > 0; n_parents--)
198 p = mempcpy(p, "../", 3);
199
200 if (*t)
201 strcpy(p, t);
202 else
203 /* Remove trailing slash */
204 *(--p) = 0;
205
206 *_r = r;
207 return 0;
208 }
209
210 char* path_startswith_strv(const char *p, char **set) {
211 char **s, *t;
212
213 STRV_FOREACH(s, set) {
214 t = path_startswith(p, *s);
215 if (t)
216 return t;
217 }
218
219 return NULL;
220 }
221
222 int path_strv_make_absolute_cwd(char **l) {
223 char **s;
224 int r;
225
226 /* Goes through every item in the string list and makes it
227 * absolute. This works in place and won't rollback any
228 * changes on failure. */
229
230 STRV_FOREACH(s, l) {
231 char *t;
232
233 r = path_make_absolute_cwd(*s, &t);
234 if (r < 0)
235 return r;
236
237 path_simplify(t, false);
238 free_and_replace(*s, t);
239 }
240
241 return 0;
242 }
243
244 char **path_strv_resolve(char **l, const char *root) {
245 char **s;
246 unsigned k = 0;
247 bool enomem = false;
248 int r;
249
250 if (strv_isempty(l))
251 return l;
252
253 /* Goes through every item in the string list and canonicalize
254 * the path. This works in place and won't rollback any
255 * changes on failure. */
256
257 STRV_FOREACH(s, l) {
258 _cleanup_free_ char *orig = NULL;
259 char *t, *u;
260
261 if (!path_is_absolute(*s)) {
262 free(*s);
263 continue;
264 }
265
266 if (root) {
267 orig = *s;
268 t = path_join(root, orig);
269 if (!t) {
270 enomem = true;
271 continue;
272 }
273 } else
274 t = *s;
275
276 r = chase_symlinks(t, root, 0, &u);
277 if (r == -ENOENT) {
278 if (root) {
279 u = TAKE_PTR(orig);
280 free(t);
281 } else
282 u = t;
283 } else if (r < 0) {
284 free(t);
285
286 if (r == -ENOMEM)
287 enomem = true;
288
289 continue;
290 } else if (root) {
291 char *x;
292
293 free(t);
294 x = path_startswith(u, root);
295 if (x) {
296 /* restore the slash if it was lost */
297 if (!startswith(x, "/"))
298 *(--x) = '/';
299
300 t = strdup(x);
301 free(u);
302 if (!t) {
303 enomem = true;
304 continue;
305 }
306 u = t;
307 } else {
308 /* canonicalized path goes outside of
309 * prefix, keep the original path instead */
310 free_and_replace(u, orig);
311 }
312 } else
313 free(t);
314
315 l[k++] = u;
316 }
317
318 l[k] = NULL;
319
320 if (enomem)
321 return NULL;
322
323 return l;
324 }
325
326 char **path_strv_resolve_uniq(char **l, const char *root) {
327
328 if (strv_isempty(l))
329 return l;
330
331 if (!path_strv_resolve(l, root))
332 return NULL;
333
334 return strv_uniq(l);
335 }
336
337 char *path_simplify(char *path, bool kill_dots) {
338 char *f, *t;
339 bool slash = false, ignore_slash = false, absolute;
340
341 assert(path);
342
343 /* Removes redundant inner and trailing slashes. Also removes unnecessary dots
344 * if kill_dots is true. Modifies the passed string in-place.
345 *
346 * ///foo//./bar/. becomes /foo/./bar/. (if kill_dots is false)
347 * ///foo//./bar/. becomes /foo/bar (if kill_dots is true)
348 * .//./foo//./bar/. becomes ././foo/./bar/. (if kill_dots is false)
349 * .//./foo//./bar/. becomes foo/bar (if kill_dots is true)
350 */
351
352 if (isempty(path))
353 return path;
354
355 absolute = path_is_absolute(path);
356
357 f = path;
358 if (kill_dots && *f == '.' && IN_SET(f[1], 0, '/')) {
359 ignore_slash = true;
360 f++;
361 }
362
363 for (t = path; *f; f++) {
364
365 if (*f == '/') {
366 slash = true;
367 continue;
368 }
369
370 if (slash) {
371 if (kill_dots && *f == '.' && IN_SET(f[1], 0, '/'))
372 continue;
373
374 slash = false;
375 if (ignore_slash)
376 ignore_slash = false;
377 else
378 *(t++) = '/';
379 }
380
381 *(t++) = *f;
382 }
383
384 /* Special rule, if we stripped everything, we either need a "/" (for the root directory)
385 * or "." for the current directory */
386 if (t == path) {
387 if (absolute)
388 *(t++) = '/';
389 else
390 *(t++) = '.';
391 }
392
393 *t = 0;
394 return path;
395 }
396
397 int path_simplify_and_warn(
398 char *path,
399 unsigned flag,
400 const char *unit,
401 const char *filename,
402 unsigned line,
403 const char *lvalue) {
404
405 bool fatal = flag & PATH_CHECK_FATAL;
406
407 assert(!FLAGS_SET(flag, PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE));
408
409 if (!utf8_is_valid(path))
410 return log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, path);
411
412 if (flag & (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)) {
413 bool absolute;
414
415 absolute = path_is_absolute(path);
416
417 if (!absolute && (flag & PATH_CHECK_ABSOLUTE))
418 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
419 "%s= path is not absolute%s: %s",
420 lvalue, fatal ? "" : ", ignoring", path);
421
422 if (absolute && (flag & PATH_CHECK_RELATIVE))
423 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
424 "%s= path is absolute%s: %s",
425 lvalue, fatal ? "" : ", ignoring", path);
426 }
427
428 path_simplify(path, true);
429
430 if (!path_is_valid(path))
431 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
432 "%s= path has invalid length (%zu bytes)%s.",
433 lvalue, strlen(path), fatal ? "" : ", ignoring");
434
435 if (!path_is_normalized(path))
436 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
437 "%s= path is not normalized%s: %s",
438 lvalue, fatal ? "" : ", ignoring", path);
439
440 return 0;
441 }
442
443 char* path_startswith(const char *path, const char *prefix) {
444 assert(path);
445 assert(prefix);
446
447 /* Returns a pointer to the start of the first component after the parts matched by
448 * the prefix, iff
449 * - both paths are absolute or both paths are relative,
450 * and
451 * - each component in prefix in turn matches a component in path at the same position.
452 * An empty string will be returned when the prefix and path are equivalent.
453 *
454 * Returns NULL otherwise.
455 */
456
457 if ((path[0] == '/') != (prefix[0] == '/'))
458 return NULL;
459
460 for (;;) {
461 size_t a, b;
462
463 path += strspn(path, "/");
464 prefix += strspn(prefix, "/");
465
466 if (*prefix == 0)
467 return (char*) path;
468
469 if (*path == 0)
470 return NULL;
471
472 a = strcspn(path, "/");
473 b = strcspn(prefix, "/");
474
475 if (a != b)
476 return NULL;
477
478 if (memcmp(path, prefix, a) != 0)
479 return NULL;
480
481 path += a;
482 prefix += b;
483 }
484 }
485
486 int path_compare(const char *a, const char *b) {
487 int d;
488
489 assert(a);
490 assert(b);
491
492 /* A relative path and an absolute path must not compare as equal.
493 * Which one is sorted before the other does not really matter.
494 * Here a relative path is ordered before an absolute path. */
495 d = (a[0] == '/') - (b[0] == '/');
496 if (d != 0)
497 return d;
498
499 for (;;) {
500 size_t j, k;
501
502 a += strspn(a, "/");
503 b += strspn(b, "/");
504
505 if (*a == 0 && *b == 0)
506 return 0;
507
508 /* Order prefixes first: "/foo" before "/foo/bar" */
509 if (*a == 0)
510 return -1;
511 if (*b == 0)
512 return 1;
513
514 j = strcspn(a, "/");
515 k = strcspn(b, "/");
516
517 /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
518 d = memcmp(a, b, MIN(j, k));
519 if (d != 0)
520 return (d > 0) - (d < 0); /* sign of d */
521
522 /* Sort "/foo/a" before "/foo/aaa" */
523 d = (j > k) - (j < k); /* sign of (j - k) */
524 if (d != 0)
525 return d;
526
527 a += j;
528 b += k;
529 }
530 }
531
532 bool path_equal(const char *a, const char *b) {
533 return path_compare(a, b) == 0;
534 }
535
536 bool path_equal_or_files_same(const char *a, const char *b, int flags) {
537 return path_equal(a, b) || files_same(a, b, flags) > 0;
538 }
539
540 char* path_join_internal(const char *first, ...) {
541 char *joined, *q;
542 const char *p;
543 va_list ap;
544 bool slash;
545 size_t sz;
546
547 /* Joins all listed strings until the sentinel and places a "/" between them unless the strings end/begin
548 * already with one so that it is unnecessary. Note that slashes which are already duplicate won't be
549 * removed. The string returned is hence always equal to or longer than the sum of the lengths of each
550 * individual string.
551 *
552 * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of which some
553 * are optional.
554 *
555 * Examples:
556 *
557 * path_join("foo", "bar") → "foo/bar"
558 * path_join("foo/", "bar") → "foo/bar"
559 * path_join("", "foo", "", "bar", "") → "foo/bar" */
560
561 sz = strlen_ptr(first);
562 va_start(ap, first);
563 while ((p = va_arg(ap, char*)) != (const char*) -1)
564 if (!isempty(p))
565 sz += 1 + strlen(p);
566 va_end(ap);
567
568 joined = new(char, sz + 1);
569 if (!joined)
570 return NULL;
571
572 if (!isempty(first)) {
573 q = stpcpy(joined, first);
574 slash = endswith(first, "/");
575 } else {
576 /* Skip empty items */
577 joined[0] = 0;
578 q = joined;
579 slash = true; /* no need to generate a slash anymore */
580 }
581
582 va_start(ap, first);
583 while ((p = va_arg(ap, char*)) != (const char*) -1) {
584 if (isempty(p))
585 continue;
586
587 if (!slash && p[0] != '/')
588 *(q++) = '/';
589
590 q = stpcpy(q, p);
591 slash = endswith(p, "/");
592 }
593 va_end(ap);
594
595 return joined;
596 }
597
598 int find_binary(const char *name, char **ret) {
599 int last_error, r;
600 const char *p;
601
602 assert(name);
603
604 if (is_path(name)) {
605 if (access(name, X_OK) < 0)
606 return -errno;
607
608 if (ret) {
609 r = path_make_absolute_cwd(name, ret);
610 if (r < 0)
611 return r;
612 }
613
614 return 0;
615 }
616
617 /**
618 * Plain getenv, not secure_getenv, because we want
619 * to actually allow the user to pick the binary.
620 */
621 p = getenv("PATH");
622 if (!p)
623 p = DEFAULT_PATH;
624
625 last_error = -ENOENT;
626
627 for (;;) {
628 _cleanup_free_ char *j = NULL, *element = NULL;
629
630 r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
631 if (r < 0)
632 return r;
633 if (r == 0)
634 break;
635
636 if (!path_is_absolute(element))
637 continue;
638
639 j = path_join(element, name);
640 if (!j)
641 return -ENOMEM;
642
643 if (access(j, X_OK) >= 0) {
644 /* Found it! */
645
646 if (ret) {
647 *ret = path_simplify(j, false);
648 j = NULL;
649 }
650
651 return 0;
652 }
653
654 /* PATH entries which we don't have access to are ignored, as per tradition. */
655 if (errno != EACCES)
656 last_error = -errno;
657 }
658
659 return last_error;
660 }
661
662 bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
663 bool changed = false;
664 const char* const* i;
665
666 assert(timestamp);
667
668 if (!paths)
669 return false;
670
671 STRV_FOREACH(i, paths) {
672 struct stat stats;
673 usec_t u;
674
675 if (stat(*i, &stats) < 0)
676 continue;
677
678 u = timespec_load(&stats.st_mtim);
679
680 /* first check */
681 if (*timestamp >= u)
682 continue;
683
684 log_debug("timestamp of '%s' changed", *i);
685
686 /* update timestamp */
687 if (update) {
688 *timestamp = u;
689 changed = true;
690 } else
691 return true;
692 }
693
694 return changed;
695 }
696
697 static int binary_is_good(const char *binary) {
698 _cleanup_free_ char *p = NULL, *d = NULL;
699 int r;
700
701 r = find_binary(binary, &p);
702 if (r == -ENOENT)
703 return 0;
704 if (r < 0)
705 return r;
706
707 /* An fsck that is linked to /bin/true is a non-existent
708 * fsck */
709
710 r = readlink_malloc(p, &d);
711 if (r == -EINVAL) /* not a symlink */
712 return 1;
713 if (r < 0)
714 return r;
715
716 return !PATH_IN_SET(d, "true"
717 "/bin/true",
718 "/usr/bin/true",
719 "/dev/null");
720 }
721
722 int fsck_exists(const char *fstype) {
723 const char *checker;
724
725 assert(fstype);
726
727 if (streq(fstype, "auto"))
728 return -EINVAL;
729
730 checker = strjoina("fsck.", fstype);
731 return binary_is_good(checker);
732 }
733
734 int mkfs_exists(const char *fstype) {
735 const char *mkfs;
736
737 assert(fstype);
738
739 if (streq(fstype, "auto"))
740 return -EINVAL;
741
742 mkfs = strjoina("mkfs.", fstype);
743 return binary_is_good(mkfs);
744 }
745
746 int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) {
747 char *p;
748 int r;
749
750 /*
751 * This function is intended to be used in command line
752 * parsers, to handle paths that are passed in. It makes the
753 * path absolute, and reduces it to NULL if omitted or
754 * root (the latter optionally).
755 *
756 * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON
757 * SUCCESS! Hence, do not pass in uninitialized pointers.
758 */
759
760 if (isempty(path)) {
761 *arg = mfree(*arg);
762 return 0;
763 }
764
765 r = path_make_absolute_cwd(path, &p);
766 if (r < 0)
767 return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
768
769 path_simplify(p, false);
770 if (suppress_root && empty_or_root(p))
771 p = mfree(p);
772
773 free_and_replace(*arg, p);
774
775 return 0;
776 }
777
778 char* dirname_malloc(const char *path) {
779 char *d, *dir, *dir2;
780
781 assert(path);
782
783 d = strdup(path);
784 if (!d)
785 return NULL;
786
787 dir = dirname(d);
788 assert(dir);
789
790 if (dir == d)
791 return d;
792
793 dir2 = strdup(dir);
794 free(d);
795
796 return dir2;
797 }
798
799 const char *last_path_component(const char *path) {
800
801 /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
802 *
803 * a/b/c → c
804 * a/b/c/ → c/
805 * x → x
806 * x/ → x/
807 * /y → y
808 * /y/ → y/
809 * / → /
810 * // → /
811 * /foo/a → a
812 * /foo/a/ → a/
813 *
814 * Also, the empty string is mapped to itself.
815 *
816 * This is different than basename(), which returns "" when a trailing slash is present.
817 */
818
819 unsigned l, k;
820
821 if (!path)
822 return NULL;
823
824 l = k = strlen(path);
825 if (l == 0) /* special case — an empty string */
826 return path;
827
828 while (k > 0 && path[k-1] == '/')
829 k--;
830
831 if (k == 0) /* the root directory */
832 return path + l - 1;
833
834 while (k > 0 && path[k-1] != '/')
835 k--;
836
837 return path + k;
838 }
839
840 int path_extract_filename(const char *p, char **ret) {
841 _cleanup_free_ char *a = NULL;
842 const char *c, *e = NULL, *q;
843
844 /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
845 * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing slashes. */
846
847 if (!p)
848 return -EINVAL;
849
850 c = last_path_component(p);
851
852 for (q = c; *q != 0; q++)
853 if (*q != '/')
854 e = q + 1;
855
856 if (!e) /* no valid character? */
857 return -EINVAL;
858
859 a = strndup(c, e - c);
860 if (!a)
861 return -ENOMEM;
862
863 if (!filename_is_valid(a))
864 return -EINVAL;
865
866 *ret = TAKE_PTR(a);
867
868 return 0;
869 }
870
871 bool filename_is_valid(const char *p) {
872 const char *e;
873
874 if (isempty(p))
875 return false;
876
877 if (dot_or_dot_dot(p))
878 return false;
879
880 e = strchrnul(p, '/');
881 if (*e != 0)
882 return false;
883
884 if (e - p > FILENAME_MAX) /* FILENAME_MAX is counted *without* the trailing NUL byte */
885 return false;
886
887 return true;
888 }
889
890 bool path_is_valid(const char *p) {
891
892 if (isempty(p))
893 return false;
894
895 if (strlen(p) >= PATH_MAX) /* PATH_MAX is counted *with* the trailing NUL byte */
896 return false;
897
898 return true;
899 }
900
901 bool path_is_normalized(const char *p) {
902
903 if (!path_is_valid(p))
904 return false;
905
906 if (dot_or_dot_dot(p))
907 return false;
908
909 if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
910 return false;
911
912 if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
913 return false;
914
915 if (strstr(p, "//"))
916 return false;
917
918 return true;
919 }
920
921 char *file_in_same_dir(const char *path, const char *filename) {
922 char *e, *ret;
923 size_t k;
924
925 assert(path);
926 assert(filename);
927
928 /* This removes the last component of path and appends
929 * filename, unless the latter is absolute anyway or the
930 * former isn't */
931
932 if (path_is_absolute(filename))
933 return strdup(filename);
934
935 e = strrchr(path, '/');
936 if (!e)
937 return strdup(filename);
938
939 k = strlen(filename);
940 ret = new(char, (e + 1 - path) + k + 1);
941 if (!ret)
942 return NULL;
943
944 memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1);
945 return ret;
946 }
947
948 bool hidden_or_backup_file(const char *filename) {
949 const char *p;
950
951 assert(filename);
952
953 if (filename[0] == '.' ||
954 streq(filename, "lost+found") ||
955 streq(filename, "aquota.user") ||
956 streq(filename, "aquota.group") ||
957 endswith(filename, "~"))
958 return true;
959
960 p = strrchr(filename, '.');
961 if (!p)
962 return false;
963
964 /* Please, let's not add more entries to the list below. If external projects think it's a good idea to come up
965 * with always new suffixes and that everybody else should just adjust to that, then it really should be on
966 * them. Hence, in future, let's not add any more entries. Instead, let's ask those packages to instead adopt
967 * one of the generic suffixes/prefixes for hidden files or backups, possibly augmented with an additional
968 * string. Specifically: there's now:
969 *
970 * The generic suffixes "~" and ".bak" for backup files
971 * The generic prefix "." for hidden files
972 *
973 * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old", ".foopkg-dist"
974 * or so registered, let's refuse that and ask them to use ".foopkg.new", ".foopkg.old" or ".foopkg~" instead.
975 */
976
977 return STR_IN_SET(p + 1,
978 "rpmnew",
979 "rpmsave",
980 "rpmorig",
981 "dpkg-old",
982 "dpkg-new",
983 "dpkg-tmp",
984 "dpkg-dist",
985 "dpkg-bak",
986 "dpkg-backup",
987 "dpkg-remove",
988 "ucf-new",
989 "ucf-old",
990 "ucf-dist",
991 "swp",
992 "bak",
993 "old",
994 "new");
995 }
996
997 bool is_device_path(const char *path) {
998
999 /* Returns true on paths that likely refer to a device, either by path in sysfs or to something in /dev */
1000
1001 return PATH_STARTSWITH_SET(path, "/dev/", "/sys/");
1002 }
1003
1004 bool valid_device_node_path(const char *path) {
1005
1006 /* Some superficial checks whether the specified path is a valid device node path, all without looking at the
1007 * actual device node. */
1008
1009 if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
1010 return false;
1011
1012 if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
1013 return false;
1014
1015 return path_is_normalized(path);
1016 }
1017
1018 bool valid_device_allow_pattern(const char *path) {
1019 assert(path);
1020
1021 /* Like valid_device_node_path(), but also allows full-subsystem expressions, like DeviceAllow= and DeviceDeny=
1022 * accept it */
1023
1024 if (STARTSWITH_SET(path, "block-", "char-"))
1025 return true;
1026
1027 return valid_device_node_path(path);
1028 }
1029
1030 int systemd_installation_has_version(const char *root, unsigned minimal_version) {
1031 const char *pattern;
1032 int r;
1033
1034 /* Try to guess if systemd installation is later than the specified version. This
1035 * is hacky and likely to yield false negatives, particularly if the installation
1036 * is non-standard. False positives should be relatively rare.
1037 */
1038
1039 NULSTR_FOREACH(pattern,
1040 /* /lib works for systems without usr-merge, and for systems with a sane
1041 * usr-merge, where /lib is a symlink to /usr/lib. /usr/lib is necessary
1042 * for Gentoo which does a merge without making /lib a symlink.
1043 */
1044 "lib/systemd/libsystemd-shared-*.so\0"
1045 "lib64/systemd/libsystemd-shared-*.so\0"
1046 "usr/lib/systemd/libsystemd-shared-*.so\0"
1047 "usr/lib64/systemd/libsystemd-shared-*.so\0") {
1048
1049 _cleanup_strv_free_ char **names = NULL;
1050 _cleanup_free_ char *path = NULL;
1051 char *c, **name;
1052
1053 path = path_join(root, pattern);
1054 if (!path)
1055 return -ENOMEM;
1056
1057 r = glob_extend(&names, path);
1058 if (r == -ENOENT)
1059 continue;
1060 if (r < 0)
1061 return r;
1062
1063 assert_se(c = endswith(path, "*.so"));
1064 *c = '\0'; /* truncate the glob part */
1065
1066 STRV_FOREACH(name, names) {
1067 /* This is most likely to run only once, hence let's not optimize anything. */
1068 char *t, *t2;
1069 unsigned version;
1070
1071 t = startswith(*name, path);
1072 if (!t)
1073 continue;
1074
1075 t2 = endswith(t, ".so");
1076 if (!t2)
1077 continue;
1078
1079 t2[0] = '\0'; /* truncate the suffix */
1080
1081 r = safe_atou(t, &version);
1082 if (r < 0) {
1083 log_debug_errno(r, "Found libsystemd shared at \"%s.so\", but failed to parse version: %m", *name);
1084 continue;
1085 }
1086
1087 log_debug("Found libsystemd shared at \"%s.so\", version %u (%s).",
1088 *name, version,
1089 version >= minimal_version ? "OK" : "too old");
1090 if (version >= minimal_version)
1091 return true;
1092 }
1093 }
1094
1095 return false;
1096 }
1097
1098 bool dot_or_dot_dot(const char *path) {
1099 if (!path)
1100 return false;
1101 if (path[0] != '.')
1102 return false;
1103 if (path[1] == 0)
1104 return true;
1105 if (path[1] != '.')
1106 return false;
1107
1108 return path[2] == 0;
1109 }
1110
1111 bool empty_or_root(const char *root) {
1112
1113 /* For operations relative to some root directory, returns true if the specified root directory is redundant,
1114 * i.e. either / or NULL or the empty string or any equivalent. */
1115
1116 if (!root)
1117 return true;
1118
1119 return root[strspn(root, "/")] == 0;
1120 }