From 1c8bd682151586419d2144274e5dc8c20915b151 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 28 Dec 2015 13:53:27 +0100 Subject: get hook-script pathname MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/satd.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/satd.c b/src/satd.c index 62f4872..0d3578f 100644 --- a/src/satd.c +++ b/src/satd.c @@ -21,6 +21,7 @@ */ #include #include +#include #include #include #include @@ -108,6 +109,47 @@ fail: } +/** + * Construct the pathname for the hook script. + * + * @param env The environment variable to use for the beginning + * of the pathname, `NULL` for the home directory. + * @param suffix The rest of the pathname. + * @return The pathname. + * + * @throws 0 The environment variable is not set, or, if `env` is + * `NULL` the user is root or homeless. + */ +static char * +hookpath(const char *env, const char *suffix) +{ + const char *prefix = NULL; + char *path; + struct passwd *pwd; + + if (!env) { + if (!getuid()) + goto try_next; + pwd = getpwuid(getuid()); + prefix = pwd ? pwd->pw_dir : NULL; + } else { + prefix = getenv(env); + } + if (!prefix || !*prefix) + goto try_next; + + path = malloc((strlen(prefix) + strlen(suffix) + 1) * sizeof(char)); + t (!path); + stpcpy(stpcpy(path, prefix), suffix); + + return path; +fail: + return NULL; +try_next: + return errno = 0, NULL; +} + + /** * The sat daemon. * @@ -169,7 +211,19 @@ main(int argc, char *argv[]) } /* Get hook-script pathname. */ - /* TODO */ + if (!getenv("SAT_HOOK_PATH")) { + int do_not_free = 0; + path = hookpath("XDG_CONFIG_HOME", "/sat/hook"); + t (!path && errno); + path = path ? path : hookpath("HOME", "/.config/sat/hook"); + t (!path && errno); + path = path ? path : hookpath(NULL, "/.config/sat/hook"); + t (!path && errno); + path = path ? path : (do_not_free = 1, "/etc/sat/hook"); + t (setenv("SAT_HOOK_PATH", path, 1)); + if (!do_not_free) + free(path); + } /* Listen for incoming conections. */ #if SOMAXCONN < SATD_BACKLOG -- cgit v1.2.3-70-g09d2