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