Set application id when forking
[src/app-framework-main.git] / src / afm-launch.c
index 87c5b12..9ed5966 100644 (file)
@@ -1,5 +1,5 @@
 /*
- Copyright 2015 IoT.bzh
+ Copyright 2015, 2016, 2017 IoT.bzh
 
  author: José Bollo <jose.bollo@iot.bzh>
 
@@ -109,6 +109,7 @@ static const char readystr[] = "READY=1";
  */
 static const int ready_timeout = 1500;
 
+#if defined(DUMP_LAUNCHERS)
 /*
  * dump all the known launchers to the 'file'
  */
@@ -132,6 +133,7 @@ static void dump_launchers(FILE *file)
                fprintf(file, "\n");
        }
 }
+#endif
 
 /*
  * update 'cread' to point the the next token
@@ -487,6 +489,7 @@ static int mkport()
 /*
 %% %
 %a appid                       desc->appid
+%b bindings                    desc->bindings
 %c content                     desc->content
 %D datadir                     params->datadir
 %H height                      desc->height
@@ -494,7 +497,6 @@ static int mkport()
 %I icondir                     FWK_ICON_DIR
 %m mime-type                   desc->type
 %n name                                desc->name
-%p plugins                     desc->plugins
 %P port                                params->port
 %r rootdir                     desc->path
 %R readyfd                      params->readyfd
@@ -571,6 +573,9 @@ static union arguments instantiate_arguments(
                                        c = *p++;
                                        switch (c) {
                                        case 'a': v = desc->appid; break;
+                                       case 'b':
+                                               v = "" /*TODO:desc->bindings*/;
+                                               break;
                                        case 'c': v = desc->content; break;
                                        case 'D': v = params->datadir; break;
                                        case 'H':
@@ -589,9 +594,6 @@ static union arguments instantiate_arguments(
                                                                params->port);
                                                v = port;
                                                break;
-                                       case 'p':
-                                               v = "" /*TODO:desc->plugins*/;
-                                               break;
                                        case 'R':
                                                if(!data)
                                                        sprintf(readyfd, "%d",
@@ -669,7 +671,7 @@ static pid_t launch(
 )
 {
        int rc;
-       char **args;
+       char **args, **env;
        pid_t pid;
        int rpipe[2];
        struct pollfd pfd;
@@ -684,9 +686,13 @@ static pid_t launch(
        /* instanciate the arguments */
        params->readyfd = rpipe[1];
        args = instantiate_arguments(exec->args, desc, params, 1).vector;
-       if (args == NULL) {
+       env = instantiate_arguments((const char * const*)environ,
+                                               desc, params, 1).vector;
+       if (args == NULL || env == NULL) {
                close(rpipe[0]);
                close(rpipe[1]);
+               free(args);
+               free(env);
                ERROR("out of memory in master");
                errno = ENOMEM;
                return -1;
@@ -701,6 +707,7 @@ static pid_t launch(
                close(rpipe[0]);
                close(rpipe[1]);
                free(args);
+               free(env);
                ERROR("master fork failed: %m");
                return -1;
        }
@@ -710,6 +717,7 @@ static pid_t launch(
 
                close(rpipe[1]);
                free(args);
+               free(env);
                pfd.fd = rpipe[0];
                pfd.events = POLLIN;
 
@@ -723,6 +731,9 @@ static pid_t launch(
 
        close(rpipe[0]);
 
+       /* set name by appid */
+       verbose_set_name(desc->appid, 0);
+
        /* avoid set-gid effect */
        setresgid(groupid, groupid, groupid);
 
@@ -759,7 +770,7 @@ static pid_t launch(
        }
 
        /* executes the process */
-       rc = execve(args[0], args, environ);
+       rc = execve(args[0], args, env);
        ERROR("failed to exec master %s: %m", args[0]);
        _exit(1);
        return -1;
@@ -848,7 +859,7 @@ static struct desc_launcher *search_launcher(const char *type,
        for (dl = launchers ; dl ; dl = dl->next)
                if (dl->mode == mode)
                        for (tl = dl->types ; tl != NULL ; tl = tl->next)
-                               if (!strcmp(tl->type, type))
+                               if (!strcasecmp(tl->type, type))
                                        return dl;
        return NULL;
 }
@@ -934,8 +945,13 @@ int afm_launch_initialize()
        else
                groupid = (gid_t)-1;
 
+       /* reads the configuration file */
        rc = read_configuration_file(FWK_LAUNCH_CONF);
-       /* dump_launchers(stderr); */
+#if defined(DUMP_LAUNCHERS)
+       if (!rc)
+               dump_launchers(stderr);
+#endif
+       
        return rc;
 }