open_transformer.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 and ... are stripped
  10. * by a macro in include/unarchive.h. On NOMMU, transformer is stripped.
  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. int fd_pipe[2];
  17. int pid;
  18. xpipe(fd_pipe);
  19. #if BB_MMU
  20. pid = fork();
  21. #else
  22. pid = vfork();
  23. #endif
  24. if (pid == -1)
  25. bb_perror_msg_and_die("fork failed");
  26. if (pid == 0) {
  27. #if !BB_MMU
  28. va_list ap;
  29. #endif
  30. /* child process */
  31. close(fd_pipe[0]); /* We don't wan't to read from the parent */
  32. // FIXME: error check?
  33. #if BB_MMU
  34. transformer(src_fd, fd_pipe[1]);
  35. if (ENABLE_FEATURE_CLEAN_UP) {
  36. close(fd_pipe[1]); /* Send EOF */
  37. close(src_fd);
  38. }
  39. exit(0);
  40. #else
  41. xmove_fd(src_fd, 0);
  42. xmove_fd(fd_pipe[1], 1);
  43. va_start(ap, transform_prog);
  44. BB_EXECVP(transform_prog, ap);
  45. bb_perror_and_die("exec failed");
  46. #endif
  47. /* notreached */
  48. }
  49. /* parent process */
  50. close(fd_pipe[1]); /* Don't want to write to the child */
  51. return fd_pipe[0];
  52. }