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