Skip to content

Commit

Permalink
fix ksh-style close-on-exec-for-fds-above-2 in dup-to-self scenario
Browse files Browse the repository at this point in the history
bugreport by catern via IRC
  • Loading branch information
mirabilos committed Dec 4, 2018
1 parent dddf369 commit 8e67d0c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
34 changes: 31 additions & 3 deletions check.t
@@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.809 2018/10/20 18:48:26 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.810 2018/12/04 21:13:44 tg Exp $
# -*- mode: sh -*-
#-
# Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
Expand Down Expand Up @@ -30,7 +30,7 @@
# (2013/12/02 20:39:44) http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date

expected-stdout:
@(#)MIRBSD KSH R56 2018/10/20
@(#)MIRBSD KSH R56 2018/12/04
description:
Check base version of full shell
stdin:
Expand All @@ -39,7 +39,7 @@ name: KSH_VERSION
category: !shell:legacy-yes
---
expected-stdout:
@(#)LEGACY KSH R56 2018/10/20
@(#)LEGACY KSH R56 2018/12/04
description:
Check base version of legacy shell
stdin:
Expand Down Expand Up @@ -11264,6 +11264,34 @@ stdin:
expected-stdout:
Fowl
---
name: fd-cloexec-3
description:
Another check for close-on-exec
stdin:
print '#!'"$__progname" >ts
cat >>ts <<'EOF'
s=ERR
read -rN-1 -u$1 s 2>/dev/null; e=$?
print -r -- "($1, $((!e)), $s)"
EOF
chmod +x ts
print foo >tx
runtest() {
s=$1; shift
print -r -- $("$__progname" "$@" -c "$s") "$@" .
}
runtest 'exec 3<tx; ./ts 3 3<&3; ./ts 3'
runtest 'exec 3<tx; ./ts 3 3<&3; ./ts 3' -o posix
runtest 'exec 3<tx; ./ts 3 3<&3; ./ts 3' -o sh
runtest 'exec 3<tx; ./ts 4 4<&3; ./ts 4 4<&3'
runtest 'exec 3<tx; ./ts 3 3<&3; ./ts 3 3<&3'
expected-stdout:
(3, 1, foo) (3, 0, ERR) .
(3, 1, foo) (3, 1, ) -o posix .
(3, 1, foo) (3, 1, ) -o sh .
(4, 1, foo) (4, 1, ) .
(3, 1, foo) (3, 1, ) .
---
name: comsub-1a
description:
COMSUB are now parsed recursively, so this works
Expand Down
14 changes: 11 additions & 3 deletions exec.c
Expand Up @@ -2,7 +2,7 @@

/*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
* 2011, 2012, 2013, 2014, 2015, 2016, 2017
* 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
* mirabilos <m@mirbsd.org>
*
* Provided that these terms and disclaimer and all copyright notices
Expand All @@ -23,7 +23,7 @@

#include "sh.h"

__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.203 2018/10/30 17:10:14 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.204 2018/12/04 21:13:47 tg Exp $");

#ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL MKSH_UNIXROOT "/bin/sh"
Expand Down Expand Up @@ -429,6 +429,12 @@ execute(struct op * volatile t,
up = makenv();
restoresigs();
cleanup_proc_env();
/* I/O redirection cleanup to be done in child process */
if (!Flag(FPOSIX) && !Flag(FSH) && t->left->ioact != NULL)
for (iowp = t->left->ioact; *iowp != NULL; iowp++)
if ((*iowp)->ioflag & IODUPSELF)
fcntl((*iowp)->unit, F_SETFD, 0);
/* try to execute */
{
union mksh_ccphack cargs;

Expand Down Expand Up @@ -1485,9 +1491,11 @@ iosetup(struct ioword *iop, struct tbl *tp)
afree(sp, ATEMP);
return (-1);
}
if (u == (int)iop->unit)
if (u == (int)iop->unit) {
/* "dup from" == "dup to" */
iop->ioflag |= IODUPSELF;
return (0);
}
break;
}
}
Expand Down
13 changes: 7 additions & 6 deletions sh.h
Expand Up @@ -182,9 +182,9 @@
#endif

#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.867 2018/10/30 17:10:16 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.868 2018/12/04 21:13:47 tg Exp $");
#endif
#define MKSH_VERSION "R56 2018/10/20"
#define MKSH_VERSION "R56 2018/12/04"

/* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES
Expand Down Expand Up @@ -1954,10 +1954,11 @@ struct ioword {
#define IOSKIP BIT(5) /* <<-, skip ^\t* */
#define IOCLOB BIT(6) /* >|, override -o noclobber */
#define IORDUP BIT(7) /* x<&y (as opposed to x>&y) */
#define IONAMEXP BIT(8) /* name has been expanded */
#define IOBASH BIT(9) /* &> etc. */
#define IOHERESTR BIT(10) /* <<< (here string) */
#define IONDELIM BIT(11) /* null delimiter (<<) */
#define IODUPSELF BIT(8) /* x>&x (as opposed to x>&y) */
#define IONAMEXP BIT(9) /* name has been expanded */
#define IOBASH BIT(10) /* &> etc. */
#define IOHERESTR BIT(11) /* <<< (here string) */
#define IONDELIM BIT(12) /* null delimiter (<<) */

/* execute/exchild flags */
#define XEXEC BIT(0) /* execute without forking */
Expand Down

0 comments on commit 8e67d0c

Please sign in to comment.