]> git.tuebingen.mpg.de Git - paraslash.git/blob - exec.c
exec: Simplify fd handling.
[paraslash.git] / exec.c
1 /* Copyright (C) 2003 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file exec.c Helper functions for spawning new processes. */
4
5 #include <regex.h>
6
7 #include "para.h"
8 #include "error.h"
9 #include "fd.h"
10 #include "string.h"
11
12 /**
13  * Spawn a new process and redirect fd 0, 1, and 2.
14  *
15  * \param pid Will hold the pid of the created process upon return.
16  * \param file Path of the executable to execute.
17  * \param args The argument array for the command.
18  * \param fds a Pointer to a value-result array.
19  *
20  * \return Standard.
21  *
22  * \sa null(4), pipe(2), dup2(2), fork(2), exec(3).
23  */
24 static int para_exec(pid_t *pid, const char *file, char *const *const args, int *fds)
25 {
26         int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
27                 null = -1; /* ;) */
28
29         ret = -E_DUP_PIPE;
30         if (fds[0] > 0 && pipe(in) < 0)
31                 goto err_out;
32         if (fds[1] > 0 && pipe(out) < 0)
33                 goto err_out;
34         if (fds[2] > 0 && pipe(err) < 0)
35                 goto err_out;
36         if (!fds[0] || !fds[1] || !fds[2]) {
37                 ret = para_open("/dev/null", O_RDWR, 42);
38                 if (ret < 0)
39                         goto err_out;
40                 null = ret;
41         }
42         ret = fork();
43         if (ret < 0) {
44                 ret = -ERRNO_TO_PARA_ERROR(errno);
45                 goto err_out;
46         }
47         *pid = ret;
48         if (!(*pid)) { /* child */
49                 if (fds[0] == 0)
50                         dup2(null, STDIN_FILENO);
51                 else if (fds[0] > 0) {
52                         close(in[1]);
53                         if (in[0] != STDIN_FILENO)
54                                 dup2(in[0], STDIN_FILENO);
55                 }
56                 if (fds[1] == 0)
57                         dup2(null, STDOUT_FILENO);
58                 else if (fds[1] > 0) {
59                         close(out[0]);
60                         if (out[1] != STDOUT_FILENO)
61                                 dup2(out[1], STDOUT_FILENO);
62                 }
63                 if (fds[2] == 0)
64                         dup2(null, STDERR_FILENO);
65                 else if (fds[2] > 0) {
66                         close(err[0]);
67                         if (err[1] != STDERR_FILENO)
68                                 dup2(err[1], STDERR_FILENO);
69                 }
70                 if (null >= 0)
71                         close(null);
72                 execvp(file, args);
73                 _exit(EXIT_FAILURE);
74         }
75         if (fds[0] > 0) {
76                 close(in[0]);
77                 *fds = in[1];
78         }
79         if (fds[1] > 0) {
80                 close(out[1]);
81                 *(fds + 1) = out[0];
82         }
83         if (fds[2] > 0) {
84                 close(err[1]);
85                 *(fds + 2) = err[0];
86         }
87         if (null >= 0)
88                 close(null);
89         return 1;
90 err_out:
91         if (err[0] >= 0)
92                 close(err[0]);
93         if (err[1] >= 0)
94                 close(err[1]);
95         if (out[0] >= 0)
96                 close(out[0]);
97         if (out[1] >= 0)
98                 close(out[1]);
99         if (in[0] >= 0)
100                 close(in[0]);
101         if (in[1] >= 0)
102                 close(in[1]);
103         if (null >= 0)
104                 close(null);
105         PARA_ERROR_LOG("%s\n", para_strerror(-ret));
106         return ret;
107 }
108
109 /**
110  * Exec the given command.
111  *
112  * \param pid Will hold the pid of the created process upon return.
113  * \param cmdline Holds the command and its arguments, separated by spaces.
114  * \param fds A pointer to a value-result array.
115  *
116  * This function uses fork/exec to create a new process. \a fds must be a
117  * pointer to three integers, corresponding to stdin, stdout and stderr
118  * respectively. It specifies how to deal with fd 0, 1, 2 in the child. The
119  * contents of \a fds are interpreted as follows:
120  *
121  *      - fd[i] < 0: leave fd \a i alone.
122  *      - fd[i] = 0: dup fd \a i to \p /dev/null.
123  *      - fd[i] > 0: create a pipe and dup i to one end of that pipe.
124  *      Upon return, fd[i] contains the file descriptor of the pipe.
125  *
126  *      In any case, all unneeded file descriptors are closed.
127  *
128  * \return Standard.
129  */
130 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds)
131 {
132         int ret;
133         char **argv;
134
135         ret = create_argv(cmdline, " \t", &argv);
136         if (ret < 0)
137                 return ret;
138         ret = para_exec(pid, argv[0], argv, fds);
139         free_argv(argv);
140         return ret;
141 }