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. int open_transformer(int src_fd,
  13. USE_DESKTOP(long long) int (*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("can't fork");
  23. #else
  24. pid = vfork();
  25. if (pid == -1)
  26. bb_perror_msg_and_die("can't 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(src_fd, fd_pipe.wr);
  34. if (ENABLE_FEATURE_CLEAN_UP) {
  35. close(fd_pipe.wr); /* Send EOF */
  36. close(src_fd);
  37. }
  38. exit(EXIT_SUCCESS);
  39. #else
  40. {
  41. char *argv[4];
  42. xmove_fd(src_fd, 0);
  43. xmove_fd(fd_pipe.wr, 1);
  44. argv[0] = (char*)transform_prog;
  45. argv[1] = (char*)"-cf";
  46. argv[2] = (char*)"-";
  47. argv[3] = NULL;
  48. BB_EXECVP(transform_prog, argv);
  49. bb_perror_msg_and_die("can't exec %s", transform_prog);
  50. }
  51. #endif
  52. /* notreached */
  53. }
  54. /* parent process */
  55. close(fd_pipe.wr); /* Don't want to write to the child */
  56. return fd_pipe.rd;
  57. }