xref: /freebsd/contrib/libarchive/libarchive/filter_fork_posix.c (revision 401026e4825a05abba6f945cf1b74b3328876fa2)
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 
80 #if !defined(POSIX_SPAWN_CLOEXEC_DEFAULT) && \
81     (HAVE_FORK || HAVE_VFORK) && \
82     (HAVE_CLOSEFROM || HAVE_CLOSE_RANGE || defined(_SC_OPEN_MAX))
83 #undef HAVE_POSIX_SPAWNP
84 #endif
85 
86 #if HAVE_POSIX_SPAWNP
87 	posix_spawnattr_t attr;
88 	posix_spawn_file_actions_t actions;
89 	int r;
90 #endif
91 	struct archive_cmdline *cmdline;
92 
93 	cmdline = __archive_cmdline_allocate();
94 	if (cmdline == NULL)
95 		goto state_allocated;
96 	if (__archive_cmdline_parse(cmdline, cmd) != ARCHIVE_OK)
97 		goto state_allocated;
98 
99 	if (pipe(stdin_pipe) == -1)
100 		goto state_allocated;
101 	if (stdin_pipe[0] == 1 /* stdout */) {
102 		if ((tmp = dup(stdin_pipe[0])) == -1)
103 			goto stdin_opened;
104 		close(stdin_pipe[0]);
105 		stdin_pipe[0] = tmp;
106 	}
107 	if (pipe(stdout_pipe) == -1)
108 		goto stdin_opened;
109 	if (stdout_pipe[1] == 0 /* stdin */) {
110 		if ((tmp = dup(stdout_pipe[1])) == -1)
111 			goto stdout_opened;
112 		close(stdout_pipe[1]);
113 		stdout_pipe[1] = tmp;
114 	}
115 
116 #if HAVE_POSIX_SPAWNP
117 
118 	r = posix_spawnattr_init(&attr);
119 	if (r != 0) {
120 		errno = r;
121 		goto stdout_opened;
122 	}
123 	r = posix_spawn_file_actions_init(&actions);
124 	if (r != 0) {
125 		errno = r;
126 		goto attr_inited;
127 	}
128 #ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
129 	r = posix_spawnattr_setflags(&attr, POSIX_SPAWN_CLOEXEC_DEFAULT);
130 	if (r != 0)
131 		goto actions_inited;
132 #endif
133 	r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[1]);
134 	if (r != 0)
135 		goto actions_inited;
136 	r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[0]);
137 	if (r != 0)
138 		goto actions_inited;
139 	/* Setup for stdin. */
140 	r = posix_spawn_file_actions_adddup2(&actions, stdin_pipe[0], 0);
141 	if (r != 0)
142 		goto actions_inited;
143 	if (stdin_pipe[0] != 0 /* stdin */) {
144 		r = posix_spawn_file_actions_addclose(&actions, stdin_pipe[0]);
145 		if (r != 0)
146 			goto actions_inited;
147 	}
148 	/* Setup for stdout. */
149 	r = posix_spawn_file_actions_adddup2(&actions, stdout_pipe[1], 1);
150 	if (r != 0)
151 		goto actions_inited;
152 	if (stdout_pipe[1] != 1 /* stdout */) {
153 		r = posix_spawn_file_actions_addclose(&actions, stdout_pipe[1]);
154 		if (r != 0)
155 			goto actions_inited;
156 	}
157 	r = posix_spawnp(&child, cmdline->path, &actions, &attr,
158 		cmdline->argv, NULL);
159 	if (r != 0)
160 		goto actions_inited;
161 	posix_spawn_file_actions_destroy(&actions);
162 	posix_spawnattr_destroy(&attr);
163 
164 #else /* HAVE_POSIX_SPAWNP */
165 
166 #if HAVE_VFORK
167 	child = vfork();
168 #else
169 	child = fork();
170 #endif
171 	if (child == -1)
172 		goto stdout_opened;
173 	if (child == 0) {
174 		close(stdin_pipe[1]);
175 		close(stdout_pipe[0]);
176 		if (dup2(stdin_pipe[0], 0 /* stdin */) == -1)
177 			_exit(254);
178 		if (stdin_pipe[0] != 0 /* stdin */)
179 			close(stdin_pipe[0]);
180 		if (dup2(stdout_pipe[1], 1 /* stdout */) == -1)
181 			_exit(254);
182 		if (stdout_pipe[1] != 1 /* stdout */)
183 			close(stdout_pipe[1]);
184 
185 #if HAVE_CLOSEFROM
186 		closefrom(3);
187 #elif HAVE_CLOSE_RANGE
188 		close_range(3, ~0U, 0);
189 #elif defined(_SC_OPEN_MAX)
190 		for (int i = sysconf(_SC_OPEN_MAX); i > 3;)
191 			close(--i);
192 #endif
193 
194 		execvp(cmdline->path, cmdline->argv);
195 		_exit(254);
196 	}
197 #endif /* HAVE_POSIX_SPAWNP */
198 
199 	close(stdin_pipe[0]);
200 	close(stdout_pipe[1]);
201 
202 	*child_stdin = stdin_pipe[1];
203 	fcntl(*child_stdin, F_SETFL, O_NONBLOCK);
204 	*child_stdout = stdout_pipe[0];
205 	fcntl(*child_stdout, F_SETFL, O_NONBLOCK);
206 	__archive_cmdline_free(cmdline);
207 
208 	*out_child = child;
209 	return ARCHIVE_OK;
210 
211 #if HAVE_POSIX_SPAWNP
212 actions_inited:
213 	errno = r;
214 	posix_spawn_file_actions_destroy(&actions);
215 attr_inited:
216 	posix_spawnattr_destroy(&attr);
217 #endif
218 stdout_opened:
219 	close(stdout_pipe[0]);
220 	close(stdout_pipe[1]);
221 stdin_opened:
222 	close(stdin_pipe[0]);
223 	close(stdin_pipe[1]);
224 state_allocated:
225 	__archive_cmdline_free(cmdline);
226 	return ARCHIVE_FAILED;
227 }
228 
229 void
__archive_check_child(int in,int out)230 __archive_check_child(int in, int out)
231 {
232 #if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H))
233 	struct pollfd fds[2];
234 	int idx;
235 
236 	idx = 0;
237 	if (in != -1) {
238 		fds[idx].fd = in;
239 		fds[idx].events = POLLOUT;
240 		++idx;
241 	}
242 	if (out != -1) {
243 		fds[idx].fd = out;
244 		fds[idx].events = POLLIN;
245 		++idx;
246 	}
247 
248 	poll(fds, idx, -1); /* -1 == INFTIM, wait forever */
249 #elif defined(HAVE_SELECT)
250 	fd_set fds_in, fds_out, fds_error;
251 
252 	FD_ZERO(&fds_in);
253 	FD_ZERO(&fds_out);
254 	FD_ZERO(&fds_error);
255 	if (out != -1) {
256 		FD_SET(out, &fds_in);
257 		FD_SET(out, &fds_error);
258 	}
259 	if (in != -1) {
260 		FD_SET(in, &fds_out);
261 		FD_SET(in, &fds_error);
262 	}
263 	select(in < out ? out + 1 : in + 1, &fds_in, &fds_out, &fds_error, NULL);
264 #else
265 	sleep(1);
266 #endif
267 }
268 
269 #endif /* defined(HAVE_PIPE) && defined(HAVE_VFORK) && defined(HAVE_FCNTL) */
270