summaryrefslogtreecommitdiffstats
path: root/src/basic
diff options
context:
space:
mode:
authorMike Yuan <me@yhndnzj.com>2024-07-22 22:17:05 +0200
committerMike Yuan <me@yhndnzj.com>2024-07-23 18:48:15 +0200
commit51b271ead7c1e767607064a57a3024bb7d9c2e2f (patch)
tree84215b8b659c31158750f3b053c3ab7e9647297a /src/basic
parentcore: clean up Set/LoadCredential= parsers (diff)
downloadsystemd-51b271ead7c1e767607064a57a3024bb7d9c2e2f.tar.xz
systemd-51b271ead7c1e767607064a57a3024bb7d9c2e2f.zip
path-util: trivial cleanup for find_executable_full()
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/path-util.c53
-rw-r--r--src/basic/path-util.h8
2 files changed, 33 insertions, 28 deletions
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index 068fb42960..0b4d7b6cf7 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -653,7 +653,7 @@ static int find_executable_impl(const char *name, const char *root, char **ret_f
* /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
* needed to avoid unforeseen regression or other complicated changes. */
if (root) {
- /* prefix root to name in case full paths are not specified */
+ /* prefix root to name in case full paths are not specified */
r = chase(name, root, CHASE_PREFIX_ROOT, &path_name, /* ret_fd= */ NULL);
if (r < 0)
return r;
@@ -680,48 +680,49 @@ static int find_executable_impl(const char *name, const char *root, char **ret_f
int find_executable_full(
const char *name,
const char *root,
- char **exec_search_path,
+ char * const *exec_search_path,
bool use_path_envvar,
char **ret_filename,
int *ret_fd) {
int last_error = -ENOENT, r = 0;
- const char *p = NULL;
assert(name);
if (is_path(name))
return find_executable_impl(name, root, ret_filename, ret_fd);
- if (use_path_envvar)
- /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
- * binary. */
- p = getenv("PATH");
- if (!p)
- p = default_PATH();
-
if (exec_search_path) {
STRV_FOREACH(element, exec_search_path) {
_cleanup_free_ char *full_path = NULL;
- if (!path_is_absolute(*element))
+ if (!path_is_absolute(*element)) {
+ log_debug("Exec search path '%s' isn't absolute, ignoring.", *element);
continue;
+ }
full_path = path_join(*element, name);
if (!full_path)
return -ENOMEM;
r = find_executable_impl(full_path, root, ret_filename, ret_fd);
- if (r < 0) {
- if (r != -EACCES)
- last_error = r;
- continue;
- }
- return 0;
+ if (r >= 0)
+ return 0;
+ if (r != -EACCES)
+ last_error = r;
}
return last_error;
}
+ const char *p = NULL;
+
+ if (use_path_envvar)
+ /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
+ * binary. */
+ p = getenv("PATH");
+ if (!p)
+ p = default_PATH();
+
/* Resolve a single-component name to a full path */
for (;;) {
_cleanup_free_ char *element = NULL;
@@ -732,22 +733,20 @@ int find_executable_full(
if (r == 0)
break;
- if (!path_is_absolute(element))
+ if (!path_is_absolute(element)) {
+ log_debug("Exec search path '%s' isn't absolute, ignoring.", element);
continue;
+ }
if (!path_extend(&element, name))
return -ENOMEM;
r = find_executable_impl(element, root, ret_filename, ret_fd);
- if (r < 0) {
- /* PATH entries which we don't have access to are ignored, as per tradition. */
- if (r != -EACCES)
- last_error = r;
- continue;
- }
-
- /* Found it! */
- return 0;
+ if (r >= 0) /* Found it! */
+ return 0;
+ /* PATH entries which we don't have access to are ignored, as per tradition. */
+ if (r != -EACCES)
+ last_error = r;
}
return last_error;
diff --git a/src/basic/path-util.h b/src/basic/path-util.h
index 8b5c8fbf3c..dff5a3a549 100644
--- a/src/basic/path-util.h
+++ b/src/basic/path-util.h
@@ -115,7 +115,13 @@ int path_strv_make_absolute_cwd(char **l);
char** path_strv_resolve(char **l, const char *root);
char** path_strv_resolve_uniq(char **l, const char *root);
-int find_executable_full(const char *name, const char *root, char **exec_search_path, bool use_path_envvar, char **ret_filename, int *ret_fd);
+int find_executable_full(
+ const char *name,
+ const char *root,
+ char * const *exec_search_path,
+ bool use_path_envvar,
+ char **ret_filename,
+ int *ret_fd);
static inline int find_executable(const char *name, char **ret_filename) {
return find_executable_full(name, /* root= */ NULL, NULL, true, ret_filename, NULL);
}