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)
79 int stdin_pipe[2], stdout_pipe[2], tmp;
81 posix_spawn_file_actions_t actions;
84 struct archive_cmdline *cmdline;
86 cmdline = __archive_cmdline_allocate();
89 if (__archive_cmdline_parse(cmdline, cmd) != ARCHIVE_OK)
92 if (pipe(stdin_pipe) == -1)
94 if (stdin_pipe[0] == 1 /* stdout */) {
95 if ((tmp = dup(stdin_pipe[0])) == -1)
100 if (pipe(stdout_pipe) == -1)
102 if (stdout_pipe[1] == 0 /* stdin */) {
103 if ((tmp = dup(stdout_pipe[1])) == -1)
105 close(stdout_pipe[1]);
106 stdout_pipe[1] = tmp;
109 #if HAVE_POSIX_SPAWNP
111 r = posix_spawn_file_actions_init(&actions);
116 r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[1]);
119 r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[0]);
122 /* Setup for stdin. */
123 r = posix_spawn_file_actions_adddup2(&actions, stdin_pipe[0], 0);
126 if (stdin_pipe[0] != 0 /* stdin */) {
127 r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[0]);
131 /* Setup for stdout. */
132 r = posix_spawn_file_actions_adddup2(&actions, stdout_pipe[1], 1);
135 if (stdout_pipe[1] != 1 /* stdout */) {
136 r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[1]);
140 r = posix_spawnp(&child, cmdline->path, &actions, NULL,
141 cmdline->argv, NULL);
144 posix_spawn_file_actions_destroy(&actions);
146 #else /* HAVE_POSIX_SPAWNP */
156 close(stdin_pipe[1]);
157 close(stdout_pipe[0]);
158 if (dup2(stdin_pipe[0], 0 /* stdin */) == -1)
160 if (stdin_pipe[0] != 0 /* stdin */)
161 close(stdin_pipe[0]);
162 if (dup2(stdout_pipe[1], 1 /* stdout */) == -1)
164 if (stdout_pipe[1] != 1 /* stdout */)
165 close(stdout_pipe[1]);
166 execvp(cmdline->path, cmdline->argv);
169 #endif /* HAVE_POSIX_SPAWNP */
171 close(stdin_pipe[0]);
172 close(stdout_pipe[1]);
174 *child_stdin = stdin_pipe[1];
175 fcntl(*child_stdin, F_SETFL, O_NONBLOCK);
176 *child_stdout = stdout_pipe[0];
177 fcntl(*child_stdout, F_SETFL, O_NONBLOCK);
178 __archive_cmdline_free(cmdline);
182 #if HAVE_POSIX_SPAWNP
185 posix_spawn_file_actions_destroy(&actions);
188 close(stdout_pipe[0]);
189 close(stdout_pipe[1]);
191 close(stdin_pipe[0]);
192 close(stdin_pipe[1]);
194 __archive_cmdline_free(cmdline);
199 __archive_check_child(int in, int out)
201 #if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H))
202 struct pollfd fds[2];
208 fds[idx].events = POLLOUT;
213 fds[idx].events = POLLIN;
217 poll(fds, idx, -1); /* -1 == INFTIM, wait forever */
218 #elif defined(HAVE_SELECT)
219 fd_set fds_in, fds_out, fds_error;
225 FD_SET(out, &fds_in);
226 FD_SET(out, &fds_error);
229 FD_SET(in, &fds_out);
230 FD_SET(in, &fds_error);
232 select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL);
238 #endif /* defined(HAVE_PIPE) && defined(HAVE_VFORK) && defined(HAVE_FCNTL) */