aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Bollo <jose.bollo@iot.bzh>2018-08-27 18:25:30 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2018-08-28 16:34:05 +0200
commit5dd7df31306b95a3fafe6d3238d4553107a6c70f (patch)
treefa490b8ea052be83349136b43eb5b09d19b1f191
parent870652219335d54099ba67b721164b456b7975e9 (diff)
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 <jose.bollo@iot.bzh> Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-config.c4
-rw-r--r--src/main-afb-daemon.c47
2 files changed, 48 insertions, 3 deletions
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 <signal.h>
#include <string.h>
#include <unistd.h>
+#include <limits.h>
+#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
@@ -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;
}