[comp.unix.wizards] A question on the implementation of /dev/stdin, etc.

akcs.kevin@wcbcs (Kevin Schoedel) (05/06/90)

This discussion has prompted me to try to implement /dev/fd/... for
System V 3.2 on a 386.  I don't have kernel source, so I can't use the
   case IFCHR:
      if( major( dev ) == fd_dev ){
          dup minor(dev)
          return;
      }
      (*cdevsw[ ....
technique that was mentioned here.  I don't want to use a file system
for /dev/fd/... because that would prevent me using the synonyms
/dev/stdin, etc.  So... is there any fundamental problem with having
a device, consisting only of an open routine, that gets rid of the
file descriptor just created for /dev/fd/N and then dup's N?
Specifically, I tried an open function containing, roughly,
   f = ...file descriptor for /dev/fd/...
   close( f )
   dup( minor( dev ) )
As I said, I don't have source, and my knowledge of the kernel implementation
is restricted to having read John Lions' book, and perhaps things have
changed a bit since version 6.  First, I tried looking in u.u_ar0[0] for
the file descriptor, but that was zero, which didn't seem right.
Then I tried searching u.u_ofile[] for a file whose inode was that of the
special file -- and sure enough, it was in u.u_ofile[0].  So now I'm
confused.  Am I doing something slightly wrong or fundamentally wrong?

-kevin

boyd@necisa.ho.necisa.oz (Boyd Roberts) (05/08/90)

In article <[2643142a:635.16]comp.unix.wizards;1@wcbcs> akcs.kevin@wcbcs (Kevin Schoedel) writes:
>This discussion has prompted me to try to implement /dev/fd/... for
>System V 3.2 on a 386.  I don't have kernel source, so I can't use the
>   case IFCHR:

Yes, do it as a device driver.  But...  you'll have to cheat!

Grody hack follows:

    fdopen(dev, mode)
    dev_t   dev;
    int	    mode;
    {
	/* u_rval1 should hold the allocated file descriptor -- gag! */

	closef(u.u_ofile[u.u_rval1]);

	if (u.u_error)
	    return;

	if (u.u_ofile[minor(dev)] != NULL)
	{
	    /* fake dup */
	    u.u_ofile[u.u_rval1] = u.u_ofile[minor(dev)];
	    u.u_ofile[minor(dev)]->f_count++;
	}
	else
	    u.u_error = EBADF;
    }


Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''

boyd@necisa.ho.necisa.oz (Boyd Roberts) (05/09/90)

In article <1706@necisa.ho.necisa.oz> (Boyd Roberts) writes:
>
>	closef(u.u_ofile[u.u_rval1]);

When I should have written:

	closef(u.u_ofile[u.u_rval1]);
	u.u_ofile[u.u_rval1] = NULL;		/* this completes the close */


Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''