1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 /* Suppose that you are a shell. You start child processes.
13 * They work and eventually exit. You want to get user input.
14 * You read stdin. But what happens if last child switched
15 * its stdin into O_NONBLOCK mode?
17 * *** SURPRISE! It will affect the parent too! ***
18 * *** BIG SURPRISE! It stays even after child exits! ***
20 * This is a design bug in UNIX API.
21 * fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
22 * will set nonblocking mode not only on _your_ stdin, but
23 * also on stdin of your parent, etc.
27 * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL) | O_NONBLOCK);
28 * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
29 * where duping is done implicitly by fork() etc.
32 * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD) | O_NONBLOCK);
33 * (note SETFD, not SETFL!) but such thing doesn't exist.
35 * Alternatively, we need nonblocking_read(fd, ...) which doesn't
36 * require O_NONBLOCK dance at all. Actually, it exists:
37 * n = recv(fd, buf, len, MSG_DONTWAIT);
39 * Enables non-blocking operation; if the operation
40 * would block, EAGAIN is returned."
41 * but recv() works only for sockets!
43 * So far I don't see any good solution, I can only propose
44 * that affected readers should be careful and use this routine,
45 * which detects EAGAIN and uses poll() to wait on the fd.
46 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
48 ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count)
54 n = safe_read(fd, buf, count);
55 if (n >= 0 || errno != EAGAIN)
57 /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
59 pfd[0].events = POLLIN;
60 /* note: safe_poll pulls in printf */
61 safe_poll(pfd, 1, -1);
65 // Reads one line a-la fgets (but doesn't save terminating '\n').
66 // Reads byte-by-byte. Useful when it is important to not read ahead.
67 // Bytes are appended to pfx (which must be malloced, or NULL).
68 char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p)
73 size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
78 if ((size_t)(p - buf) == sz) {
80 buf = xrealloc(buf, sz + 128);
84 if (nonblock_immune_read(fd, p, 1) != 1) {
86 if (p == buf) { /* we read nothing */
100 return xrealloc(buf, p - buf);
103 // Read (potentially big) files in one go. File size is estimated
104 // by stat. Extra '\0' byte is appended.
105 void* FAST_FUNC xmalloc_read_with_initial_buf(int fd, size_t *maxsz_p, char *buf, size_t total)
107 size_t size, rd_size;
111 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 */
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;
123 buf = xrealloc(buf, total + size + 1);
124 rd_size = full_read(fd, buf + total, size);
125 if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */
130 if (rd_size < size) /* EOF */
132 if (to_read <= rd_size)
135 /* grow by 1/8, but in [1k..64k] bounds */
136 size = ((total / 8) | 0x3ff) + 1;
140 buf = xrealloc(buf, total + 1);
148 void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p)
150 return xmalloc_read_with_initial_buf(fd, maxsz_p, NULL, 0);
153 #ifdef USING_LSEEK_TO_GET_SIZE
154 /* Alternatively, file size can be obtained by lseek to the end.
155 * The code is slightly bigger. Retained in case fstat approach
156 * will not work for some weird cases (/proc, block devices, etc).
157 * (NB: lseek also can fail to work for some weird files) */
159 // Read (potentially big) files in one go. File size is estimated by
161 void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
168 fd = open(filename, O_RDONLY);
172 /* /proc/N/stat files report len 0 here */
173 /* In order to make such files readable, we add small const */
174 size = 0x3ff; /* read only 1k on unseekable files */
175 len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
176 if (len != (off_t)-1) {
177 xlseek(fd, 0, SEEK_SET);
178 size = maxsz_p ? *maxsz_p : (INT_MAX - 4095);
183 buf = xmalloc(size + 1);
184 size = read_close(fd, buf, size);
185 if ((ssize_t)size < 0) {
189 buf = xrealloc(buf, size + 1);
198 // Read (potentially big) files in one go. File size is estimated
200 void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p)
205 fd = open(filename, O_RDONLY);
209 buf = xmalloc_read(fd, maxsz_p);
214 /* Die with an error message if we can't read the entire buffer. */
215 void FAST_FUNC xread(int fd, void *buf, size_t count)
218 ssize_t size = full_read(fd, buf, count);
219 if ((size_t)size != count)
220 bb_simple_error_msg_and_die("short read");
224 /* Die with an error message if we can't read one character. */
225 unsigned char FAST_FUNC xread_char(int fd)
232 void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p)
234 void *buf = xmalloc_open_read_close(filename, maxsz_p);
236 bb_perror_msg_and_die("can't read '%s'", filename);