[git commit] ash, hush: fix read builtin versus already-handled signal
Denys Vlasenko
vda.linux at googlemail.com
Sun Oct 11 16:17:43 UTC 2026
commit: https://git.busybox.net/busybox/commit/?id=538499b65ac7bb2dc29d4a4fe18407a237607705
branch: https://git.busybox.net/busybox/log/?h=master
function old new delta
builtin_read 178 205 +27
shell_builtin_read 1340 1350 +10
pgetc 623 633 +10
dotrap 157 164 +7
check_and_run_traps 278 285 +7
readcmd 227 222 -5
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/1 up/down: 61/-5) Total: 56 bytes
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
shell/ash.c | 15 +++++++++------
shell/ash_test/{ash-misc => ash-read}/read.right | 0
shell/ash_test/{ash-misc => ash-read}/read.tests | 0
shell/ash_test/ash-read/read_and_incoming_signal.right | 3 +++
shell/ash_test/ash-read/read_and_incoming_signal.tests | 2 ++
shell/ash_test/ash-read/read_and_past_signal.right | 3 +++
shell/ash_test/ash-read/read_and_past_signal.tests | 2 ++
shell/hush.c | 12 +++++++++---
shell/hush_test/{hush-misc => hush-read}/read.right | 0
shell/hush_test/{hush-misc => hush-read}/read.tests | 0
.../hush_test/hush-read/read_and_incoming_signal.right | 3 +++
.../hush_test/hush-read/read_and_incoming_signal.tests | 2 ++
shell/hush_test/hush-read/read_and_past_signal.right | 3 +++
shell/hush_test/hush-read/read_and_past_signal.tests | 2 ++
shell/shell_common.c | 18 ++++++++++++++++++
15 files changed, 56 insertions(+), 9 deletions(-)
diff --git a/shell/ash.c b/shell/ash.c
index 16d82ab21..46f3778fc 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -3668,7 +3668,7 @@ signal_handler(int signo)
if (!trap[SIGCHLD])
return;
}
-#if ENABLE_FEATURE_EDITING
+#if 1 // ENABLE_FEATURE_EDITING || IF_READ_WHICH_WE_ALWAYS_SUPPORT
//TODO: don't do it if it's SIGCHLD?
bb_got_signal = signo; /* for read_line_input / read builtin: "we got a signal" */
#endif
@@ -9743,8 +9743,7 @@ static int funcline; /* starting line number of current function, or
static int evalstring(char *s, int flags);
/* Called to execute a trap.
- * Single callsite - at the end of evaltree().
- * If we return non-zero, evaltree raises EXEXIT exception.
+ * Called in at the beginning and end of evaltree(), and in readcmd() if signal is trapped.
*
* Perhaps we should avoid entering new trap handlers
* while we are executing a trap handler. [is it a TODO?]
@@ -9765,6 +9764,7 @@ dotrap(void)
status = exitstatus;
savestatus = status;
}
+ bb_got_signal = 0; /* or else read builtin gets immediately "interrupted" by already handled past signal */
pending_sig = 0;
barrier();
@@ -11540,6 +11540,8 @@ preadfd(void)
* #(bash 5.0.17 exits after first "T", looks like a bug)
*/
bb_got_signal = 0;
+ if (pending_sig)
+ goto got_signal;
INTOFF; /* no longjmp'ing out of read_line_input please */
nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ);
if (bb_got_signal == SIGINT)
@@ -11564,6 +11566,7 @@ preadfd(void)
}
if (nr < 0) {
if (errno == EINTR) { /* got signal? */
+ got_signal:
if (bb_got_signal != SIGINT)
write(STDERR_FILENO, "\n", 1);
goto empty_line_input;
@@ -14807,14 +14810,14 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
again:
INTOFF;
r = shell_builtin_read(¶ms);
- INTON;
+ INTON; /* here non-blocked SIGINT will longjmp */
if ((uintptr_t)r == 1 && errno == EINTR) {
/* To get SIGCHLD: sleep 1 & read x; echo $x
* Correct behavior is to not exit "read"
*/
- if (pending_sig == 0)
- goto again;
+ dotrap(); /* NOP if pending_sig == 0 */
+ goto again;
}
if ((uintptr_t)r == 2) /* -t SEC timeout? */
diff --git a/shell/ash_test/ash-misc/read.right b/shell/ash_test/ash-read/read.right
similarity index 100%
rename from shell/ash_test/ash-misc/read.right
rename to shell/ash_test/ash-read/read.right
diff --git a/shell/ash_test/ash-misc/read.tests b/shell/ash_test/ash-read/read.tests
similarity index 100%
rename from shell/ash_test/ash-misc/read.tests
rename to shell/ash_test/ash-read/read.tests
diff --git a/shell/ash_test/ash-read/read_and_incoming_signal.right b/shell/ash_test/ash-read/read_and_incoming_signal.right
new file mode 100644
index 000000000..a6ae3f80f
--- /dev/null
+++ b/shell/ash_test/ash-read/read_and_incoming_signal.right
@@ -0,0 +1,3 @@
+I
+Input:INPUT
+Ok:0
diff --git a/shell/ash_test/ash-read/read_and_incoming_signal.tests b/shell/ash_test/ash-read/read_and_incoming_signal.tests
new file mode 100755
index 000000000..2dfb51fe8
--- /dev/null
+++ b/shell/ash_test/ash-read/read_and_incoming_signal.tests
@@ -0,0 +1,2 @@
+(sleep 2; echo INPUT) | $THIS_SH -c 'trap "echo I" int; (sleep 1;kill -int $$)& read -pP: v;echo Input:$v'
+echo Ok:$?
diff --git a/shell/ash_test/ash-read/read_and_past_signal.right b/shell/ash_test/ash-read/read_and_past_signal.right
new file mode 100644
index 000000000..a6ae3f80f
--- /dev/null
+++ b/shell/ash_test/ash-read/read_and_past_signal.right
@@ -0,0 +1,3 @@
+I
+Input:INPUT
+Ok:0
diff --git a/shell/ash_test/ash-read/read_and_past_signal.tests b/shell/ash_test/ash-read/read_and_past_signal.tests
new file mode 100755
index 000000000..19a41bf81
--- /dev/null
+++ b/shell/ash_test/ash-read/read_and_past_signal.tests
@@ -0,0 +1,2 @@
+echo INPUT | $THIS_SH -c 'trap "echo I" int; (sleep 1;kill -int $$)& sleep 2; read -pP: v;echo Input:$v'
+echo Ok:$?
diff --git a/shell/hush.c b/shell/hush.c
index ac5a01719..afed0b506 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -2085,7 +2085,7 @@ enum {
static void record_pending_signo(int sig)
{
sigaddset(&G.pending_set, sig);
-#if ENABLE_FEATURE_EDITING
+#if ENABLE_FEATURE_EDITING || ENABLE_HUSH_READ
if (sig != SIGCHLD
|| (G_traps && G_traps[SIGCHLD] && G_traps[SIGCHLD][0])
/* ^^^ if SIGCHLD, interrupt line reading only if it has a trap */
@@ -2280,7 +2280,9 @@ static NORETURN void save_history_run_exit_trap_and_exit(int exitcode)
static int check_and_run_traps(void)
{
int last_sig = 0;
-
+#if ENABLE_HUSH_READ
+ bb_got_signal = 0; /* or else read builtin gets immediately "interrupted" by already handled past signal */
+#endif
while (1) {
int sig;
@@ -11753,8 +11755,12 @@ static int FAST_FUNC builtin_read(char **argv)
if ((uintptr_t)r == 1 && errno == EINTR) {
unsigned sig = check_and_run_traps();
- if (sig != SIGINT)
+ if (sig != SIGINT
+ || (G_traps && G_traps[SIGINT] && G_traps[SIGINT][0])
+ ) {
+ /* Redo the read after signals other than untrapped SIGINT */
goto again;
+ }
}
if ((uintptr_t)r == 2) /* -t SEC timeout? */
diff --git a/shell/hush_test/hush-misc/read.right b/shell/hush_test/hush-read/read.right
similarity index 100%
rename from shell/hush_test/hush-misc/read.right
rename to shell/hush_test/hush-read/read.right
diff --git a/shell/hush_test/hush-misc/read.tests b/shell/hush_test/hush-read/read.tests
similarity index 100%
rename from shell/hush_test/hush-misc/read.tests
rename to shell/hush_test/hush-read/read.tests
diff --git a/shell/hush_test/hush-read/read_and_incoming_signal.right b/shell/hush_test/hush-read/read_and_incoming_signal.right
new file mode 100644
index 000000000..a6ae3f80f
--- /dev/null
+++ b/shell/hush_test/hush-read/read_and_incoming_signal.right
@@ -0,0 +1,3 @@
+I
+Input:INPUT
+Ok:0
diff --git a/shell/hush_test/hush-read/read_and_incoming_signal.tests b/shell/hush_test/hush-read/read_and_incoming_signal.tests
new file mode 100755
index 000000000..2dfb51fe8
--- /dev/null
+++ b/shell/hush_test/hush-read/read_and_incoming_signal.tests
@@ -0,0 +1,2 @@
+(sleep 2; echo INPUT) | $THIS_SH -c 'trap "echo I" int; (sleep 1;kill -int $$)& read -pP: v;echo Input:$v'
+echo Ok:$?
diff --git a/shell/hush_test/hush-read/read_and_past_signal.right b/shell/hush_test/hush-read/read_and_past_signal.right
new file mode 100644
index 000000000..a6ae3f80f
--- /dev/null
+++ b/shell/hush_test/hush-read/read_and_past_signal.right
@@ -0,0 +1,3 @@
+I
+Input:INPUT
+Ok:0
diff --git a/shell/hush_test/hush-read/read_and_past_signal.tests b/shell/hush_test/hush-read/read_and_past_signal.tests
new file mode 100755
index 000000000..19a41bf81
--- /dev/null
+++ b/shell/hush_test/hush-read/read_and_past_signal.tests
@@ -0,0 +1,2 @@
+echo INPUT | $THIS_SH -c 'trap "echo I" int; (sleep 1;kill -int $$)& sleep 2; read -pP: v;echo Input:$v'
+echo Ok:$?
diff --git a/shell/shell_common.c b/shell/shell_common.c
index f0ed0008d..47170da53 100644
--- a/shell/shell_common.c
+++ b/shell/shell_common.c
@@ -224,6 +224,24 @@ shell_builtin_read(struct builtin_read_params *params)
retval = (const char *)(uintptr_t)2;
break;
}
+ // Both ash and hush set bb_got_signal in their signal handlers.
+// This can be handled cleaner via:
+// bb_got_signal = 0;
+// if (shell_already_knows_it_has_pending_signal())
+// return -1 + EINTR;
+// check_got_signal_and_poll()
+// ...
+// but shell_already_knows_it_has_pending_signal() check is shell-specific!
+// Therefore we can't clear bb_got_signal here - we depend on trap processing
+// clearing it every time traps are run (not leaving it stuck in a set state).
+ if (errno == EINTR) {
+ // We got a signal and our caller will act on it after seeing EINTR.
+ // Clear its indicator (if any) so that we do not "get signal" ad infinitum.
+ // Testcases:
+ // ash -c 'trap "echo I" int; (sleep 1;kill -int $$)& read -pP: v'
+ // ash -c 'trap "echo I" int; (sleep 1;kill -int $$)& sleep 2; read -pP: v'
+ bb_got_signal = 0;
+ }
retval = (const char *)(uintptr_t)1;
break;
}
More information about the busybox-cvs
mailing list