When forking a child shell, arrange for errors/exit to always unwind

to the main handler, rather than wherever the parent shell would go.

nb: not needed for vfork(), after vfork() we never go that path - which
is good or we'd be corrupting the parent's handler.

This allows the child to always exit (when it should) rather than being
caught up doing something else (and while it would eventually exit, the
status would be incorrect in some cases).

One test is:
	sh -c 'trap "(! :) && echo BUG || echo nobug" EXIT'
from Martijn Dekker

Fix from FreeBSD (missed earlier).

XXX - 2b part of the 48875 pullup to -8
This commit is contained in:
kre 2018-12-03 02:38:30 +00:00
parent e7b3b8a289
commit 7f63ac72c5
3 changed files with 13 additions and 9 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: jobs.c,v 1.102 2018/10/28 18:16:01 kre Exp $ */
/* $NetBSD: jobs.c,v 1.103 2018/12/03 02:38:30 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)jobs.c 8.5 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: jobs.c,v 1.102 2018/10/28 18:16:01 kre Exp $");
__RCSID("$NetBSD: jobs.c,v 1.103 2018/12/03 02:38:30 kre Exp $");
#endif
#endif /* not lint */
@ -1161,8 +1161,11 @@ forkchild(struct job *jp, union node *n, int mode, int vforked)
wasroot = rootshell;
CTRACE(DBG_JOBS, ("Child shell %d %sforked from %d (mode %d)\n",
getpid(), vforked?"v":"", getppid(), mode));
if (!vforked)
if (!vforked) {
rootshell = 0;
handler = &main_handler;
}
closescript(vforked);
clear_traps(vforked);

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.76 2018/08/22 20:08:54 kre Exp $ */
/* $NetBSD: main.c,v 1.77 2018/12/03 02:38:30 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
#if 0
static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
#else
__RCSID("$NetBSD: main.c,v 1.76 2018/08/22 20:08:54 kre Exp $");
__RCSID("$NetBSD: main.c,v 1.77 2018/12/03 02:38:30 kre Exp $");
#endif
#endif /* not lint */
@ -83,6 +83,7 @@ __RCSID("$NetBSD: main.c,v 1.76 2018/08/22 20:08:54 kre Exp $");
int rootpid;
int rootshell;
struct jmploc main_handler;
int max_user_fd;
#if PROFILE
short profile_buf[16384];
@ -102,7 +103,6 @@ STATIC void read_profile(const char *);
int
main(int argc, char **argv)
{
struct jmploc jmploc;
struct stackmark smark;
volatile int state;
char *shinit;
@ -123,7 +123,7 @@ main(int argc, char **argv)
monitor(4, etext, profile_buf, sizeof profile_buf, 50);
#endif
state = 0;
if (setjmp(jmploc.loc)) {
if (setjmp(main_handler.loc)) {
/*
* When a shell procedure is executed, we raise the
* exception EXSHELLPROC to clean up before executing
@ -170,7 +170,7 @@ main(int argc, char **argv)
else
goto state4;
}
handler = &jmploc;
handler = &main_handler;
#ifdef DEBUG
#if DEBUG >= 2
debug = 1; /* this may be reset by procargs() later */

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.h,v 1.11 2011/06/18 21:18:46 christos Exp $ */
/* $NetBSD: main.h,v 1.12 2018/12/03 02:38:30 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -36,6 +36,7 @@
extern int rootpid; /* pid of main shell */
extern int rootshell; /* true if we aren't a child of the main shell */
extern struct jmploc main_handler; /* top level exception handler */
void readcmdfile(char *);
void cmdloop(int);