• bring back automatic turn-on of FPOSIX if called as sh/-sh

‣ only if !MKSH_SMALL
  ‣ add appropriate regression test
• if FPOSIX is set, do not close fds > 2 on exec, Debian #499139
• add appropriate regression tests for keeping fds private or not
This commit is contained in:
tg 2008-09-17 19:31:30 +00:00
parent a04ad530a2
commit 8a853f24be
5 changed files with 78 additions and 12 deletions

48
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.221 2008/09/14 20:24:57 tg Exp $ # $MirOS: src/bin/mksh/check.t,v 1.222 2008/09/17 19:31:28 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $ # $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -7,7 +7,7 @@
# http://www.research.att.com/~gsf/public/ifs.sh # http://www.research.att.com/~gsf/public/ifs.sh
expected-stdout: expected-stdout:
@(#)MIRBSD KSH R35 2008/09/14 @(#)MIRBSD KSH R35 2008/09/17
description: description:
Check version of shell. Check version of shell.
stdin: stdin:
@ -4251,6 +4251,25 @@ expected-stdout:
posix posix
brex brex
--- ---
name: posix-mode-2
description:
Check that posix mode is automatically turned on
category: !smksh
stdin:
ln -s "$__progname" ksh
ln -s "$__progname" sh
ln -s "$__progname" -ksh
ln -s "$__progname" -sh
for shell in {,-}{,k}sh; do
print -- $shell $(./$shell +l -c \
'[[ $(set +o) == *@(-o posix)@(| *) ]] && echo posix || echo noposix')
done
expected-stdout:
sh posix
ksh noposix
-sh posix
-ksh noposix
---
name: pipeline-1 name: pipeline-1
description: description:
pdksh bug: last command of a pipeline is executed in a pdksh bug: last command of a pipeline is executed in a
@ -5307,3 +5326,28 @@ stdin:
exit 1 exit 1
fi fi
--- ---
name: fd-cloexec-1
description:
Verify that file descriptors > 2 are private for Korn shells
file-setup: file 644 "test.sh"
print -u3 Fowl
stdin:
exec 3>&1
"$__progname" test.sh
expected-exit: e != 0
expected-stderr:
test.sh[1]: print: -u: 3: bad file descriptor
---
name: fd-cloexec-2
description:
Verify that file descriptors > 2 are not private for POSIX shells
See Debian Bug #154540, Closes: #499139
file-setup: file 644 "test.sh"
print -u3 Fowl
stdin:
set -o posix
exec 3>&1
"$__progname" test.sh
expected-stdout:
Fowl
---

View File

@ -5,7 +5,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.86 2008/08/02 17:45:11 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.87 2008/09/17 19:31:29 tg Exp $");
/* A leading = means assignments before command are kept; /* A leading = means assignments before command are kept;
* a leading * means a POSIX special builtin; * a leading * means a POSIX special builtin;
@ -2308,8 +2308,8 @@ c_exec(const char **wp __unused)
for (i = 0; i < NUFILE; i++) { for (i = 0; i < NUFILE; i++) {
if (e->savefd[i] > 0) if (e->savefd[i] > 0)
close(e->savefd[i]); close(e->savefd[i]);
/* For ksh keep anything > 2 private */ /* For ksh (but not sh), keep anything > 2 private */
if (i > 2 && e->savefd[i]) if (!Flag(FPOSIX) && i > 2 && e->savefd[i])
fcntl(i, F_SETFD, FD_CLOEXEC); fcntl(i, F_SETFD, FD_CLOEXEC);
} }
e->savefd = NULL; e->savefd = NULL;

17
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h> #include <locale.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.101 2008/07/10 18:34:08 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.102 2008/09/17 19:31:29 tg Exp $");
extern char **environ; extern char **environ;
@ -173,6 +173,21 @@ main(int argc, const char *argv[])
Flag(FVITABCOMPLETE) = 1; Flag(FVITABCOMPLETE) = 1;
#endif #endif
#ifndef MKSH_SMALL
/* Set FPOSIX if we're called as -sh or /bin/sh or so */
{
const char *cc;
cc = kshname;
i = 0; argi = 0;
while (cc[i] != '\0')
if ((cc[i++] | 2) == '/')
argi = i;
if (((cc[argi] | 0x20) == 's') && ((cc[argi + 1] | 0x20) == 'h'))
change_flag(FPOSIX, OF_FIRSTTIME, 1);
}
#endif
/* import environment */ /* import environment */
if (environ != NULL) if (environ != NULL)
for (wp = (const char **)environ; *wp != NULL; wp++) for (wp = (const char **)environ; *wp != NULL; wp++)

15
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.135 2008/09/17 19:27:16 tg Exp $ .\" $MirOS: src/bin/mksh/mksh.1,v 1.136 2008/09/17 19:31:30 tg Exp $
.\" $OpenBSD: ksh.1,v 1.122 2008/05/17 23:31:52 sobrado Exp $ .\" $OpenBSD: ksh.1,v 1.122 2008/05/17 23:31:52 sobrado Exp $
.\"- .\"-
.\" Try to make GNU groff and AT&T nroff more compatible .\" Try to make GNU groff and AT&T nroff more compatible
@ -2080,6 +2080,11 @@ pipelines are created and in the order they are given, so the following
will print an error with a line number prepended to it: will print an error with a line number prepended to it:
.Pp .Pp
.D1 $ cat /foo/bar 2\*(Gt&1 \*(Gt /dev/null \*(Ba cat \-n .D1 $ cat /foo/bar 2\*(Gt&1 \*(Gt /dev/null \*(Ba cat \-n
.Pp
File descriptors created by input/output redirections are private to the
Korn shell, but passed to sub-processes if
.Fl o Ic posix
is set.
.Ss Arithmetic expressions .Ss Arithmetic expressions
Integer arithmetic expressions can be used with the Integer arithmetic expressions can be used with the
.Ic let .Ic let
@ -3580,9 +3585,11 @@ and
commands above for more details. commands above for more details.
.It Ic posix .It Ic posix
Enable POSIX mode. Enable POSIX mode.
Currently, this just turns off Automatically enabled if the basename of the shell invocation begins with
.Dq sh .
As a side effect, setting this flag turns off
.Ic braceexpand .Ic braceexpand
mode when turned on, which can be turned back on manually. mode, which can be turned back on manually.
.It Ic restricted .It Ic restricted
The shell is a restricted shell. The shell is a restricted shell.
This option can only be used when the shell is invoked. This option can only be used when the shell is invoked.
@ -5525,7 +5532,7 @@ and many other persons, and is currently maintained by
.An Thorsten Glaser Aq tg@mirbsd.de . .An Thorsten Glaser Aq tg@mirbsd.de .
.Sh BUGS .Sh BUGS
This document attempts to describe This document attempts to describe
.Nm mksh\ R35 .Nm mksh\ R35c
and up, and up,
compiled without any options impacting functionality, such as compiled without any options impacting functionality, such as
.Dv MKSH_SMALL , .Dv MKSH_SMALL ,

4
sh.h
View File

@ -100,9 +100,9 @@
#define __SCCSID(x) __IDSTRING(sccsid,x) #define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.231 2008/09/14 20:24:59 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.232 2008/09/17 19:31:30 tg Exp $");
#endif #endif
#define MKSH_VERSION "R35 2008/09/14" #define MKSH_VERSION "R35 2008/09/17"
#ifndef MKSH_INCLUDES_ONLY #ifndef MKSH_INCLUDES_ONLY