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"
28 __FBSDID("$FreeBSD$");
30 #ifdef HAVE_SYS_WAIT_H
31 # include <sys/wait.h>
56 #include "archive_private.h"
57 #include "archive_string.h"
58 #include "archive_read_private.h"
59 #include "filter_fork.h"
62 #if ARCHIVE_VERSION_NUMBER < 4000000
63 /* Deprecated; remove in libarchive 4.0 */
65 archive_read_support_compression_program(struct archive *a, const char *cmd)
67 return archive_read_support_filter_program(a, cmd);
71 archive_read_support_compression_program_signature(struct archive *a,
72 const char *cmd, const void *signature, size_t signature_len)
74 return archive_read_support_filter_program_signature(a,
75 cmd, signature, signature_len);
80 archive_read_support_filter_program(struct archive *a, const char *cmd)
82 return (archive_read_support_filter_program_signature(a, cmd, NULL, 0));
86 * The bidder object stores the command and the signature to watch for.
87 * The 'inhibit' entry here is used to ensure that unchecked filters never
88 * bid twice in the same pipeline.
90 struct program_bidder {
98 static int program_bidder_bid(struct archive_read_filter_bidder *,
99 struct archive_read_filter *upstream);
100 static int program_bidder_init(struct archive_read_filter *);
101 static void program_bidder_free(struct archive_read_filter_bidder *);
104 * The actual filter needs to track input and output data.
106 struct program_filter {
107 struct archive_string description;
108 #if defined(_WIN32) && !defined(__CYGWIN__)
115 int child_stdin, child_stdout;
121 static ssize_t program_filter_read(struct archive_read_filter *,
123 static int program_filter_close(struct archive_read_filter *);
124 static void free_state(struct program_bidder *);
126 static const struct archive_read_filter_bidder_vtable
127 program_bidder_vtable = {
128 .bid = program_bidder_bid,
129 .init = program_bidder_init,
130 .free = program_bidder_free,
134 archive_read_support_filter_program_signature(struct archive *_a,
135 const char *cmd, const void *signature, size_t signature_len)
137 struct archive_read *a = (struct archive_read *)_a;
138 struct program_bidder *state;
141 * Allocate our private state.
143 state = (struct program_bidder *)calloc(1, sizeof (*state));
146 state->cmd = strdup(cmd);
147 if (state->cmd == NULL)
150 if (signature != NULL && signature_len > 0) {
151 state->signature_len = signature_len;
152 state->signature = malloc(signature_len);
153 memcpy(state->signature, signature, signature_len);
156 if (__archive_read_register_bidder(a, state, NULL,
157 &program_bidder_vtable) != ARCHIVE_OK) {
159 return (ARCHIVE_FATAL);
165 archive_set_error(_a, ENOMEM, "Can't allocate memory");
166 return (ARCHIVE_FATAL);
170 program_bidder_free(struct archive_read_filter_bidder *self)
172 struct program_bidder *state = (struct program_bidder *)self->data;
178 free_state(struct program_bidder *state)
183 free(state->signature);
189 * If we do have a signature, bid only if that matches.
191 * If there's no signature, we bid INT_MAX the first time
192 * we're called, then never bid again.
195 program_bidder_bid(struct archive_read_filter_bidder *self,
196 struct archive_read_filter *upstream)
198 struct program_bidder *state = self->data;
201 /* If we have a signature, use that to match. */
202 if (state->signature_len > 0) {
203 p = __archive_read_filter_ahead(upstream,
204 state->signature_len, NULL);
207 /* No match, so don't bid. */
208 if (memcmp(p, state->signature, state->signature_len) != 0)
210 return ((int)state->signature_len * 8);
213 /* Otherwise, bid once and then never bid again. */
221 * Shut down the child, return ARCHIVE_OK if it exited normally.
223 * Note that the return value is sticky; if we're called again,
224 * we won't reap the child again, but we will return the same status
225 * (including error message if the child came to a bad end).
228 child_stop(struct archive_read_filter *self, struct program_filter *state)
230 /* Close our side of the I/O with the child. */
231 if (state->child_stdin != -1) {
232 close(state->child_stdin);
233 state->child_stdin = -1;
235 if (state->child_stdout != -1) {
236 close(state->child_stdout);
237 state->child_stdout = -1;
240 if (state->child != 0) {
241 /* Reap the child. */
243 state->waitpid_return
244 = waitpid(state->child, &state->exit_status, 0);
245 } while (state->waitpid_return == -1 && errno == EINTR);
246 #if defined(_WIN32) && !defined(__CYGWIN__)
247 CloseHandle(state->child);
252 if (state->waitpid_return < 0) {
253 /* waitpid() failed? This is ugly. */
254 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
255 "Child process exited badly");
256 return (ARCHIVE_WARN);
259 #if !defined(_WIN32) || defined(__CYGWIN__)
260 if (WIFSIGNALED(state->exit_status)) {
262 /* If the child died because we stopped reading before
263 * it was done, that's okay. Some archive formats
264 * have padding at the end that we routinely ignore. */
265 /* The alternative to this would be to add a step
266 * before close(child_stdout) above to read from the
267 * child until the child has no more to write. */
268 if (WTERMSIG(state->exit_status) == SIGPIPE)
271 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
272 "Child process exited with signal %d",
273 WTERMSIG(state->exit_status));
274 return (ARCHIVE_WARN);
276 #endif /* !_WIN32 || __CYGWIN__ */
278 if (WIFEXITED(state->exit_status)) {
279 if (WEXITSTATUS(state->exit_status) == 0)
282 archive_set_error(&self->archive->archive,
284 "Child process exited with status %d",
285 WEXITSTATUS(state->exit_status));
286 return (ARCHIVE_WARN);
289 return (ARCHIVE_WARN);
293 * Use select() to decide whether the child is ready for read or write.
296 child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
298 struct program_filter *state = self->data;
299 ssize_t ret, requested, avail;
301 #if defined(_WIN32) && !defined(__CYGWIN__)
302 HANDLE handle = (HANDLE)_get_osfhandle(state->child_stdout);
305 requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
309 #if defined(_WIN32) && !defined(__CYGWIN__)
310 /* Avoid infinity wait.
311 * Note: If there is no data in the pipe, ReadFile()
312 * called in read() never returns and so we won't
313 * write remaining encoded data to the pipe.
314 * Note: This way may cause performance problem.
315 * we are looking forward to great code to resolve
317 DWORD pipe_avail = -1;
320 while (PeekNamedPipe(handle, NULL, 0, NULL,
321 &pipe_avail, NULL) != 0 && pipe_avail == 0 &&
324 if (pipe_avail == 0) {
330 ret = read(state->child_stdout, buf, requested);
331 } while (ret == -1 && errno == EINTR);
335 if (ret == 0 || (ret == -1 && errno == EPIPE))
336 /* Child has closed its output; reap the child
337 * and return the status. */
338 return (child_stop(self, state));
339 if (ret == -1 && errno != EAGAIN)
342 if (state->child_stdin == -1) {
343 /* Block until child has some I/O ready. */
344 __archive_check_child(state->child_stdin,
345 state->child_stdout);
349 /* Get some more data from upstream. */
350 p = __archive_read_filter_ahead(self->upstream, 1, &avail);
352 close(state->child_stdin);
353 state->child_stdin = -1;
354 fcntl(state->child_stdout, F_SETFL, 0);
361 ret = write(state->child_stdin, p, avail);
362 } while (ret == -1 && errno == EINTR);
365 /* Consume whatever we managed to write. */
366 __archive_read_filter_consume(self->upstream, ret);
367 } else if (ret == -1 && errno == EAGAIN) {
368 /* Block until child has some I/O ready. */
369 __archive_check_child(state->child_stdin,
370 state->child_stdout);
373 close(state->child_stdin);
374 state->child_stdin = -1;
375 fcntl(state->child_stdout, F_SETFL, 0);
376 /* If it was a bad error, we're done; otherwise
377 * it was EPIPE or EOF, and we can still read
379 if (ret == -1 && errno != EPIPE)
385 static const struct archive_read_filter_vtable
386 program_reader_vtable = {
387 .read = program_filter_read,
388 .close = program_filter_close,
392 __archive_read_program(struct archive_read_filter *self, const char *cmd)
394 struct program_filter *state;
395 static const size_t out_buf_len = 65536;
397 const char *prefix = "Program: ";
401 l = strlen(prefix) + strlen(cmd) + 1;
402 state = (struct program_filter *)calloc(1, sizeof(*state));
403 out_buf = (char *)malloc(out_buf_len);
404 if (state == NULL || out_buf == NULL ||
405 archive_string_ensure(&state->description, l) == NULL) {
406 archive_set_error(&self->archive->archive, ENOMEM,
407 "Can't allocate input data");
409 archive_string_free(&state->description);
413 return (ARCHIVE_FATAL);
415 archive_strcpy(&state->description, prefix);
416 archive_strcat(&state->description, cmd);
418 self->code = ARCHIVE_FILTER_PROGRAM;
419 self->name = state->description.s;
421 state->out_buf = out_buf;
422 state->out_buf_len = out_buf_len;
424 ret = __archive_create_child(cmd, &state->child_stdin,
425 &state->child_stdout, &state->child);
426 if (ret != ARCHIVE_OK) {
427 free(state->out_buf);
428 archive_string_free(&state->description);
430 archive_set_error(&self->archive->archive, EINVAL,
431 "Can't initialize filter; unable to run program \"%s\"",
433 return (ARCHIVE_FATAL);
437 self->vtable = &program_reader_vtable;
439 /* XXX Check that we can read at least one byte? */
444 program_bidder_init(struct archive_read_filter *self)
446 struct program_bidder *bidder_state;
448 bidder_state = (struct program_bidder *)self->bidder->data;
449 return (__archive_read_program(self, bidder_state->cmd));
453 program_filter_read(struct archive_read_filter *self, const void **buff)
455 struct program_filter *state;
460 state = (struct program_filter *)self->data;
464 while (state->child_stdout != -1 && total < state->out_buf_len) {
465 bytes = child_read(self, p, state->out_buf_len - total);
467 /* No recovery is possible if we can no longer
468 * read from the child. */
469 return (ARCHIVE_FATAL);
471 /* We got EOF from the child. */
477 *buff = state->out_buf;
482 program_filter_close(struct archive_read_filter *self)
484 struct program_filter *state;
487 state = (struct program_filter *)self->data;
488 e = child_stop(self, state);
490 /* Release our private data. */
491 free(state->out_buf);
492 archive_string_free(&state->description);