read_printf.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_safe_read(int fd, void *buf, size_t count)
  55. {
  56. struct pollfd pfd[1];
  57. ssize_t n;
  58. while (1) {
  59. n = safe_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. safe_poll(pfd, 1, -1); /* note: this pulls in printf */
  66. }
  67. }
  68. // Reads one line a-la fgets (but doesn't save terminating '\n').
  69. // Reads byte-by-byte. Useful when it is important to not read ahead.
  70. // Bytes are appended to pfx (which must be malloced, or NULL).
  71. char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
  72. {
  73. char *p;
  74. size_t sz = buf ? strlen(buf) : 0;
  75. size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
  76. goto jump_in;
  77. while (sz < maxsz) {
  78. if ((size_t)(p - buf) == sz) {
  79. jump_in:
  80. buf = xrealloc(buf, sz + 128);
  81. p = buf + sz;
  82. sz += 128;
  83. }
  84. /* nonblock_safe_read() because we are used by e.g. shells */
  85. if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
  86. if (p == buf) { /* we read nothing */
  87. free(buf);
  88. return NULL;
  89. }
  90. break;
  91. }
  92. if (*p == '\n')
  93. break;
  94. p++;
  95. }
  96. *p = '\0';
  97. if (maxsz_p)
  98. *maxsz_p = p - buf;
  99. p++;
  100. return xrealloc(buf, p - buf);
  101. }
  102. // Read (potentially big) files in one go. File size is estimated
  103. // by stat. Extra '\0' byte is appended.
  104. void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
  105. {
  106. char *buf;
  107. size_t size, rd_size, total;
  108. size_t to_read;
  109. struct stat st;
  110. to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */
  111. /* Estimate file size */
  112. st.st_size = 0; /* in case fstat fails, assume 0 */
  113. fstat(fd, &st);
  114. /* /proc/N/stat files report st_size 0 */
  115. /* In order to make such files readable, we add small const */
  116. size = (st.st_size | 0x3ff) + 1;
  117. total = 0;
  118. buf = NULL;
  119. while (1) {
  120. if (to_read < size)
  121. size = to_read;
  122. buf = xrealloc(buf, total + size + 1);
  123. rd_size = full_read(fd, buf + total, size);
  124. if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
  125. free(buf);
  126. return NULL;
  127. }
  128. total += rd_size;
  129. if (rd_size < size) /* EOF */
  130. break;
  131. if (to_read <= rd_size)
  132. break;
  133. to_read -= rd_size;
  134. /* grow by 1/8, but in [1k..64k] bounds */
  135. size = ((total / 8) | 0x3ff) + 1;
  136. if (size > 64*1024)
  137. size = 64*1024;
  138. }
  139. buf = xrealloc(buf, total + 1);
  140. buf[total] = '\0';
  141. if (maxsz_p)
  142. *maxsz_p = total;
  143. return buf;
  144. }
  145. #ifdef USING_LSEEK_TO_GET_SIZE
  146. /* Alternatively, file size can be obtained by lseek to the end.
  147. * The code is slightly bigger. Retained in case fstat approach
  148. * will not work for some weird cases (/proc, block devices, etc).
  149. * (NB: lseek also can fail to work for some weird files) */
  150. // Read (potentially big) files in one go. File size is estimated by
  151. // lseek to end.
  152. void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
  153. {
  154. char *buf;
  155. size_t size;
  156. int fd;
  157. off_t len;
  158. fd = open(filename, O_RDONLY);
  159. if (fd < 0)
  160. return NULL;
  161. /* /proc/N/stat files report len 0 here */
  162. /* In order to make such files readable, we add small const */
  163. size = 0x3ff; /* read only 1k on unseekable files */
  164. len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
  165. if (len != (off_t)-1) {
  166. xlseek(fd, 0, SEEK_SET);
  167. size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
  168. if (len < size)
  169. size = len;
  170. }
  171. buf = xmalloc(size + 1);
  172. size = read_close(fd, buf, size);
  173. if ((ssize_t)size < 0) {
  174. free(buf);
  175. return NULL;
  176. }
  177. buf = xrealloc(buf, size + 1);
  178. buf[size] = '\0';
  179. if (maxsz_p)
  180. *maxsz_p = size;
  181. return buf;
  182. }
  183. #endif
  184. // Read (potentially big) files in one go. File size is estimated
  185. // by stat.
  186. void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
  187. {
  188. char *buf;
  189. int fd;
  190. fd = open(filename, O_RDONLY);
  191. if (fd < 0)
  192. return NULL;
  193. buf = xmalloc_read(fd, maxsz_p);
  194. close(fd);
  195. return buf;
  196. }
  197. /* Die with an error message if we can't read the entire buffer. */
  198. void FAST_FUNC xread(int fd, void *buf, size_t count)
  199. {
  200. if (count) {
  201. ssize_t size = full_read(fd, buf, count);
  202. if ((size_t)size != count)
  203. bb_error_msg_and_die("short read");
  204. }
  205. }
  206. /* Die with an error message if we can't read one character. */
  207. unsigned char FAST_FUNC xread_char(int fd)
  208. {
  209. char tmp;
  210. xread(fd, &tmp, 1);
  211. return tmp;
  212. }
  213. void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
  214. {
  215. void *buf = xmalloc_open_read_close(filename, maxsz_p);
  216. if (!buf)
  217. bb_perror_msg_and_die("can't read '%s'", filename);
  218. return buf;
  219. }
  220. /* Used by e.g. rpm which gives us a fd without filename,
  221. * thus we can't guess the format from filename's extension.
  222. */
  223. #if ZIPPED
  224. void FAST_FUNC setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/)
  225. {
  226. const int fail_if_not_detected = 1;
  227. union {
  228. uint8_t b[4];
  229. uint16_t b16[2];
  230. uint32_t b32[1];
  231. } magic;
  232. int offset = -2;
  233. # if BB_MMU
  234. IF_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd);
  235. enum { xformer_prog = 0 };
  236. # else
  237. enum { xformer = 0 };
  238. const char *xformer_prog;
  239. # endif
  240. /* .gz and .bz2 both have 2-byte signature, and their
  241. * unpack_XXX_stream wants this header skipped. */
  242. xread(fd, magic.b16, sizeof(magic.b16[0]));
  243. if (ENABLE_FEATURE_SEAMLESS_GZ
  244. && magic.b16[0] == GZIP_MAGIC
  245. ) {
  246. # if BB_MMU
  247. xformer = unpack_gz_stream;
  248. # else
  249. xformer_prog = "gunzip";
  250. # endif
  251. goto found_magic;
  252. }
  253. if (ENABLE_FEATURE_SEAMLESS_BZ2
  254. && magic.b16[0] == BZIP2_MAGIC
  255. ) {
  256. # if BB_MMU
  257. xformer = unpack_bz2_stream;
  258. # else
  259. xformer_prog = "bunzip2";
  260. # endif
  261. goto found_magic;
  262. }
  263. if (ENABLE_FEATURE_SEAMLESS_XZ
  264. && magic.b16[0] == XZ_MAGIC1
  265. ) {
  266. offset = -6;
  267. xread(fd, magic.b32, sizeof(magic.b32[0]));
  268. if (magic.b32[0] == XZ_MAGIC2) {
  269. # if BB_MMU
  270. xformer = unpack_xz_stream;
  271. /* unpack_xz_stream wants fd at position 6, no need to seek */
  272. //xlseek(fd, offset, SEEK_CUR);
  273. # else
  274. xformer_prog = "unxz";
  275. # endif
  276. goto found_magic;
  277. }
  278. }
  279. /* No known magic seen */
  280. if (fail_if_not_detected)
  281. bb_error_msg_and_die("no gzip"
  282. IF_FEATURE_SEAMLESS_BZ2("/bzip2")
  283. IF_FEATURE_SEAMLESS_XZ("/xz")
  284. " magic");
  285. xlseek(fd, offset, SEEK_CUR);
  286. return;
  287. found_magic:
  288. # if !BB_MMU
  289. /* NOMMU version of open_transformer execs
  290. * an external unzipper that wants
  291. * file position at the start of the file */
  292. xlseek(fd, offset, SEEK_CUR);
  293. # endif
  294. open_transformer(fd, xformer, xformer_prog);
  295. }
  296. #endif /* ZIPPED */
  297. int FAST_FUNC open_zipped(const char *fname)
  298. {
  299. #if !ZIPPED
  300. return open(fname, O_RDONLY);
  301. #else
  302. char *sfx;
  303. int fd;
  304. fd = open(fname, O_RDONLY);
  305. if (fd < 0)
  306. return fd;
  307. sfx = strrchr(fname, '.');
  308. if (sfx) {
  309. sfx++;
  310. if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, "lzma") == 0)
  311. /* .lzma has no header/signature, just trust it */
  312. open_transformer(fd, unpack_lzma_stream, "unlzma");
  313. else
  314. if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, "gz") == 0)
  315. || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, "bz2") == 0)
  316. || (ENABLE_FEATURE_SEAMLESS_XZ && strcmp(sfx, "xz") == 0)
  317. ) {
  318. setup_unzip_on_fd(fd /*, fail_if_not_detected: 1*/);
  319. }
  320. }
  321. return fd;
  322. #endif
  323. }
  324. void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p)
  325. {
  326. int fd;
  327. char *image;
  328. fd = open_zipped(fname);
  329. if (fd < 0)
  330. return NULL;
  331. image = xmalloc_read(fd, maxsz_p);
  332. if (!image)
  333. bb_perror_msg("read error from '%s'", fname);
  334. close(fd);
  335. return image;
  336. }