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