#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <poll.h>
+#include <signal.h>
extern char **environ;
char type[1];
};
+struct exec_vector {
+ int has_readyfd;
+ const char **args;
+};
+
struct desc_list {
struct desc_list *next;
enum afm_launch_mode mode;
struct type_list *types;
- char **execs[2];
+ struct exec_vector execs[2];
};
struct launchparam {
int port;
+ int readyfd;
+ char **uri;
const char *secret;
const char *datadir;
- const char **master;
- const char **slave;
+ struct exec_vector *execs;
};
struct confread {
static gid_t groupid = 0;
-const char separators[] = " \t\n";
+static const char separators[] = " \t\n";
+static const char readystr[] = "READY=1";
+static const int ready_timeout = 1500;
static void dump_launchers()
{
for (type = desc->types ; type != NULL ; type = type->next)
printf("%s\n", type->type);
for ( j = 0 ; j < 2 ; j++)
- if (desc->execs[j] != NULL) {
- for (k = 0 ; desc->execs[j][k] != NULL ; k++)
- printf(" %s", desc->execs[j][k]);
+ if (desc->execs[j].args != NULL) {
+ for (k = 0 ; desc->execs[j].args[k] != NULL ; k++)
+ printf(" %s", desc->execs[j].args[k]);
printf("\n");
}
printf("\n");
static int next_token(struct confread *cread)
{
- cread->index += cread->length + strspn(&cread->buffer[cread->index + cread->length], separators);
+ int idx = cread->index + cread->length;
+ cread->index = idx + strspn(&cread->buffer[idx], separators);
cread->length = strcspn(&cread->buffer[cread->index], separators);
return cread->length;
}
return 0;
}
-static char **read_vector(struct confread *cread)
+static const char **read_vector(struct confread *cread)
{
int index0, length0;
- char **vector, *args;
+ const char **vector;
+ char *args;
int count, length;
/* record origin */
/* check no extra characters */
if (next_token(cread)) {
- ERROR("%s:%d: extra characters found after type %.*s", cread->filepath, cread->lineno, length, &cread->buffer[index]);
+ ERROR("%s:%d: extra characters found after type %.*s",
+ cread->filepath, cread->lineno, length, &cread->buffer[index]);
errno = EINVAL;
return NULL;
}
/* check no extra characters */
if (next_token(cread)) {
- ERROR("%s:%d: extra characters found after mode %.*s", cread->filepath, cread->lineno, length, &cread->buffer[index]);
+ ERROR("%s:%d: extra characters found after mode %.*s",
+ cread->filepath, cread->lineno, length, &cread->buffer[index]);
errno = EINVAL;
return invalid_launch_mode;
}
cread->buffer[index + length] = 0;
result = launch_mode_of_string(&cread->buffer[index]);
if (result == invalid_launch_mode) {
- ERROR("%s:%d: invalid mode value %s", cread->filepath, cread->lineno, &cread->buffer[index]);
+ ERROR("%s:%d: invalid mode value %s",
+ cread->filepath, cread->lineno, &cread->buffer[index]);
errno = EINVAL;
}
return result;
static int read_launchers(struct confread *cread)
{
- int rc;
+ int rc, has_readyfd;
struct type_list *types, *lt;
struct desc_list *desc;
enum afm_launch_mode mode;
- char **vector;
+ const char **vector;
/* reads the file */
lt = NULL;
rc = read_line(cread);
while (rc > 0) {
if (cread->index == 0) {
- if (cread->length == 4 && !memcmp(&cread->buffer[cread->index], "mode", 4)) {
+ if (cread->length == 4
+ && !memcmp(&cread->buffer[cread->index], "mode", 4)) {
/* check if allowed */
if (types != NULL) {
- ERROR("%s:%d: mode found before launch vector", cread->filepath, cread->lineno);
+ ERROR("%s:%d: mode found before launch vector",
+ cread->filepath, cread->lineno);
errno = EINVAL;
free_type_list(types);
return -1;
return -1;
} else {
if (mode == invalid_launch_mode) {
- ERROR("%s:%d: mode not found before type", cread->filepath, cread->lineno);
+ ERROR("%s:%d: mode not found before type",
+ cread->filepath, cread->lineno);
errno = EINVAL;
assert(types == NULL);
return -1;
desc = NULL;
} else if (types == NULL && desc == NULL) {
if (lt == NULL)
- ERROR("%s:%d: untyped launch vector found", cread->filepath, cread->lineno);
+ ERROR("%s:%d: untyped launch vector found",
+ cread->filepath, cread->lineno);
else
- ERROR("%s:%d: extra launch vector found (2 max)", cread->filepath, cread->lineno);
+ ERROR("%s:%d: extra launch vector found (2 max)",
+ cread->filepath, cread->lineno);
errno = EINVAL;
return -1;
} else {
+ has_readyfd = NULL != strstr(&cread->buffer[cread->index], "%R");
vector = read_vector(cread);
if (vector == NULL) {
- ERROR("%s:%d: out of memory", cread->filepath, cread->lineno);
+ ERROR("%s:%d: out of memory",
+ cread->filepath, cread->lineno);
free_type_list(types);
errno = ENOMEM;
return -1;
assert(desc == NULL);
desc = malloc(sizeof * desc);
if (desc == NULL) {
- ERROR("%s:%d: out of memory", cread->filepath, cread->lineno);
+ ERROR("%s:%d: out of memory",
+ cread->filepath, cread->lineno);
free_type_list(types);
errno = ENOMEM;
return -1;
desc->next = launchers;
desc->mode = mode;
desc->types = types;
- desc->execs[0] = vector;
- desc->execs[1] = NULL;
+ desc->execs[0].has_readyfd = has_readyfd;
+ desc->execs[0].args = vector;
+ desc->execs[1].has_readyfd = 0;
+ desc->execs[1].args = NULL;
types = NULL;
launchers = desc;
} else {
- desc->execs[1] = vector;
+ desc->execs[1].has_readyfd = has_readyfd;
+ desc->execs[1].args = vector;
desc = NULL;
}
}
rc = read_line(cread);
}
if (types != NULL) {
- ERROR("%s:%d: end of file found before launch vector", cread->filepath, cread->lineno);
+ ERROR("%s:%d: end of file found before launch vector",
+ cread->filepath, cread->lineno);
free_type_list(types);
errno = EINVAL;
return -1;
}
/*
-%I icondir FWK_ICON_DIR
-%P port params->port
-%S secret params->secret
-%D datadir params->datadir
-%r rootdir desc->path
-%h homedir desc->home
-%t tag (smack label) desc->tag
+%% %
%a appid desc->appid
%c content desc->content
+%D datadir params->datadir
+%H height desc->height
+%h homedir desc->home
+%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
+%S secret params->secret
%W width desc->width
-%H height desc->height
-%% %
*/
-static char **instantiate_arguments(const char **args, struct afm_launch_desc *desc, struct launchparam *params)
+union arguments {
+ char *scalar;
+ char **vector;
+};
+
+static union arguments instantiate_arguments(
+ const char * const *args,
+ struct afm_launch_desc *desc,
+ struct launchparam *params,
+ int wants_vector
+)
{
- const char **iter, *p, *v;
- char *data, **result, port[20], width[20], height[20], mini[3], c;
+ const char * const *iter;
+ const char *p, *v;
+ char *data, port[20], width[20], height[20], readyfd[20], mini[3], c, sep;
int n, s;
+ union arguments result;
/* init */
+ sep = wants_vector ? 0 : ' ';
mini[0] = '%';
mini[2] = 0;
/* loop that either compute the size and build the result */
+ result.vector = NULL;
+ result.scalar = NULL;
data = NULL;
- result = NULL;
n = s = 0;
for (;;) {
iter = args;
n = 0;
while (*iter) {
p = *iter++;
- if (data)
- result[n] = data;
+ if (data && !sep)
+ result.vector[n] = data;
n++;
while((c = *p++) != 0) {
if (c != '%') {
} else {
c = *p++;
switch (c) {
- case 'I': v = FWK_ICON_DIR; break;
- case 'P': if(!data) sprintf(port, "%d", params->port); v = port; break;
- case 'S': v = params->secret; break;
- case 'D': v = params->datadir; break;
- case 'r': v = desc->path; break;
- case 'h': v = desc->home; break;
- case 't': v = desc->tag; break;
case 'a': v = desc->appid; break;
case 'c': v = desc->content; break;
+ case 'D': v = params->datadir; break;
+ case 'H':
+ if(!data)
+ sprintf(height, "%d", desc->height);
+ v = height;
+ break;
+ case 'h': v = desc->home; break;
+ case 'I': v = FWK_ICON_DIR; break;
case 'm': v = desc->type; break;
case 'n': v = desc->name; break;
+ case 'P':
+ if(!data)
+ sprintf(port, "%d", params->port);
+ v = port;
+ break;
case 'p': v = "" /*desc->plugins*/; break;
- case 'W': if(!data) sprintf(width, "%d", desc->width); v = width; break;
- case 'H': if(!data) sprintf(height, "%d", desc->height); v = height; break;
- case '%': c = 0;
- default: mini[1] = c; v = mini; break;
+ case 'R':
+ if(!data)
+ sprintf(readyfd, "%d", params->readyfd);
+ v = readyfd;
+ break;
+ case 'r': v = desc->path; break;
+ case 'S': v = params->secret; break;
+ case 'W':
+ if(!data)
+ sprintf(width, "%d", desc->width);
+ v = width;
+ break;
+ case '%':
+ c = 0;
+ default:
+ mini[1] = c;
+ v = mini;
+ break;
}
if (data)
data = stpcpy(data, v);
}
}
if (data)
- *data++ = 0;
+ *data++ = sep;
else
s++;
}
- if (data) {
- result[n] = NULL;
- return result;
- }
- /* allocation */
- result = malloc((n+1)*sizeof(char*) + s);
- if (result == NULL) {
- errno = ENOMEM;
- return NULL;
+ if (sep) {
+ assert(!wants_vector);
+ if (data) {
+ *--data = 0;
+ return result;
+ }
+ /* allocation */
+ result.scalar = malloc(s);
+ if (result.scalar == NULL) {
+ errno = ENOMEM;
+ return result;
+ }
+ data = result.scalar;
+ } else {
+ assert(wants_vector);
+ if (data) {
+ result.vector[n] = NULL;
+ return result;
+ }
+ /* allocation */
+ result.vector = malloc((n+1)*sizeof(char*) + s);
+ if (result.vector == NULL) {
+ errno = ENOMEM;
+ return result;
+ }
+ data = (char*)(&result.vector[n + 1]);
}
- data = (char*)(&result[n + 1]);
}
}
-static void mksecret(char buffer[9])
-{
- snprintf(buffer, 9, "%08lX", (0xffffffff & random()));
-}
-
-static int mkport()
-{
- static int port_ring = 12345;
- int port = port_ring;
- if (port < 12345 || port > 15432)
- port = 12345;
- port_ring = port + 1;
- return port;
-}
-
-static int launchexec1(struct afm_launch_desc *desc, pid_t children[2], struct launchparam *params)
+static pid_t launch(
+ struct afm_launch_desc *desc,
+ struct launchparam *params,
+ struct exec_vector *exec,
+ pid_t progrp
+)
{
int rc;
char **args;
+ pid_t pid;
+ int rpipe[2];
+ struct pollfd pfd;
+
+ /* prepare the pipes */
+ rc = pipe(rpipe);
+ if (rc < 0) {
+ ERROR("error while calling pipe2: %m");
+ return -1;
+ }
+
+ /* instanciate the arguments */
+ params->readyfd = rpipe[1];
+ args = instantiate_arguments(exec->args, desc, params, 1).vector;
+ if (args == NULL) {
+ close(rpipe[0]);
+ close(rpipe[1]);
+ ERROR("out of memory in master");
+ errno = ENOMEM;
+ return -1;
+ }
/* fork the master child */
- children[0] = fork();
- if (children[0] < 0) {
+ pid = fork();
+ if (pid < 0) {
+
+ /********* can't fork ************/
+
+ close(rpipe[0]);
+ close(rpipe[1]);
+ free(args);
ERROR("master fork failed: %m");
return -1;
}
- if (children[0]) {
+ if (pid) {
+
/********* in the parent process ************/
- return 0;
+
+ close(rpipe[1]);
+ free(args);
+ pfd.fd = rpipe[0];
+ pfd.events = POLLIN;
+ poll(&pfd, 1, ready_timeout);
+ close(rpipe[0]);
+ return pid;
}
- /********* in the master child ************/
+ /********* in the child process ************/
+
+ close(rpipe[0]);
/* avoid set-gid effect */
setresgid(groupid, groupid, groupid);
/* enter the process group */
- rc = setpgid(0, 0);
+ rc = setpgid(0, progrp);
if (rc) {
ERROR("setpgid failed");
_exit(1);
}
/* enter security mode */
- rc = secmgr_prepare_exec(desc->tag);
+ rc = secmgr_prepare_exec(desc->appid);
if (rc < 0) {
ERROR("call to secmgr_prepare_exec failed: %m");
_exit(1);
_exit(1);
}
- args = instantiate_arguments(params->master, desc, params);
- if (args == NULL) {
- ERROR("out of memory in master");
- }
- else {
- rc = execve(args[0], args, environ);
- ERROR("failed to exec master %s: %m", args[0]);
+ /* signal if needed */
+ if (!exec->has_readyfd) {
+ write(rpipe[1], readystr, sizeof(readystr) - 1);
+ close(rpipe[1]);
}
+
+ /* executes the process */
+ rc = execve(args[0], args, environ);
+ ERROR("failed to exec master %s: %m", args[0]);
_exit(1);
+ return -1;
}
-static int launchexec2(struct afm_launch_desc *desc, pid_t children[2], struct launchparam *params)
+static int launch_local(
+ struct afm_launch_desc *desc,
+ pid_t children[2],
+ struct launchparam *params
+)
{
- int rc;
- char message[10];
- int mpipe[2];
- int spipe[2];
- char **args;
-
- /* prepare the pipes */
- rc = pipe2(mpipe, O_CLOEXEC);
- if (rc < 0) {
- ERROR("error while calling pipe2: %m");
+ children[0] = launch(desc, params, ¶ms->execs[0], 0);
+ if (children[0] <= 0)
return -1;
- }
- rc = pipe2(spipe, O_CLOEXEC);
- if (rc < 0) {
- ERROR("error while calling pipe2: %m");
- close(spipe[0]);
- close(spipe[1]);
- return -1;
- }
- /* fork the master child */
- children[0] = fork();
- if (children[0] < 0) {
- ERROR("master fork failed: %m");
- close(mpipe[0]);
- close(mpipe[1]);
- close(spipe[0]);
- close(spipe[1]);
- return -1;
- }
- if (children[0]) {
- /********* in the parent process ************/
- close(mpipe[1]);
- close(spipe[0]);
- /* wait the ready signal (that transmit the slave pid) */
- rc = read(mpipe[0], &children[1], sizeof children[1]);
- close(mpipe[0]);
- if (rc <= 0) {
- ERROR("reading master pipe failed: %m");
- close(spipe[1]);
- return -1;
- }
- assert(rc == sizeof children[1]);
- /* start the child */
- rc = write(spipe[1], "start", 5);
- if (rc < 0) {
- ERROR("writing slave pipe failed: %m");
- close(spipe[1]);
- return -1;
- }
- assert(rc == 5);
- close(spipe[1]);
+ if (params->execs[1].args == NULL)
return 0;
- }
-
- /********* in the master child ************/
- close(mpipe[0]);
- close(spipe[1]);
-
- /* avoid set-gid effect */
- setresgid(groupid, groupid, groupid);
- /* enter the process group */
- rc = setpgid(0, 0);
- if (rc) {
- ERROR("setpgid failed");
- _exit(1);
- }
+ children[1] = launch(desc, params, ¶ms->execs[1], children[0]);
+ if (children[1] > 0)
+ return 0;
- /* enter security mode */
- rc = secmgr_prepare_exec(desc->tag);
- if (rc < 0) {
- ERROR("call to secmgr_prepare_exec failed: %m");
- _exit(1);
- }
+ killpg(children[0], SIGKILL);
+ return -1;
+}
- /* enter the datadirectory */
- rc = mkdir(params->datadir, 0755);
- if (rc && errno != EEXIST) {
- ERROR("creation of datadir %s failed: %m", params->datadir);
- _exit(1);
- }
- rc = chdir(params->datadir);
- if (rc) {
- ERROR("can't enter the datadir %s: %m", params->datadir);
- _exit(1);
- }
+static int launch_remote(
+ struct afm_launch_desc *desc,
+ pid_t children[2],
+ struct launchparam *params
+)
+{
+ char *uri;
- /* fork the slave child */
- children[1] = fork();
- if (children[1] < 0) {
- ERROR("slave fork failed: %m");
- _exit(1);
+ /* instanciate the uri */
+ if (params->execs[1].args == NULL)
+ uri = NULL;
+ else
+ uri = instantiate_arguments(params->execs[1].args, desc, params, 0).scalar;
+ if (uri == NULL) {
+ ERROR("out of memory for remote uri");
+ errno = ENOMEM;
+ return -1;
}
- if (children[1] == 0) {
- /********* in the slave child ************/
- close(mpipe[0]);
- rc = read(spipe[0], message, sizeof message);
- if (rc <= 0) {
- ERROR("reading slave pipe failed: %m");
- _exit(1);
- }
- args = instantiate_arguments(params->slave, desc, params);
- if (args == NULL) {
- ERROR("out of memory in slave");
- }
- else {
- rc = execve(args[0], args, environ);
- ERROR("failed to exec slave %s: %m", args[0]);
- }
- _exit(1);
+ /* launch the command */
+ children[0] = launch(desc, params, ¶ms->execs[0], 0);
+ if (children[0] <= 0) {
+ free(uri);
+ return -1;
}
- /********* still in the master child ************/
- close(spipe[1]);
- args = instantiate_arguments(params->master, desc, params);
- if (args == NULL) {
- ERROR("out of memory in master");
- }
- else {
- rc = write(mpipe[1], &children[1], sizeof children[1]);
- if (rc <= 0) {
- ERROR("can't write master pipe: %m");
- }
- else {
- close(mpipe[1]);
- rc = execve(args[0], args, environ);
- ERROR("failed to exec master %s: %m", args[0]);
- }
- }
- _exit(1);
+ *params->uri = uri;
+ return 0;
}
-int afm_launch_initialize()
+static void mksecret(char buffer[9])
{
- int rc;
- gid_t r, e, s;
-
- getresgid(&r, &e, &s);
- if (s && s != e)
- groupid = s;
- else
- groupid = -1;
+ snprintf(buffer, 9, "%08lX", (0xffffffff & random()));
+}
- rc = read_configuration_file(FWK_LAUNCH_CONF);
- dump_launchers();
- return rc;
+static int mkport()
+{
+ static int port_ring = 12345;
+ int port = port_ring;
+ if (port < 12345 || port > 15432)
+ port = 12345;
+ port_ring = port + 1;
+ return port;
}
static struct desc_list *search_launcher(const char *type, enum afm_launch_mode mode)
int afm_launch(struct afm_launch_desc *desc, pid_t children[2], char **uri)
{
- char datadir[PATH_MAX];
int rc;
+ char datadir[PATH_MAX];
char secret[9];
struct launchparam params;
const char *type;
/* should be init */
assert(groupid != 0);
assert(launch_mode_is_valid(desc->mode));
+ assert(desc->mode == mode_local || uri != NULL);
+ assert(uri == NULL || *uri == NULL);
/* init */
children[0] = 0;
}
/* prepare paths */
- rc = snprintf(datadir, sizeof datadir, "%s/%s", desc->home, desc->tag);
+ rc = snprintf(datadir, sizeof datadir, "%s/%s", desc->home, desc->appid);
if (rc < 0 || rc >= sizeof datadir) {
ERROR("overflow for datadir");
errno = EINVAL;
/* make the secret and port */
mksecret(secret);
+ params.uri = uri;
params.port = mkport();
params.secret = secret;
params.datadir = datadir;
- params.master = (const char **)dl->execs[0];
- params.slave = (const char **)dl->execs[1];
+ params.execs = dl->execs;
+
+ switch (desc->mode) {
+ case mode_local:
+ return launch_local(desc, children, ¶ms);
+ case mode_remote:
+ return launch_remote(desc, children, ¶ms);
+ default:
+ assert(0);
+ return -1;
+ }
+}
- return params.slave ? launchexec2(desc, children, ¶ms) : launchexec1(desc, children, ¶ms);
+int afm_launch_initialize()
+{
+ int rc;
+ gid_t r, e, s;
+
+ getresgid(&r, &e, &s);
+ if (s && s != e)
+ groupid = s;
+ else
+ groupid = -1;
+
+ rc = read_configuration_file(FWK_LAUNCH_CONF);
+ /* dump_launchers(); */
+ return rc;
}