"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 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 7 | #include "bb_archive.h" |
Bernhard Reutner-Fischer | cfb53df | 2006-04-02 21:50:01 +0000 | [diff] [blame] | 8 | |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 9 | void FAST_FUNC init_transformer_aux_data(transformer_aux_data_t *aux) |
| 10 | { |
| 11 | memset(aux, 0, sizeof(*aux)); |
| 12 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 13 | |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 14 | int FAST_FUNC check_signature16(transformer_aux_data_t *aux, int src_fd, unsigned magic16) |
| 15 | { |
| 16 | if (aux && aux->check_signature) { |
| 17 | uint16_t magic2; |
| 18 | if (full_read(src_fd, &magic2, 2) != 2 || magic2 != magic16) { |
| 19 | bb_error_msg("invalid magic"); |
| 20 | #if 0 /* possible future extension */ |
| 21 | if (aux->check_signature > 1) |
| 22 | xfunc_die(); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 23 | #endif |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 24 | return -1; |
| 25 | } |
| 26 | } |
| 27 | return 0; |
| 28 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 29 | |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 30 | void check_errors_in_children(int signo) |
| 31 | { |
| 32 | int status; |
| 33 | |
| 34 | if (!signo) { |
| 35 | /* block waiting for any child */ |
| 36 | if (wait(&status) < 0) |
Denys Vlasenko | 577235d | 2013-02-28 16:38:25 +0100 | [diff] [blame] | 37 | //FIXME: check EINTR? |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 38 | return; /* probably there are no children */ |
| 39 | goto check_status; |
| 40 | } |
| 41 | |
| 42 | /* Wait for any child without blocking */ |
| 43 | for (;;) { |
| 44 | if (wait_any_nohang(&status) < 0) |
Denys Vlasenko | 577235d | 2013-02-28 16:38:25 +0100 | [diff] [blame] | 45 | //FIXME: check EINTR? |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 46 | /* wait failed?! I'm confused... */ |
| 47 | return; |
| 48 | check_status: |
Denys Vlasenko | 577235d | 2013-02-28 16:38:25 +0100 | [diff] [blame] | 49 | /*if (WIFEXITED(status) && WEXITSTATUS(status) == 0)*/ |
| 50 | /* On Linux, the above can be checked simply as: */ |
| 51 | if (status == 0) |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 52 | /* this child exited with 0 */ |
| 53 | continue; |
Denys Vlasenko | 577235d | 2013-02-28 16:38:25 +0100 | [diff] [blame] | 54 | /* Cannot happen: |
| 55 | if (!WIFSIGNALED(status) && !WIFEXITED(status)) ???; |
| 56 | */ |
Denys Vlasenko | faac1d3 | 2012-03-06 16:33:42 +0100 | [diff] [blame] | 57 | bb_got_signal = 1; |
| 58 | } |
| 59 | } |
| 60 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 61 | /* transformer(), more than meets the eye */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 62 | #if BB_MMU |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 63 | void FAST_FUNC open_transformer(int fd, |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 64 | int check_signature, |
| 65 | IF_DESKTOP(long long) int FAST_FUNC (*transformer)(transformer_aux_data_t *aux, int src_fd, int dst_fd) |
| 66 | ) |
| 67 | #else |
| 68 | void FAST_FUNC open_transformer(int fd, const char *transform_prog) |
| 69 | #endif |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 70 | { |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 71 | struct fd_pair fd_pipe; |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 72 | int pid; |
| 73 | |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 74 | xpiped_pair(fd_pipe); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 75 | pid = BB_MMU ? xfork() : xvfork(); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 76 | if (pid == 0) { |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 77 | /* Child */ |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 78 | 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] | 79 | // FIXME: error check? |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 80 | #if BB_MMU |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 81 | { |
| 82 | transformer_aux_data_t aux; |
| 83 | init_transformer_aux_data(&aux); |
| 84 | aux.check_signature = check_signature; |
| 85 | transformer(&aux, fd, fd_pipe.wr); |
| 86 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 87 | close(fd_pipe.wr); /* send EOF */ |
| 88 | close(fd); |
| 89 | } |
| 90 | /* must be _exit! bug was actually seen here */ |
| 91 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 7e0fbf9 | 2007-09-04 19:33:22 +0000 | [diff] [blame] | 92 | } |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 93 | #else |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame] | 94 | { |
| 95 | char *argv[4]; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 96 | xmove_fd(fd, 0); |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 97 | xmove_fd(fd_pipe.wr, 1); |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame] | 98 | argv[0] = (char*)transform_prog; |
| 99 | argv[1] = (char*)"-cf"; |
| 100 | argv[2] = (char*)"-"; |
| 101 | argv[3] = NULL; |
| 102 | BB_EXECVP(transform_prog, argv); |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 103 | bb_perror_msg_and_die("can't execute '%s'", transform_prog); |
Denis Vlasenko | 059c917 | 2007-11-12 02:13:12 +0000 | [diff] [blame] | 104 | } |
Denis Vlasenko | 211f7f8 | 2007-09-05 11:48:32 +0000 | [diff] [blame] | 105 | #endif |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 106 | /* notreached */ |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 107 | } |
Glenn L McGrath | 20872be | 2003-11-18 21:31:19 +0000 | [diff] [blame] | 108 | |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 109 | /* parent process */ |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 110 | close(fd_pipe.wr); /* don't want to write to the child */ |
| 111 | xmove_fd(fd_pipe.rd, fd); |
Glenn L McGrath | 5699b85 | 2003-11-15 23:19:05 +0000 | [diff] [blame] | 112 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 113 | |
| 114 | |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 115 | #if SEAMLESS_COMPRESSION |
| 116 | |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 117 | /* Used by e.g. rpm which gives us a fd without filename, |
| 118 | * thus we can't guess the format from filename's extension. |
| 119 | */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 120 | int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 121 | { |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 122 | union { |
| 123 | uint8_t b[4]; |
| 124 | uint16_t b16[2]; |
| 125 | uint32_t b32[1]; |
| 126 | } magic; |
| 127 | int offset = -2; |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 128 | USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_aux_data_t *aux, int src_fd, int dst_fd);) |
| 129 | USE_FOR_NOMMU(const char *xformer_prog;) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 130 | |
| 131 | /* .gz and .bz2 both have 2-byte signature, and their |
| 132 | * unpack_XXX_stream wants this header skipped. */ |
| 133 | xread(fd, magic.b16, sizeof(magic.b16[0])); |
| 134 | if (ENABLE_FEATURE_SEAMLESS_GZ |
| 135 | && magic.b16[0] == GZIP_MAGIC |
| 136 | ) { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 137 | USE_FOR_MMU(xformer = unpack_gz_stream;) |
| 138 | USE_FOR_NOMMU(xformer_prog = "gunzip";) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 139 | goto found_magic; |
| 140 | } |
| 141 | if (ENABLE_FEATURE_SEAMLESS_BZ2 |
| 142 | && magic.b16[0] == BZIP2_MAGIC |
| 143 | ) { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 144 | USE_FOR_MMU(xformer = unpack_bz2_stream;) |
| 145 | USE_FOR_NOMMU(xformer_prog = "bunzip2";) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 146 | goto found_magic; |
| 147 | } |
| 148 | if (ENABLE_FEATURE_SEAMLESS_XZ |
| 149 | && magic.b16[0] == XZ_MAGIC1 |
| 150 | ) { |
| 151 | offset = -6; |
| 152 | xread(fd, magic.b32, sizeof(magic.b32[0])); |
| 153 | if (magic.b32[0] == XZ_MAGIC2) { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 154 | USE_FOR_MMU(xformer = unpack_xz_stream;) |
| 155 | USE_FOR_NOMMU(xformer_prog = "unxz";) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 156 | goto found_magic; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* No known magic seen */ |
| 161 | if (fail_if_not_detected) |
| 162 | bb_error_msg_and_die("no gzip" |
| 163 | IF_FEATURE_SEAMLESS_BZ2("/bzip2") |
| 164 | IF_FEATURE_SEAMLESS_XZ("/xz") |
| 165 | " magic"); |
| 166 | xlseek(fd, offset, SEEK_CUR); |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 167 | return 1; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 168 | |
| 169 | found_magic: |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 170 | # if BB_MMU |
| 171 | open_transformer_with_no_sig(fd, xformer); |
| 172 | # else |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 173 | /* NOMMU version of open_transformer execs |
| 174 | * an external unzipper that wants |
| 175 | * file position at the start of the file */ |
| 176 | xlseek(fd, offset, SEEK_CUR); |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 177 | open_transformer_with_sig(fd, xformer, xformer_prog); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 178 | # endif |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 179 | return 0; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 180 | } |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 181 | |
| 182 | int FAST_FUNC open_zipped(const char *fname) |
| 183 | { |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 184 | char *sfx; |
| 185 | int fd; |
| 186 | |
| 187 | fd = open(fname, O_RDONLY); |
| 188 | if (fd < 0) |
| 189 | return fd; |
| 190 | |
| 191 | sfx = strrchr(fname, '.'); |
| 192 | if (sfx) { |
| 193 | sfx++; |
| 194 | if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0) |
| 195 | /* .lzma has no header/signature, just trust it */ |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 196 | open_transformer_with_sig(fd, unpack_lzma_stream, "unlzma"); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 197 | else |
| 198 | if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0) |
| 199 | || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0) |
| 200 | || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0) |
| 201 | ) { |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 202 | setup_unzip_on_fd(fd, /*fail_if_not_detected:*/ 1); |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | return fd; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Denys Vlasenko | 8a6a2f9 | 2012-03-06 16:27:48 +0100 | [diff] [blame] | 209 | #endif /* SEAMLESS_COMPRESSION */ |
| 210 | |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 211 | void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) |
| 212 | { |
| 213 | int fd; |
| 214 | char *image; |
| 215 | |
| 216 | fd = open_zipped(fname); |
| 217 | if (fd < 0) |
| 218 | return NULL; |
| 219 | |
| 220 | image = xmalloc_read(fd, maxsz_p); |
| 221 | if (!image) |
| 222 | bb_perror_msg("read error from '%s'", fname); |
| 223 | close(fd); |
| 224 | |
| 225 | return image; |
| 226 | } |