summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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;
}