1/* vi: set sw=4 ts=4: */
2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
6#include "libbb.h"
7#include "unarchive.h"
8
9/* transformer(), more than meets the eye */
10/*
11 * On MMU machine, the transform_prog and ... are stripped
12 * by a macro in include/unarchive.h. On NOMMU, transformer is stripped.
13 */
14int open_transformer(int src_fd,
15	USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd),
16	const char *transform_prog, ...)
17{
18	int fd_pipe[2];
19	int pid;
20
21	xpipe(fd_pipe);
22
23#if BB_MMU
24	pid = fork();
25#else
26	pid = vfork();
27#endif
28	if (pid == -1)
29		bb_perror_msg_and_die("fork failed");
30
31	if (pid == 0) {
32#if !BB_MMU
33		va_list ap;
34#endif
35		/* child process */
36		close(fd_pipe[0]); /* We don't wan't to read from the parent */
37#if BB_MMU
38		transformer(src_fd, fd_pipe[1]);
39		if (ENABLE_FEATURE_CLEAN_UP) {
40			close(fd_pipe[1]); /* Send EOF */
41			close(src_fd);
42		}
43		exit(0);
44#else
45		xmove_fd(src_fd, 0);
46		xmove_fd(fd_pipe[1], 1);
47		va_start(ap, transform_prog);
48		BB_EXECVP(transform_prog, ap);
49		bb_perror_and_die("exec failed");
50#endif
51		/* notreached */
52	}
53
54	/* parent process */
55	close(fd_pipe[1]); /* Don't want to write to the child */
56
57	return fd_pipe[0];
58}
59