Re: file descritor 255 is always associated with /dev/tty[?] ?

Jim Nance (jnance@nortelnetworks.com)
Tue, 26 Oct 1999 11:17:37 -0400


On Tue, Oct 26, 1999 at 03:11:25PM +0100, Alan Cox wrote:
> > the process i.e /dev/tty[?] (current->files->fd[255] is not NULL). This is
> > the case with the shell (bash) process and since other processes are
> > forked by shell, this fd is inherited. I looked at the kernel sources and
> > bash source to see who sets this fd 255 but could not find. Can someone
> > please mail me (or give links to references) why this is set, who sets it,
> > and when?
>
> bash sets it, always. Not sure why

I remember running into this a couple of years ago. I _THINK_ what I remember
finding out is that bash needed a file descriptor for its own internal use
for executing commands like "source myfile.sh". The use of FD 255 was an
attempt to get hold of a file descriptor that was not likely to collide with
file descriptors that a user might be using in their scripts. I dont remember
the exact details. Here is the code from jobs.c of the bash 1.14.7 sources:

/* Make sure that we are using the new line discipline. */

/* Get our controlling terminal. If job_control is set, or
interactive is set, then this is an interactive shell no
matter what. */
shell_tty = dup (fileno (stderr));

/* Find the highest unused file descriptor we can. */
{
int ignore, nds = getdtablesize ();

if (nds <= 0)
nds = 20;
else if (nds > 256)
nds = 256;

while (--nds > 3)
{
if (fcntl (nds, F_GETFD, &ignore) == -1)
break;
}

Jim

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/