-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)
-{
- int rc;
- char **args;
-
- /* fork the master child */
- children[0] = fork();
- if (children[0] < 0) {
- ERROR("master fork failed: %m");
- return -1;
- }
- if (children[0]) {
- /********* in the parent process ************/
- return 0;
- }
-
- /********* in the master child ************/
-
- /* avoid set-gid effect */
- setresgid(groupid, groupid, groupid);
-
- /* enter the process group */
- rc = setpgid(0, 0);
- if (rc) {
- ERROR("setpgid failed");
- _exit(1);
- }
-
- /* enter security mode */
- rc = secmgr_prepare_exec(desc->tag);
- if (rc < 0) {
- ERROR("call to secmgr_prepare_exec failed: %m");
- _exit(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);
- }
-
- 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]);
- }
- _exit(1);
-}
-
-static int launchexec2(struct afm_launch_desc *desc, pid_t children[2], struct launchparam *params)
+/*
+ * Launchs (fork-execs) the program described by 'exec'
+ * using the parameters of 'desc' and 'params' to instantiate
+ * it. The created process is attached to the process group 'progrp'.
+ *
+ * After being created and before to be launched, the process
+ * is put in its security environment and its directory is
+ * changed to params->datadir.
+ *
+ * Returns the pid of the created process or -1 in case of error.
+ */
+static pid_t launch(
+ struct afm_launch_desc *desc,
+ struct launchparam *params,
+ struct exec_vector *exec,
+ pid_t progrp
+)