4e7acd51b1a5ade3036ffd35222615dad26c5298
[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 #include <signal.h>
11
12
13 #include "gcc-compat.h"
14 #include "error.h"
15 #include "string.h"
16
17
18 /**
19  * Spawn a new process and redirect fd 0, 1, and 2.
20  *
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.
25  *
26  * \return Standard.
27  *
28  * \sa null(4), pipe(2), dup2(2), fork(2), exec(3).
29  */
30 int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds)
31 {
32         int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
33                 null = -1; /* ;) */
34
35         ret = -E_DUP_PIPE;
36         if (fds[0] > 0 && pipe(in) < 0)
37                 goto err_out;
38         if (fds[1] > 0 && pipe(out) < 0)
39                 goto err_out;
40         if (fds[2] > 0 && pipe(err) < 0)
41                 goto err_out;
42         if (!fds[0] || !fds[1] || !fds[2]) {
43                 ret = -E_NULL_OPEN;
44                 null = open("/dev/null", O_RDONLY);
45                 if (null < 0)
46                         goto err_out;
47         }
48         if ((*pid = fork()) < 0)
49                 exit(EXIT_FAILURE);
50         if (!(*pid)) { /* child */
51                 if (fds[0] >= 0) {
52                         if (fds[0]) {
53                                 close(in[1]);
54                                 if (in[0] != STDIN_FILENO)
55                                         dup2(in[0], STDIN_FILENO);
56                         } else
57                                 dup2(null, STDIN_FILENO);
58                 }
59                 if (fds[1] >= 0) {
60                         if (fds[1]) {
61                                 close(out[0]);
62                                 if (out[1] != STDOUT_FILENO)
63                                         dup2(out[1], STDOUT_FILENO);
64                         } else
65                                 dup2(null, STDOUT_FILENO);
66                 }
67                 if (fds[2] >= 0) {
68                         if (fds[2]) {
69                                 close(err[0]);
70                                 if (err[1] != STDERR_FILENO)
71                                         dup2(err[1], STDERR_FILENO);
72                         } else
73                                 dup2(null, STDERR_FILENO);
74                 }
75                 if (null >= 0)
76                         close(null);
77                 signal(SIGINT, SIG_DFL);
78                 signal(SIGTERM, SIG_DFL);
79                 signal(SIGCHLD, SIG_DFL);
80                 execvp(file, args);
81                 _exit(EXIT_FAILURE);
82         }
83         if (fds[0] > 0) {
84                 close(in[0]);
85                 *fds = in[1];
86         }
87         if (fds[1] > 0) {
88                 close(out[1]);
89                 *(fds + 1) = out[0];
90         }
91         if (fds[2] > 0) {
92                 close(err[1]);
93                 *(fds + 2) = err[0];
94         }
95         if (null >= 0)
96                 close(null);
97         return 1;
98 err_out:
99         make_err_msg("failed to exec %s", file);
100         if (err[0] >= 0)
101                 close(err[0]);
102         if (err[1] >= 0)
103                 close(err[1]);
104         if (out[0] >= 0)
105                 close(out[0]);
106         if (out[1] >= 0)
107                 close(out[1]);
108         if (in[0] >= 0)
109                 close(in[0]);
110         if (in[1] >= 0)
111                 close(in[1]);
112         if (null >= 0)
113                 close(null);
114         return ret;
115 }
116
117
118 /**
119  * Exec the given command.
120  *
121  * \param pid Will hold the pid of the created process upon return.
122  * \param cmdline Holds the command and its arguments, seperated by spaces.
123  * \param fds A pointer to a value-result array.
124  *
125  * This function uses fork/exec to create a new process. \a fds must be a
126  * pointer to three integers, corresponding to stdin, stdout and stderr
127  * respectively. It specifies how to deal with fd 0, 1, 2 in the child. The
128  * contents of \a fds are interpreted as follows:
129  *
130  *      - fd[i] < 0: leave fd \a i alone.
131  *      - fd[i] = 0: dup fd \a i to \p /dev/null.
132  *      - fd[i] > 0: create a pipe and dup i to one end of that pipe.
133  *      Upon return, fd[i] contains the file descriptor of the pipe.
134  *
135  *      In any case, all unneeded filedescriptors are closed.
136  *
137  * \return Standard.
138  */
139 int dss_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds)
140 {
141         int argc, ret;
142         char **argv;
143         char *tmp = dss_strdup(cmdline);
144
145         if (!tmp)
146                 exit(EXIT_FAILURE);
147         argc = split_args(tmp, &argv, " \t");
148         ret = dss_exec(pid, argv[0], argv, fds);
149         free(argv);
150         free(tmp);
151         return ret;
152 }
153
154