"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 | /* |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame^] | 11 | * On MMU machine, the transform_prog is removed by macro magic |
| 12 | * in include/unarchive.h. On NOMMU, transformer is removed. |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 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), |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame^] | 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) { |
| 32 | /* child process */ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 33 | close(fd_pipe[0]); /* We don't wan't to read from the parent */ |
| 34 | // FIXME: error check? |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 35 | #if BB_MMU |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 36 | transformer(src_fd, fd_pipe[1]); |
Denis Vlasenko | 7e0fbf9 | 2007-09-04 19:33:22 +0000 | [diff] [blame] | 37 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 38 | close(fd_pipe[1]); /* Send EOF */ |
| 39 | close(src_fd); |
| 40 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 41 | exit(0); |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 42 | #else |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame^] | 43 | { |
| 44 | char *argv[4]; |
| 45 | xmove_fd(src_fd, 0); |
| 46 | xmove_fd(fd_pipe[1], 1); |
| 47 | argv[0] = (char*)transform_prog; |
| 48 | argv[1] = (char*)"-cf"; |
| 49 | argv[2] = (char*)"-"; |
| 50 | argv[3] = NULL; |
| 51 | BB_EXECVP(transform_prog, argv); |
| 52 | bb_perror_msg_and_die("exec failed"); |
| 53 | } |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 54 | #endif |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 55 | /* notreached */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 56 | } |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 57 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 58 | /* parent process */ |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 59 | 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] | 60 | |
Denis Vlasenko | 97a8dd3 | 2006-10-01 15:55:11 +0000 | [diff] [blame] | 61 | return fd_pipe[0]; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 62 | } |