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 __FBSDID("$FreeBSD: head/lib/libarchive/filter_fork.c 182958 2008-09-12 05:33:00Z kientzle $"); 34 35 #if defined(HAVE_SYS_TYPES_H) 36 # include <sys/types.h> 37 #endif 38 #ifdef HAVE_ERRNO_H 39 # include <errno.h> 40 #endif 41 #ifdef HAVE_STRING_H 42 # include <string.h> 43 #endif 44 #if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) 45 # if defined(HAVE_POLL_H) 46 # include <poll.h> 47 # elif defined(HAVE_SYS_POLL_H) 48 # include <sys/poll.h> 49 # endif 50 #elif defined(HAVE_SELECT) 51 # if defined(HAVE_SYS_SELECT_H) 52 # include <sys/select.h> 53 # elif defined(HAVE_UNISTD_H) 54 # include <unistd.h> 55 # endif 56 #endif 57 #ifdef HAVE_FCNTL_H 58 # include <fcntl.h> 59 #endif 60 #ifdef HAVE_SPAWN_H 61 # include <spawn.h> 62 #endif 63 #ifdef HAVE_STDLIB_H 64 # include <stdlib.h> 65 #endif 66 #ifdef HAVE_UNISTD_H 67 # include <unistd.h> 68 #endif 69 70 #include "archive.h" 71 #include "archive_cmdline_private.h" 72 73 #include "filter_fork.h" 74 75 int 76 __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout, 77 pid_t *out_child) 78 { 79 pid_t child; 80 int stdin_pipe[2], stdout_pipe[2], tmp; 81 #if HAVE_POSIX_SPAWNP 82 posix_spawn_file_actions_t actions; 83 int r; 84 #endif 85 struct archive_cmdline *cmdline; 86 87 cmdline = __archive_cmdline_allocate(); 88 if (cmdline == NULL) 89 goto state_allocated; 90 if (__archive_cmdline_parse(cmdline, cmd) != ARCHIVE_OK) 91 goto state_allocated; 92 93 if (pipe(stdin_pipe) == -1) 94 goto state_allocated; 95 if (stdin_pipe[0] == 1 /* stdout */) { 96 if ((tmp = dup(stdin_pipe[0])) == -1) 97 goto stdin_opened; 98 close(stdin_pipe[0]); 99 stdin_pipe[0] = tmp; 100 } 101 if (pipe(stdout_pipe) == -1) 102 goto stdin_opened; 103 if (stdout_pipe[1] == 0 /* stdin */) { 104 if ((tmp = dup(stdout_pipe[1])) == -1) 105 goto stdout_opened; 106 close(stdout_pipe[1]); 107 stdout_pipe[1] = tmp; 108 } 109 110 #if HAVE_POSIX_SPAWNP 111 112 r = posix_spawn_file_actions_init(&actions); 113 if (r != 0) { 114 errno = r; 115 goto stdout_opened; 116 } 117 r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[1]); 118 if (r != 0) 119 goto actions_inited; 120 r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[0]); 121 if (r != 0) 122 goto actions_inited; 123 /* Setup for stdin. */ 124 r = posix_spawn_file_actions_adddup2(&actions, stdin_pipe[0], 0); 125 if (r != 0) 126 goto actions_inited; 127 if (stdin_pipe[0] != 0 /* stdin */) { 128 r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[0]); 129 if (r != 0) 130 goto actions_inited; 131 } 132 /* Setup for stdout. */ 133 r = posix_spawn_file_actions_adddup2(&actions, stdout_pipe[1], 1); 134 if (r != 0) 135 goto actions_inited; 136 if (stdout_pipe[1] != 1 /* stdout */) { 137 r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[1]); 138 if (r != 0) 139 goto actions_inited; 140 } 141 r = posix_spawnp(&child, cmdline->path, &actions, NULL, 142 cmdline->argv, NULL); 143 if (r != 0) 144 goto actions_inited; 145 posix_spawn_file_actions_destroy(&actions); 146 147 #else /* HAVE_POSIX_SPAWNP */ 148 149 #if HAVE_VFORK 150 child = vfork(); 151 #else 152 child = fork(); 153 #endif 154 if (child == -1) 155 goto stdout_opened; 156 if (child == 0) { 157 close(stdin_pipe[1]); 158 close(stdout_pipe[0]); 159 if (dup2(stdin_pipe[0], 0 /* stdin */) == -1) 160 _exit(254); 161 if (stdin_pipe[0] != 0 /* stdin */) 162 close(stdin_pipe[0]); 163 if (dup2(stdout_pipe[1], 1 /* stdout */) == -1) 164 _exit(254); 165 if (stdout_pipe[1] != 1 /* stdout */) 166 close(stdout_pipe[1]); 167 execvp(cmdline->path, cmdline->argv); 168 _exit(254); 169 } 170 #endif /* HAVE_POSIX_SPAWNP */ 171 172 close(stdin_pipe[0]); 173 close(stdout_pipe[1]); 174 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); 180 181 *out_child = child; 182 return ARCHIVE_OK; 183 184 #if HAVE_POSIX_SPAWNP 185 actions_inited: 186 errno = r; 187 posix_spawn_file_actions_destroy(&actions); 188 #endif 189 stdout_opened: 190 close(stdout_pipe[0]); 191 close(stdout_pipe[1]); 192 stdin_opened: 193 close(stdin_pipe[0]); 194 close(stdin_pipe[1]); 195 state_allocated: 196 __archive_cmdline_free(cmdline); 197 return ARCHIVE_FAILED; 198 } 199 200 void 201 __archive_check_child(int in, int out) 202 { 203 #if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) 204 struct pollfd fds[2]; 205 int idx; 206 207 idx = 0; 208 if (in != -1) { 209 fds[idx].fd = in; 210 fds[idx].events = POLLOUT; 211 ++idx; 212 } 213 if (out != -1) { 214 fds[idx].fd = out; 215 fds[idx].events = POLLIN; 216 ++idx; 217 } 218 219 poll(fds, idx, -1); /* -1 == INFTIM, wait forever */ 220 #elif defined(HAVE_SELECT) 221 fd_set fds_in, fds_out, fds_error; 222 223 FD_ZERO(&fds_in); 224 FD_ZERO(&fds_out); 225 FD_ZERO(&fds_error); 226 if (out != -1) { 227 FD_SET(out, &fds_in); 228 FD_SET(out, &fds_error); 229 } 230 if (in != -1) { 231 FD_SET(in, &fds_out); 232 FD_SET(in, &fds_error); 233 } 234 select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL); 235 #else 236 sleep(1); 237 #endif 238 } 239 240 #endif /* defined(HAVE_PIPE) && defined(HAVE_VFORK) && defined(HAVE_FCNTL) */ 241