1 /*-
2 * Copyright (c) 2007 Joerg Sonnenberger
3 * Copyright (c) 2012 Michihiro NAKAJIMA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "archive_platform.h"
28
29 /* This capability is only available on POSIX systems. */
30 #if defined(HAVE_PIPE) && defined(HAVE_FCNTL) && \
31 (defined(HAVE_FORK) || defined(HAVE_VFORK) || defined(HAVE_POSIX_SPAWNP))
32
33 #if defined(HAVE_SYS_TYPES_H)
34 # include <sys/types.h>
35 #endif
36 #ifdef HAVE_ERRNO_H
37 # include <errno.h>
38 #endif
39 #ifdef HAVE_STRING_H
40 # include <string.h>
41 #endif
42 #if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H))
43 # if defined(HAVE_POLL_H)
44 # include <poll.h>
45 # elif defined(HAVE_SYS_POLL_H)
46 # include <sys/poll.h>
47 # endif
48 #elif defined(HAVE_SELECT)
49 # if defined(HAVE_SYS_SELECT_H)
50 # include <sys/select.h>
51 # elif defined(HAVE_UNISTD_H)
52 # include <unistd.h>
53 # endif
54 #endif
55 #ifdef HAVE_FCNTL_H
56 # include <fcntl.h>
57 #endif
58 #ifdef HAVE_SPAWN_H
59 # include <spawn.h>
60 #endif
61 #ifdef HAVE_STDLIB_H
62 # include <stdlib.h>
63 #endif
64 #ifdef HAVE_UNISTD_H
65 # include <unistd.h>
66 #endif
67
68 #include "archive.h"
69 #include "archive_cmdline_private.h"
70
71 #include "filter_fork.h"
72
73 int
__archive_create_child(const char * cmd,int * child_stdin,int * child_stdout,pid_t * out_child)74 __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
75 pid_t *out_child)
76 {
77 pid_t child = -1;
78 int stdin_pipe[2], stdout_pipe[2], tmp;
79 #if HAVE_POSIX_SPAWNP
80 posix_spawn_file_actions_t actions;
81 int r;
82 #endif
83 struct archive_cmdline *cmdline;
84
85 cmdline = __archive_cmdline_allocate();
86 if (cmdline == NULL)
87 goto state_allocated;
88 if (__archive_cmdline_parse(cmdline, cmd) != ARCHIVE_OK)
89 goto state_allocated;
90
91 if (pipe(stdin_pipe) == -1)
92 goto state_allocated;
93 if (stdin_pipe[0] == 1 /* stdout */) {
94 if ((tmp = dup(stdin_pipe[0])) == -1)
95 goto stdin_opened;
96 close(stdin_pipe[0]);
97 stdin_pipe[0] = tmp;
98 }
99 if (pipe(stdout_pipe) == -1)
100 goto stdin_opened;
101 if (stdout_pipe[1] == 0 /* stdin */) {
102 if ((tmp = dup(stdout_pipe[1])) == -1)
103 goto stdout_opened;
104 close(stdout_pipe[1]);
105 stdout_pipe[1] = tmp;
106 }
107
108 #if HAVE_POSIX_SPAWNP
109
110 r = posix_spawn_file_actions_init(&actions);
111 if (r != 0) {
112 errno = r;
113 goto stdout_opened;
114 }
115 r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[1]);
116 if (r != 0)
117 goto actions_inited;
118 r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[0]);
119 if (r != 0)
120 goto actions_inited;
121 /* Setup for stdin. */
122 r = posix_spawn_file_actions_adddup2(&actions, stdin_pipe[0], 0);
123 if (r != 0)
124 goto actions_inited;
125 if (stdin_pipe[0] != 0 /* stdin */) {
126 r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[0]);
127 if (r != 0)
128 goto actions_inited;
129 }
130 /* Setup for stdout. */
131 r = posix_spawn_file_actions_adddup2(&actions, stdout_pipe[1], 1);
132 if (r != 0)
133 goto actions_inited;
134 if (stdout_pipe[1] != 1 /* stdout */) {
135 r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[1]);
136 if (r != 0)
137 goto actions_inited;
138 }
139 r = posix_spawnp(&child, cmdline->path, &actions, NULL,
140 cmdline->argv, NULL);
141 if (r != 0)
142 goto actions_inited;
143 posix_spawn_file_actions_destroy(&actions);
144
145 #else /* HAVE_POSIX_SPAWNP */
146
147 #if HAVE_VFORK
148 child = vfork();
149 #else
150 child = fork();
151 #endif
152 if (child == -1)
153 goto stdout_opened;
154 if (child == 0) {
155 close(stdin_pipe[1]);
156 close(stdout_pipe[0]);
157 if (dup2(stdin_pipe[0], 0 /* stdin */) == -1)
158 _exit(254);
159 if (stdin_pipe[0] != 0 /* stdin */)
160 close(stdin_pipe[0]);
161 if (dup2(stdout_pipe[1], 1 /* stdout */) == -1)
162 _exit(254);
163 if (stdout_pipe[1] != 1 /* stdout */)
164 close(stdout_pipe[1]);
165 execvp(cmdline->path, cmdline->argv);
166 _exit(254);
167 }
168 #endif /* HAVE_POSIX_SPAWNP */
169
170 close(stdin_pipe[0]);
171 close(stdout_pipe[1]);
172
173 *child_stdin = stdin_pipe[1];
174 fcntl(*child_stdin, F_SETFL, O_NONBLOCK);
175 *child_stdout = stdout_pipe[0];
176 fcntl(*child_stdout, F_SETFL, O_NONBLOCK);
177 __archive_cmdline_free(cmdline);
178
179 *out_child = child;
180 return ARCHIVE_OK;
181
182 #if HAVE_POSIX_SPAWNP
183 actions_inited:
184 errno = r;
185 posix_spawn_file_actions_destroy(&actions);
186 #endif
187 stdout_opened:
188 close(stdout_pipe[0]);
189 close(stdout_pipe[1]);
190 stdin_opened:
191 close(stdin_pipe[0]);
192 close(stdin_pipe[1]);
193 state_allocated:
194 __archive_cmdline_free(cmdline);
195 return ARCHIVE_FAILED;
196 }
197
198 void
__archive_check_child(int in,int out)199 __archive_check_child(int in, int out)
200 {
201 #if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H))
202 struct pollfd fds[2];
203 int idx;
204
205 idx = 0;
206 if (in != -1) {
207 fds[idx].fd = in;
208 fds[idx].events = POLLOUT;
209 ++idx;
210 }
211 if (out != -1) {
212 fds[idx].fd = out;
213 fds[idx].events = POLLIN;
214 ++idx;
215 }
216
217 poll(fds, idx, -1); /* -1 == INFTIM, wait forever */
218 #elif defined(HAVE_SELECT)
219 fd_set fds_in, fds_out, fds_error;
220
221 FD_ZERO(&fds_in);
222 FD_ZERO(&fds_out);
223 FD_ZERO(&fds_error);
224 if (out != -1) {
225 FD_SET(out, &fds_in);
226 FD_SET(out, &fds_error);
227 }
228 if (in != -1) {
229 FD_SET(in, &fds_out);
230 FD_SET(in, &fds_error);
231 }
232 select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL);
233 #else
234 sleep(1);
235 #endif
236 }
237
238 #endif /* defined(HAVE_PIPE) && defined(HAVE_VFORK) && defined(HAVE_FCNTL) */
239