"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 2 | /* |
Bernhard Reutner-Fischer | cfb53df | 2006-04-02 21:50:01 +0000 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 6 | #include "libbb.h" |
Bernhard Reutner-Fischer | cfb53df | 2006-04-02 21:50:01 +0000 | [diff] [blame] | 7 | #include "unarchive.h" |
| 8 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 9 | /* transformer(), more than meets the eye */ |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame^] | 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 | */ |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 14 | int open_transformer(int src_fd, |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame^] | 15 | USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd), |
| 16 | const char *transform_prog, ...) |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 17 | { |
| 18 | int fd_pipe[2]; |
| 19 | int pid; |
| 20 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 21 | xpipe(fd_pipe); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame^] | 23 | #if BB_MMU |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 24 | pid = fork(); |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame^] | 25 | #else |
| 26 | pid = vfork(); |
| 27 | #endif |
| 28 | if (pid == -1) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 29 | bb_perror_msg_and_die("fork failed"); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 30 | |
| 31 | if (pid == 0) { |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame^] | 32 | #if !BB_MMU |
| 33 | va_list ap; |
| 34 | #endif |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 35 | /* child process */ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 36 | close(fd_pipe[0]); /* We don't wan't to read from the parent */ |
| 37 | // FIXME: error check? |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame^] | 38 | #if BB_MMU |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 39 | transformer(src_fd, fd_pipe[1]); |
Denis Vlasenko | 7e0fbf9 | 2007-09-04 19:33:22 +0000 | [diff] [blame] | 40 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 41 | close(fd_pipe[1]); /* Send EOF */ |
| 42 | close(src_fd); |
| 43 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 44 | exit(0); |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame^] | 45 | #else |
| 46 | xmove_fd(src_fd, 0); |
| 47 | xmove_fd(fd_pipe[1], 1); |
| 48 | va_start(ap, transform_prog); |
| 49 | BB_EXECVP(transform_prog, ap); |
| 50 | bb_perror_and_die("exec failed"); |
| 51 | #endif |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 52 | /* notreached */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 53 | } |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 54 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 55 | /* parent process */ |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 56 | close(fd_pipe[1]); /* Don't want to write to the child */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 58 | return fd_pipe[0]; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 59 | } |