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