hush: make read builtin interruptible.

function                                             old     new   delta
builtin_read                                         185     471    +286
check_and_run_traps                                  200     262     +62
nonblock_immune_read                                  73     119     +46
sigismember                                            -      44     +44
record_signal                                          -      21     +21
sigisemptyset                                          -      16     +16
...
------------------------------------------------------------------------------
(add/remove: 5/0 grow/shrink: 7/5 up/down: 483/-46)           Total: 437 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2011-05-08 21:23:43 +02:00
parent 80c5b6893d
commit 80542bad2f
5 changed files with 120 additions and 12 deletions

View File

@ -55,19 +55,20 @@
* which detects EAGAIN and uses poll() to wait on the fd.
* Thankfully, poll() doesn't care about O_NONBLOCK flag.
*/
ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count)
ssize_t FAST_FUNC nonblock_immune_read(int fd, void *buf, size_t count, int loop_on_EINTR)
{
struct pollfd pfd[1];
ssize_t n;
while (1) {
n = safe_read(fd, buf, count);
n = loop_on_EINTR ? safe_read(fd, buf, count) : read(fd, buf, count);
if (n >= 0 || errno != EAGAIN)
return n;
/* fd is in O_NONBLOCK mode. Wait using poll and repeat */
pfd[0].fd = fd;
pfd[0].events = POLLIN;
safe_poll(pfd, 1, -1); /* note: this pulls in printf */
/* note: safe_poll pulls in printf */
loop_on_EINTR ? safe_poll(pfd, 1, -1) : poll(pfd, 1, -1);
}
}
@ -90,7 +91,7 @@ char* FAST_FUNC xmalloc_reads(int fd, size_t *maxsz_p)
p = buf + sz;
sz += 128;
}
if (nonblock_immune_read(fd, p, 1) != 1) {
if (nonblock_immune_read(fd, p, 1, /*loop_on_EINTR:*/ 1) != 1) {
/* EOF/error */
if (p == buf) { /* we read nothing */
free(buf);