fix the plm database tool
[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  * This function uses fork/exec to create a new process.  \a fds must be a
34  * pointer to three integers, corresponding to stdin, stdout and stderr
35  * respectively. It specifies how to deal with fd 0, 1, 2 in the child.  The
36  * contents of \a fds are interpreted as follows:
37  *
38  *      - fd[i] < 0: leave fd \a i alone
39  *      - fd[i] = 0: dup fd \a i to /dev/null
40  *      - fd[i] > 0: create a pipe and dup i to one end of that pipe.
41  *      Upon return, fd[i] contains the file descriptor of the pipe.
42  *
43  *      In any case, all unneeded filedescriptors are closed.
44  *
45  * \return Negative on errors, positive on success.
46  *
47  * \sa null(4), pipe(2), dup2(2), fork(2), exec(3)
48  */
49 int para_exec(pid_t *pid, const char *file, char *const args[], int *fds)
50 {
51         int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
52                 null = -1; /* ;) */
53
54         ret = -E_DUP_PIPE;
55         if (fds[0] > 0 && pipe(in) < 0)
56                 goto err_out;
57         if (fds[1] > 0 && pipe(out) < 0)
58                 goto err_out;
59         if (fds[2] > 0 && pipe(err) < 0)
60                 goto err_out;
61         if (!fds[0] || !fds[1] || !fds[2]) {
62                 ret = -E_NULL_OPEN;
63                 null = open("/dev/null", O_RDONLY);
64                 if (null < 0)
65                         goto err_out;
66         }
67         if ((*pid = fork()) < 0)
68                 exit(EXIT_FAILURE);
69         if (!(*pid)) { /* child */
70                 close_listed_fds(); /* close unneeded fds */
71                 if (fds[0] >= 0) {
72                         if (fds[0]) {
73                                 close(in[1]);
74                                 if (in[0] != STDIN_FILENO)
75                                         dup2(in[0], STDIN_FILENO);
76                         } else
77                                 dup2(null, STDIN_FILENO);
78                 }
79                 if (fds[1] >= 0) {
80                         if (fds[1]) {
81                                 close(out[0]);
82                                 if (out[1] != STDOUT_FILENO)
83                                         dup2(out[1], STDOUT_FILENO);
84                         } else
85                                 dup2(null, STDOUT_FILENO);
86                 }
87                 if (fds[2] >= 0) {
88                         if (fds[2]) {
89                                 close(err[0]);
90                                 if (err[1] != STDERR_FILENO)
91                                         dup2(err[1], STDERR_FILENO);
92                         } else
93                                 dup2(null, STDERR_FILENO);
94                 }
95                 if (null >= 0)
96                         close(null);
97                 execvp(file, args);
98                 _exit(EXIT_FAILURE);
99         }
100         if (fds[0] > 0) {
101                 close(in[0]);
102                 *fds = in[1];
103         }
104         if (fds[1] > 0) {
105                 close(out[1]);
106                 *(fds + 1) = out[0];
107         }
108         if (fds[2] > 0) {
109                 close(err[1]);
110                 *(fds + 2) = err[0];
111         }
112         if (null >= 0)
113                 close(null);
114         return 1;
115 err_out:
116         if (err[0] >= 0)
117                 close(err[0]);
118         if (err[1] >= 0)
119                 close(err[1]);
120         if (out[0] >= 0)
121                 close(out[0]);
122         if (out[1] >= 0)
123                 close(out[1]);
124         if (in[0] >= 0)
125                 close(in[0]);
126         if (in[1] >= 0)
127                 close(in[1]);
128         if (null >= 0)
129                 close(null);
130         return ret;
131 }
132
133
134 /**
135  * exec the given command
136  *
137  * \param pid the same meaning as in para_exec()
138  * \param cmdline holds the command and its arguments, seperated by spaces
139  * \param fds the same meaning as in para_exec()
140  *
141  * A wrapper for para_exec() which calls split_args() to seperate
142  * the command line arguments.
143  *
144  * \return positive on success, negative on errors
145  */
146 int para_exec_cmdline_pid(pid_t *pid, char *cmdline, int *fds)
147 {
148         int argc, ret;
149         char **argv, *tmp = para_strdup(cmdline);
150
151         if (!tmp)
152                 exit(EXIT_FAILURE);
153         argc = split_args(tmp, &argv, ' ');
154         ret = para_exec(pid, argv[0], argv, fds);
155         free(argv);
156         free(tmp);
157         return ret;
158 }
159
160 /**
161  * check whether a file exists
162  *
163  * \param fn the file name
164  *
165  * \return Non-zero iff file exists.
166  */
167 int file_exists(const char *fn)
168 {
169         struct stat statbuf;
170
171         return !stat(fn, &statbuf);
172 }