read.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 tarball for details.
  8. */
  9. #include "libbb.h"
  10. ssize_t safe_read(int fd, void *buf, size_t count)
  11. {
  12. ssize_t n;
  13. do {
  14. n = read(fd, buf, count);
  15. } while (n < 0 && errno == EINTR);
  16. return n;
  17. }
  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, 0) | 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, 0) | 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, 0) | 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 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);
  66. }
  67. }
  68. /*
  69. * Read all of the supplied buffer from a file.
  70. * This does multiple reads as necessary.
  71. * Returns the amount read, or -1 on an error.
  72. * A short read is returned on an end of file.
  73. */
  74. ssize_t full_read(int fd, void *buf, size_t len)
  75. {
  76. ssize_t cc;
  77. ssize_t total;
  78. total = 0;
  79. while (len) {
  80. cc = safe_read(fd, buf, len);
  81. if (cc < 0) {
  82. if (total) {
  83. /* we already have some! */
  84. /* user can do another read to know the error code */
  85. return total;
  86. }
  87. return cc; /* read() returns -1 on failure. */
  88. }
  89. if (cc == 0)
  90. break;
  91. buf = ((char *)buf) + cc;
  92. total += cc;
  93. len -= cc;
  94. }
  95. return total;
  96. }
  97. // Die with an error message if we can't read the entire buffer.
  98. void xread(int fd, void *buf, size_t count)
  99. {
  100. if (count) {
  101. ssize_t size = full_read(fd, buf, count);
  102. if ((size_t)size != count)
  103. bb_error_msg_and_die("short read");
  104. }
  105. }
  106. // Die with an error message if we can't read one character.
  107. unsigned char xread_char(int fd)
  108. {
  109. char tmp;
  110. xread(fd, &tmp, 1);
  111. return tmp;
  112. }
  113. // Read one line a-la fgets. Works only on seekable streams
  114. char *reads(int fd, char *buffer, size_t size)
  115. {
  116. char *p;
  117. if (size < 2)
  118. return NULL;
  119. size = full_read(fd, buffer, size-1);
  120. if ((ssize_t)size <= 0)
  121. return NULL;
  122. buffer[size] = '\0';
  123. p = strchr(buffer, '\n');
  124. if (p) {
  125. off_t offset;
  126. *p++ = '\0';
  127. // avoid incorrect (unsigned) widening
  128. offset = (off_t)(p - buffer) - (off_t)size;
  129. // set fd position right after '\n'
  130. if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
  131. return NULL;
  132. }
  133. return buffer;
  134. }
  135. // Reads one line a-la fgets (but doesn't save terminating '\n').
  136. // Reads byte-by-byte. Useful when it is important to not read ahead.
  137. // Bytes are appended to pfx (which must be malloced, or NULL).
  138. char *xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
  139. {
  140. char *p;
  141. size_t sz = buf ? strlen(buf) : 0;
  142. size_t maxsz = maxsz_p ? *maxsz_p : MAXINT(size_t);
  143. goto jump_in;
  144. while (sz < maxsz) {
  145. if ((size_t)(p - buf) == sz) {
  146. jump_in:
  147. buf = xrealloc(buf, sz + 128);
  148. p = buf + sz;
  149. sz += 128;
  150. }
  151. /* nonblock_safe_read() because we are used by e.g. shells */
  152. if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
  153. if (p == buf) { /* we read nothing */
  154. free(buf);
  155. return NULL;
  156. }
  157. break;
  158. }
  159. if (*p == '\n')
  160. break;
  161. p++;
  162. }
  163. *p = '\0';
  164. if (maxsz_p)
  165. *maxsz_p = p - buf;
  166. p++;
  167. return xrealloc(buf, p - buf);
  168. }
  169. ssize_t read_close(int fd, void *buf, size_t size)
  170. {
  171. /*int e;*/
  172. size = full_read(fd, buf, size);
  173. /*e = errno;*/
  174. close(fd);
  175. /*errno = e;*/
  176. return size;
  177. }
  178. ssize_t open_read_close(const char *filename, void *buf, size_t size)
  179. {
  180. int fd = open(filename, O_RDONLY);
  181. if (fd < 0)
  182. return fd;
  183. return read_close(fd, buf, size);
  184. }
  185. // Read (potentially big) files in one go. File size is estimated
  186. // by stat.
  187. void *xmalloc_open_read_close(const char *filename, size_t *sizep)
  188. {
  189. char *buf;
  190. size_t size;
  191. int fd;
  192. off_t len;
  193. struct stat st;
  194. fd = open(filename, O_RDONLY);
  195. if (fd < 0)
  196. return NULL;
  197. st.st_size = 0; /* in case fstat fail, define to 0 */
  198. fstat(fd, &st);
  199. /* /proc/N/stat files report len 0 here */
  200. /* In order to make such files readable, we add small const */
  201. len = st.st_size | 0x3ff; /* read only 1k on unseekable files */
  202. size = sizep ? *sizep : INT_MAX;
  203. if (len < size)
  204. size = len;
  205. buf = xmalloc(size + 1);
  206. size = read_close(fd, buf, size);
  207. if ((ssize_t)size < 0) {
  208. free(buf);
  209. return NULL;
  210. }
  211. xrealloc(buf, size + 1);
  212. buf[size] = '\0';
  213. if (sizep)
  214. *sizep = size;
  215. return buf;
  216. }
  217. #ifdef USING_LSEEK_TO_GET_SIZE
  218. /* Alternatively, file size can be obtained by lseek to the end.
  219. * The code is slightly bigger. Retained in case fstat approach
  220. * will not work for some weird cases (/proc, block devices, etc).
  221. * (NB: lseek also can fail to work for some weird files) */
  222. // Read (potentially big) files in one go. File size is estimated by
  223. // lseek to end.
  224. void *xmalloc_open_read_close(const char *filename, size_t *sizep)
  225. {
  226. char *buf;
  227. size_t size;
  228. int fd;
  229. off_t len;
  230. fd = open(filename, O_RDONLY);
  231. if (fd < 0)
  232. return NULL;
  233. /* /proc/N/stat files report len 0 here */
  234. /* In order to make such files readable, we add small const */
  235. size = 0x3ff; /* read only 1k on unseekable files */
  236. len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
  237. if (len != (off_t)-1) {
  238. xlseek(fd, 0, SEEK_SET);
  239. size = sizep ? *sizep : INT_MAX;
  240. if (len < size)
  241. size = len;
  242. }
  243. buf = xmalloc(size + 1);
  244. size = read_close(fd, buf, size);
  245. if ((ssize_t)size < 0) {
  246. free(buf);
  247. return NULL;
  248. }
  249. xrealloc(buf, size + 1);
  250. buf[size] = '\0';
  251. if (sizep)
  252. *sizep = size;
  253. return buf;
  254. }
  255. #endif
  256. void *xmalloc_xopen_read_close(const char *filename, size_t *sizep)
  257. {
  258. void *buf = xmalloc_open_read_close(filename, sizep);
  259. if (!buf)
  260. bb_perror_msg_and_die("can't read '%s'", filename);
  261. return buf;
  262. }