archive_read_support_compression_program.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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, const 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(sizeof (*state), 1);
  136. if (state == NULL)
  137. return (ARCHIVE_FATAL);
  138. state->cmd = strdup(cmd);
  139. if (signature != NULL && signature_len > 0) {
  140. state->signature_len = signature_len;
  141. state->signature = malloc(signature_len);
  142. memcpy(state->signature, signature, signature_len);
  143. }
  144. /*
  145. * Fill in the bidder object.
  146. */
  147. bidder->data = state;
  148. bidder->bid = program_bidder_bid;
  149. bidder->init = program_bidder_init;
  150. bidder->options = NULL;
  151. bidder->free = program_bidder_free;
  152. return (ARCHIVE_OK);
  153. }
  154. static int
  155. program_bidder_free(struct archive_read_filter_bidder *self)
  156. {
  157. struct program_bidder *state = (struct program_bidder *)self->data;
  158. free(state->cmd);
  159. free(state->signature);
  160. free(self->data);
  161. return (ARCHIVE_OK);
  162. }
  163. /*
  164. * If we do have a signature, bid only if that matches.
  165. *
  166. * If there's no signature, we bid INT_MAX the first time
  167. * we're called, then never bid again.
  168. */
  169. static int
  170. program_bidder_bid(struct archive_read_filter_bidder *self,
  171. struct archive_read_filter *upstream)
  172. {
  173. struct program_bidder *state = self->data;
  174. const char *p;
  175. /* If we have a signature, use that to match. */
  176. if (state->signature_len > 0) {
  177. p = __archive_read_filter_ahead(upstream,
  178. state->signature_len, NULL);
  179. if (p == NULL)
  180. return (0);
  181. /* No match, so don't bid. */
  182. if (memcmp(p, state->signature, state->signature_len) != 0)
  183. return (0);
  184. return (state->signature_len * 8);
  185. }
  186. /* Otherwise, bid once and then never bid again. */
  187. if (state->inhibit)
  188. return (0);
  189. state->inhibit = 1;
  190. return (INT_MAX);
  191. }
  192. /*
  193. * Shut down the child, return ARCHIVE_OK if it exited normally.
  194. *
  195. * Note that the return value is sticky; if we're called again,
  196. * we won't reap the child again, but we will return the same status
  197. * (including error message if the child came to a bad end).
  198. */
  199. static int
  200. child_stop(struct archive_read_filter *self, struct program_filter *state)
  201. {
  202. /* Close our side of the I/O with the child. */
  203. if (state->child_stdin != -1) {
  204. close(state->child_stdin);
  205. state->child_stdin = -1;
  206. }
  207. if (state->child_stdout != -1) {
  208. close(state->child_stdout);
  209. state->child_stdout = -1;
  210. }
  211. if (state->child != 0) {
  212. /* Reap the child. */
  213. do {
  214. state->waitpid_return
  215. = waitpid(state->child, &state->exit_status, 0);
  216. } while (state->waitpid_return == -1 && errno == EINTR);
  217. state->child = 0;
  218. }
  219. if (state->waitpid_return < 0) {
  220. /* waitpid() failed? This is ugly. */
  221. archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
  222. "Child process exited badly");
  223. return (ARCHIVE_WARN);
  224. }
  225. if (WIFSIGNALED(state->exit_status)) {
  226. #ifdef SIGPIPE
  227. /* If the child died because we stopped reading before
  228. * it was done, that's okay. Some archive formats
  229. * have padding at the end that we routinely ignore. */
  230. /* The alternative to this would be to add a step
  231. * before close(child_stdout) above to read from the
  232. * child until the child has no more to write. */
  233. if (WTERMSIG(state->exit_status) == SIGPIPE)
  234. return (ARCHIVE_OK);
  235. #endif
  236. archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
  237. "Child process exited with signal %d",
  238. WTERMSIG(state->exit_status));
  239. return (ARCHIVE_WARN);
  240. }
  241. if (WIFEXITED(state->exit_status)) {
  242. if (WEXITSTATUS(state->exit_status) == 0)
  243. return (ARCHIVE_OK);
  244. archive_set_error(&self->archive->archive,
  245. ARCHIVE_ERRNO_MISC,
  246. "Child process exited with status %d",
  247. WEXITSTATUS(state->exit_status));
  248. return (ARCHIVE_WARN);
  249. }
  250. return (ARCHIVE_WARN);
  251. }
  252. /*
  253. * Use select() to decide whether the child is ready for read or write.
  254. */
  255. static ssize_t
  256. child_read(struct archive_read_filter *self, char *buf, size_t buf_len)
  257. {
  258. struct program_filter *state = self->data;
  259. ssize_t ret, requested, avail;
  260. const char *p;
  261. requested = buf_len > SSIZE_MAX ? SSIZE_MAX : buf_len;
  262. for (;;) {
  263. do {
  264. ret = read(state->child_stdout, buf, requested);
  265. } while (ret == -1 && errno == EINTR);
  266. if (ret > 0)
  267. return (ret);
  268. if (ret == 0 || (ret == -1 && errno == EPIPE))
  269. /* Child has closed its output; reap the child
  270. * and return the status. */
  271. return (child_stop(self, state));
  272. if (ret == -1 && errno != EAGAIN)
  273. return (-1);
  274. if (state->child_stdin == -1) {
  275. /* Block until child has some I/O ready. */
  276. __archive_check_child(state->child_stdin,
  277. state->child_stdout);
  278. continue;
  279. }
  280. /* Get some more data from upstream. */
  281. p = __archive_read_filter_ahead(self->upstream, 1, &avail);
  282. if (p == NULL) {
  283. close(state->child_stdin);
  284. state->child_stdin = -1;
  285. fcntl(state->child_stdout, F_SETFL, 0);
  286. if (avail < 0)
  287. return (avail);
  288. continue;
  289. }
  290. do {
  291. ret = write(state->child_stdin, p, avail);
  292. } while (ret == -1 && errno == EINTR);
  293. if (ret > 0) {
  294. /* Consume whatever we managed to write. */
  295. __archive_read_filter_consume(self->upstream, ret);
  296. } else if (ret == -1 && errno == EAGAIN) {
  297. /* Block until child has some I/O ready. */
  298. __archive_check_child(state->child_stdin,
  299. state->child_stdout);
  300. } else {
  301. /* Write failed. */
  302. close(state->child_stdin);
  303. state->child_stdin = -1;
  304. fcntl(state->child_stdout, F_SETFL, 0);
  305. /* If it was a bad error, we're done; otherwise
  306. * it was EPIPE or EOF, and we can still read
  307. * from the child. */
  308. if (ret == -1 && errno != EPIPE)
  309. return (-1);
  310. }
  311. }
  312. }
  313. int
  314. __archive_read_program(struct archive_read_filter *self, const char *cmd)
  315. {
  316. struct program_filter *state;
  317. static const size_t out_buf_len = 65536;
  318. char *out_buf;
  319. char *description;
  320. const char *prefix = "Program: ";
  321. state = (struct program_filter *)calloc(1, sizeof(*state));
  322. out_buf = (char *)malloc(out_buf_len);
  323. description = (char *)malloc(strlen(prefix) + strlen(cmd) + 1);
  324. if (state == NULL || out_buf == NULL || description == NULL) {
  325. archive_set_error(&self->archive->archive, ENOMEM,
  326. "Can't allocate input data");
  327. free(state);
  328. free(out_buf);
  329. free(description);
  330. return (ARCHIVE_FATAL);
  331. }
  332. self->code = ARCHIVE_COMPRESSION_PROGRAM;
  333. state->description = description;
  334. strcpy(state->description, prefix);
  335. strcat(state->description, cmd);
  336. self->name = state->description;
  337. state->out_buf = out_buf;
  338. state->out_buf_len = out_buf_len;
  339. if ((state->child = __archive_create_child(cmd,
  340. &state->child_stdin, &state->child_stdout)) == -1) {
  341. free(state->out_buf);
  342. free(state);
  343. archive_set_error(&self->archive->archive, EINVAL,
  344. "Can't initialise filter");
  345. return (ARCHIVE_FATAL);
  346. }
  347. self->data = state;
  348. self->read = program_filter_read;
  349. self->skip = NULL;
  350. self->close = program_filter_close;
  351. /* XXX Check that we can read at least one byte? */
  352. return (ARCHIVE_OK);
  353. }
  354. static int
  355. program_bidder_init(struct archive_read_filter *self)
  356. {
  357. struct program_bidder *bidder_state;
  358. bidder_state = (struct program_bidder *)self->bidder->data;
  359. return (__archive_read_program(self, bidder_state->cmd));
  360. }
  361. static ssize_t
  362. program_filter_read(struct archive_read_filter *self, const void **buff)
  363. {
  364. struct program_filter *state;
  365. ssize_t bytes;
  366. size_t total;
  367. char *p;
  368. state = (struct program_filter *)self->data;
  369. total = 0;
  370. p = state->out_buf;
  371. while (state->child_stdout != -1 && total < state->out_buf_len) {
  372. bytes = child_read(self, p, state->out_buf_len - total);
  373. if (bytes < 0)
  374. /* No recovery is possible if we can no longer
  375. * read from the child. */
  376. return (ARCHIVE_FATAL);
  377. if (bytes == 0)
  378. /* We got EOF from the child. */
  379. break;
  380. total += bytes;
  381. p += bytes;
  382. }
  383. *buff = state->out_buf;
  384. return (total);
  385. }
  386. static int
  387. program_filter_close(struct archive_read_filter *self)
  388. {
  389. struct program_filter *state;
  390. int e;
  391. state = (struct program_filter *)self->data;
  392. e = child_stop(self, state);
  393. /* Release our private data. */
  394. free(state->out_buf);
  395. free(state->description);
  396. free(state);
  397. return (e);
  398. }
  399. #endif /* !defined(HAVE_PIPE) || !defined(HAVE_VFORK) || !defined(HAVE_FCNTL) */