Module Name: src Committed By: kre Date: Sat Apr 22 16:02:39 UTC 2017
Modified Files: src/bin/sh: main.c redir.c redir.h Log Message: Keep track of the biggest fd used by, or available to, the user/script and use that to control which fd's are examined by a (bare) fdflags (with no fd args). Usually this will mean that fdflags will no longer show the shell's internal use fds, only user fds. This is only a partial fix however, a user can easily discover the shell's fd usage (eg: using fstat) and can then still use fdflags to manipulate those fds (or even send output to them). The shell needs to monitor its own fd usage better, and keep out of the way of user fds - coming sometime later... To generate a diff of this commit: cvs rdiff -u -r1.67 -r1.68 src/bin/sh/main.c cvs rdiff -u -r1.52 -r1.53 src/bin/sh/redir.c cvs rdiff -u -r1.21 -r1.22 src/bin/sh/redir.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/bin/sh/main.c diff -u src/bin/sh/main.c:1.67 src/bin/sh/main.c:1.68 --- src/bin/sh/main.c:1.67 Mon May 9 21:03:10 2016 +++ src/bin/sh/main.c Sat Apr 22 16:02:39 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.67 2016/05/09 21:03:10 kre Exp $ */ +/* $NetBSD: main.c,v 1.68 2017/04/22 16:02:39 kre Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 19 #if 0 static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95"; #else -__RCSID("$NetBSD: main.c,v 1.67 2016/05/09 21:03:10 kre Exp $"); +__RCSID("$NetBSD: main.c,v 1.68 2017/04/22 16:02:39 kre Exp $"); #endif #endif /* not lint */ @@ -77,11 +77,13 @@ __RCSID("$NetBSD: main.c,v 1.67 2016/05/ #include "mystring.h" #include "exec.h" #include "cd.h" +#include "redir.h" #define PROFILE 0 int rootpid; int rootshell; +int max_user_fd; #if PROFILE short profile_buf[16384]; extern int etext(); @@ -111,6 +113,10 @@ main(int argc, char **argv) uid = getuid(); gid = getgid(); + max_user_fd = fcntl(0, F_MAXFD); + if (max_user_fd < 2) + max_user_fd = 2; + setlocale(LC_ALL, ""); posix = getenv("POSIXLY_CORRECT") != NULL; Index: src/bin/sh/redir.c diff -u src/bin/sh/redir.c:1.52 src/bin/sh/redir.c:1.53 --- src/bin/sh/redir.c:1.52 Sat Apr 22 15:54:53 2017 +++ src/bin/sh/redir.c Sat Apr 22 16:02:39 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: redir.c,v 1.52 2017/04/22 15:54:53 kre Exp $ */ +/* $NetBSD: redir.c,v 1.53 2017/04/22 16:02:39 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.52 2017/04/22 15:54:53 kre Exp $"); +__RCSID("$NetBSD: redir.c,v 1.53 2017/04/22 16:02:39 kre Exp $"); #endif #endif /* not lint */ @@ -189,6 +189,8 @@ redirect(union node *redir, int flags) } for (n = redir ; n ; n = n->nfile.next) { fd = n->nfile.fd; + if (fd > max_user_fd) + max_user_fd = fd; if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && n->ndup.dupfd == fd) { /* redirect from/to same file descriptor */ @@ -738,22 +740,17 @@ fdflagscmd(int argc, char *argv[]) parseflags(setflags, &pos, &neg); if (argc == 0) { - int maxfd; + int i; if (setflags) goto msg; /* - * XXX this has to go, maxfd might be 700 (or something) - * * XXX we should only ever operate on user defined fds * XXX not on sh internal fds that might be open. * XXX but for that we need to know their range (later) */ - maxfd = fcntl(0, F_MAXFD); - if (maxfd == -1) - error("Can't get max fd (%s)", strerror(errno)); - for (int i = 0; i <= maxfd; i++) + for (i = 0; i <= max_user_fd; i++) printone(i, 0, verbose, 1); return 0; } Index: src/bin/sh/redir.h diff -u src/bin/sh/redir.h:1.21 src/bin/sh/redir.h:1.22 --- src/bin/sh/redir.h:1.21 Thu May 12 13:31:37 2016 +++ src/bin/sh/redir.h Sat Apr 22 16:02:39 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: redir.h,v 1.21 2016/05/12 13:31:37 kre Exp $ */ +/* $NetBSD: redir.h,v 1.22 2017/04/22 16:02:39 kre Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -47,3 +47,5 @@ int fd0_redirected_p(void); void clearredir(int); int movefd(int, int); int to_upper_fd(int); + +int max_user_fd; /* highest fd used by user */