hush: use SA_RESTARTed signal handlers across read.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2011-05-11 11:49:21 +02:00
parent 12bc152b31
commit 10c0131a8a
2 changed files with 42 additions and 29 deletions

View File

@ -792,8 +792,13 @@ struct globals {
unsigned handled_SIGCHLD; unsigned handled_SIGCHLD;
smallint we_have_children; smallint we_have_children;
#endif #endif
/* which signals have non-DFL handler (even with no traps set)? */ /* Which signals have non-DFL handler (even with no traps set)?
unsigned non_DFL_mask; * Set at the start to:
* (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOB_SIGS)
* SPECIAL_INTERACTIVE_SIGS are cleared after fork.
* Other than these two times, never modified.
*/
unsigned special_sig_mask;
char **traps; /* char *traps[NSIG] */ char **traps; /* char *traps[NSIG] */
/* Signal mask on the entry to the (top-level) shell. Never modified. */ /* Signal mask on the entry to the (top-level) shell. Never modified. */
sigset_t inherited_set; sigset_t inherited_set;
@ -1341,11 +1346,11 @@ static void restore_G_args(save_arg_t *sv, char **argv)
* After each pipe execution, we extract any pending signals via sigtimedwait() * After each pipe execution, we extract any pending signals via sigtimedwait()
* and act on them. * and act on them.
* *
* unsigned non_DFL_mask: a mask of such "special" signals * unsigned special_sig_mask: a mask of such "special" signals
* sigset_t blocked_set: current blocked signal set * sigset_t blocked_set: current blocked signal set
* *
* "trap - SIGxxx": * "trap - SIGxxx":
* clear bit in blocked_set unless it is also in non_DFL_mask * clear bit in blocked_set unless it is also in special_sig_mask
* "trap 'cmd' SIGxxx": * "trap 'cmd' SIGxxx":
* set bit in blocked_set (even if 'cmd' is '') * set bit in blocked_set (even if 'cmd' is '')
* after [v]fork, if we plan to be a shell: * after [v]fork, if we plan to be a shell:
@ -5376,7 +5381,7 @@ static void reset_traps_to_defaults(void)
* Testcase: (while :; do :; done) + ^Z should background. * Testcase: (while :; do :; done) + ^Z should background.
* Same goes for SIGTERM, SIGHUP, SIGINT. * Same goes for SIGTERM, SIGHUP, SIGINT.
*/ */
if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) if (!G.traps && !(G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS))
return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */ return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */
/* Switching off SPECIAL_INTERACTIVE_SIGS. /* Switching off SPECIAL_INTERACTIVE_SIGS.
@ -5394,10 +5399,10 @@ static void reset_traps_to_defaults(void)
} }
} }
/* Our homegrown sig mask is saner to work with :) */ /* Our homegrown sig mask is saner to work with :) */
G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS;
/* Resetting all traps to default except empty ones */ /* Resetting all traps to default except empty ones */
mask = G.non_DFL_mask; mask = G.special_sig_mask;
if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) {
if (!G.traps[sig] || !G.traps[sig][0]) if (!G.traps[sig] || !G.traps[sig][0])
continue; continue;
@ -7440,9 +7445,6 @@ static void init_sigmasks(void)
/* POSIX allows shell to re-enable SIGCHLD /* POSIX allows shell to re-enable SIGCHLD
* even if it was SIG_IGN on entry */ * even if it was SIG_IGN on entry */
#if ENABLE_HUSH_FAST
G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */
#endif
if (!G.inherited_set_is_saved) { if (!G.inherited_set_is_saved) {
#if ENABLE_HUSH_FAST #if ENABLE_HUSH_FAST
signal(SIGCHLD, SIGCHLD_handler); signal(SIGCHLD, SIGCHLD_handler);
@ -7460,7 +7462,7 @@ static void init_sigmasks(void)
if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */
mask |= SPECIAL_JOB_SIGS; mask |= SPECIAL_JOB_SIGS;
} }
G.non_DFL_mask = mask; G.special_sig_mask = mask;
/* Block them. And unblock SIGCHLD */ /* Block them. And unblock SIGCHLD */
sig = 0; sig = 0;
@ -7504,10 +7506,10 @@ static void set_fatal_handlers_to_sigexit(void)
/*+ (1 << SIGINT )*/ /*+ (1 << SIGINT )*/
; ;
/* non_DFL_mask'ed signals are, well, masked, /* special_sig_mask'ed signals are, well, masked,
* no need to set handler for them. * no need to set handler for them.
*/ */
fatal_sigs &= ~G.non_DFL_mask; fatal_sigs &= ~G.special_sig_mask;
/* For each sig in fatal_sigs... */ /* For each sig in fatal_sigs... */
sig = 0; sig = 0;
@ -7571,8 +7573,11 @@ int hush_main(int argc, char **argv)
struct variable *shell_ver; struct variable *shell_ver;
INIT_G(); INIT_G();
if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */ if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */
G.last_exitcode = EXIT_SUCCESS; G.last_exitcode = EXIT_SUCCESS;
#if ENABLE_HUSH_FAST
G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */
#endif
#if !BB_MMU #if !BB_MMU
G.argv0_for_re_execing = argv[0]; G.argv0_for_re_execing = argv[0];
#endif #endif
@ -8303,7 +8308,7 @@ static int FAST_FUNC builtin_trap(char **argv)
/* There was a trap handler, we are removing it /* There was a trap handler, we are removing it
* (if sig has non-DFL handling, * (if sig has non-DFL handling,
* we don't need to do anything) */ * we don't need to do anything) */
if (sig < 32 && (G.non_DFL_mask & (1 << sig))) if (sig < sizeof(G.special_sig_mask)*8 && (G.special_sig_mask & (1 << sig)))
continue; continue;
sigdelset(&G.blocked_set, sig); sigdelset(&G.blocked_set, sig);
} }
@ -8565,7 +8570,7 @@ static int FAST_FUNC builtin_read(char **argv)
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
sigfillset(&sa.sa_mask); sigfillset(&sa.sa_mask);
/*sa.sa_flags = 0;*/ sa.sa_flags = SA_RESTART;
sa.sa_handler = record_signal; sa.sa_handler = record_signal;
sig = 0; sig = 0;

View File

@ -159,32 +159,40 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
bufpos = 0; bufpos = 0;
do { do {
char c; char c;
struct pollfd pfd[1];
int timeout;
errno = 0; if ((bufpos & 0xff) == 0)
buffer = xrealloc(buffer, bufpos + 0x100);
timeout = -1;
if (end_ms) { if (end_ms) {
int timeout;
struct pollfd pfd[1];
pfd[0].fd = fd;
pfd[0].events = POLLIN;
timeout = end_ms - (unsigned)monotonic_ms(); timeout = end_ms - (unsigned)monotonic_ms();
if (timeout <= 0 /* already late? */ if (timeout <= 0) { /* already late? */
|| poll(pfd, 1, timeout) != 1 /* no? wait... */
) { /* timed out! */
err = errno;
retval = (const char *)(uintptr_t)1; retval = (const char *)(uintptr_t)1;
goto ret; goto ret;
} }
} }
if ((bufpos & 0xff) == 0) /* We must poll even if timeout is -1:
buffer = xrealloc(buffer, bufpos + 0x100); * we want to be interrupted if signal arrives,
if (nonblock_immune_read(fd, &buffer[bufpos], 1, /*loop_on_EINTR:*/ 0) != 1) { * regardless of SA_RESTART-ness of that signal!
*/
errno = 0;
pfd[0].fd = fd;
pfd[0].events = POLLIN;
if (poll(pfd, 1, timeout) != 1) {
/* timed out, or EINTR */
err = errno;
retval = (const char *)(uintptr_t)1;
goto ret;
}
if (read(fd, &buffer[bufpos], 1) != 1) {
err = errno; err = errno;
retval = (const char *)(uintptr_t)1; retval = (const char *)(uintptr_t)1;
break; break;
} }
c = buffer[bufpos]; c = buffer[bufpos];
if (c == '\0') if (c == '\0')
continue; continue;