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