diff options
author | Ron Yorston <rmy@pobox.com> | 2012-03-22 15:21:20 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2012-03-22 15:21:20 +0000 |
commit | 0d8b2c4a929ea9d3ac37499319fe0d8e7941a0c2 (patch) | |
tree | 6709ddd6071a9c238ba69233540bbcfe560c6a44 /shell/shell_common.c | |
parent | 67758035a4fe040c6ac69b39d61bcd6bddd7b827 (diff) | |
parent | 56a3b82e9692a25ef9c9269e88feac0d579ce8e8 (diff) | |
download | busybox-w32-0d8b2c4a929ea9d3ac37499319fe0d8e7941a0c2.tar.gz busybox-w32-0d8b2c4a929ea9d3ac37499319fe0d8e7941a0c2.tar.bz2 busybox-w32-0d8b2c4a929ea9d3ac37499319fe0d8e7941a0c2.zip |
Merge commit '56a3b82e9692a25ef9c9269e88feac0d579ce8e8' into merge
Conflicts:
coreutils/ls.c
include/platform.h
libbb/bb_basename.c
Diffstat (limited to 'shell/shell_common.c')
-rw-r--r-- | shell/shell_common.c | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/shell/shell_common.c b/shell/shell_common.c index 75f4b3e54..4329ca05c 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')) { |
@@ -152,28 +159,40 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
152 | bufpos = 0; | 159 | bufpos = 0; |
153 | do { | 160 | do { |
154 | char c; | 161 | char c; |
162 | struct pollfd pfd[1]; | ||
163 | int timeout; | ||
155 | 164 | ||
156 | if (end_ms) { | 165 | if ((bufpos & 0xff) == 0) |
157 | int timeout; | 166 | buffer = xrealloc(buffer, bufpos + 0x100); |
158 | struct pollfd pfd[1]; | ||
159 | 167 | ||
160 | pfd[0].fd = fd; | 168 | timeout = -1; |
161 | pfd[0].events = POLLIN; | 169 | if (end_ms) { |
162 | timeout = end_ms - (unsigned)monotonic_ms(); | 170 | timeout = end_ms - (unsigned)monotonic_ms(); |
163 | if (timeout <= 0 /* already late? */ | 171 | if (timeout <= 0) { /* already late? */ |
164 | || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */ | ||
165 | ) { /* timed out! */ | ||
166 | retval = (const char *)(uintptr_t)1; | 172 | retval = (const char *)(uintptr_t)1; |
167 | goto ret; | 173 | goto ret; |
168 | } | 174 | } |
169 | } | 175 | } |
170 | 176 | ||
171 | if ((bufpos & 0xff) == 0) | 177 | /* We must poll even if timeout is -1: |
172 | buffer = xrealloc(buffer, bufpos + 0x100); | 178 | * we want to be interrupted if signal arrives, |
173 | if (nonblock_safe_read(fd, &buffer[bufpos], 1) != 1) { | 179 | * regardless of SA_RESTART-ness of that signal! |
180 | */ | ||
181 | errno = 0; | ||
182 | pfd[0].fd = fd; | ||
183 | pfd[0].events = POLLIN; | ||
184 | if (poll(pfd, 1, timeout) != 1) { | ||
185 | /* timed out, or EINTR */ | ||
186 | err = errno; | ||
187 | retval = (const char *)(uintptr_t)1; | ||
188 | goto ret; | ||
189 | } | ||
190 | if (read(fd, &buffer[bufpos], 1) != 1) { | ||
191 | err = errno; | ||
174 | retval = (const char *)(uintptr_t)1; | 192 | retval = (const char *)(uintptr_t)1; |
175 | break; | 193 | break; |
176 | } | 194 | } |
195 | |||
177 | c = buffer[bufpos]; | 196 | c = buffer[bufpos]; |
178 | if (c == '\0' || (ENABLE_PLATFORM_MINGW32 && c == '\r')) | 197 | if (c == '\0' || (ENABLE_PLATFORM_MINGW32 && c == '\r')) |
179 | continue; | 198 | continue; |
@@ -240,6 +259,8 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val), | |||
240 | free(buffer); | 259 | free(buffer); |
241 | if (read_flags & BUILTIN_READ_SILENT) | 260 | if (read_flags & BUILTIN_READ_SILENT) |
242 | tcsetattr(fd, TCSANOW, &old_tty); | 261 | tcsetattr(fd, TCSANOW, &old_tty); |
262 | |||
263 | errno = err; | ||
243 | return retval; | 264 | return retval; |
244 | } | 265 | } |
245 | 266 | ||