1 /** \file exec.c Helper functions for spawning new processes. */
13 #include "gcc-compat.h"
19 * Spawn a new process and redirect fd 0, 1, and 2.
21 * \param pid Will hold the pid of the created process upon return.
22 * \param file Path of the executable to execute.
23 * \param args The argument array for the command.
24 * \param fds a Pointer to a value-result array.
28 * \sa null(4), pipe(2), dup2(2), fork(2), exec(3).
30 int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds)
32 int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
36 if (fds[0] > 0 && pipe(in) < 0)
38 if (fds[1] > 0 && pipe(out) < 0)
40 if (fds[2] > 0 && pipe(err) < 0)
42 if (!fds[0] || !fds[1] || !fds[2]) {
44 null = open("/dev/null", O_RDONLY);
48 if ((*pid = fork()) < 0)
50 if (!(*pid)) { /* child */
54 if (in[0] != STDIN_FILENO)
55 dup2(in[0], STDIN_FILENO);
57 dup2(null, STDIN_FILENO);
62 if (out[1] != STDOUT_FILENO)
63 dup2(out[1], STDOUT_FILENO);
65 dup2(null, STDOUT_FILENO);
70 if (err[1] != STDERR_FILENO)
71 dup2(err[1], STDERR_FILENO);
73 dup2(null, STDERR_FILENO);
77 signal(SIGINT, SIG_DFL);
78 signal(SIGTERM, SIG_DFL);
79 signal(SIGCHLD, SIG_DFL);
99 make_err_msg("failed to exec %s", file);