1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 /* When we include libgen.h because we need dirname() we immediately
10 * undefine basename() since libgen.h defines it as a macro to the
11 * POSIX version which is really broken. We prefer GNU basename(). */
15 #include "alloc-util.h"
16 #include "chase-symlinks.h"
17 #include "extract-word.h"
20 #include "glob-util.h"
23 #include "nulstr-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "stat-util.h"
27 #include "string-util.h"
29 #include "time-util.h"
32 int path_split_and_make_absolute(const char *p
, char ***ret
) {
39 l
= strv_split(p
, ":");
43 r
= path_strv_make_absolute_cwd(l
);
53 char *path_make_absolute(const char *p
, const char *prefix
) {
56 /* Makes every item in the list an absolute path by prepending
57 * the prefix, if specified and necessary */
59 if (path_is_absolute(p
) || isempty(prefix
))
62 return path_join(prefix
, p
);
65 int safe_getcwd(char **ret
) {
68 cwd
= get_current_dir_name();
70 return negative_errno();
72 /* Let's make sure the directory is really absolute, to protect us from the logic behind
83 int path_make_absolute_cwd(const char *p
, char **ret
) {
90 /* Similar to path_make_absolute(), but prefixes with the
91 * current working directory. */
93 if (path_is_absolute(p
))
96 _cleanup_free_
char *cwd
= NULL
;
98 r
= safe_getcwd(&cwd
);
102 c
= path_join(cwd
, p
);
111 int path_make_relative(const char *from
, const char *to
, char **ret
) {
112 _cleanup_free_
char *result
= NULL
;
122 /* Strips the common part, and adds ".." elements as necessary. */
124 if (!path_is_absolute(from
) || !path_is_absolute(to
))
128 r
= path_find_first_component(&from
, true, &f
);
132 k
= path_find_first_component(&to
, true, &t
);
139 /* from and to are equivalent. */
140 result
= strdup(".");
144 /* 'to' is inside of 'from'. */
149 path_simplify(result
);
151 if (!path_is_valid(result
))
155 *ret
= TAKE_PTR(result
);
159 if (r
!= k
|| !strneq(f
, t
, r
))
163 /* If we're here, then "from_dir" has one or more elements that need to
164 * be replaced with "..". */
166 for (n_parents
= 1;; n_parents
++) {
167 /* If this includes ".." we can't do a simple series of "..". */
168 r
= path_find_first_component(&from
, false, &f
);
175 if (isempty(t
) && n_parents
* 3 > PATH_MAX
)
176 /* PATH_MAX is counted *with* the trailing NUL byte */
179 result
= new(char, n_parents
* 3 + !isempty(t
) + strlen_ptr(t
));
183 for (p
= result
; n_parents
> 0; n_parents
--)
184 p
= mempcpy(p
, "../", 3);
187 /* Remove trailing slash and terminate string. */
189 *ret
= TAKE_PTR(result
);
195 path_simplify(result
);
197 if (!path_is_valid(result
))
200 *ret
= TAKE_PTR(result
);
204 char* path_startswith_strv(const char *p
, char **set
) {
205 STRV_FOREACH(s
, set
) {
208 t
= path_startswith(p
, *s
);
216 int path_strv_make_absolute_cwd(char **l
) {
219 /* Goes through every item in the string list and makes it
220 * absolute. This works in place and won't rollback any
221 * changes on failure. */
226 r
= path_make_absolute_cwd(*s
, &t
);
231 free_and_replace(*s
, t
);
237 char **path_strv_resolve(char **l
, const char *root
) {
245 /* Goes through every item in the string list and canonicalize
246 * the path. This works in place and won't rollback any
247 * changes on failure. */
250 _cleanup_free_
char *orig
= NULL
;
253 if (!path_is_absolute(*s
)) {
260 t
= path_join(root
, orig
);
268 r
= chase_symlinks(t
, root
, 0, &u
, NULL
);
286 x
= path_startswith(u
, root
);
288 /* restore the slash if it was lost */
289 if (!startswith(x
, "/"))
300 /* canonicalized path goes outside of
301 * prefix, keep the original path instead */
302 free_and_replace(u
, orig
);
318 char **path_strv_resolve_uniq(char **l
, const char *root
) {
323 if (!path_strv_resolve(l
, root
))
329 char *path_simplify(char *path
) {
330 bool add_slash
= false;
336 /* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
337 * Modifies the passed string in-place.
339 * ///foo//./bar/. becomes /foo/bar
340 * .//./foo//./bar/. becomes foo/bar
346 if (path_is_absolute(path
))
349 for (const char *p
= f
;;) {
352 r
= path_find_first_component(&p
, true, &e
);
360 /* if path is invalid, then refuse to simplify remaining part. */
361 memmove(f
, p
, strlen(p
) + 1);
371 /* Special rule, if we stripped everything, we need a "." for the current directory. */
379 int path_simplify_and_warn(
383 const char *filename
,
385 const char *lvalue
) {
387 bool fatal
= flag
& PATH_CHECK_FATAL
;
389 assert(!FLAGS_SET(flag
, PATH_CHECK_ABSOLUTE
| PATH_CHECK_RELATIVE
));
391 if (!utf8_is_valid(path
))
392 return log_syntax_invalid_utf8(unit
, LOG_ERR
, filename
, line
, path
);
394 if (flag
& (PATH_CHECK_ABSOLUTE
| PATH_CHECK_RELATIVE
)) {
397 absolute
= path_is_absolute(path
);
399 if (!absolute
&& (flag
& PATH_CHECK_ABSOLUTE
))
400 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
401 "%s= path is not absolute%s: %s",
402 lvalue
, fatal
? "" : ", ignoring", path
);
404 if (absolute
&& (flag
& PATH_CHECK_RELATIVE
))
405 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
406 "%s= path is absolute%s: %s",
407 lvalue
, fatal
? "" : ", ignoring", path
);
412 if (!path_is_valid(path
))
413 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
414 "%s= path has invalid length (%zu bytes)%s.",
415 lvalue
, strlen(path
), fatal
? "" : ", ignoring");
417 if (!path_is_normalized(path
))
418 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
419 "%s= path is not normalized%s: %s",
420 lvalue
, fatal
? "" : ", ignoring", path
);
425 char *path_startswith_full(const char *path
, const char *prefix
, bool accept_dot_dot
) {
429 /* Returns a pointer to the start of the first component after the parts matched by
431 * - both paths are absolute or both paths are relative,
433 * - each component in prefix in turn matches a component in path at the same position.
434 * An empty string will be returned when the prefix and path are equivalent.
436 * Returns NULL otherwise.
439 if ((path
[0] == '/') != (prefix
[0] == '/'))
446 r
= path_find_first_component(&path
, accept_dot_dot
, &p
);
450 k
= path_find_first_component(&prefix
, accept_dot_dot
, &q
);
455 return (char*) (p
?: path
);
460 if (!strneq(p
, q
, r
))
465 int path_compare(const char *a
, const char *b
) {
468 /* Order NULL before non-NULL */
473 /* A relative path and an absolute path must not compare as equal.
474 * Which one is sorted before the other does not really matter.
475 * Here a relative path is ordered before an absolute path. */
476 r
= CMP(path_is_absolute(a
), path_is_absolute(b
));
484 j
= path_find_first_component(&a
, true, &aa
);
485 k
= path_find_first_component(&b
, true, &bb
);
487 if (j
< 0 || k
< 0) {
488 /* When one of paths is invalid, order invalid path after valid one. */
489 r
= CMP(j
< 0, k
< 0);
493 /* fallback to use strcmp() if both paths are invalid. */
497 /* Order prefixes first: "/foo" before "/foo/bar" */
506 /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
507 r
= memcmp(aa
, bb
, MIN(j
, k
));
511 /* Sort "/foo/a" before "/foo/aaa" */
518 bool path_equal_or_files_same(const char *a
, const char *b
, int flags
) {
519 return path_equal(a
, b
) || files_same(a
, b
, flags
) > 0;
522 bool path_equal_filename(const char *a
, const char *b
) {
523 _cleanup_free_
char *a_basename
= NULL
, *b_basename
= NULL
;
529 r
= path_extract_filename(a
, &a_basename
);
531 log_debug_errno(r
, "Failed to parse basename of %s: %m", a
);
534 r
= path_extract_filename(b
, &b_basename
);
536 log_debug_errno(r
, "Failed to parse basename of %s: %m", b
);
540 return path_equal(a_basename
, b_basename
);
543 char* path_extend_internal(char **x
, ...) {
550 /* Joins all listed strings until the sentinel and places a "/" between them unless the strings end/begin
551 * already with one so that it is unnecessary. Note that slashes which are already duplicate won't be
552 * removed. The string returned is hence always equal to or longer than the sum of the lengths of each
555 * The first argument may be an already allocated string that is extended via realloc() if
556 * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
557 * first parameter to distinguish the two operations.
559 * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of which some
564 * path_join("foo", "bar") → "foo/bar"
565 * path_join("foo/", "bar") → "foo/bar"
566 * path_join("", "foo", "", "bar", "") → "foo/bar" */
568 sz
= old_sz
= x
? strlen_ptr(*x
) : 0;
570 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
577 if (sz
> SIZE_MAX
- add
) { /* overflow check */
586 nx
= realloc(x
? *x
: NULL
, GREEDY_ALLOC_ROUND_UP(sz
+1));
593 slash
= nx
[old_sz
-1] == '/';
596 slash
= true; /* no need to generate a slash anymore */
602 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
606 if (!slash
&& p
[0] != '/')
610 slash
= endswith(p
, "/");
617 static int check_x_access(const char *path
, int *ret_fd
) {
618 _cleanup_close_
int fd
= -1;
621 /* We need to use O_PATH because there may be executables for which we have only exec
622 * permissions, but not read (usually suid executables). */
623 fd
= open(path
, O_PATH
|O_CLOEXEC
);
627 r
= fd_verify_regular(fd
);
631 r
= access_fd(fd
, X_OK
);
633 /* /proc is not mounted. Fallback to access(). */
634 if (access(path
, X_OK
) < 0)
640 *ret_fd
= TAKE_FD(fd
);
645 static int find_executable_impl(const char *name
, const char *root
, char **ret_filename
, int *ret_fd
) {
646 _cleanup_close_
int fd
= -1;
647 _cleanup_free_
char *path_name
= NULL
;
652 /* Function chase_symlinks() is invoked only when root is not NULL, as using it regardless of
653 * root value would alter the behavior of existing callers for example: /bin/sleep would become
654 * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
655 * needed to avoid unforeseen regression or other complicated changes. */
657 r
= chase_symlinks(name
,
661 /* ret_fd= */ NULL
); /* prefix root to name in case full paths are not specified */
668 r
= check_x_access(name
, ret_fd
? &fd
: NULL
);
673 r
= path_make_absolute_cwd(name
, ret_filename
);
679 *ret_fd
= TAKE_FD(fd
);
684 int find_executable_full(const char *name
, const char *root
, char **exec_search_path
, bool use_path_envvar
, char **ret_filename
, int *ret_fd
) {
685 int last_error
= -ENOENT
, r
= 0;
686 const char *p
= NULL
;
691 return find_executable_impl(name
, root
, ret_filename
, ret_fd
);
694 /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
700 if (exec_search_path
) {
701 STRV_FOREACH(element
, exec_search_path
) {
702 _cleanup_free_
char *full_path
= NULL
;
704 if (!path_is_absolute(*element
))
707 full_path
= path_join(*element
, name
);
711 r
= find_executable_impl(full_path
, root
, ret_filename
, ret_fd
);
722 /* Resolve a single-component name to a full path */
724 _cleanup_free_
char *element
= NULL
;
726 r
= extract_first_word(&p
, &element
, ":", EXTRACT_RELAX
|EXTRACT_DONT_COALESCE_SEPARATORS
);
732 if (!path_is_absolute(element
))
735 if (!path_extend(&element
, name
))
738 r
= find_executable_impl(element
, root
, ret_filename
, ret_fd
);
740 /* PATH entries which we don't have access to are ignored, as per tradition. */
753 bool paths_check_timestamp(const char* const* paths
, usec_t
*timestamp
, bool update
) {
754 bool changed
= false, originally_unset
;
761 originally_unset
= *timestamp
== 0;
763 STRV_FOREACH(i
, paths
) {
767 if (stat(*i
, &stats
) < 0)
770 u
= timespec_load(&stats
.st_mtim
);
776 log_debug(originally_unset
? "Loaded timestamp for '%s'." : "Timestamp of '%s' changed.", *i
);
778 /* update timestamp */
789 static int executable_is_good(const char *executable
) {
790 _cleanup_free_
char *p
= NULL
, *d
= NULL
;
793 r
= find_executable(executable
, &p
);
799 /* An fsck that is linked to /bin/true is a non-existent fsck */
801 r
= readlink_malloc(p
, &d
);
802 if (r
== -EINVAL
) /* not a symlink */
807 return !PATH_IN_SET(d
, "true"
813 int fsck_exists(const char *fstype
) {
818 if (streq(fstype
, "auto"))
821 checker
= strjoina("fsck.", fstype
);
822 return executable_is_good(checker
);
825 char* dirname_malloc(const char *path
) {
826 char *d
, *dir
, *dir2
;
846 static const char *skip_slash_or_dot(const char *p
) {
847 for (; !isempty(p
); p
++) {
850 if (startswith(p
, "./")) {
859 int path_find_first_component(const char **p
, bool accept_dot_dot
, const char **ret
) {
860 const char *q
, *first
, *end_first
, *next
;
865 /* When a path is input, then returns the pointer to the first component and its length, and
866 * move the input pointer to the next component or nul. This skips both over any '/'
867 * immediately *before* and *after* the first component before returning.
870 * Input: p: "//.//aaa///bbbbb/cc"
871 * Output: p: "bbbbb///cc"
872 * ret: "aaa///bbbbb/cc"
873 * return value: 3 (== strlen("aaa"))
876 * Output: p: (pointer to NUL)
878 * return value: 3 (== strlen("aaa"))
880 * Input: p: "/", ".", ""
881 * Output: p: (pointer to NUL)
890 * Input: p: "(too long component)"
891 * Output: return value: -EINVAL
893 * (when accept_dot_dot is false)
894 * Input: p: "//..//aaa///bbbbb/cc"
895 * Output: return value: -EINVAL
900 first
= skip_slash_or_dot(q
);
901 if (isempty(first
)) {
907 if (streq(first
, ".")) {
914 end_first
= strchrnul(first
, '/');
915 len
= end_first
- first
;
919 if (!accept_dot_dot
&& len
== 2 && first
[0] == '.' && first
[1] == '.')
922 next
= skip_slash_or_dot(end_first
);
924 *p
= next
+ streq(next
, ".");
930 static const char *skip_slash_or_dot_backward(const char *path
, const char *q
) {
933 for (; q
>= path
; q
--) {
936 if (q
> path
&& strneq(q
- 1, "/.", 2))
943 int path_find_last_component(const char *path
, bool accept_dot_dot
, const char **next
, const char **ret
) {
944 const char *q
, *last_end
, *last_begin
;
947 /* Similar to path_find_first_component(), but search components from the end.
950 * Input: path: "//.//aaa///bbbbb/cc//././"
952 * Output: next: "/cc//././"
954 * return value: 2 (== strlen("cc"))
956 * Input: path: "//.//aaa///bbbbb/cc//././"
958 * Output: next: "///bbbbb/cc//././"
959 * ret: "bbbbb/cc//././"
960 * return value: 5 (== strlen("bbbbb"))
962 * Input: path: "/", ".", "", or NULL
963 * Output: next: equivalent to path
967 * Input: path: "(too long component)"
968 * Output: return value: -EINVAL
970 * (when accept_dot_dot is false)
971 * Input: path: "//..//aaa///bbbbb/cc/..//"
972 * Output: return value: -EINVAL
984 if (*next
< path
|| *next
> path
+ strlen(path
))
991 if (!IN_SET(**next
, '\0', '/'))
995 q
= path
+ strlen(path
) - 1;
997 q
= skip_slash_or_dot_backward(path
, q
);
998 if ((q
< path
) || /* the root directory */
999 (q
== path
&& *q
== '.')) { /* path is "." or "./" */
1009 while (q
>= path
&& *q
!= '/')
1013 len
= last_end
- last_begin
;
1017 if (!accept_dot_dot
&& len
== 2 && strneq(last_begin
, "..", 2))
1021 q
= skip_slash_or_dot_backward(path
, q
);
1033 const char *last_path_component(const char *path
) {
1035 /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1048 * Also, the empty string is mapped to itself.
1050 * This is different than basename(), which returns "" when a trailing slash is present.
1052 * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1060 l
= k
= strlen(path
);
1061 if (l
== 0) /* special case — an empty string */
1064 while (k
> 0 && path
[k
-1] == '/')
1067 if (k
== 0) /* the root directory */
1068 return path
+ l
- 1;
1070 while (k
> 0 && path
[k
-1] != '/')
1076 int path_extract_filename(const char *path
, char **ret
) {
1077 _cleanup_free_
char *a
= NULL
;
1078 const char *c
, *next
= NULL
;
1081 /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
1082 * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing
1085 * -EINVAL → if the path is not valid
1086 * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir
1087 * itself or "." is specified
1088 * -ENOMEM → no memory
1090 * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1091 * indicate the referenced file must be a directory.
1093 * This function guarantees to return a fully valid filename, i.e. one that passes
1094 * filename_is_valid() – this means "." and ".." are not accepted. */
1096 if (!path_is_valid(path
))
1099 r
= path_find_last_component(path
, false, &next
, &c
);
1102 if (r
== 0) /* root directory */
1103 return -EADDRNOTAVAIL
;
1110 return strlen(c
) > (size_t)r
? O_DIRECTORY
: 0;
1113 int path_extract_directory(const char *path
, char **ret
) {
1114 _cleanup_free_
char *a
= NULL
;
1115 const char *c
, *next
= NULL
;
1118 /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns:
1120 * -EINVAL → if the path is not valid
1121 * -EDESTADDRREQ → if no directory was specified in the passed in path, i.e. only a filename was passed
1122 * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e.
1123 * the root dir itself or "." was specified
1124 * -ENOMEM → no memory (surprise!)
1126 * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid().
1129 r
= path_find_last_component(path
, false, &next
, &c
);
1132 if (r
== 0) /* empty or root */
1133 return isempty(path
) ? -EINVAL
: -EADDRNOTAVAIL
;
1135 if (*path
!= '/') /* filename only */
1136 return -EDESTADDRREQ
;
1145 a
= strndup(path
, next
- path
);
1151 if (!path_is_valid(a
))
1158 bool filename_is_valid(const char *p
) {
1164 if (dot_or_dot_dot(p
))
1167 e
= strchrnul(p
, '/');
1171 if (e
- p
> NAME_MAX
) /* NAME_MAX is counted *without* the trailing NUL byte */
1177 bool path_is_valid_full(const char *p
, bool accept_dot_dot
) {
1181 for (const char *e
= p
;;) {
1184 r
= path_find_first_component(&e
, accept_dot_dot
, NULL
);
1188 if (e
- p
>= PATH_MAX
) /* Already reached the maximum length for a path? (PATH_MAX is counted
1189 * *with* the trailing NUL byte) */
1191 if (*e
== 0) /* End of string? Yay! */
1196 bool path_is_normalized(const char *p
) {
1197 if (!path_is_safe(p
))
1200 if (streq(p
, ".") || startswith(p
, "./") || endswith(p
, "/.") || strstr(p
, "/./"))
1203 if (strstr(p
, "//"))
1209 char *file_in_same_dir(const char *path
, const char *filename
) {
1216 /* This removes the last component of path and appends
1217 * filename, unless the latter is absolute anyway or the
1220 if (path_is_absolute(filename
))
1221 return strdup(filename
);
1223 e
= strrchr(path
, '/');
1225 return strdup(filename
);
1227 k
= strlen(filename
);
1228 ret
= new(char, (e
+ 1 - path
) + k
+ 1);
1232 memcpy(mempcpy(ret
, path
, e
+ 1 - path
), filename
, k
+ 1);
1236 bool hidden_or_backup_file(const char *filename
) {
1239 if (filename
[0] == '.' ||
1240 streq(filename
, "lost+found") ||
1241 streq(filename
, "aquota.user") ||
1242 streq(filename
, "aquota.group") ||
1243 endswith(filename
, "~"))
1246 const char *dot
= strrchr(filename
, '.');
1250 /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1251 * to come up with always new suffixes and that everybody else should just adjust to that, then it
1252 * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1253 * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1254 * possibly augmented with an additional string. Specifically: there's now:
1256 * The generic suffixes "~" and ".bak" for backup files
1257 * The generic prefix "." for hidden files
1259 * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1260 * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1261 * ".foopkg.old" or ".foopkg~" instead.
1264 return STR_IN_SET(dot
+ 1,
1284 bool is_device_path(const char *path
) {
1286 /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1289 return PATH_STARTSWITH_SET(path
, "/dev/", "/sys/");
1292 bool valid_device_node_path(const char *path
) {
1294 /* Some superficial checks whether the specified path is a valid device node path, all without
1295 * looking at the actual device node. */
1297 if (!PATH_STARTSWITH_SET(path
, "/dev/", "/run/systemd/inaccessible/"))
1300 if (endswith(path
, "/")) /* can't be a device node if it ends in a slash */
1303 return path_is_normalized(path
);
1306 bool valid_device_allow_pattern(const char *path
) {
1309 /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1310 * DeviceAllow= and DeviceDeny=. */
1312 if (STARTSWITH_SET(path
, "block-", "char-"))
1315 return valid_device_node_path(path
);
1318 int systemd_installation_has_version(const char *root
, unsigned minimal_version
) {
1319 const char *pattern
;
1322 /* Try to guess if systemd installation is later than the specified version. This
1323 * is hacky and likely to yield false negatives, particularly if the installation
1324 * is non-standard. False positives should be relatively rare.
1327 NULSTR_FOREACH(pattern
,
1328 /* /lib works for systems without usr-merge, and for systems with a sane
1329 * usr-merge, where /lib is a symlink to /usr/lib. /usr/lib is necessary
1330 * for Gentoo which does a merge without making /lib a symlink.
1332 "lib/systemd/libsystemd-shared-*.so\0"
1333 "lib64/systemd/libsystemd-shared-*.so\0"
1334 "usr/lib/systemd/libsystemd-shared-*.so\0"
1335 "usr/lib64/systemd/libsystemd-shared-*.so\0") {
1337 _cleanup_strv_free_
char **names
= NULL
;
1338 _cleanup_free_
char *path
= NULL
;
1341 path
= path_join(root
, pattern
);
1345 r
= glob_extend(&names
, path
, 0);
1351 assert_se(c
= endswith(path
, "*.so"));
1352 *c
= '\0'; /* truncate the glob part */
1354 STRV_FOREACH(name
, names
) {
1355 /* This is most likely to run only once, hence let's not optimize anything. */
1359 t
= startswith(*name
, path
);
1363 t2
= endswith(t
, ".so");
1367 t2
[0] = '\0'; /* truncate the suffix */
1369 r
= safe_atou(t
, &version
);
1371 log_debug_errno(r
, "Found libsystemd shared at \"%s.so\", but failed to parse version: %m", *name
);
1375 log_debug("Found libsystemd shared at \"%s.so\", version %u (%s).",
1377 version
>= minimal_version
? "OK" : "too old");
1378 if (version
>= minimal_version
)
1386 bool dot_or_dot_dot(const char *path
) {
1396 return path
[2] == 0;
1399 bool empty_or_root(const char *path
) {
1401 /* For operations relative to some root directory, returns true if the specified root directory is
1402 * redundant, i.e. either / or NULL or the empty string or any equivalent. */
1407 return path_equal(path
, "/");
1410 bool path_strv_contains(char **l
, const char *path
) {
1412 if (path_equal(*i
, path
))
1418 bool prefixed_path_strv_contains(char **l
, const char *path
) {
1419 STRV_FOREACH(i
, l
) {
1426 if (path_equal(j
, path
))