From: Jose Bollo Date: Mon, 27 Aug 2018 16:25:30 +0000 (+0200) Subject: main-afb-daemon: Set AFB_ROOTDIR and AFB_WORKDIR in environment X-Git-Tag: 5.99.5~2 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fapp-framework-binder.git;a=commitdiff_plain;h=5dd7df31306b95a3fafe6d3238d4553107a6c70f main-afb-daemon: Set AFB_ROOTDIR and AFB_WORKDIR in environment It may be difficult to retrieve the root directory from bindings and from process spawn by option --exec. In the same way, well identifying the workdir might interest programs or bindings. So, from now, the environment variables below are set: - AFB_ROOTDIR: identify the rootdir as set by --rootdir and in the context of AGL it will be the widget directory - AFB_WORKDIR: identify the workdir as set by --workdir To avoid any confusion, the function realpath is used to export absolute path names. Bug-AGL: SPEC-1694 Change-Id: Id272e009ca975e28aaab8b14fa2a98fbd2216e73 Signed-off-by: Jose Bollo Signed-off-by: José Bollo --- diff --git a/src/afb-config.c b/src/afb-config.c index c10b2937..38dd9b71 100644 --- a/src/afb-config.c +++ b/src/afb-config.c @@ -170,8 +170,8 @@ static struct option_desc optdefs[] = { {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default " d2s(DEFAULT_CACHE_TIMEOUT) "]"}, {SET_WORK_DIR, 1, "workdir", "Set the working directory [default: $PWD or current working directory]"}, - {SET_UPLOAD_DIR, 1, "uploaddir", "Directory for uploading files [default: workdir]"}, - {SET_ROOT_DIR, 1, "rootdir", "Root Directory of the application [default: workdir]"}, + {SET_UPLOAD_DIR, 1, "uploaddir", "Directory for uploading files [default: workdir] relative to workdir"}, + {SET_ROOT_DIR, 1, "rootdir", "Root Directory of the application [default: workdir] relative to workdir"}, {ADD_LDPATH, 1, "ldpaths", "Load bindings from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"}, {ADD_BINDING, 1, "binding", "Load the binding of path"}, diff --git a/src/main-afb-daemon.c b/src/main-afb-daemon.c index cb362a8b..0db84bc1 100644 --- a/src/main-afb-daemon.c +++ b/src/main-afb-daemon.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include #include @@ -74,6 +76,44 @@ struct json_object *main_config; static pid_t childpid; +/** + * Tiny helper around putenv: add the variable name=value + * + * @param name name of the variable to set + * @param value value to set to the variable + * + * @return 0 in case of success or -1 in case of error (with errno set to ENOMEM) + */ +static int addenv(const char *name, const char *value) +{ + char *head, *middle; + + head = malloc(2 + strlen(name) + strlen(value)); + if (head == NULL) { + errno = ENOMEM; + return -1; + } + middle = stpcpy(head, name); + middle[0] = '='; + strcpy(&middle[1], value); + return putenv(head); +} + +/** + * Tiny helper around addenv that export the real path + * + * @param name name of the variable to set + * @param path the path value to export to the variable + * + * @return 0 in case of success or -1 in case of error (with errno set to ENOMEM) + */ +static int addenv_realpath(const char *name, const char *path) +{ + char buffer[PATH_MAX]; + char *p = realpath(path, buffer); + return p ? addenv(name, p) : -1; +} + /*---------------------------------------------------------- | helpers for handling list of arguments +--------------------------------------------------------- */ @@ -669,7 +709,12 @@ static void start(int signum, void *arg) goto error; } if (afb_common_rootdir_set(rootdir) < 0) { - ERROR("failed to set common root directory"); + ERROR("failed to set common root directory %s", rootdir); + goto error; + } + if (addenv_realpath("AFB_WORKDIR", "." /* resolved by realpath */) + || addenv_realpath("AFB_ROOTDIR", rootdir /* relative to current directory */)) { + ERROR("can't set environment"); goto error; }