summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2009-11-22 18:23:30 +0000
committerjilles <jilles@FreeBSD.org>2009-11-22 18:23:30 +0000
commit760264169e554e3e2ae8312f8f192d1ad95c7854 (patch)
treed42fd7d5720d96daa7589c733afd3cc29c10f330 /bin
parentba91da3aff4fb68aa0ec546f7eeb21855b267fc7 (diff)
downloadFreeBSD-src-760264169e554e3e2ae8312f8f192d1ad95c7854.zip
FreeBSD-src-760264169e554e3e2ae8312f8f192d1ad95c7854.tar.gz
Fix various things about SIGINT handling:
* exception handlers are now run with interrupts disabled, which avoids many race conditions * fix some cases where SIGINT only aborts one command and continues the script, in particular if a SIGINT causes an EINTR error which trumped the interrupt. Example: sh -c 'echo < /some/fifo; echo This should not be printed' The fifo should not have writers. When pressing ctrl+c to abort the open, the shell used to continue with the next command. Example: sh -c '/bin/echo < /some/fifo; echo This should not be printed' Similar. Note, however, that this particular case did not and does not work in interactive mode with job control enabled.
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/error.c15
-rw-r--r--bin/sh/error.h2
-rw-r--r--bin/sh/eval.c2
-rw-r--r--bin/sh/parser.c2
-rw-r--r--bin/sh/redir.c7
-rw-r--r--bin/sh/var.c3
6 files changed, 25 insertions, 6 deletions
diff --git a/bin/sh/error.c b/bin/sh/error.c
index 0c981a3..72061b8 100644
--- a/bin/sh/error.c
+++ b/bin/sh/error.c
@@ -73,11 +73,15 @@ static void exverror(int, const char *, va_list) __printf0like(2, 0);
* Called to raise an exception. Since C doesn't include exceptions, we
* just do a longjmp to the exception handler. The type of exception is
* stored in the global variable "exception".
+ *
+ * Interrupts are disabled; they should be reenabled when the exception is
+ * caught.
*/
void
exraise(int e)
{
+ INTOFF;
if (handler == NULL)
abort();
exception = e;
@@ -138,8 +142,15 @@ onint(void)
static void
exverror(int cond, const char *msg, va_list ap)
{
- CLEAR_PENDING_INT;
- INTOFF;
+ /*
+ * An interrupt trumps an error. Certain places catch error
+ * exceptions or transform them to a plain nonzero exit code
+ * in child processes, and if an error exception can be handled,
+ * an interrupt can be handled as well.
+ *
+ * exraise() will disable interrupts for the exception handler.
+ */
+ FORCEINTON;
#ifdef DEBUG
if (msg)
diff --git a/bin/sh/error.h b/bin/sh/error.h
index 4611821..38488c2 100644
--- a/bin/sh/error.h
+++ b/bin/sh/error.h
@@ -72,6 +72,8 @@ extern volatile sig_atomic_t intpending;
#define INTOFF suppressint++
#define INTON { if (--suppressint == 0 && intpending) onint(); }
+#define is_int_on() suppressint
+#define SETINTON(s) suppressint = (s)
#define FORCEINTON {suppressint = 0; if (intpending) onint();}
#define CLEAR_PENDING_INT intpending = 0
#define int_pending() intpending
diff --git a/bin/sh/eval.c b/bin/sh/eval.c
index 81de2d7..2bad138 100644
--- a/bin/sh/eval.c
+++ b/bin/sh/eval.c
@@ -782,7 +782,6 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
savelocalvars = localvars;
localvars = NULL;
reffunc(cmdentry.u.func);
- INTON;
savehandler = handler;
if (setjmp(jmploc.loc)) {
if (exception == EXSHELLPROC)
@@ -798,6 +797,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd)
longjmp(handler->loc, 1);
}
handler = &jmploc;
+ INTON;
for (sp = varlist.list ; sp ; sp = sp->next)
mklocal(sp->text);
funcnest++;
diff --git a/bin/sh/parser.c b/bin/sh/parser.c
index 5218dc0..855becc 100644
--- a/bin/sh/parser.c
+++ b/bin/sh/parser.c
@@ -1312,6 +1312,7 @@ parsebackq: {
int saveprompt;
const int bq_startlinno = plinno;
+ str = NULL;
if (setjmp(jmploc.loc)) {
if (str)
ckfree(str);
@@ -1323,7 +1324,6 @@ parsebackq: {
longjmp(handler->loc, 1);
}
INTOFF;
- str = NULL;
savelen = out - stackblock();
if (savelen > 0) {
str = ckmalloc(savelen);
diff --git a/bin/sh/redir.c b/bin/sh/redir.c
index 695e150..08878f6 100644
--- a/bin/sh/redir.c
+++ b/bin/sh/redir.c
@@ -166,8 +166,11 @@ openredirect(union node *redir, char memory[10])
/*
* We suppress interrupts so that we won't leave open file
- * descriptors around. This may not be such a good idea because
- * an open of a device or a fifo can block indefinitely.
+ * descriptors around. Because the signal handler remains
+ * installed and we do not use system call restart, interrupts
+ * will still abort blocking opens such as fifos (they will fail
+ * with EINTR). There is, however, a race condition if an interrupt
+ * arrives after INTOFF and before open blocks.
*/
INTOFF;
memory[fd] = 0;
diff --git a/bin/sh/var.c b/bin/sh/var.c
index 2c1caf1..6caf956 100644
--- a/bin/sh/var.c
+++ b/bin/sh/var.c
@@ -195,7 +195,9 @@ setvarsafe(char *name, char *val, int flags)
struct jmploc jmploc;
struct jmploc *const savehandler = handler;
int err = 0;
+ int inton;
+ inton = is_int_on();
if (setjmp(jmploc.loc))
err = 1;
else {
@@ -203,6 +205,7 @@ setvarsafe(char *name, char *val, int flags)
setvar(name, val, flags);
}
handler = savehandler;
+ SETINTON(inton);
return err;
}
OpenPOWER on IntegriCloud