34643b8f61bf94affac5d45987e0389d87a9e9c7
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 * 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:
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.
43 * In any case, all unneeded filedescriptors are closed.
45 * \return Negative on errors, positive on success.
47 * \sa null(4), pipe(2), dup2(2), fork(2), exec(3)
49 int para_exec(pid_t
*pid
, const char *file
, char *const args
[], int *fds
)
51 int ret
, in
[2] = {-1, -1}, out
[2] = {-1, -1}, err
[2] = {-1, -1},
55 if (fds
[0] > 0 && pipe(in
) < 0)
57 if (fds
[1] > 0 && pipe(out
) < 0)
59 if (fds
[2] > 0 && pipe(err
) < 0)
61 if (!fds
[0] || !fds
[1] || !fds
[2]) {
63 null
= open("/dev/null", O_RDONLY
);
67 if ((*pid
= fork()) < 0)
69 if (!(*pid
)) { /* child */
70 close_listed_fds(); /* close unneeded fds */
74 if (in
[0] != STDIN_FILENO
)
75 dup2(in
[0], STDIN_FILENO
);
77 dup2(null
, STDIN_FILENO
);
82 if (out
[1] != STDOUT_FILENO
)
83 dup2(out
[1], STDOUT_FILENO
);
85 dup2(null
, STDOUT_FILENO
);
90 if (err
[1] != STDERR_FILENO
)
91 dup2(err
[1], STDERR_FILENO
);
93 dup2(null
, STDERR_FILENO
);
135 * exec the given command
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()
141 * A wrapper for para_exec() which calls split_args() to seperate
142 * the command line arguments.
144 * \return positive on success, negative on errors
146 int para_exec_cmdline_pid(pid_t
*pid
, char *cmdline
, int *fds
)
149 char **argv
, *tmp
= para_strdup(cmdline
);
153 argc
= split_args(tmp
, &argv
, ' ');
154 ret
= para_exec(pid
, argv
[0], argv
, fds
);
161 * check whether a file exists
163 * \param fn the file name
165 * \return Non-zero iff file exists.
167 int file_exists(const char *fn
)
171 return !stat(fn
, &statbuf
);