2 * Copyright (C) 2003-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file exec.c helper functions for spawning new processes */
21 #include "close_on_fork.h"
26 * spawn a new process and redirect fd 0, 1, and 2
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
33 * \return Negative on errors, positive on success.
35 * \sa null(4), pipe(2), dup2(2), fork(2), exec(3)
37 static int para_exec(pid_t *pid, const char *file, char *const args[], int *fds)
39 int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
43 if (fds[0] > 0 && pipe(in) < 0)
45 if (fds[1] > 0 && pipe(out) < 0)
47 if (fds[2] > 0 && pipe(err) < 0)
49 if (!fds[0] || !fds[1] || !fds[2]) {
51 null = open("/dev/null", O_RDONLY);
55 if ((*pid = fork()) < 0)
57 if (!(*pid)) { /* child */
58 close_listed_fds(); /* close unneeded fds */
62 if (in[0] != STDIN_FILENO)
63 dup2(in[0], STDIN_FILENO);
65 dup2(null, STDIN_FILENO);
70 if (out[1] != STDOUT_FILENO)
71 dup2(out[1], STDOUT_FILENO);
73 dup2(null, STDOUT_FILENO);
78 if (err[1] != STDERR_FILENO)
79 dup2(err[1], STDERR_FILENO);
81 dup2(null, STDERR_FILENO);
123 * exec the given command
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
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:
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.
139 * In any case, all unneeded filedescriptors are closed.
141 * \return positive on success, negative on errors
143 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds)
146 char **argv, *tmp = para_strdup(cmdline);
150 argc = split_args(tmp, &argv, " \t");
151 ret = para_exec(pid, argv[0], argv, fds);