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