main-afb-daemon: Set AFB_ROOTDIR and AFB_WORKDIR in environment 83/16383/2
authorJose Bollo <jose.bollo@iot.bzh>
Mon, 27 Aug 2018 16:25:30 +0000 (18:25 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 28 Aug 2018 14:34:05 +0000 (16:34 +0200)
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>
src/afb-config.c
src/main-afb-daemon.c

index c10b293..38dd9b7 100644 (file)
@@ -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"},
index cb362a8..0db84bc 100644 (file)
@@ -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;
        }