Don't close-on-exec redirections created explicitly for the command being

ran; i.e. we want this to work:
	$ cat succ1
	#!/bin/sh
	./succ2 6>out

	$ cat succ2
	#!/bin/sh
	echo succ2 >&6

	$ ./succ1

And this to fail:
	$ cat fail1
	#!/bin/sh
	exec 6> out
	echo "fail1" >&6
	./fail2
	exec 6>&-

	$ cat fail2
	#!obj.amd64/sh
	echo "fail2" >&6

	$ ./fail1
	./fail2: 6: Bad file descriptor

XXX: Do we want a -k (keep flag on exec to make redirections not close-on-exec?
This commit is contained in:
christos 2016-03-12 21:35:13 +00:00
parent dde5a5b4ad
commit 5047abd140
3 changed files with 12 additions and 10 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: eval.c,v 1.116 2016/03/12 14:59:26 christos Exp $ */
/* $NetBSD: eval.c,v 1.117 2016/03/12 21:35:13 christos Exp $ */
/*-
* Copyright (c) 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
#else
__RCSID("$NetBSD: eval.c,v 1.116 2016/03/12 14:59:26 christos Exp $");
__RCSID("$NetBSD: eval.c,v 1.117 2016/03/12 21:35:13 christos Exp $");
#endif
#endif /* not lint */
@ -1118,7 +1118,8 @@ normal_fork:
#ifdef DEBUG
trputs("normal command: "); trargs(argv);
#endif
redirect(cmd->ncmd.redirect, vforked ? REDIR_VFORK : 0);
redirect(cmd->ncmd.redirect,
(vforked ? REDIR_VFORK : 0) | REDIR_KEEP);
if (!vforked)
for (sp = varlist.list ; sp ; sp = sp->next)
setvareq(sp->text, VEXPORT|VSTACK);

View File

@ -1,4 +1,4 @@
/* $NetBSD: redir.c,v 1.39 2016/01/04 13:57:15 christos Exp $ */
/* $NetBSD: redir.c,v 1.40 2016/03/12 21:35:13 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: redir.c,v 1.39 2016/01/04 13:57:15 christos Exp $");
__RCSID("$NetBSD: redir.c,v 1.40 2016/03/12 21:35:13 christos Exp $");
#endif
#endif /* not lint */
@ -244,7 +244,7 @@ openredirect(union node *redir, char memory[10], int flags)
}
if (f != fd) {
copyfd(f, fd, 1, fd > 2);
copyfd(f, fd, 1, fd > 2 && (flags & REDIR_KEEP) == 0);
close(f);
} else if (f > 2)
(void)fcntl(f, F_SETFD, FD_CLOEXEC);

View File

@ -1,4 +1,4 @@
/* $NetBSD: redir.h,v 1.17 2016/01/04 03:00:24 christos Exp $ */
/* $NetBSD: redir.h,v 1.18 2016/03/12 21:35:13 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -35,9 +35,10 @@
*/
/* flags passed to redirect */
#define REDIR_PUSH 01 /* save previous values of file descriptors */
#define REDIR_BACKQ 02 /* save the command output in memory */
#define REDIR_VFORK 04 /* running under vfork(2), be careful */
#define REDIR_PUSH 0x01 /* save previous values of file descriptors */
#define REDIR_BACKQ 0x02 /* save the command output in memory */
#define REDIR_VFORK 0x04 /* running under vfork(2), be careful */
#define REDIR_KEEP 0x08 /* don't close-on-exec */
union node;
void redirect(union node *, int);