From e1237f7e7f978e23c86cf51b69742291c316f75c Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Sat, 27 Mar 2010 02:58:57 +0200 Subject: [PATCH] Added t_binary_abspath(). --HG-- branch : HEAD --- src/lib-master/master-service-settings.c | 22 +++------------- src/lib/abspath.c | 32 ++++++++++++++++++++++++ src/lib/abspath.h | 8 ++++++ 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/lib-master/master-service-settings.c b/src/lib-master/master-service-settings.c index 40c1d07ccf..49e87c5761 100644 --- a/src/lib-master/master-service-settings.c +++ b/src/lib-master/master-service-settings.c @@ -66,25 +66,9 @@ static void ATTR_NORETURN master_service_exec_config(struct master_service *service, const struct master_service_settings_input *input) { - const char **conf_argv, *path, *const *paths, *binary_path; - - binary_path = service->argv[0]; - if (*service->argv[0] == '/') { - /* already have the path */ - } else if (strchr(service->argv[0], '/') != NULL) { - /* relative to current directory */ - binary_path = t_abspath(service->argv[0]); - } else if ((path = getenv("PATH")) != NULL) { - /* we have to find our executable from path */ - paths = t_strsplit(path, ":"); - for (; *paths != NULL; paths++) { - path = t_strconcat(*paths, "/", binary_path, NULL); - if (access(path, X_OK) == 0) { - binary_path = path; - break; - } - } - } + const char **conf_argv, *binary_path = service->argv[0]; + + (void)t_binary_abspath(&binary_path); if (!service->keep_environment) master_service_env_clean(input->preserve_home); diff --git a/src/lib/abspath.c b/src/lib/abspath.c index 1ddbde263f..8bee1f036e 100644 --- a/src/lib/abspath.c +++ b/src/lib/abspath.c @@ -1,8 +1,10 @@ /* Copyright (c) 2009-2010 Dovecot authors, see the included COPYING file */ #include "lib.h" +#include "str.h" #include "abspath.h" +#include #include const char *t_abspath(const char *path) @@ -63,3 +65,33 @@ int t_readlink(const char *path, const char **dest_r) *dest_r = dest; return 0; } + +bool t_binary_abspath(const char **binpath) +{ + const char *path_env, *const *paths; + string_t *path; + + if (**binpath == '/') { + /* already have absolute path */ + return TRUE; + } else if (strchr(*binpath, '/') != NULL) { + /* relative to current directory */ + *binpath = t_abspath(*binpath); + return TRUE; + } else if ((path_env = getenv("PATH")) != NULL) { + /* we have to find our executable from path */ + path = t_str_new(256); + paths = t_strsplit(path_env, ":"); + for (; *paths != NULL; paths++) { + str_append(path, *paths); + str_append_c(path, '/'); + str_append(path, *binpath); + if (access(str_c(path), X_OK) == 0) { + *binpath = str_c(path); + return TRUE; + } + str_truncate(path, 0); + } + } + return FALSE; +} diff --git a/src/lib/abspath.h b/src/lib/abspath.h index 27956fb775..efc9e780d3 100644 --- a/src/lib/abspath.h +++ b/src/lib/abspath.h @@ -12,4 +12,12 @@ int t_get_current_dir(const char **dir_r); /* Returns symlink destination, allocated from data stack. */ int t_readlink(const char *path, const char **dest_r); +/* Update binpath to be absolute: + a) begins with '/' -> no change + b) contains '/' -> assume relative to working directory + c) set to first executable that's found from $PATH + + If no usable binary was found, return FALSE. */ +bool t_binary_abspath(const char **binpath); + #endif -- 2.47.3