diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-05-08 21:23:43 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-05-08 21:23:43 +0200 |
commit | 80542bad2f1df9d99b579c9eeb3c2675c14c72c0 (patch) | |
tree | 906cdea5609e0272fda16dc02caa3c683912c228 /shell/shell_common.c | |
parent | 80c5b6893d4708b3683ad9a51c990a326a8f1dff (diff) | |
download | busybox-w32-80542bad2f1df9d99b579c9eeb3c2675c14c72c0.tar.gz busybox-w32-80542bad2f1df9d99b579c9eeb3c2675c14c72c0.tar.bz2 busybox-w32-80542bad2f1df9d99b579c9eeb3c2675c14c72c0.zip |
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>
Diffstat (limited to '')
-rw-r--r-- | shell/shell_common.c | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/shell/shell_common.c b/shell/shell_common.c index 86a6493ed..a5c455c8e 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c | |||
@@ -36,6 +36,10 @@ int FAST_FUNC is_well_formed_var_name(const char *s, char terminator) | |||
36 | 36 | ||
37 | /* read builtin */ | 37 | /* read builtin */ |
38 | 38 | ||
39 | /* Needs to be interruptible: shell mush handle traps and shell-special signals | ||
40 | * while inside read. To implement this, be sure to not loop on EINTR | ||
41 | * and return errno == EINTR reliably. | ||
42 | */ | ||
39 | //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL" | 43 | //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL" |
40 | //string. hush naturally has it, and ash has setvareq(). | 44 | //string. hush naturally has it, and ash has setvareq(). |
41 | //Here we can simply store "VAR=" at buffer start and store read data directly | 45 | //Here we can simply store "VAR=" at buffer start and store read data directly |
@@ -51,6 +55,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
51 | const char *opt_u | 55 | const char *opt_u |
52 | ) | 56 | ) |
53 | { | 57 | { |
58 | unsigned err; | ||
54 | unsigned end_ms; /* -t TIMEOUT */ | 59 | unsigned end_ms; /* -t TIMEOUT */ |
55 | int fd; /* -u FD */ | 60 | int fd; /* -u FD */ |
56 | int nchars; /* -n NUM */ | 61 | int nchars; /* -n NUM */ |
@@ -62,6 +67,8 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
62 | int startword; | 67 | int startword; |
63 | smallint backslash; | 68 | smallint backslash; |
64 | 69 | ||
70 | errno = err = 0; | ||
71 | |||
65 | pp = argv; | 72 | pp = argv; |
66 | while (*pp) { | 73 | while (*pp) { |
67 | if (!is_well_formed_var_name(*pp, '\0')) { | 74 | if (!is_well_formed_var_name(*pp, '\0')) { |
@@ -153,6 +160,8 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
153 | do { | 160 | do { |
154 | char c; | 161 | char c; |
155 | 162 | ||
163 | errno = 0; | ||
164 | |||
156 | if (end_ms) { | 165 | if (end_ms) { |
157 | int timeout; | 166 | int timeout; |
158 | struct pollfd pfd[1]; | 167 | struct pollfd pfd[1]; |
@@ -161,8 +170,9 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
161 | pfd[0].events = POLLIN; | 170 | pfd[0].events = POLLIN; |
162 | timeout = end_ms - (unsigned)monotonic_ms(); | 171 | timeout = end_ms - (unsigned)monotonic_ms(); |
163 | if (timeout <= 0 /* already late? */ | 172 | if (timeout <= 0 /* already late? */ |
164 | || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */ | 173 | || poll(pfd, 1, timeout) != 1 /* no? wait... */ |
165 | ) { /* timed out! */ | 174 | ) { /* timed out! */ |
175 | err = errno; | ||
166 | retval = (const char *)(uintptr_t)1; | 176 | retval = (const char *)(uintptr_t)1; |
167 | goto ret; | 177 | goto ret; |
168 | } | 178 | } |
@@ -170,7 +180,8 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
170 | 180 | ||
171 | if ((bufpos & 0xff) == 0) | 181 | if ((bufpos & 0xff) == 0) |
172 | buffer = xrealloc(buffer, bufpos + 0x100); | 182 | buffer = xrealloc(buffer, bufpos + 0x100); |
173 | if (nonblock_immune_read(fd, &buffer[bufpos], 1) != 1) { | 183 | if (nonblock_immune_read(fd, &buffer[bufpos], 1, /*loop_on_EINTR:*/ 0) != 1) { |
184 | err = errno; | ||
174 | retval = (const char *)(uintptr_t)1; | 185 | retval = (const char *)(uintptr_t)1; |
175 | break; | 186 | break; |
176 | } | 187 | } |
@@ -240,6 +251,8 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
240 | free(buffer); | 251 | free(buffer); |
241 | if (read_flags & BUILTIN_READ_SILENT) | 252 | if (read_flags & BUILTIN_READ_SILENT) |
242 | tcsetattr(fd, TCSANOW, &old_tty); | 253 | tcsetattr(fd, TCSANOW, &old_tty); |
254 | |||
255 | errno = err; | ||
243 | return retval; | 256 | return retval; |
244 | } | 257 | } |
245 | 258 | ||