read_printf.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \
  11. || ENABLE_FEATURE_SEAMLESS_BZ2 \
  12. || ENABLE_FEATURE_SEAMLESS_GZ \
  13. /* || ENABLE_FEATURE_SEAMLESS_Z */ \
  14. )
  15. #if ZIPPED
  16. # include "archive.h"
  17. #endif
  18. /* Suppose that you are a shell. You start child processes.
  19. * They work and eventually exit. You want to get user input.
  20. * You read stdin. But what happens if last child switched
  21. * its stdin into O_NONBLOCK mode?
  22. *
  23. * *** SURPRISE! It will affect the parent too! ***
  24. * *** BIG SURPRISE! It stays even after child exits! ***
  25. *
  26. * This is a design bug in UNIX API.
  27. * fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
  28. * will set nonblocking mode not only on _your_ stdin, but
  29. * also on stdin of your parent, etc.
  30. *
  31. * In general,
  32. * fd2 = dup(fd1);
  33. * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK);
  34. * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
  35. * where duping is done implicitly by fork() etc.
  36. *
  37. * We need
  38. * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK);
  39. * (note SETFD, not SETFL!) but such thing doesn't exist.
  40. *
  41. * Alternatively, we need nonblocking_read(fd, ...) which doesn't
  42. * require O_NONBLOCK dance at all. Actually, it exists:
  43. * n = recv(fd, buf, len, MSG_DONTWAIT);
  44. * "MSG_DONTWAIT:
  45. * Enables non-blocking operation; if the operation
  46. * would block, EAGAIN is returned."
  47. * but recv() works only for sockets!
  48. *
  49. * So far I don't see any good solution, I can only propose
  50. * that affected readers should be careful and use this routine,
  51. * which detects EAGAIN and uses poll() to wait on the fd.
  52. * Thankfully, poll() doesn't care about O_NONBLOCK flag.
  53. */
  54. ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count, int loop_on_EINTR)
  55. {
  56. struct pollfd pfd[1];
  57. ssize_t n;
  58. while (1) {
  59. n = loop_on_EINTR ? safe_read(fd, buf, count) : read(fd, buf, count);
  60. if (n >= 0 || errno != EAGAIN)
  61. return n;
  62. /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
  63. pfd[0].fd = fd;
  64. pfd[0].events = POLLIN;
  65. /* note: safe_poll pulls in printf */
  66. loop_on_EINTR ? safe_poll(pfd, 1, -1) : poll(pfd, 1, -1);
  67. }
  68. }
  69. // Reads one line a-la fgets (but doesn't save terminating '\n').
  70. // Reads byte-by-byte. Useful when it is important to not read ahead.
  71. // Bytes are appended to pfx (which must be malloced, or NULL).
  72. char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p)
  73. {
  74. char *p;
  75. char *buf = NULL;
  76. size_t sz = 0;
  77. size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
  78. goto jump_in;
  79. while (sz < maxsz) {
  80. if ((size_t)(p - buf) == sz) {
  81. jump_in:
  82. buf = xrealloc(buf, sz + 128);
  83. p = buf + sz;
  84. sz += 128;
  85. }
  86. if (nonblock_immune_read(fd, p, 1, /*loop_on_EINTR:*/ 1) != 1) {
  87. /* EOF/error */
  88. if (p == buf) { /* we read nothing */
  89. free(buf);
  90. return NULL;
  91. }
  92. break;
  93. }
  94. if (*p == '\n')
  95. break;
  96. p++;
  97. }
  98. *p = '\0';
  99. if (maxsz_p)
  100. *maxsz_p = p - buf;
  101. p++;
  102. return xrealloc(buf, p - buf);
  103. }
  104. // Read (potentially big) files in one go. File size is estimated
  105. // by stat. Extra '\0' byte is appended.
  106. void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
  107. {
  108. char *buf;
  109. size_t size, rd_size, total;
  110. size_t to_read;
  111. struct stat st;
  112. to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
  113. /* Estimate file size */
  114. st.st_size = 0; /* in case fstat fails, assume 0 */
  115. fstat(fd, &st);
  116. /* /proc/N/stat files report st_size 0 */
  117. /* In order to make such files readable, we add small const */
  118. size = (st.st_size | 0x3ff) + 1;
  119. total = 0;
  120. buf = NULL;
  121. while (1) {
  122. if (to_read < size)
  123. size = to_read;
  124. buf = xrealloc(buf, total + size + 1);
  125. rd_size = full_read(fd, buf + total, size);
  126. if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
  127. free(buf);
  128. return NULL;
  129. }
  130. total += rd_size;
  131. if (rd_size < size) /* EOF */
  132. break;
  133. if (to_read <= rd_size)
  134. break;
  135. to_read -= rd_size;
  136. /* grow by 1/8, but in [1k..64k] bounds */
  137. size = ((total / 8) | 0x3ff) + 1;
  138. if (size > 64*1024)
  139. size = 64*1024;
  140. }
  141. buf = xrealloc(buf, total + 1);
  142. buf[total] = '\0';
  143. if (maxsz_p)
  144. *maxsz_p = total;
  145. return buf;
  146. }
  147. #ifdef USING_LSEEK_TO_GET_SIZE
  148. /* Alternatively, file size can be obtained by lseek to the end.
  149. * The code is slightly bigger. Retained in case fstat approach
  150. * will not work for some weird cases (/proc, block devices, etc).
  151. * (NB: lseek also can fail to work for some weird files) */
  152. // Read (potentially big) files in one go. File size is estimated by
  153. // lseek to end.
  154. void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
  155. {
  156. char *buf;
  157. size_t size;
  158. int fd;
  159. off_t len;
  160. fd = open(filename, O_RDONLY);
  161. if (fd < 0)
  162. return NULL;
  163. /* /proc/N/stat files report len 0 here */
  164. /* In order to make such files readable, we add small const */
  165. size = 0x3ff; /* read only 1k on unseekable files */
  166. len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
  167. if (len != (off_t)-1) {
  168. xlseek(fd, 0, SEEK_SET);
  169. size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
  170. if (len < size)
  171. size = len;
  172. }
  173. buf = xmalloc(size + 1);
  174. size = read_close(fd, buf, size);
  175. if ((ssize_t)size < 0) {
  176. free(buf);
  177. return NULL;
  178. }
  179. buf = xrealloc(buf, size + 1);
  180. buf[size] = '\0';
  181. if (maxsz_p)
  182. *maxsz_p = size;
  183. return buf;
  184. }
  185. #endif
  186. // Read (potentially big) files in one go. File size is estimated
  187. // by stat.
  188. void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
  189. {
  190. char *buf;
  191. int fd;
  192. fd = open(filename, O_RDONLY);
  193. if (fd < 0)
  194. return NULL;
  195. buf = xmalloc_read(fd, maxsz_p);
  196. close(fd);
  197. return buf;
  198. }
  199. /* Die with an error message if we can't read the entire buffer. */
  200. void FAST_FUNC xread(int fd, void *buf, size_t count)
  201. {
  202. if (count) {
  203. ssize_t size = full_read(fd, buf, count);
  204. if ((size_t)size != count)
  205. bb_error_msg_and_die("short read");
  206. }
  207. }
  208. /* Die with an error message if we can't read one character. */
  209. unsigned char FAST_FUNC xread_char(int fd)
  210. {
  211. char tmp;
  212. xread(fd, &tmp, 1);
  213. return tmp;
  214. }
  215. void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
  216. {
  217. void *buf = xmalloc_open_read_close(filename, maxsz_p);
  218. if (!buf)
  219. bb_perror_msg_and_die("can't read '%s'", filename);
  220. return buf;
  221. }
  222. /* Used by e.g. rpm which gives us a fd without filename,
  223. * thus we can't guess the format from filename's extension.
  224. */
  225. #if ZIPPED
  226. void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
  227. {
  228. const int fail_if_not_detected = 1;
  229. union {
  230. uint8_t b[4];
  231. uint16_t b16[2];
  232. uint32_t b32[1];
  233. } magic;
  234. int offset = -2;
  235. # if BB_MMU
  236. IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
  237. enum { xformer_prog = 0 };
  238. # else
  239. enum { xformer = 0 };
  240. const char *xformer_prog;
  241. # endif
  242. /* .gz and .bz2 both have 2-byte signature, and their
  243. * unpack_XXX_stream wants this header skipped. */
  244. xread(fd, magic.b16, sizeof(magic.b16[0]));
  245. if (ENABLE_FEATURE_SEAMLESS_GZ
  246. && magic.b16[0] == GZIP_MAGIC
  247. ) {
  248. # if BB_MMU
  249. xformer = unpack_gz_stream;
  250. # else
  251. xformer_prog = "gunzip";
  252. # endif
  253. goto found_magic;
  254. }
  255. if (ENABLE_FEATURE_SEAMLESS_BZ2
  256. && magic.b16[0] == BZIP2_MAGIC
  257. ) {
  258. # if BB_MMU
  259. xformer = unpack_bz2_stream;
  260. # else
  261. xformer_prog = "bunzip2";
  262. # endif
  263. goto found_magic;
  264. }
  265. if (ENABLE_FEATURE_SEAMLESS_XZ
  266. && magic.b16[0] == XZ_MAGIC1
  267. ) {
  268. offset = -6;
  269. xread(fd, magic.b32, sizeof(magic.b32[0]));
  270. if (magic.b32[0] == XZ_MAGIC2) {
  271. # if BB_MMU
  272. xformer = unpack_xz_stream;
  273. /* unpack_xz_stream wants fd at position 6, no need to seek */
  274. //xlseek(fd, offset, SEEK_CUR);
  275. # else
  276. xformer_prog = "unxz";
  277. # endif
  278. goto found_magic;
  279. }
  280. }
  281. /* No known magic seen */
  282. if (fail_if_not_detected)
  283. bb_error_msg_and_die("no gzip"
  284. IF_FEATURE_SEAMLESS_BZ2("/bzip2")
  285. IF_FEATURE_SEAMLESS_XZ("/xz")
  286. " magic");
  287. xlseek(fd, offset, SEEK_CUR);
  288. return;
  289. found_magic:
  290. # if !BB_MMU
  291. /* NOMMU version of open_transformer execs
  292. * an external unzipper that wants
  293. * file position at the start of the file */
  294. xlseek(fd, offset, SEEK_CUR);
  295. # endif
  296. open_transformer(fd, xformer, xformer_prog);
  297. }
  298. #endif /* ZIPPED */
  299. int FAST_FUNC open_zipped(const char *fname)
  300. {
  301. #if !ZIPPED
  302. return open(fname, O_RDONLY);
  303. #else
  304. char *sfx;
  305. int fd;
  306. fd = open(fname, O_RDONLY);
  307. if (fd < 0)
  308. return fd;
  309. sfx = strrchr(fname, '.');
  310. if (sfx) {
  311. sfx++;
  312. if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0)
  313. /* .lzma has no header/signature, just trust it */
  314. open_transformer(fd, unpack_lzma_stream, "unlzma");
  315. else
  316. if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0)
  317. || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0)
  318. || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0)
  319. ) {
  320. setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/);
  321. }
  322. }
  323. return fd;
  324. #endif
  325. }
  326. void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
  327. {
  328. int fd;
  329. char *image;
  330. fd = open_zipped(fname);
  331. if (fd < 0)
  332. return NULL;
  333. image = xmalloc_read(fd, maxsz_p);
  334. if (!image)
  335. bb_perror_msg("read error from '%s'", fname);
  336. close(fd);
  337. return image;
  338. }