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
, int loop_on_EINTR
)
54 n
= loop_on_EINTR
? safe_read(fd
, buf
, count
) : 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 loop_on_EINTR
? safe_poll(pfd
, 1, -1) : 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, /*loop_on_EINTR:*/ 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(int fd
, size_t *maxsz_p
)
108 size_t size
, rd_size
, total
;
112 to_read
= maxsz_p
? *maxsz_p
: (INT_MAX
- 4095); /* max to read */
114 /* Estimate file size */
115 st
.st_size
= 0; /* in case fstat fails, assume 0 */
117 /* /proc/N/stat files report st_size 0 */
118 /* In order to make such files readable, we add small const */
119 size
= (st
.st_size
| 0x3ff) + 1;
126 buf
= xrealloc(buf
, total
+ size
+ 1);
127 rd_size
= full_read(fd
, buf
+ total
, size
);
128 if ((ssize_t
)rd_size
== (ssize_t
)(-1)) { /* error */
133 if (rd_size
< size
) /* EOF */
135 if (to_read
<= rd_size
)
138 /* grow by 1/8, but in [1k..64k] bounds */
139 size
= ((total
/ 8) | 0x3ff) + 1;
143 buf
= xrealloc(buf
, total
+ 1);
151 #ifdef USING_LSEEK_TO_GET_SIZE
152 /* Alternatively, file size can be obtained by lseek to the end.
153 * The code is slightly bigger. Retained in case fstat approach
154 * will not work for some weird cases (/proc, block devices, etc).
155 * (NB: lseek also can fail to work for some weird files) */
157 // Read (potentially big) files in one go. File size is estimated by
159 void* FAST_FUNC
xmalloc_open_read_close(const char *filename
, size_t *maxsz_p
)
166 fd
= open(filename
, O_RDONLY
);
170 /* /proc/N/stat files report len 0 here */
171 /* In order to make such files readable, we add small const */
172 size
= 0x3ff; /* read only 1k on unseekable files */
173 len
= lseek(fd
, 0, SEEK_END
) | 0x3ff; /* + up to 1k */
174 if (len
!= (off_t
)-1) {
175 xlseek(fd
, 0, SEEK_SET
);
176 size
= maxsz_p
? *maxsz_p
: (INT_MAX
- 4095);
181 buf
= xmalloc(size
+ 1);
182 size
= read_close(fd
, buf
, size
);
183 if ((ssize_t
)size
< 0) {
187 buf
= xrealloc(buf
, size
+ 1);
196 // Read (potentially big) files in one go. File size is estimated
198 void* FAST_FUNC
xmalloc_open_read_close(const char *filename
, size_t *maxsz_p
)
203 fd
= open(filename
, O_RDONLY
);
207 buf
= xmalloc_read(fd
, maxsz_p
);
212 /* Die with an error message if we can't read the entire buffer. */
213 void FAST_FUNC
xread(int fd
, void *buf
, size_t count
)
216 ssize_t size
= full_read(fd
, buf
, count
);
217 if ((size_t)size
!= count
)
218 bb_error_msg_and_die("short read");
222 /* Die with an error message if we can't read one character. */
223 unsigned char FAST_FUNC
xread_char(int fd
)
230 void* FAST_FUNC
xmalloc_xopen_read_close(const char *filename
, size_t *maxsz_p
)
232 void *buf
= xmalloc_open_read_close(filename
, maxsz_p
);
234 bb_perror_msg_and_die("can't read '%s'", filename
);