Switch to gengetopt's group options and add more documentation.
[dss.git] / exec.c
1 /** \file exec.c Helper functions for spawning new processes. */
2
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <assert.h>
9 #include <stdlib.h>
10
11
12 #include "gcc-compat.h"
13 #include "error.h"
14 #include "string.h"
15
16
17 /**
18  * Spawn a new process and redirect fd 0, 1, and 2.
19  *
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.
24  *
25  * \return Standard.
26  *
27  * \sa null(4), pipe(2), dup2(2), fork(2), exec(3).
28  */
29 int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds)
30 {
31         int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
32                 null = -1; /* ;) */
33
34         ret = -E_DUP_PIPE;
35         if (fds[0] > 0 && pipe(in) < 0)
36                 goto err_out;
37         if (fds[1] > 0 && pipe(out) < 0)
38                 goto err_out;
39         if (fds[2] > 0 && pipe(err) < 0)
40                 goto err_out;
41         if (!fds[0] || !fds[1] || !fds[2]) {
42                 ret = -E_NULL_OPEN;
43                 null = open("/dev/null", O_RDONLY);
44                 if (null < 0)
45                         goto err_out;
46         }
47         if ((*pid = fork()) < 0)
48                 exit(EXIT_FAILURE);
49         if (!(*pid)) { /* child */
50                 if (fds[0] >= 0) {
51                         if (fds[0]) {
52                                 close(in[1]);
53                                 if (in[0] != STDIN_FILENO)
54                                         dup2(in[0], STDIN_FILENO);
55                         } else
56                                 dup2(null, STDIN_FILENO);
57                 }
58                 if (fds[1] >= 0) {
59                         if (fds[1]) {
60                                 close(out[0]);
61                                 if (out[1] != STDOUT_FILENO)
62                                         dup2(out[1], STDOUT_FILENO);
63                         } else
64                                 dup2(null, STDOUT_FILENO);
65                 }
66                 if (fds[2] >= 0) {
67                         if (fds[2]) {
68                                 close(err[0]);
69                                 if (err[1] != STDERR_FILENO)
70                                         dup2(err[1], STDERR_FILENO);
71                         } else
72                                 dup2(null, STDERR_FILENO);
73                 }
74                 if (null >= 0)
75                         close(null);
76                 execvp(file, args);
77                 _exit(EXIT_FAILURE);
78         }
79         if (fds[0] > 0) {
80                 close(in[0]);
81                 *fds = in[1];
82         }
83         if (fds[1] > 0) {
84                 close(out[1]);
85                 *(fds + 1) = out[0];
86         }
87         if (fds[2] > 0) {
88                 close(err[1]);
89                 *(fds + 2) = err[0];
90         }
91         if (null >= 0)
92                 close(null);
93         return 1;
94 err_out:
95         make_err_msg("failed to exec %s", file);
96         if (err[0] >= 0)
97                 close(err[0]);
98         if (err[1] >= 0)
99                 close(err[1]);
100         if (out[0] >= 0)
101                 close(out[0]);
102         if (out[1] >= 0)
103                 close(out[1]);
104         if (in[0] >= 0)
105                 close(in[0]);
106         if (in[1] >= 0)
107                 close(in[1]);
108         if (null >= 0)
109                 close(null);
110         return ret;
111 }