open_transformer.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #if BB_MMU
  20. pid = fork();
  21. if (pid == -1)
  22. bb_perror_msg_and_die("vfork" + 1);
  23. #else
  24. pid = vfork();
  25. if (pid == -1)
  26. bb_perror_msg_and_die("vfork");
  27. #endif
  28. if (pid == 0) {
  29. /* child process */
  30. close(fd_pipe.rd); /* we don't want to read from the parent */
  31. // FIXME: error check?
  32. #if BB_MMU
  33. transformer(fd, fd_pipe.wr);
  34. if (ENABLE_FEATURE_CLEAN_UP) {
  35. close(fd_pipe.wr); /* send EOF */
  36. close(fd);
  37. }
  38. /* must be _exit! bug was actually seen here */
  39. _exit(EXIT_SUCCESS);
  40. #else
  41. {
  42. char *argv[4];
  43. xmove_fd(fd, 0);
  44. xmove_fd(fd_pipe.wr, 1);
  45. argv[0] = (char*)transform_prog;
  46. argv[1] = (char*)"-cf";
  47. argv[2] = (char*)"-";
  48. argv[3] = NULL;
  49. BB_EXECVP(transform_prog, argv);
  50. bb_perror_msg_and_die("can't execute '%s'", transform_prog);
  51. }
  52. #endif
  53. /* notreached */
  54. }
  55. /* parent process */
  56. close(fd_pipe.wr); /* don't want to write to the child */
  57. xmove_fd(fd_pipe.rd, fd);
  58. }