Module Name:    src
Committed By:   kre
Date:           Mon Dec  3 02:38:30 UTC 2018

Modified Files:
        src/bin/sh: jobs.c main.c main.h

Log Message:
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


To generate a diff of this commit:
cvs rdiff -u -r1.102 -r1.103 src/bin/sh/jobs.c
cvs rdiff -u -r1.76 -r1.77 src/bin/sh/main.c
cvs rdiff -u -r1.11 -r1.12 src/bin/sh/main.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/jobs.c
diff -u src/bin/sh/jobs.c:1.102 src/bin/sh/jobs.c:1.103
--- src/bin/sh/jobs.c:1.102	Sun Oct 28 18:16:01 2018
+++ src/bin/sh/jobs.c	Mon Dec  3 02:38:30 2018
@@ -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,
 	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);

Index: src/bin/sh/main.c
diff -u src/bin/sh/main.c:1.76 src/bin/sh/main.c:1.77
--- src/bin/sh/main.c:1.76	Wed Aug 22 20:08:54 2018
+++ src/bin/sh/main.c	Mon Dec  3 02:38:30 2018
@@ -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, 19
 #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/
 
 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 */

Index: src/bin/sh/main.h
diff -u src/bin/sh/main.h:1.11 src/bin/sh/main.h:1.12
--- src/bin/sh/main.h:1.11	Sat Jun 18 21:18:46 2011
+++ src/bin/sh/main.h	Mon Dec  3 02:38:30 2018
@@ -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);

Reply via email to