2 * Copyright (c) 2007 Joerg Sonnenberger
3 * Copyright (c) 2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include "archive_platform.h"
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))
33 __FBSDID("$FreeBSD: head/lib/libarchive/filter_fork.c 182958 2008-09-12 05:33:00Z kientzle $");
35 #if defined(HAVE_SYS_TYPES_H)
36 # include <sys/types.h>
44 #if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H))
45 # if defined(HAVE_POLL_H)
47 # elif defined(HAVE_SYS_POLL_H)
48 # include <sys/poll.h>
50 #elif defined(HAVE_SELECT)
51 # if defined(HAVE_SYS_SELECT_H)
52 # include <sys/select.h>
53 # elif defined(HAVE_UNISTD_H)
71 #include "archive_cmdline_private.h"
73 #include "filter_fork.h"
76 __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
80 int stdin_pipe[2], stdout_pipe[2], tmp;
82 posix_spawn_file_actions_t actions;
85 struct archive_cmdline *cmdline;
87 cmdline = __archive_cmdline_allocate();
90 if (__archive_cmdline_parse(cmdline, cmd) != ARCHIVE_OK)
93 if (pipe(stdin_pipe) == -1)
95 if (stdin_pipe[0] == 1 /* stdout */) {
96 if ((tmp = dup(stdin_pipe[0])) == -1)
101 if (pipe(stdout_pipe) == -1)
103 if (stdout_pipe[1] == 0 /* stdin */) {
104 if ((tmp = dup(stdout_pipe[1])) == -1)
106 close(stdout_pipe[1]);
107 stdout_pipe[1] = tmp;
110 #if HAVE_POSIX_SPAWNP
112 r = posix_spawn_file_actions_init(&actions);
117 r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[1]);
120 r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[0]);
123 /* Setup for stdin. */
124 r = posix_spawn_file_actions_adddup2(&actions, stdin_pipe[0], 0);
127 if (stdin_pipe[0] != 0 /* stdin */) {
128 r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[0]);
132 /* Setup for stdout. */
133 r = posix_spawn_file_actions_adddup2(&actions, stdout_pipe[1], 1);
136 if (stdout_pipe[1] != 1 /* stdout */) {
137 r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[1]);
141 r = posix_spawnp(&child, cmdline->path, &actions, NULL,
142 cmdline->argv, NULL);
145 posix_spawn_file_actions_destroy(&actions);
147 #else /* HAVE_POSIX_SPAWNP */
157 close(stdin_pipe[1]);
158 close(stdout_pipe[0]);
159 if (dup2(stdin_pipe[0], 0 /* stdin */) == -1)
161 if (stdin_pipe[0] != 0 /* stdin */)
162 close(stdin_pipe[0]);
163 if (dup2(stdout_pipe[1], 1 /* stdout */) == -1)
165 if (stdout_pipe[1] != 1 /* stdout */)
166 close(stdout_pipe[1]);
167 execvp(cmdline->path, cmdline->argv);
170 #endif /* HAVE_POSIX_SPAWNP */
172 close(stdin_pipe[0]);
173 close(stdout_pipe[1]);
175 *child_stdin = stdin_pipe[1];
176 fcntl(*child_stdin, F_SETFL, O_NONBLOCK);
177 *child_stdout = stdout_pipe[0];
178 fcntl(*child_stdout, F_SETFL, O_NONBLOCK);
179 __archive_cmdline_free(cmdline);
184 #if HAVE_POSIX_SPAWNP
187 posix_spawn_file_actions_destroy(&actions);
190 close(stdout_pipe[0]);
191 close(stdout_pipe[1]);
193 close(stdin_pipe[0]);
194 close(stdin_pipe[1]);
196 __archive_cmdline_free(cmdline);
197 return ARCHIVE_FAILED;
201 __archive_check_child(int in, int out)
203 #if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H))
204 struct pollfd fds[2];
210 fds[idx].events = POLLOUT;
215 fds[idx].events = POLLIN;
219 poll(fds, idx, -1); /* -1 == INFTIM, wait forever */
220 #elif defined(HAVE_SELECT)
221 fd_set fds_in, fds_out, fds_error;
227 FD_SET(out, &fds_in);
228 FD_SET(out, &fds_error);
231 FD_SET(in, &fds_out);
232 FD_SET(in, &fds_error);
234 select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL);
240 #endif /* defined(HAVE_PIPE) && defined(HAVE_VFORK) && defined(HAVE_FCNTL) */