/dev/fd/3

Robert Watson rwatson at freebsd.org
Sun Oct 24 10:49:46 PDT 2004


On Sun, 24 Oct 2004, Stephen Montgomery-Smith wrote:

> I have a program that needs to write to file descriptor 3 via /dev/fd/3. 
>   But FreeBSD-5.3 doesn't let me do that, since only fd/0-2 exist.  How
> do I do this?  "man fd" didn't help me. 

By default, /dev/fd is services by devfs, and is initialized by
fildesc_drvinit() in kern_descrip.c.  It creates a hard-coded three
entries:

        dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
        make_dev_alias(dev, "stdin");
        dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
        make_dev_alias(dev, "stdout");
        dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
        make_dev_alias(dev, "stderr");

You can mount fdescfs on top of /dev/fd using the following command as
root:

    mount -t fdescfs fdescfs /dev/fd

This will cause the number of entries in /dev/fd to correspond to the
number of file descriptors the current process has open.  I'm not sure if
there are caveats associated with that approach, but from a simple
experiment here it appears to work.

Robert N M Watson             FreeBSD Core Team, TrustedBSD Projects
robert at fledge.watson.org      Principal Research Scientist, McAfee Research



More information about the freebsd-stable mailing list