From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13018 invoked from network); 10 May 2002 17:25:10 -0000 Received: from sunsite.dk (130.225.247.90) by ns1.primenet.com.au with SMTP; 10 May 2002 17:25:10 -0000 Received: (qmail 5344 invoked by alias); 10 May 2002 17:25:02 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 17120 Received: (qmail 5325 invoked from network); 10 May 2002 17:25:02 -0000 To: zsh-workers@sunsite.dk (Zsh hackers list) Subject: Re: File descriptor madness In-reply-to: "Bart Schaefer"'s message of "Thu, 09 May 2002 13:36:49 PDT." Date: Fri, 10 May 2002 18:24:32 +0100 Message-ID: <2536.1021051472@csr.com> From: Peter Stephenson Bart Schaefer wrote: > On Thu, 9 May 2002, Peter Stephenson wrote: > > > Apart from 0, which I suppose we need to test with open(), I had in mind > > just using dup() to dup 0 and see where the fd ended up. This would in > > one go show open fd's (avoided) and open new fd's to block them. Or > > doesn't that work? > > Just one dup() will only show you where the first hole in the sequence of > fd numbers is, not whether there are any more descriptors open above that. Yes, I thought of that. Here's an attempt. It seems to work, according to lsof. I can't think of a good test of whether it's necessary, though. Perhaps there are enough systems potentially using NIS or something similar that it's always reasonable. Index: Src/init.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/init.c,v retrieving revision 1.22 diff -u -r1.22 init.c --- Src/init.c 6 May 2002 14:50:13 -0000 1.22 +++ Src/init.c 10 May 2002 17:16:11 -0000 @@ -613,14 +613,11 @@ #endif struct timezone dummy_tz; char *ptr; -#ifdef HAVE_GETRLIMIT - int i; -#endif + int i, j; #if defined(SITEFPATH_DIR) || defined(FPATH_DIR) char **fpathptr; # if defined(FPATH_DIR) && defined(FPATH_SUBDIRS) char *fpath_subdirs[] = FPATH_SUBDIRS; - int j; # endif # ifdef SITEFPATH_DIR int fpathlen = 1; @@ -628,6 +625,43 @@ int fpathlen = 0; # endif #endif + int close_fds[10], tmppipe[2]; + + /* + * Workaround a problem with NIS (in one guise or another) which + * grabs file descriptors and keeps them for future reference. + * We don't want these to be in the range where the user can + * open fd's, i.e. 0 to 9 inclusive. So we make sure all + * fd's in that range are in use. + */ + memset(close_fds, 0, 10*sizeof(int)); + if (pipe(tmppipe) == 0) { + /* + * Strategy: Make sure we have at least fd 0 open (hence + * the pipe). From then on, keep dup'ing until we are + * up to 9. If we go over the top, close immediately, else + * mark for later closure. + */ + i = -1; /* max fd we have checked */ + while (i < 9) { + /* j is current fd */ + if (i < tmppipe[0]) + j = tmppipe[0]; + else if (i < tmppipe[1]) + j = tmppipe[1]; + else { + j = dup(0); + if (j == -1) + break; + } + if (j < 10) + close_fds[j] = 1; + else + close(j); + if (i < j) + i = j; + } + } addhookdefs(argzero, zshhooks, sizeof(zshhooks)/sizeof(*zshhooks)); @@ -813,6 +847,11 @@ } times(&shtms); + + /* Close the file descriptors we opened to block off 0 to 9 */ + for (i = 0; i < 10; i++) + if (close_fds[i]) + close(i); } /* Initialize signal handling */ -- Peter Stephenson Software Engineer CSR Ltd., Science Park, Milton Road, Cambridge, CB4 0WH, UK Tel: +44 (0)1223 392070 ********************************************************************** The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer. **********************************************************************