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