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 pid_t
76 __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout)
77 {
78 	pid_t child;
79 	int stdin_pipe[2], stdout_pipe[2], tmp;
80 #if HAVE_POSIX_SPAWNP
81 	posix_spawn_file_actions_t actions;
82 	int r;
83 #endif
84 	struct archive_cmdline *cmdline;
85 
86 	cmdline = __archive_cmdline_allocate();
87 	if (cmdline == NULL)
88 		goto state_allocated;
89 	if (__archive_cmdline_parse(cmdline, cmd) != ARCHIVE_OK)
90 		goto state_allocated;
91 
92 	if (pipe(stdin_pipe) == -1)
93 		goto state_allocated;
94 	if (stdin_pipe[0] == 1 /* stdout */) {
95 		if ((tmp = dup(stdin_pipe[0])) == -1)
96 			goto stdin_opened;
97 		close(stdin_pipe[0]);
98 		stdin_pipe[0] = tmp;
99 	}
100 	if (pipe(stdout_pipe) == -1)
101 		goto stdin_opened;
102 	if (stdout_pipe[1] == 0 /* stdin */) {
103 		if ((tmp = dup(stdout_pipe[1])) == -1)
104 			goto stdout_opened;
105 		close(stdout_pipe[1]);
106 		stdout_pipe[1] = tmp;
107 	}
108 
109 #if HAVE_POSIX_SPAWNP
110 
111 	r = posix_spawn_file_actions_init(&actions);
112 	if (r != 0) {
113 		errno = r;
114 		goto stdout_opened;
115 	}
116 	r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[1]);
117 	if (r != 0)
118 		goto actions_inited;
119 	r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[0]);
120 	if (r != 0)
121 		goto actions_inited;
122 	/* Setup for stdin. */
123 	r = posix_spawn_file_actions_adddup2(&actions, stdin_pipe[0], 0);
124 	if (r != 0)
125 		goto actions_inited;
126 	if (stdin_pipe[0] != 0 /* stdin */) {
127 		r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[0]);
128 		if (r != 0)
129 			goto actions_inited;
130 	}
131 	/* Setup for stdout. */
132 	r = posix_spawn_file_actions_adddup2(&actions, stdout_pipe[1], 1);
133 	if (r != 0)
134 		goto actions_inited;
135 	if (stdout_pipe[1] != 1 /* stdout */) {
136 		r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[1]);
137 		if (r != 0)
138 			goto actions_inited;
139 	}
140 	r = posix_spawnp(&child, cmdline->path, &actions, NULL,
141 		cmdline->argv, NULL);
142 	if (r != 0)
143 		goto actions_inited;
144 	posix_spawn_file_actions_destroy(&actions);
145 
146 #else /* HAVE_POSIX_SPAWNP */
147 
148 #if HAVE_VFORK
149 	child = vfork();
150 #else
151 	child = fork();
152 #endif
153 	if (child == -1)
154 		goto stdout_opened;
155 	if (child == 0) {
156 		close(stdin_pipe[1]);
157 		close(stdout_pipe[0]);
158 		if (dup2(stdin_pipe[0], 0 /* stdin */) == -1)
159 			_exit(254);
160 		if (stdin_pipe[0] != 0 /* stdin */)
161 			close(stdin_pipe[0]);
162 		if (dup2(stdout_pipe[1], 1 /* stdout */) == -1)
163 			_exit(254);
164 		if (stdout_pipe[1] != 1 /* stdout */)
165 			close(stdout_pipe[1]);
166 		execvp(cmdline->path, cmdline->argv);
167 		_exit(254);
168 	}
169 #endif /* HAVE_POSIX_SPAWNP */
170 
171 	close(stdin_pipe[0]);
172 	close(stdout_pipe[1]);
173 
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);
179 
180 	return child;
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 -1;
196 }
197 
198 void
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