blob: 1aeba13bc9eb6f1256ed4ab2660935ff0734b993 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath5699b852003-11-15 23:19:05 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath5699b852003-11-15 23:19:05 +00004 */
5
Glenn L McGrath5699b852003-11-15 23:19:05 +00006#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +02007#include "bb_archive.h"
Bernhard Reutner-Fischercfb53df2006-04-02 21:50:01 +00008
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +01009void FAST_FUNC init_transformer_aux_data(transformer_aux_data_t *aux)
10{
11 memset(aux, 0, sizeof(*aux));
12}
Denys Vlasenko59655072012-03-06 16:23:50 +010013
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010014int 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 Vlasenko59655072012-03-06 16:23:50 +010023#endif
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010024 return -1;
25 }
26 }
27 return 0;
28}
Denys Vlasenko59655072012-03-06 16:23:50 +010029
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010030void 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 Vlasenko577235d2013-02-28 16:38:25 +010037//FIXME: check EINTR?
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010038 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 Vlasenko577235d2013-02-28 16:38:25 +010045//FIXME: check EINTR?
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010046 /* wait failed?! I'm confused... */
47 return;
48 check_status:
Denys Vlasenko577235d2013-02-28 16:38:25 +010049 /*if (WIFEXITED(status) && WEXITSTATUS(status) == 0)*/
50 /* On Linux, the above can be checked simply as: */
51 if (status == 0)
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010052 /* this child exited with 0 */
53 continue;
Denys Vlasenko577235d2013-02-28 16:38:25 +010054 /* Cannot happen:
55 if (!WIFSIGNALED(status) && !WIFEXITED(status)) ???;
56 */
Denys Vlasenkofaac1d32012-03-06 16:33:42 +010057 bb_got_signal = 1;
58 }
59}
60
Glenn L McGrath5699b852003-11-15 23:19:05 +000061/* transformer(), more than meets the eye */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010062#if BB_MMU
Denis Vlasenkob6052722008-07-10 17:43:01 +000063void FAST_FUNC open_transformer(int fd,
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010064 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
68void FAST_FUNC open_transformer(int fd, const char *transform_prog)
69#endif
Glenn L McGrath5699b852003-11-15 23:19:05 +000070{
Denis Vlasenko37188322008-02-16 13:20:56 +000071 struct fd_pair fd_pipe;
Glenn L McGrath5699b852003-11-15 23:19:05 +000072 int pid;
73
Denis Vlasenko37188322008-02-16 13:20:56 +000074 xpiped_pair(fd_pipe);
Pascal Bellard926031b2010-07-04 15:32:38 +020075 pid = BB_MMU ? xfork() : xvfork();
Glenn L McGrath5699b852003-11-15 23:19:05 +000076 if (pid == 0) {
Pascal Bellard926031b2010-07-04 15:32:38 +020077 /* Child */
Denis Vlasenkob6052722008-07-10 17:43:01 +000078 close(fd_pipe.rd); /* we don't want to read from the parent */
Denis Vlasenkoea620772006-10-14 02:23:43 +000079 // FIXME: error check?
Denis Vlasenko211f7f82007-09-05 11:48:32 +000080#if BB_MMU
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010081 {
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +010082 IF_DESKTOP(long long) int r;
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010083 transformer_aux_data_t aux;
84 init_transformer_aux_data(&aux);
85 aux.check_signature = check_signature;
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +010086 r = transformer(&aux, fd, fd_pipe.wr);
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010087 if (ENABLE_FEATURE_CLEAN_UP) {
88 close(fd_pipe.wr); /* send EOF */
89 close(fd);
90 }
91 /* must be _exit! bug was actually seen here */
Denys Vlasenkoebfa9b52013-11-19 14:44:04 +010092 _exit(/*error if:*/ r < 0);
Denis Vlasenko7e0fbf92007-09-04 19:33:22 +000093 }
Denis Vlasenko211f7f82007-09-05 11:48:32 +000094#else
Denis Vlasenko059c9172007-11-12 02:13:12 +000095 {
96 char *argv[4];
Denis Vlasenkob6052722008-07-10 17:43:01 +000097 xmove_fd(fd, 0);
Denis Vlasenko37188322008-02-16 13:20:56 +000098 xmove_fd(fd_pipe.wr, 1);
Denis Vlasenko059c9172007-11-12 02:13:12 +000099 argv[0] = (char*)transform_prog;
100 argv[1] = (char*)"-cf";
101 argv[2] = (char*)"-";
102 argv[3] = NULL;
103 BB_EXECVP(transform_prog, argv);
Denis Vlasenkof9d4fc32009-04-21 20:40:51 +0000104 bb_perror_msg_and_die("can't execute '%s'", transform_prog);
Denis Vlasenko059c9172007-11-12 02:13:12 +0000105 }
Denis Vlasenko211f7f82007-09-05 11:48:32 +0000106#endif
Denis Vlasenkoea620772006-10-14 02:23:43 +0000107 /* notreached */
Glenn L McGrath5699b852003-11-15 23:19:05 +0000108 }
Glenn L McGrath20872be2003-11-18 21:31:19 +0000109
Glenn L McGrath5699b852003-11-15 23:19:05 +0000110 /* parent process */
Denis Vlasenkob6052722008-07-10 17:43:01 +0000111 close(fd_pipe.wr); /* don't want to write to the child */
112 xmove_fd(fd_pipe.rd, fd);
Glenn L McGrath5699b852003-11-15 23:19:05 +0000113}
Denys Vlasenko59655072012-03-06 16:23:50 +0100114
115
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100116#if SEAMLESS_COMPRESSION
117
Denys Vlasenko59655072012-03-06 16:23:50 +0100118/* Used by e.g. rpm which gives us a fd without filename,
119 * thus we can't guess the format from filename's extension.
120 */
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100121int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected)
Denys Vlasenko59655072012-03-06 16:23:50 +0100122{
Denys Vlasenko59655072012-03-06 16:23:50 +0100123 union {
124 uint8_t b[4];
125 uint16_t b16[2];
126 uint32_t b32[1];
127 } magic;
128 int offset = -2;
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100129 USE_FOR_MMU(IF_DESKTOP(long long) int FAST_FUNC (*xformer)(transformer_aux_data_t *aux, int src_fd, int dst_fd);)
130 USE_FOR_NOMMU(const char *xformer_prog;)
Denys Vlasenko59655072012-03-06 16:23:50 +0100131
132 /* .gz and .bz2 both have 2-byte signature, and their
133 * unpack_XXX_stream wants this header skipped. */
134 xread(fd, magic.b16, sizeof(magic.b16[0]));
135 if (ENABLE_FEATURE_SEAMLESS_GZ
136 && magic.b16[0] == GZIP_MAGIC
137 ) {
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100138 USE_FOR_MMU(xformer = unpack_gz_stream;)
139 USE_FOR_NOMMU(xformer_prog = "gunzip";)
Denys Vlasenko59655072012-03-06 16:23:50 +0100140 goto found_magic;
141 }
142 if (ENABLE_FEATURE_SEAMLESS_BZ2
143 && magic.b16[0] == BZIP2_MAGIC
144 ) {
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100145 USE_FOR_MMU(xformer = unpack_bz2_stream;)
146 USE_FOR_NOMMU(xformer_prog = "bunzip2";)
Denys Vlasenko59655072012-03-06 16:23:50 +0100147 goto found_magic;
148 }
149 if (ENABLE_FEATURE_SEAMLESS_XZ
150 && magic.b16[0] == XZ_MAGIC1
151 ) {
152 offset = -6;
153 xread(fd, magic.b32, sizeof(magic.b32[0]));
154 if (magic.b32[0] == XZ_MAGIC2) {
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100155 USE_FOR_MMU(xformer = unpack_xz_stream;)
156 USE_FOR_NOMMU(xformer_prog = "unxz";)
Denys Vlasenko59655072012-03-06 16:23:50 +0100157 goto found_magic;
158 }
159 }
160
161 /* No known magic seen */
162 if (fail_if_not_detected)
163 bb_error_msg_and_die("no gzip"
164 IF_FEATURE_SEAMLESS_BZ2("/bzip2")
165 IF_FEATURE_SEAMLESS_XZ("/xz")
166 " magic");
167 xlseek(fd, offset, SEEK_CUR);
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100168 return 1;
Denys Vlasenko59655072012-03-06 16:23:50 +0100169
170 found_magic:
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100171# if BB_MMU
172 open_transformer_with_no_sig(fd, xformer);
173# else
Denys Vlasenko59655072012-03-06 16:23:50 +0100174 /* NOMMU version of open_transformer execs
175 * an external unzipper that wants
176 * file position at the start of the file */
177 xlseek(fd, offset, SEEK_CUR);
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100178 open_transformer_with_sig(fd, xformer, xformer_prog);
Denys Vlasenko59655072012-03-06 16:23:50 +0100179# endif
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100180 return 0;
Denys Vlasenko59655072012-03-06 16:23:50 +0100181}
Denys Vlasenko59655072012-03-06 16:23:50 +0100182
183int FAST_FUNC open_zipped(const char *fname)
184{
Denys Vlasenko59655072012-03-06 16:23:50 +0100185 int fd;
186
187 fd = open(fname, O_RDONLY);
188 if (fd < 0)
189 return fd;
190
Denys Vlasenko7c47b562014-01-10 14:06:57 +0100191 if (ENABLE_FEATURE_SEAMLESS_LZMA) {
192 /* .lzma has no header/signature, can only detect it by extension */
193 char *sfx = strrchr(fname, '.');
194 if (sfx && strcmp(sfx+1, "lzma") == 0) {
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100195 open_transformer_with_sig(fd, unpack_lzma_stream, "unlzma");
Denys Vlasenko7c47b562014-01-10 14:06:57 +0100196 return fd;
Denys Vlasenko59655072012-03-06 16:23:50 +0100197 }
198 }
Denys Vlasenko7c47b562014-01-10 14:06:57 +0100199 if ((ENABLE_FEATURE_SEAMLESS_GZ)
200 || (ENABLE_FEATURE_SEAMLESS_BZ2)
201 || (ENABLE_FEATURE_SEAMLESS_XZ)
202 ) {
203 setup_unzip_on_fd(fd, /*fail_if_not_detected:*/ 1);
204 }
Denys Vlasenko59655072012-03-06 16:23:50 +0100205
206 return fd;
Denys Vlasenko59655072012-03-06 16:23:50 +0100207}
208
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100209#endif /* SEAMLESS_COMPRESSION */
210
Denys Vlasenko59655072012-03-06 16:23:50 +0100211void* 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}