Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Now that the shell is protecting its internal fds pro...



details:   https://anonhg.NetBSD.org/src/rev/046f3499e6c6
branches:  trunk
changeset: 824257:046f3499e6c6
user:      kre <kre%NetBSD.org@localhost>
date:      Mon May 29 22:21:00 2017 +0000

description:
Now that the shell is protecting its internal fds properly, moving
them whenever the user tries to step on one, we can change our behaviour
back to what the kernel considers to be that of a well behaved shell
(wrt file descriptor usage).  If our user causes problems, we will soon
move into recalcitrant process territory, but that should normally be
rare.  This should reduce kernel overheads a little.

diffstat:

 bin/sh/redir.c |  15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)

diffs (43 lines):

diff -r 0125cb88daf0 -r 046f3499e6c6 bin/sh/redir.c
--- a/bin/sh/redir.c    Mon May 29 14:03:23 2017 +0000
+++ b/bin/sh/redir.c    Mon May 29 22:21:00 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: redir.c,v 1.56 2017/05/18 13:56:58 kre Exp $   */
+/*     $NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre 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.56 2017/05/18 13:56:58 kre Exp $");
+__RCSID("$NetBSD: redir.c,v 1.57 2017/05/29 22:21:00 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -518,7 +518,7 @@
 find_big_fd(void)
 {
        int i, fd;
-       static int last_start = 6;
+       static int last_start = 3; /* aim to keep sh fd's under 20 */
 
        if (last_start < 10)
                last_start++;
@@ -630,6 +630,15 @@
 #define        CLOEXEC(fd)
 #endif
 
+       /*
+        * if we have had a collision, and the sh fd was a "big" one
+        * try moving the sh fd base to a higher number (if possible)
+        * so future sh fds are less likely to be in the user's sights
+        * (incl this one when moved)
+        */
+       if (fp->fd >= big_sh_fd)
+               find_big_fd();
+
        to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd);
        if (to == -1)
                to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd/2);



Home | Main Index | Thread Index | Old Index