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