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