[Bug 3547] shell read is maybe too safe

Ian Wienand ianw at vmware.com
Thu Apr 7 20:50:43 UTC 2011


Hi,

The following doesn't exit when SIGHUP is sent to busybox ash/hush,
but waits for something to be read.

---
#!/bin/sh
echo $$
trap "exit 0" HUP
while [ 1 ]
do
	read ignored
done
---

bash and dash on my system does exit straight away

safe_read appears to ignore EINTR, which I guess it too safe in this
situation

-i

Signed-off-by: Ian Wienand <ianw at vmware.com>
---
 libbb/read_printf.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/libbb/read_printf.c b/libbb/read_printf.c
index 8664bc6..23943db 100644
--- a/libbb/read_printf.c
+++ b/libbb/read_printf.c
@@ -61,9 +61,16 @@ ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count)
 	ssize_t n;
 
 	while (1) {
-		n = safe_read(fd, buf, count);
-		if (n >= 0 || errno != EAGAIN)
+                /*
+                 * note safe_read is a little too safe here; if a
+                 * signal comes in while we are reading (causing read
+                 * to return EINTR) we should terminate so any
+                 * potential trap handler can run.
+                 */
+		n = 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;
-- 
1.7.4.1



More information about the busybox mailing list