blob: 42fdd96a6cd3a0e4e80dda99b00c6a18acef1d53 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath5699b852003-11-15 23:19:05 +00002/*
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +00003 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath5699b852003-11-15 23:19:05 +00004 */
5
Glenn L McGrath5699b852003-11-15 23:19:05 +00006#include "libbb.h"
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +00007#include "unarchive.h"
8
Glenn L McGrath5699b852003-11-15 23:19:05 +00009/* transformer(), more than meets the eye */
Denis Vlasenko211f7f82007-09-05 11:48:32 +000010/*
Denis Vlasenko059c9172007-11-12 02:13:12 +000011 * On MMU machine, the transform_prog is removed by macro magic
12 * in include/unarchive.h. On NOMMU, transformer is removed.
Denis Vlasenko211f7f82007-09-05 11:48:32 +000013 */
Denis Vlasenkob6052722008-07-10 17:43:01 +000014void FAST_FUNC open_transformer(int fd,
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000015 USE_DESKTOP(long long) int FAST_FUNC (*transformer)(int src_fd, int dst_fd),
Denis Vlasenko059c9172007-11-12 02:13:12 +000016 const char *transform_prog)
Glenn L McGrath5699b852003-11-15 23:19:05 +000017{
Denis Vlasenko37188322008-02-16 13:20:56 +000018 struct fd_pair fd_pipe;
Glenn L McGrath5699b852003-11-15 23:19:05 +000019 int pid;
20
Denis Vlasenko37188322008-02-16 13:20:56 +000021 xpiped_pair(fd_pipe);
Glenn L McGrath5699b852003-11-15 23:19:05 +000022
Denis Vlasenko82604e92008-07-01 15:59:42 +000023#if BB_MMU
24 pid = fork();
25 if (pid == -1)
Denis Vlasenkofa0b56d2008-07-01 16:09:07 +000026 bb_perror_msg_and_die("vfork" + 1);
Denis Vlasenko82604e92008-07-01 15:59:42 +000027#else
28 pid = vfork();
29 if (pid == -1)
Denis Vlasenkofa0b56d2008-07-01 16:09:07 +000030 bb_perror_msg_and_die("vfork");
Denis Vlasenko82604e92008-07-01 15:59:42 +000031#endif
32
Glenn L McGrath5699b852003-11-15 23:19:05 +000033 if (pid == 0) {
34 /* child process */
Denis Vlasenkob6052722008-07-10 17:43:01 +000035 close(fd_pipe.rd); /* we don't want to read from the parent */
Denis Vlasenkoea620772006-10-14 02:23:43 +000036 // FIXME: error check?
Denis Vlasenko211f7f82007-09-05 11:48:32 +000037#if BB_MMU
Denis Vlasenkob6052722008-07-10 17:43:01 +000038 transformer(fd, fd_pipe.wr);
Denis Vlasenko7e0fbf92007-09-04 19:33:22 +000039 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkob6052722008-07-10 17:43:01 +000040 close(fd_pipe.wr); /* send EOF */
41 close(fd);
Denis Vlasenko7e0fbf92007-09-04 19:33:22 +000042 }
Denis Vlasenko2649f212008-06-26 03:26:57 +000043 /* must be _exit! bug was actually seen here */
44 _exit(EXIT_SUCCESS);
Denis Vlasenko211f7f82007-09-05 11:48:32 +000045#else
Denis Vlasenko059c9172007-11-12 02:13:12 +000046 {
47 char *argv[4];
Denis Vlasenkob6052722008-07-10 17:43:01 +000048 xmove_fd(fd, 0);
Denis Vlasenko37188322008-02-16 13:20:56 +000049 xmove_fd(fd_pipe.wr, 1);
Denis Vlasenko059c9172007-11-12 02:13:12 +000050 argv[0] = (char*)transform_prog;
51 argv[1] = (char*)"-cf";
52 argv[2] = (char*)"-";
53 argv[3] = NULL;
54 BB_EXECVP(transform_prog, argv);
Denis Vlasenko5a07a1d2008-04-21 21:56:07 +000055 bb_perror_msg_and_die("can't exec %s", transform_prog);
Denis Vlasenko059c9172007-11-12 02:13:12 +000056 }
Denis Vlasenko211f7f82007-09-05 11:48:32 +000057#endif
Denis Vlasenkoea620772006-10-14 02:23:43 +000058 /* notreached */
Glenn L McGrath5699b852003-11-15 23:19:05 +000059 }
Glenn L McGrath20872be2003-11-18 21:31:19 +000060
Glenn L McGrath5699b852003-11-15 23:19:05 +000061 /* parent process */
Denis Vlasenkob6052722008-07-10 17:43:01 +000062 close(fd_pipe.wr); /* don't want to write to the child */
63 xmove_fd(fd_pipe.rd, fd);
Glenn L McGrath5699b852003-11-15 23:19:05 +000064}