2 * Copyright (c) 2007 Joerg Sonnenberger
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
29 #ifdef HAVE_SYS_WAIT_H
30 # include <sys/wait.h>
55 #include "archive_private.h"
56 #include "archive_read_private.h"
59 #if ARCHIVE_VERSION_NUMBER < 4000000
60 /* Deprecated; remove in libarchive 4.0 */
62 archive_read_support_compression_program(struct archive *a, const char *cmd)
64 return archive_read_support_filter_program(a, cmd);
68 archive_read_support_compression_program_signature(struct archive *a,
69 const char *cmd, const void *signature, size_t signature_len)
71 return archive_read_support_filter_program_signature(a,
72 cmd, signature, signature_len);
77 archive_read_support_filter_program(struct archive *a, const char *cmd)
79 return (archive_read_support_filter_program_signature(a, cmd, NULL, 0));
83 /* This capability is only available on POSIX systems. */
84 #if (!defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
85 !(defined(HAVE_FORK) || defined(HAVE_VFORK))) && (!defined(_WIN32) || defined(__CYGWIN__))
88 * On non-Posix systems, allow the program to build, but choke if
89 * this function is actually invoked.
92 archive_read_support_filter_program_signature(struct archive *_a,
93 const char *cmd, const void *signature, size_t signature_len)
95 (void)_a; /* UNUSED */
96 (void)cmd; /* UNUSED */
97 (void)signature; /* UNUSED */
98 (void)signature_len; /* UNUSED */
100 archive_set_error(_a, -1,
101 "External compression programs not supported on this platform");
102 return (ARCHIVE_FATAL);
106 __archive_read_program(struct archive_read_filter *self, const char *cmd)
108 (void)self; /* UNUSED */
109 (void)cmd; /* UNUSED */
111 archive_set_error(&self->archive->archive, -1,
112 "External compression programs not supported on this platform");
113 return (ARCHIVE_FATAL);
118 #include "filter_fork.h"
121 * The bidder object stores the command and the signature to watch for.
122 * The 'inhibit' entry here is used to ensure that unchecked filters never
123 * bid twice in the same pipeline.
125 struct program_bidder {
128 size_t signature_len;
132 static int program_bidder_bid(struct archive_read_filter_bidder *,
133 struct archive_read_filter *upstream);
134 static int program_bidder_init(struct archive_read_filter *);
135 static int program_bidder_free(struct archive_read_filter_bidder *);
138 * The actual filter needs to track input and output data.
140 struct program_filter {
145 int child_stdin, child_stdout;
151 static ssize_t program_filter_read(struct archive_read_filter *,
153 static int program_filter_close(struct archive_read_filter *);
156 archive_read_support_filter_program_signature(struct archive *_a,
157 const char *cmd, const void *signature, size_t signature_len)
159 struct archive_read *a = (struct archive_read *)_a;
160 struct archive_read_filter_bidder *bidder;
161 struct program_bidder *state;
164 * Get a bidder object from the read core.
166 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
167 return (ARCHIVE_FATAL);
170 * Allocate our private state.
172 state = (struct program_bidder *)calloc(sizeof (*state), 1);
174 return (ARCHIVE_FATAL);
175 state->cmd = strdup(cmd);
176 if (signature != NULL && signature_len > 0) {
177 state->signature_len = signature_len;
178 state->signature = malloc(signature_len);
179 memcpy(state->signature, signature, signature_len);
183 * Fill in the bidder object.
185 bidder->data = state;
186 bidder->bid = program_bidder_bid;
187 bidder->init = program_bidder_init;
188 bidder->options = NULL;
189 bidder->free = program_bidder_free;
194 program_bidder_free(struct archive_read_filter_bidder *self)
196 struct program_bidder *state = (struct program_bidder *)self->data;
198 free(state->signature);
204 * If we do have a signature, bid only if that matches.
206 * If there's no signature, we bid INT_MAX the first time
207 * we're called, then never bid again.
210 program_bidder_bid(struct archive_read_filter_bidder *self,
211 struct archive_read_filter *upstream)
213 struct program_bidder *state = self->data;
216 /* If we have a signature, use that to match. */
217 if (state->signature_len > 0) {
218 p = __archive_read_filter_ahead(upstream,
219 state->signature_len, NULL);
222 /* No match, so don't bid. */
223 if (memcmp(p, state->signature, state->signature_len) != 0)
225 return ((int)state->signature_len * 8);
228 /* Otherwise, bid once and then never bid again. */
236 * Shut down the child, return ARCHIVE_OK if it exited normally.
238 * Note that the return value is sticky; if we're called again,
239 * we won't reap the child again, but we will return the same status
240 * (including error message if the child came to a bad end).
243 child_stop(struct archive_read_filter *self, struct program_filter *state)
245 /* Close our side of the I/O with the child. */
246 if (state->child_stdin != -1) {
247 close(state->child_stdin);
248 state->child_stdin = -1;
250 if (state->child_stdout != -1) {
251 close(state->child_stdout);
252 state->child_stdout = -1;
255 if (state->child != 0) {
256 /* Reap the child. */
258 state->waitpid_return
259 = waitpid(state->child, &state->exit_status, 0);
260 } while (state->waitpid_return == -1 && errno == EINTR);
264 if (state->waitpid_return < 0) {
265 /* waitpid() failed? This is ugly. */
266 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
267 "Child process exited badly");
268 return (ARCHIVE_WARN);
271 #if !defined(_WIN32) || defined(__CYGWIN__)
272 if (WIFSIGNALED(state->exit_status)) {
274 /* If the child died because we stopped reading before
275 * it was done, that's okay. Some archive formats
276 * have padding at the end that we routinely ignore. */
277 /* The alternative to this would be to add a step
278 * before close(child_stdout) above to read from the
279 * child until the child has no more to write. */
280 if (WTERMSIG(state->exit_status) == SIGPIPE)
283 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
284 "Child process exited with signal %d",
285 WTERMSIG(state->exit_status));
286 return (ARCHIVE_WARN);
288 #endif /* !_WIN32 || __CYGWIN__ */
290 if (WIFEXITED(state->exit_status)) {
291 if (WEXITSTATUS(state->exit_status) == 0)
294 archive_set_error(&self->archive->archive,
296 "Child process exited with status %d",
297 WEXITSTATUS(state->exit_status));
298 return (ARCHIVE_WARN);
301 return (ARCHIVE_WARN);
305 * Use select() to decide whether the child is ready for read or write.
308 child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
310 struct program_filter *state = self->data;
311 ssize_t ret, requested, avail;
314 requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
318 ret = read(state->child_stdout, buf, requested);
319 } while (ret == -1 && errno == EINTR);
323 if (ret == 0 || (ret == -1 && errno == EPIPE))
324 /* Child has closed its output; reap the child
325 * and return the status. */
326 return (child_stop(self, state));
327 if (ret == -1 && errno != EAGAIN)
330 if (state->child_stdin == -1) {
331 /* Block until child has some I/O ready. */
332 __archive_check_child(state->child_stdin,
333 state->child_stdout);
337 /* Get some more data from upstream. */
338 p = __archive_read_filter_ahead(self->upstream, 1, &avail);
340 close(state->child_stdin);
341 state->child_stdin = -1;
342 fcntl(state->child_stdout, F_SETFL, 0);
349 ret = write(state->child_stdin, p, avail);
350 } while (ret == -1 && errno == EINTR);
353 /* Consume whatever we managed to write. */
354 __archive_read_filter_consume(self->upstream, ret);
355 } else if (ret == -1 && errno == EAGAIN) {
356 /* Block until child has some I/O ready. */
357 __archive_check_child(state->child_stdin,
358 state->child_stdout);
361 close(state->child_stdin);
362 state->child_stdin = -1;
363 fcntl(state->child_stdout, F_SETFL, 0);
364 /* If it was a bad error, we're done; otherwise
365 * it was EPIPE or EOF, and we can still read
367 if (ret == -1 && errno != EPIPE)
374 __archive_read_program(struct archive_read_filter *self, const char *cmd)
376 struct program_filter *state;
377 static const size_t out_buf_len = 65536;
380 const char *prefix = "Program: ";
382 state = (struct program_filter *)calloc(1, sizeof(*state));
383 out_buf = (char *)malloc(out_buf_len);
384 description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
385 if (state == NULL || out_buf == NULL || description == NULL) {
386 archive_set_error(&self->archive->archive, ENOMEM,
387 "Can't allocate input data");
391 return (ARCHIVE_FATAL);
394 self->code = ARCHIVE_COMPRESSION_PROGRAM;
395 state->description = description;
396 strcpy(state->description, prefix);
397 strcat(state->description, cmd);
398 self->name = state->description;
400 state->out_buf = out_buf;
401 state->out_buf_len = out_buf_len;
403 if ((state->child = __archive_create_child(cmd,
404 &state->child_stdin, &state->child_stdout)) == -1) {
405 free(state->out_buf);
407 archive_set_error(&self->archive->archive, EINVAL,
408 "Can't initialize filter; unable to run program \"%s\"", cmd);
409 return (ARCHIVE_FATAL);
413 self->read = program_filter_read;
415 self->close = program_filter_close;
417 /* XXX Check that we can read at least one byte? */
422 program_bidder_init(struct archive_read_filter *self)
424 struct program_bidder *bidder_state;
426 bidder_state = (struct program_bidder *)self->bidder->data;
427 return (__archive_read_program(self, bidder_state->cmd));
431 program_filter_read(struct archive_read_filter *self, const void **buff)
433 struct program_filter *state;
438 state = (struct program_filter *)self->data;
442 while (state->child_stdout != -1 && total < state->out_buf_len) {
443 bytes = child_read(self, p, state->out_buf_len - total);
445 /* No recovery is possible if we can no longer
446 * read from the child. */
447 return (ARCHIVE_FATAL);
449 /* We got EOF from the child. */
455 *buff = state->out_buf;
460 program_filter_close(struct archive_read_filter *self)
462 struct program_filter *state;
465 state = (struct program_filter *)self->data;
466 e = child_stop(self, state);
468 /* Release our private data. */
469 free(state->out_buf);
470 free(state->description);
476 #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */