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