archive_read_support_compression_program.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*-
  2. * Copyright (c) 2007 Joerg Sonnenberger
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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.
  13. *
  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.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_compression_program.c,v 1.6 2008/12/06 06:45:15 kientzle Exp $");
  27. #ifdef HAVE_SYS_WAIT_H
  28. # include <sys/wait.h>
  29. #endif
  30. #ifdef HAVE_ERRNO_H
  31. # include <errno.h>
  32. #endif
  33. #ifdef HAVE_FCNTL_H
  34. # include <fcntl.h>
  35. #endif
  36. #ifdef HAVE_LIMITS_H
  37. # include <limits.h>
  38. #endif
  39. #ifdef HAVE_SIGNAL_H
  40. # include <signal.h>
  41. #endif
  42. #ifdef HAVE_STDLIB_H
  43. # include <stdlib.h>
  44. #endif
  45. #ifdef HAVE_STRING_H
  46. # include <string.h>
  47. #endif
  48. #ifdef HAVE_UNISTD_H
  49. # include <unistd.h>
  50. #endif
  51. #include "archive.h"
  52. #include "archive_private.h"
  53. #include "archive_read_private.h"
  54. int
  55. archive_read_support_compression_program(struct archive *a, const char *cmd)
  56. {
  57. return (archive_read_support_compression_program_signature(a, cmd, NULL, 0));
  58. }
  59. /* This capability is only available on POSIX systems. */
  60. #if (!defined(HAVE_PIPE) || !defined(HAVE_FCNTL) || \
  61. !(defined(HAVE_FORK) || defined(HAVE_VFORK))) && (!defined(_WIN32) || defined(__CYGWIN__))
  62. /*
  63. * On non-Posix systems, allow the program to build, but choke if
  64. * this function is actually invoked.
  65. */
  66. int
  67. archive_read_support_compression_program_signature(struct archive *_a,
  68. const char *cmd, void *signature, size_t signature_len)
  69. {
  70. (void)_a; /* UNUSED */
  71. (void)cmd; /* UNUSED */
  72. (void)signature; /* UNUSED */
  73. (void)signature_len; /* UNUSED */
  74. archive_set_error(_a, -1,
  75. "External compression programs not supported on this platform");
  76. return (ARCHIVE_FATAL);
  77. }
  78. int
  79. __archive_read_program(struct archive_read_filter *self, const char *cmd)
  80. {
  81. (void)self; /* UNUSED */
  82. (void)cmd; /* UNUSED */
  83. archive_set_error(&self->archive->archive, -1,
  84. "External compression programs not supported on this platform");
  85. return (ARCHIVE_FATAL);
  86. }
  87. #else
  88. #include "filter_fork.h"
  89. /*
  90. * The bidder object stores the command and the signature to watch for.
  91. * The 'inhibit' entry here is used to ensure that unchecked filters never
  92. * bid twice in the same pipeline.
  93. */
  94. struct program_bidder {
  95. char *cmd;
  96. void *signature;
  97. size_t signature_len;
  98. int inhibit;
  99. };
  100. static int program_bidder_bid(struct archive_read_filter_bidder *,
  101. struct archive_read_filter *upstream);
  102. static int program_bidder_init(struct archive_read_filter *);
  103. static int program_bidder_free(struct archive_read_filter_bidder *);
  104. /*
  105. * The actual filter needs to track input and output data.
  106. */
  107. struct program_filter {
  108. char *description;
  109. pid_t child;
  110. int exit_status;
  111. int waitpid_return;
  112. int child_stdin, child_stdout;
  113. char *out_buf;
  114. size_t out_buf_len;
  115. };
  116. static ssize_t program_filter_read(struct archive_read_filter *,
  117. const void **);
  118. static int program_filter_close(struct archive_read_filter *);
  119. int
  120. archive_read_support_compression_program_signature(struct archive *_a,
  121. const char *cmd, const void *signature, size_t signature_len)
  122. {
  123. struct archive_read *a = (struct archive_read *)_a;
  124. struct archive_read_filter_bidder *bidder;
  125. struct program_bidder *state;
  126. /*
  127. * Get a bidder object from the read core.
  128. */
  129. bidder = __archive_read_get_bidder(a);
  130. if (bidder == NULL)
  131. return (ARCHIVE_FATAL);
  132. /*
  133. * Allocate our private state.
  134. */
  135. state = (struct program_bidder *)calloc(1, sizeof (*state));
  136. if (state == NULL)
  137. goto memerr;
  138. state->cmd = strdup(cmd);
  139. if (state->cmd == NULL)
  140. goto memerr;
  141. if (signature != NULL && signature_len > 0) {
  142. state->signature_len = signature_len;
  143. state->signature = malloc(signature_len);
  144. memcpy(state->signature, signature, signature_len);
  145. }
  146. /*
  147. * Fill in the bidder object.
  148. */
  149. bidder->data = state;
  150. bidder->bid = program_bidder_bid;
  151. bidder->init = program_bidder_init;
  152. bidder->options = NULL;
  153. bidder->free = program_bidder_free;
  154. return (ARCHIVE_OK);
  155. memerr:
  156. free(state);
  157. archive_set_error(_a, ENOMEM, "Can't allocate memory");
  158. return (ARCHIVE_FATAL);
  159. }
  160. static int
  161. program_bidder_free(struct archive_read_filter_bidder *self)
  162. {
  163. struct program_bidder *state = (struct program_bidder *)self->data;
  164. free(state->cmd);
  165. free(state->signature);
  166. free(self->data);
  167. return (ARCHIVE_OK);
  168. }
  169. /*
  170. * If we do have a signature, bid only if that matches.
  171. *
  172. * If there's no signature, we bid INT_MAX the first time
  173. * we're called, then never bid again.
  174. */
  175. static int
  176. program_bidder_bid(struct archive_read_filter_bidder *self,
  177. struct archive_read_filter *upstream)
  178. {
  179. struct program_bidder *state = self->data;
  180. const char *p;
  181. /* If we have a signature, use that to match. */
  182. if (state->signature_len > 0) {
  183. p = __archive_read_filter_ahead(upstream,
  184. state->signature_len, NULL);
  185. if (p == NULL)
  186. return (0);
  187. /* No match, so don't bid. */
  188. if (memcmp(p, state->signature, state->signature_len) != 0)
  189. return (0);
  190. return (state->signature_len * 8);
  191. }
  192. /* Otherwise, bid once and then never bid again. */
  193. if (state->inhibit)
  194. return (0);
  195. state->inhibit = 1;
  196. return (INT_MAX);
  197. }
  198. /*
  199. * Shut down the child, return ARCHIVE_OK if it exited normally.
  200. *
  201. * Note that the return value is sticky; if we're called again,
  202. * we won't reap the child again, but we will return the same status
  203. * (including error message if the child came to a bad end).
  204. */
  205. static int
  206. child_stop(struct archive_read_filter *self, struct program_filter *state)
  207. {
  208. /* Close our side of the I/O with the child. */
  209. if (state->child_stdin != -1) {
  210. close(state->child_stdin);
  211. state->child_stdin = -1;
  212. }
  213. if (state->child_stdout != -1) {
  214. close(state->child_stdout);
  215. state->child_stdout = -1;
  216. }
  217. if (state->child != 0) {
  218. /* Reap the child. */
  219. do {
  220. state->waitpid_return
  221. = waitpid(state->child, &state->exit_status, 0);
  222. } while (state->waitpid_return == -1 && errno == EINTR);
  223. state->child = 0;
  224. }
  225. if (state->waitpid_return < 0) {
  226. /* waitpid() failed? This is ugly. */
  227. archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
  228. "Child process exited badly");
  229. return (ARCHIVE_WARN);
  230. }
  231. if (WIFSIGNALED(state->exit_status)) {
  232. #ifdef SIGPIPE
  233. /* If the child died because we stopped reading before
  234. * it was done, that's okay. Some archive formats
  235. * have padding at the end that we routinely ignore. */
  236. /* The alternative to this would be to add a step
  237. * before close(child_stdout) above to read from the
  238. * child until the child has no more to write. */
  239. if (WTERMSIG(state->exit_status) == SIGPIPE)
  240. return (ARCHIVE_OK);
  241. #endif
  242. archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
  243. "Child process exited with signal %d",
  244. WTERMSIG(state->exit_status));
  245. return (ARCHIVE_WARN);
  246. }
  247. if (WIFEXITED(state->exit_status)) {
  248. if (WEXITSTATUS(state->exit_status) == 0)
  249. return (ARCHIVE_OK);
  250. archive_set_error(&self->archive->archive,
  251. ARCHIVE_ERRNO_MISC,
  252. "Child process exited with status %d",
  253. WEXITSTATUS(state->exit_status));
  254. return (ARCHIVE_WARN);
  255. }
  256. return (ARCHIVE_WARN);
  257. }
  258. /*
  259. * Use select() to decide whether the child is ready for read or write.
  260. */
  261. static ssize_t
  262. child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
  263. {
  264. struct program_filter *state = self->data;
  265. ssize_t ret, requested, avail;
  266. const char *p;
  267. int saved_errno;
  268. requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
  269. for (;;) {
  270. do {
  271. ret = read(state->child_stdout, buf, requested);
  272. } while (ret == -1 && errno == EINTR);
  273. if (ret > 0)
  274. return (ret);
  275. if (ret == 0 || (ret == -1 && errno == EPIPE))
  276. /* Child has closed its output; reap the child
  277. * and return the status. */
  278. return (child_stop(self, state));
  279. if (ret == -1 && errno != EAGAIN)
  280. return (-1);
  281. if (state->child_stdin == -1) {
  282. /* Block until child has some I/O ready. */
  283. __archive_check_child(state->child_stdin,
  284. state->child_stdout);
  285. continue;
  286. }
  287. /* Get some more data from upstream. */
  288. p = __archive_read_filter_ahead(self->upstream, 1, &avail);
  289. if (p == NULL) {
  290. close(state->child_stdin);
  291. state->child_stdin = -1;
  292. fcntl(state->child_stdout, F_SETFL, 0);
  293. if (avail < 0)
  294. return (avail);
  295. continue;
  296. }
  297. do {
  298. ret = write(state->child_stdin, p, avail);
  299. } while (ret == -1 && errno == EINTR);
  300. if (ret > 0) {
  301. /* Consume whatever we managed to write. */
  302. __archive_read_filter_consume(self->upstream, ret);
  303. } else if (ret == -1 && errno == EAGAIN) {
  304. /* Block until child has some I/O ready. */
  305. __archive_check_child(state->child_stdin,
  306. state->child_stdout);
  307. } else {
  308. /* Save errno from the write. */
  309. saved_errno = errno;
  310. /* Write failed. */
  311. close(state->child_stdin);
  312. state->child_stdin = -1;
  313. fcntl(state->child_stdout, F_SETFL, 0);
  314. /* Restore errno. */
  315. errno = saved_errno;
  316. /* If it was a bad error, we're done; otherwise
  317. * it was EPIPE, and we can still read
  318. * from the child. */
  319. if (ret == -1 && errno != EPIPE)
  320. return (-1);
  321. }
  322. }
  323. }
  324. int
  325. __archive_read_program(struct archive_read_filter *self, const char *cmd)
  326. {
  327. struct program_filter *state;
  328. static const size_t out_buf_len = 65536;
  329. char *out_buf;
  330. char *description;
  331. const char *prefix = "Program: ";
  332. state = (struct program_filter *)calloc(1, sizeof(*state));
  333. out_buf = (char *)malloc(out_buf_len);
  334. description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
  335. if (state == NULL || out_buf == NULL || description == NULL) {
  336. archive_set_error(&self->archive->archive, ENOMEM,
  337. "Can't allocate input data");
  338. free(state);
  339. free(out_buf);
  340. free(description);
  341. return (ARCHIVE_FATAL);
  342. }
  343. self->code = ARCHIVE_COMPRESSION_PROGRAM;
  344. state->description = description;
  345. strcpy(state->description, prefix);
  346. strcat(state->description, cmd);
  347. self->name = state->description;
  348. state->out_buf = out_buf;
  349. state->out_buf_len = out_buf_len;
  350. if ((state->child = __archive_create_child(cmd,
  351. &state->child_stdin, &state->child_stdout)) == -1) {
  352. free(state->out_buf);
  353. free(state);
  354. archive_set_error(&self->archive->archive, EINVAL,
  355. "Can't initialise filter");
  356. return (ARCHIVE_FATAL);
  357. }
  358. self->data = state;
  359. self->read = program_filter_read;
  360. self->skip = NULL;
  361. self->close = program_filter_close;
  362. /* XXX Check that we can read at least one byte? */
  363. return (ARCHIVE_OK);
  364. }
  365. static int
  366. program_bidder_init(struct archive_read_filter *self)
  367. {
  368. struct program_bidder *bidder_state;
  369. bidder_state = (struct program_bidder *)self->bidder->data;
  370. return (__archive_read_program(self, bidder_state->cmd));
  371. }
  372. static ssize_t
  373. program_filter_read(struct archive_read_filter *self, const void **buff)
  374. {
  375. struct program_filter *state;
  376. ssize_t bytes;
  377. size_t total;
  378. char *p;
  379. state = (struct program_filter *)self->data;
  380. total = 0;
  381. p = state->out_buf;
  382. while (state->child_stdout != -1 && total < state->out_buf_len) {
  383. bytes = child_read(self, p, state->out_buf_len - total);
  384. if (bytes < 0)
  385. /* No recovery is possible if we can no longer
  386. * read from the child. */
  387. return (ARCHIVE_FATAL);
  388. if (bytes == 0)
  389. /* We got EOF from the child. */
  390. break;
  391. total += bytes;
  392. p += bytes;
  393. }
  394. *buff = state->out_buf;
  395. return (total);
  396. }
  397. static int
  398. program_filter_close(struct archive_read_filter *self)
  399. {
  400. struct program_filter *state;
  401. int e;
  402. state = (struct program_filter *)self->data;
  403. e = child_stop(self, state);
  404. /* Release our private data. */
  405. free(state->out_buf);
  406. free(state->description);
  407. free(state);
  408. return (e);
  409. }
  410. #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */