1 /** \file exec.c Helper functions for spawning new processes. */
12 #include "gcc-compat.h"
18 * Spawn a new process and redirect fd 0, 1, and 2.
20 * \param pid Will hold the pid of the created process upon return.
21 * \param file Path of the executable to execute.
22 * \param args The argument array for the command.
23 * \param fds a Pointer to a value-result array.
27 * \sa null(4), pipe(2), dup2(2), fork(2), exec(3).
29 int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds)
31 int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
35 if (fds[0] > 0 && pipe(in) < 0)
37 if (fds[1] > 0 && pipe(out) < 0)
39 if (fds[2] > 0 && pipe(err) < 0)
41 if (!fds[0] || !fds[1] || !fds[2]) {
43 null = open("/dev/null", O_RDONLY);
47 if ((*pid = fork()) < 0)
49 if (!(*pid)) { /* child */
53 if (in[0] != STDIN_FILENO)
54 dup2(in[0], STDIN_FILENO);
56 dup2(null, STDIN_FILENO);
61 if (out[1] != STDOUT_FILENO)
62 dup2(out[1], STDOUT_FILENO);
64 dup2(null, STDOUT_FILENO);
69 if (err[1] != STDERR_FILENO)
70 dup2(err[1], STDERR_FILENO);
72 dup2(null, STDERR_FILENO);
95 make_err_msg("failed to exec %s", file);