Revert "xtables-addons: fix nathelper-rtsp dependencies"
[openwrt/openwrt.git] / package / utils / busybox / 501-ash-hush-fix-SIGCHLD-interrupting-read-builtin.patch
1 From f5470419404d643070db99d058405b714695b817 Mon Sep 17 00:00:00 2001
2 From: Denys Vlasenko <vda.linux@googlemail.com>
3 Date: Mon, 22 May 2017 19:34:45 +0200
4 Subject: [PATCH] ash,hush: fix SIGCHLD interrupting read builtin
5
6 function old new delta
7 readcmd 169 217 +48
8 shell_builtin_read 1087 1097 +10
9 localcmd 366 364 -2
10 builtin_read 197 193 -4
11 ------------------------------------------------------------------------------
12 (add/remove: 0/0 grow/shrink: 2/2 up/down: 58/-6) Total: 52 bytes
13
14 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
15 Signed-off-by: Bastian Bittorf <bb@npl.de>
16 ---
17 shell/ash.c | 7 +++++++
18 shell/ash_test/ash-read/read_SIGCHLD.right | 2 ++
19 shell/ash_test/ash-read/read_SIGCHLD.tests | 4 ++++
20 shell/hush.c | 5 ++++-
21 shell/hush_test/hush-read/read_SIGCHLD.right | 2 ++
22 shell/hush_test/hush-read/read_SIGCHLD.tests | 4 ++++
23 shell/shell_common.c | 20 +++++++++++---------
24 7 files changed, 34 insertions(+), 10 deletions(-)
25 create mode 100644 shell/ash_test/ash-read/read_SIGCHLD.right
26 create mode 100755 shell/ash_test/ash-read/read_SIGCHLD.tests
27 create mode 100644 shell/hush_test/hush-read/read_SIGCHLD.right
28 create mode 100755 shell/hush_test/hush-read/read_SIGCHLD.tests
29
30 diff --git a/shell/ash.c b/shell/ash.c
31 index 70ee15e..60c8ffe 100644
32 --- a/shell/ash.c
33 +++ b/shell/ash.c
34 @@ -13268,6 +13268,7 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
35 /* "read -s" needs to save/restore termios, can't allow ^C
36 * to jump out of it.
37 */
38 + again:
39 INT_OFF;
40 r = shell_builtin_read(setvar0,
41 argptr,
42 @@ -13280,6 +13281,12 @@ readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
43 );
44 INT_ON;
45
46 + if ((uintptr_t)r == 1 && errno == EINTR) {
47 + /* to get SIGCHLD: sleep 1 & read x; echo $x */
48 + if (pending_sig == 0)
49 + goto again;
50 + }
51 +
52 if ((uintptr_t)r > 1)
53 ash_msg_and_raise_error(r);
54
55 diff --git a/shell/ash_test/ash-read/read_SIGCHLD.right b/shell/ash_test/ash-read/read_SIGCHLD.right
56 new file mode 100644
57 index 0000000..b3dc7ab
58 --- /dev/null
59 +++ b/shell/ash_test/ash-read/read_SIGCHLD.right
60 @@ -0,0 +1,2 @@
61 +x='Ok'
62 +exitcode:0
63 diff --git a/shell/ash_test/ash-read/read_SIGCHLD.tests b/shell/ash_test/ash-read/read_SIGCHLD.tests
64 new file mode 100755
65 index 0000000..c5f673a
66 --- /dev/null
67 +++ b/shell/ash_test/ash-read/read_SIGCHLD.tests
68 @@ -0,0 +1,4 @@
69 +x=BAD
70 +{ sleep 0.4; echo Ok; } | { sleep 0.2 & read x; echo "x='$x'"; }
71 +echo "exitcode:$?"
72 +
73 diff --git a/shell/hush.c b/shell/hush.c
74 index e18920f..125463a 100644
75 --- a/shell/hush.c
76 +++ b/shell/hush.c
77 @@ -9038,6 +9038,9 @@ static int FAST_FUNC builtin_type(char **argv)
78 * - terminates shell (regardless of interactivity);
79 * if it has non-empty trap:
80 * - executes trap and returns to read;
81 + * SIGCHLD from children:
82 + * - does not interrupt read regardless of interactivity:
83 + * try: sleep 1 & read x; echo $x
84 */
85 static int FAST_FUNC builtin_read(char **argv)
86 {
87 @@ -9071,7 +9074,7 @@ static int FAST_FUNC builtin_read(char **argv)
88
89 if ((uintptr_t)r == 1 && errno == EINTR) {
90 unsigned sig = check_and_run_traps();
91 - if (sig && sig != SIGINT)
92 + if (sig != SIGINT)
93 goto again;
94 }
95
96 diff --git a/shell/hush_test/hush-read/read_SIGCHLD.right b/shell/hush_test/hush-read/read_SIGCHLD.right
97 new file mode 100644
98 index 0000000..b3dc7ab
99 --- /dev/null
100 +++ b/shell/hush_test/hush-read/read_SIGCHLD.right
101 @@ -0,0 +1,2 @@
102 +x='Ok'
103 +exitcode:0
104 diff --git a/shell/hush_test/hush-read/read_SIGCHLD.tests b/shell/hush_test/hush-read/read_SIGCHLD.tests
105 new file mode 100755
106 index 0000000..c5f673a
107 --- /dev/null
108 +++ b/shell/hush_test/hush-read/read_SIGCHLD.tests
109 @@ -0,0 +1,4 @@
110 +x=BAD
111 +{ sleep 0.4; echo Ok; } | { sleep 0.2 & read x; echo "x='$x'"; }
112 +echo "exitcode:$?"
113 +
114 diff --git a/shell/shell_common.c b/shell/shell_common.c
115 index fb86e68..03b7d0b 100644
116 --- a/shell/shell_common.c
117 +++ b/shell/shell_common.c
118 @@ -204,15 +204,17 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
119 c = buffer[bufpos];
120 if (c == '\0')
121 continue;
122 - if (backslash) {
123 - backslash = 0;
124 - if (c != '\n')
125 - goto put;
126 - continue;
127 - }
128 - if (!(read_flags & BUILTIN_READ_RAW) && c == '\\') {
129 - backslash = 1;
130 - continue;
131 + if (!(read_flags & BUILTIN_READ_RAW)) {
132 + if (backslash) {
133 + backslash = 0;
134 + if (c != '\n')
135 + goto put;
136 + continue;
137 + }
138 + if (c == '\\') {
139 + backslash = 1;
140 + continue;
141 + }
142 }
143 if (c == '\n')
144 break;
145 --
146 1.9.1
147