server: call afs_send_chunk() even if no timeout occured.
[paraslash.git] / exec.c
1 /*
2  * Copyright (C) 2003-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file exec.c helper functions for spawning new processes */
20 #include "para.h"
21 #include "close_on_fork.h"
22 #include "error.h"
23 #include "string.h"
24
25 /**
26  * spawn a new process and redirect fd 0, 1, and 2
27  *
28  * \param pid will hold the pid of the created process upon return
29  * \param file path of the executable to execute
30  * \param args the argument array for the command
31  * \param fds a pointer to a value-result array
32  *
33  * \return Negative on errors, positive on success.
34  *
35  * \sa null(4), pipe(2), dup2(2), fork(2), exec(3)
36  */
37 static int para_exec(pid_t *pid, const char *file, char *const args[], int *fds)
38 {
39         int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
40                 null = -1; /* ;) */
41
42         ret = -E_DUP_PIPE;
43         if (fds[0] > 0 && pipe(in) < 0)
44                 goto err_out;
45         if (fds[1] > 0 && pipe(out) < 0)
46                 goto err_out;
47         if (fds[2] > 0 && pipe(err) < 0)
48                 goto err_out;
49         if (!fds[0] || !fds[1] || !fds[2]) {
50                 ret = -E_NULL_OPEN;
51                 null = open("/dev/null", O_RDONLY);
52                 if (null < 0)
53                         goto err_out;
54         }
55         if ((*pid = fork()) < 0)
56                 exit(EXIT_FAILURE);
57         if (!(*pid)) { /* child */
58                 close_listed_fds(); /* close unneeded fds */
59                 if (fds[0] >= 0) {
60                         if (fds[0]) {
61                                 close(in[1]);
62                                 if (in[0] != STDIN_FILENO)
63                                         dup2(in[0], STDIN_FILENO);
64                         } else
65                                 dup2(null, STDIN_FILENO);
66                 }
67                 if (fds[1] >= 0) {
68                         if (fds[1]) {
69                                 close(out[0]);
70                                 if (out[1] != STDOUT_FILENO)
71                                         dup2(out[1], STDOUT_FILENO);
72                         } else
73                                 dup2(null, STDOUT_FILENO);
74                 }
75                 if (fds[2] >= 0) {
76                         if (fds[2]) {
77                                 close(err[0]);
78                                 if (err[1] != STDERR_FILENO)
79                                         dup2(err[1], STDERR_FILENO);
80                         } else
81                                 dup2(null, STDERR_FILENO);
82                 }
83                 if (null >= 0)
84                         close(null);
85                 execvp(file, args);
86                 _exit(EXIT_FAILURE);
87         }
88         if (fds[0] > 0) {
89                 close(in[0]);
90                 *fds = in[1];
91         }
92         if (fds[1] > 0) {
93                 close(out[1]);
94                 *(fds + 1) = out[0];
95         }
96         if (fds[2] > 0) {
97                 close(err[1]);
98                 *(fds + 2) = err[0];
99         }
100         if (null >= 0)
101                 close(null);
102         return 1;
103 err_out:
104         if (err[0] >= 0)
105                 close(err[0]);
106         if (err[1] >= 0)
107                 close(err[1]);
108         if (out[0] >= 0)
109                 close(out[0]);
110         if (out[1] >= 0)
111                 close(out[1]);
112         if (in[0] >= 0)
113                 close(in[0]);
114         if (in[1] >= 0)
115                 close(in[1]);
116         if (null >= 0)
117                 close(null);
118         return ret;
119 }
120
121
122 /**
123  * exec the given command
124  *
125  * \param pid will hold the pid of the created process upon return
126  * \param cmdline holds the command and its arguments, seperated by spaces
127  * \param fds a pointer to a value-result array
128  *
129  * This function uses fork/exec to create a new process. \a fds must be a
130  * pointer to three integers, corresponding to stdin, stdout and stderr
131  * respectively. It specifies how to deal with fd 0, 1, 2 in the child. The
132  * contents of \a fds are interpreted as follows:
133  *
134  *      - fd[i] < 0: leave fd \a i alone
135  *      - fd[i] = 0: dup fd \a i to /dev/null
136  *      - fd[i] > 0: create a pipe and dup i to one end of that pipe.
137  *      Upon return, fd[i] contains the file descriptor of the pipe.
138  *
139  *      In any case, all unneeded filedescriptors are closed.
140  *
141  * \return positive on success, negative on errors
142  */
143 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds)
144 {
145         int argc, ret;
146         char **argv, *tmp = para_strdup(cmdline);
147
148         if (!tmp)
149                 exit(EXIT_FAILURE);
150         argc = split_args(tmp, &argv, " \t");
151         ret = para_exec(pid, argv[0], argv, fds);
152         free(argv);
153         free(tmp);
154         return ret;
155 }
156