nrelease - fix/improve livecd
[dragonfly.git] / contrib / libarchive / libarchive / archive_read_support_filter_program.c
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 __FBSDID("$FreeBSD$");
29
30 #ifdef HAVE_SYS_WAIT_H
31 #  include <sys/wait.h>
32 #endif
33 #ifdef HAVE_ERRNO_H
34 #  include <errno.h>
35 #endif
36 #ifdef HAVE_FCNTL_H
37 #  include <fcntl.h>
38 #endif
39 #ifdef HAVE_LIMITS_H
40 #  include <limits.h>
41 #endif
42 #ifdef HAVE_SIGNAL_H
43 #  include <signal.h>
44 #endif
45 #ifdef HAVE_STDLIB_H
46 #  include <stdlib.h>
47 #endif
48 #ifdef HAVE_STRING_H
49 #  include <string.h>
50 #endif
51 #ifdef HAVE_UNISTD_H
52 #  include <unistd.h>
53 #endif
54
55 #include "archive.h"
56 #include "archive_private.h"
57 #include "archive_string.h"
58 #include "archive_read_private.h"
59 #include "filter_fork.h"
60
61
62 #if ARCHIVE_VERSION_NUMBER < 4000000
63 /* Deprecated; remove in libarchive 4.0 */
64 int
65 archive_read_support_compression_program(struct archive *a, const char *cmd)
66 {
67         return archive_read_support_filter_program(a, cmd);
68 }
69
70 int
71 archive_read_support_compression_program_signature(struct archive *a,
72     const char *cmd, const void *signature, size_t signature_len)
73 {
74         return archive_read_support_filter_program_signature(a,
75             cmd, signature, signature_len);
76 }
77 #endif
78
79 int
80 archive_read_support_filter_program(struct archive *a, const char *cmd)
81 {
82         return (archive_read_support_filter_program_signature(a, cmd, NULL, 0));
83 }
84
85 /*
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.
89  */
90 struct program_bidder {
91         char *description;
92         char *cmd;
93         void *signature;
94         size_t signature_len;
95         int inhibit;
96 };
97
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 *);
102
103 /*
104  * The actual filter needs to track input and output data.
105  */
106 struct program_filter {
107         struct archive_string description;
108 #if defined(_WIN32) && !defined(__CYGWIN__)
109         HANDLE           child;
110 #else
111         pid_t            child;
112 #endif
113         int              exit_status;
114         int              waitpid_return;
115         int              child_stdin, child_stdout;
116
117         char            *out_buf;
118         size_t           out_buf_len;
119 };
120
121 static ssize_t  program_filter_read(struct archive_read_filter *,
122                     const void **);
123 static int      program_filter_close(struct archive_read_filter *);
124 static void     free_state(struct program_bidder *);
125
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,
131 };
132
133 int
134 archive_read_support_filter_program_signature(struct archive *_a,
135     const char *cmd, const void *signature, size_t signature_len)
136 {
137         struct archive_read *a = (struct archive_read *)_a;
138         struct program_bidder *state;
139
140         /*
141          * Allocate our private state.
142          */
143         state = (struct program_bidder *)calloc(1, sizeof (*state));
144         if (state == NULL)
145                 goto memerr;
146         state->cmd = strdup(cmd);
147         if (state->cmd == NULL)
148                 goto memerr;
149
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);
154         }
155
156         if (__archive_read_register_bidder(a, state, NULL,
157                                 &program_bidder_vtable) != ARCHIVE_OK) {
158                 free_state(state);
159                 return (ARCHIVE_FATAL);
160         }
161         return (ARCHIVE_OK);
162
163 memerr:
164         free_state(state);
165         archive_set_error(_a, ENOMEM, "Can't allocate memory");
166         return (ARCHIVE_FATAL);
167 }
168
169 static void
170 program_bidder_free(struct archive_read_filter_bidder *self)
171 {
172         struct program_bidder *state = (struct program_bidder *)self->data;
173
174         free_state(state);
175 }
176
177 static void
178 free_state(struct program_bidder *state)
179 {
180
181         if (state) {
182                 free(state->cmd);
183                 free(state->signature);
184                 free(state);
185         }
186 }
187
188 /*
189  * If we do have a signature, bid only if that matches.
190  *
191  * If there's no signature, we bid INT_MAX the first time
192  * we're called, then never bid again.
193  */
194 static int
195 program_bidder_bid(struct archive_read_filter_bidder *self,
196     struct archive_read_filter *upstream)
197 {
198         struct program_bidder *state = self->data;
199         const char *p;
200
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);
205                 if (p == NULL)
206                         return (0);
207                 /* No match, so don't bid. */
208                 if (memcmp(p, state->signature, state->signature_len) != 0)
209                         return (0);
210                 return ((int)state->signature_len * 8);
211         }
212
213         /* Otherwise, bid once and then never bid again. */
214         if (state->inhibit)
215                 return (0);
216         state->inhibit = 1;
217         return (INT_MAX);
218 }
219
220 /*
221  * Shut down the child, return ARCHIVE_OK if it exited normally.
222  *
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).
226  */
227 static int
228 child_stop(struct archive_read_filter *self, struct program_filter *state)
229 {
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;
234         }
235         if (state->child_stdout != -1) {
236                 close(state->child_stdout);
237                 state->child_stdout = -1;
238         }
239
240         if (state->child != 0) {
241                 /* Reap the child. */
242                 do {
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);
248 #endif
249                 state->child = 0;
250         }
251
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);
257         }
258
259 #if !defined(_WIN32) || defined(__CYGWIN__)
260         if (WIFSIGNALED(state->exit_status)) {
261 #ifdef SIGPIPE
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)
269                         return (ARCHIVE_OK);
270 #endif
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);
275         }
276 #endif /* !_WIN32 || __CYGWIN__ */
277
278         if (WIFEXITED(state->exit_status)) {
279                 if (WEXITSTATUS(state->exit_status) == 0)
280                         return (ARCHIVE_OK);
281
282                 archive_set_error(&self->archive->archive,
283                     ARCHIVE_ERRNO_MISC,
284                     "Child process exited with status %d",
285                     WEXITSTATUS(state->exit_status));
286                 return (ARCHIVE_WARN);
287         }
288
289         return (ARCHIVE_WARN);
290 }
291
292 /*
293  * Use select() to decide whether the child is ready for read or write.
294  */
295 static ssize_t
296 child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
297 {
298         struct program_filter *state = self->data;
299         ssize_t ret, requested, avail;
300         const char *p;
301 #if defined(_WIN32) && !defined(__CYGWIN__)
302         HANDLE handle = (HANDLE)_get_osfhandle(state->child_stdout);
303 #endif
304
305         requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
306
307         for (;;) {
308                 do {
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
316                          * this.  */
317                         DWORD pipe_avail = -1;
318                         int cnt = 2;
319
320                         while (PeekNamedPipe(handle, NULL, 0, NULL,
321                             &pipe_avail, NULL) != 0 && pipe_avail == 0 &&
322                             cnt--)
323                                 Sleep(5);
324                         if (pipe_avail == 0) {
325                                 ret = -1;
326                                 errno = EAGAIN;
327                                 break;
328                         }
329 #endif
330                         ret = read(state->child_stdout, buf, requested);
331                 } while (ret == -1 && errno == EINTR);
332
333                 if (ret > 0)
334                         return (ret);
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)
340                         return (-1);
341
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);
346                         continue;
347                 }
348
349                 /* Get some more data from upstream. */
350                 p = __archive_read_filter_ahead(self->upstream, 1, &avail);
351                 if (p == NULL) {
352                         close(state->child_stdin);
353                         state->child_stdin = -1;
354                         fcntl(state->child_stdout, F_SETFL, 0);
355                         if (avail < 0)
356                                 return (avail);
357                         continue;
358                 }
359
360                 do {
361                         ret = write(state->child_stdin, p, avail);
362                 } while (ret == -1 && errno == EINTR);
363
364                 if (ret > 0) {
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);
371                 } else {
372                         /* Write failed. */
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
378                          * from the child. */
379                         if (ret == -1 && errno != EPIPE)
380                                 return (-1);
381                 }
382         }
383 }
384
385 static const struct archive_read_filter_vtable
386 program_reader_vtable = {
387         .read = program_filter_read,
388         .close = program_filter_close,
389 };
390
391 int
392 __archive_read_program(struct archive_read_filter *self, const char *cmd)
393 {
394         struct program_filter   *state;
395         static const size_t out_buf_len = 65536;
396         char *out_buf;
397         const char *prefix = "Program: ";
398         int ret;
399         size_t l;
400
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");
408                 if (state != NULL) {
409                         archive_string_free(&state->description);
410                         free(state);
411                 }
412                 free(out_buf);
413                 return (ARCHIVE_FATAL);
414         }
415         archive_strcpy(&state->description, prefix);
416         archive_strcat(&state->description, cmd);
417
418         self->code = ARCHIVE_FILTER_PROGRAM;
419         self->name = state->description.s;
420
421         state->out_buf = out_buf;
422         state->out_buf_len = out_buf_len;
423
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);
429                 free(state);
430                 archive_set_error(&self->archive->archive, EINVAL,
431                     "Can't initialize filter; unable to run program \"%s\"",
432                     cmd);
433                 return (ARCHIVE_FATAL);
434         }
435
436         self->data = state;
437         self->vtable = &program_reader_vtable;
438
439         /* XXX Check that we can read at least one byte? */
440         return (ARCHIVE_OK);
441 }
442
443 static int
444 program_bidder_init(struct archive_read_filter *self)
445 {
446         struct program_bidder   *bidder_state;
447
448         bidder_state = (struct program_bidder *)self->bidder->data;
449         return (__archive_read_program(self, bidder_state->cmd));
450 }
451
452 static ssize_t
453 program_filter_read(struct archive_read_filter *self, const void **buff)
454 {
455         struct program_filter *state;
456         ssize_t bytes;
457         size_t total;
458         char *p;
459
460         state = (struct program_filter *)self->data;
461
462         total = 0;
463         p = state->out_buf;
464         while (state->child_stdout != -1 && total < state->out_buf_len) {
465                 bytes = child_read(self, p, state->out_buf_len - total);
466                 if (bytes < 0)
467                         /* No recovery is possible if we can no longer
468                          * read from the child. */
469                         return (ARCHIVE_FATAL);
470                 if (bytes == 0)
471                         /* We got EOF from the child. */
472                         break;
473                 total += bytes;
474                 p += bytes;
475         }
476
477         *buff = state->out_buf;
478         return (total);
479 }
480
481 static int
482 program_filter_close(struct archive_read_filter *self)
483 {
484         struct program_filter   *state;
485         int e;
486
487         state = (struct program_filter *)self->data;
488         e = child_stop(self, state);
489
490         /* Release our private data. */
491         free(state->out_buf);
492         archive_string_free(&state->description);
493         free(state);
494
495         return (e);
496 }