read.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 != 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. // Read one line a-la fgets. Reads byte-by-byte.
  136. // 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)
  139. {
  140. char *p;
  141. int sz = buf ? strlen(buf) : 0;
  142. goto jump_in;
  143. while (1) {
  144. if (p - buf == sz) {
  145. jump_in:
  146. buf = xrealloc(buf, sz + 128);
  147. p = buf + sz;
  148. sz += 128;
  149. }
  150. /* nonblock_safe_read() because we are used by e.g. shells */
  151. if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
  152. if (p == buf) { /* we read nothing */
  153. free(buf);
  154. return NULL;
  155. }
  156. break;
  157. }
  158. if (*p == '\n')
  159. break;
  160. p++;
  161. }
  162. *p++ = '\0';
  163. return xrealloc(buf, p - buf);
  164. }
  165. ssize_t read_close(int fd, void *buf, size_t size)
  166. {
  167. /*int e;*/
  168. size = full_read(fd, buf, size);
  169. /*e = errno;*/
  170. close(fd);
  171. /*errno = e;*/
  172. return size;
  173. }
  174. ssize_t open_read_close(const char *filename, void *buf, size_t size)
  175. {
  176. int fd = open(filename, O_RDONLY);
  177. if (fd < 0)
  178. return fd;
  179. return read_close(fd, buf, size);
  180. }
  181. // Read (potentially big) files in one go. File size is estimated by
  182. // lseek to end.
  183. void *xmalloc_open_read_close(const char *filename, size_t *sizep)
  184. {
  185. char *buf;
  186. size_t size = sizep ? *sizep : INT_MAX;
  187. int fd;
  188. off_t len;
  189. fd = xopen(filename, O_RDONLY);
  190. /* /proc/N/stat files report len 0 here */
  191. /* In order to make such files readable, we add small const */
  192. len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
  193. xlseek(fd, 0, SEEK_SET);
  194. if (len < size)
  195. size = len;
  196. buf = xmalloc(size + 1);
  197. size = read_close(fd, buf, size);
  198. if ((ssize_t)size < 0)
  199. bb_perror_msg_and_die("'%s'", filename);
  200. xrealloc(buf, size + 1);
  201. buf[size] = '\0';
  202. if (sizep)
  203. *sizep = size;
  204. return buf;
  205. }