tags 499139 = upstream confirmed fixed-upstream patch
retitle 499139 mksh closes file descriptors > 2 on exec
thanks

Dixi quod…

>This seems to be related to #154540 – found after I got the information
>that it only happens to pdksh with FSH not set.

It in fact is. This is fixed upstream with changeset 10048D15ABE2EA76C75,
which I have attached as-is for further inspection. For Korn shells, this
behaviour is actually expected, but there is a way to make it behave more
like a POSIX shell, so I revived a code path enabling that mode if called
as sh or -sh, and put the appropriate code change in there.

>Would a fix for this bug be worth a freeze exception? Otherwise, I’ll
>just fix it upstream, and the Debian package would be updated with a
>new upstream version in ~1-2 months or so.

The patch is more than only a couple of lines, but for Lenny I could
prepare a smaller patch with only the required functionality (and,
optionally, the test cases). Opinions?

//mirabilos
-- 
> Hi, does anyone sell openbsd stickers by themselves and not packaged
> with other products?
No, the only way I've seen them sold is for $40 with a free OpenBSD CD.
        -- Haroon Khalid and Steve Shockley in gmane.os.openbsd.misc
Index: src/bin/mksh/check.t
diff -up src/bin/mksh/check.t:1.221 src/bin/mksh/check.t:1.222
--- src/bin/mksh/check.t:1.221  Sun Sep 14 20:24:57 2008
+++ src/bin/mksh/check.t        Wed Sep 17 19:31:28 2008
@@ -7,7 +7,7 @@
 # http://www.research.att.com/~gsf/public/ifs.sh
 
 expected-stdout:
-       @(#)MIRBSD KSH R35 2008/09/14
+       @(#)MIRBSD KSH R35 2008/09/17
 description:
        Check version of shell.
 stdin:
@@ -4251,6 +4251,25 @@ expected-stdout:
        posix
        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
 description:
        pdksh bug: last command of a pipeline is executed in a
@@ -5307,3 +5326,28 @@ stdin:
                exit 1
        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
+---
Index: src/bin/mksh/funcs.c
diff -up src/bin/mksh/funcs.c:1.86 src/bin/mksh/funcs.c:1.87
--- src/bin/mksh/funcs.c:1.86   Sat Aug  2 17:45:11 2008
+++ src/bin/mksh/funcs.c        Wed Sep 17 19:31:29 2008
@@ -2308,8 +2308,8 @@ c_exec(const char **wp __unused)
                for (i = 0; i < NUFILE; i++) {
                        if (e->savefd[i] > 0)
                                close(e->savefd[i]);
-                       /* For ksh keep anything > 2 private */
-                       if (i > 2 && e->savefd[i])
+                       /* For ksh (but not sh), keep anything > 2 private */
+                       if (!Flag(FPOSIX) && i > 2 && e->savefd[i])
                                fcntl(i, F_SETFD, FD_CLOEXEC);
                }
                e->savefd = NULL;
Index: src/bin/mksh/main.c
diff -up src/bin/mksh/main.c:1.101 src/bin/mksh/main.c:1.102
--- src/bin/mksh/main.c:1.101   Thu Jul 10 18:34:08 2008
+++ src/bin/mksh/main.c Wed Sep 17 19:31:29 2008
@@ -173,6 +173,21 @@ main(int argc, const char *argv[])
        Flag(FVITABCOMPLETE) = 1;
 #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 */
        if (environ != NULL)
                for (wp = (const char **)environ; *wp != NULL; wp++)
Index: src/bin/mksh/mksh.1
diff -up src/bin/mksh/mksh.1:1.135 src/bin/mksh/mksh.1:1.136
--- src/bin/mksh/mksh.1:1.135   Wed Sep 17 19:27:16 2008
+++ src/bin/mksh/mksh.1 Wed Sep 17 19:31:30 2008
@@ -2080,6 +2080,11 @@ pipelines are created and in the order t
 will print an error with a line number prepended to it:
 .Pp
 .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
 Integer arithmetic expressions can be used with the
 .Ic let
@@ -3580,9 +3585,11 @@ and
 commands above for more details.
 .It Ic posix
 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
-mode when turned on, which can be turned back on manually.
+mode, which can be turned back on manually.
 .It Ic restricted
 The shell is a restricted shell.
 This option can only be used when the shell is invoked.
@@ -5525,7 +5532,7 @@ and many other persons, and is currently
 .An Thorsten Glaser Aq [EMAIL PROTECTED] .
 .Sh BUGS
 This document attempts to describe
-.Nm mksh\ R35
+.Nm mksh\ R35c
 and up,
 compiled without any options impacting functionality, such as
 .Dv MKSH_SMALL ,
Index: src/bin/mksh/sh.h
diff -up src/bin/mksh/sh.h:1.231 src/bin/mksh/sh.h:1.232
--- src/bin/mksh/sh.h:1.231     Sun Sep 14 20:24:59 2008
+++ src/bin/mksh/sh.h   Wed Sep 17 19:31:30 2008
@@ -102,7 +102,7 @@
 #ifdef EXTERN
 __RCSID("$MirOS$");
 #endif
-#define MKSH_VERSION "R35 2008/09/14"
+#define MKSH_VERSION "R35 2008/09/17"
 
 #ifndef MKSH_INCLUDES_ONLY
 

Reply via email to