open_transformer.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  4. */
  5. #include "libbb.h"
  6. #include "unarchive.h"
  7. /* transformer(), more than meets the eye */
  8. /*
  9. * On MMU machine, the transform_prog is removed by macro magic
  10. * in include/unarchive.h. On NOMMU, transformer is removed.
  11. */
  12. void FAST_FUNC open_transformer(int fd,
  13. IF_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd),
  14. const char *transform_prog)
  15. {
  16. struct fd_pair fd_pipe;
  17. int pid;
  18. xpiped_pair(fd_pipe);
  19. pid = BB_MMU ? xfork() : xvfork();
  20. if (pid == 0) {
  21. /* Child */
  22. close(fd_pipe.rd); /* we don't want to read from the parent */
  23. // FIXME: error check?
  24. #if BB_MMU
  25. transformer(fd, fd_pipe.wr);
  26. if (ENABLE_FEATURE_CLEAN_UP) {
  27. close(fd_pipe.wr); /* send EOF */
  28. close(fd);
  29. }
  30. /* must be _exit! bug was actually seen here */
  31. _exit(EXIT_SUCCESS);
  32. #else
  33. {
  34. char *argv[4];
  35. xmove_fd(fd, 0);
  36. xmove_fd(fd_pipe.wr, 1);
  37. argv[0] = (char*)transform_prog;
  38. argv[1] = (char*)"-cf";
  39. argv[2] = (char*)"-";
  40. argv[3] = NULL;
  41. BB_EXECVP(transform_prog, argv);
  42. bb_perror_msg_and_die("can't execute '%s'", transform_prog);
  43. }
  44. #endif
  45. /* notreached */
  46. }
  47. /* parent process */
  48. close(fd_pipe.wr); /* don't want to write to the child */
  49. xmove_fd(fd_pipe.rd, fd);
  50. }