"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 | { |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame^] | 18 | struct fd_pair fd_pipe; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 19 | int pid; |
| 20 | |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame^] | 21 | xpiped_pair(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 | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame^] | 33 | close(fd_pipe.rd); /* We don't want to read from the parent */ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 34 | // FIXME: error check? |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 35 | #if BB_MMU |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame^] | 36 | transformer(src_fd, fd_pipe.wr); |
Denis Vlasenko | 7e0fbf9 | 2007-09-04 19:33:22 +0000 | [diff] [blame] | 37 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame^] | 38 | close(fd_pipe.wr); /* Send EOF */ |
Denis Vlasenko | 7e0fbf9 | 2007-09-04 19:33:22 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame^] | 46 | xmove_fd(fd_pipe.wr, 1); |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame] | 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 */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame^] | 59 | close(fd_pipe.wr); /* Don't want to write to the child */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame^] | 61 | return fd_pipe.rd; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 62 | } |