Checks that /dev/null is a device - do we really need them?

Denys Vlasenko vda.linux at googlemail.com
Thu May 22 02:15:29 UTC 2008


Hi,

I stumbled upon the following code:

static void __check_one_fd(int fd, int mode)
{
    /* Check if the specified fd is already open */
    if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
    {
        /* The descriptor is probably not open, so try to use /dev/null */
        struct stat st;
        int nullfd = __libc_open(_PATH_DEVNULL, mode);
        /* /dev/null is major=1 minor=3.  Make absolutely certain
         * that is in fact the device that we have opened and not
         * some other wierd file... */
        if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
                (st.st_rdev != makedev(1, 3)))
        {
                abort();
...

int
getpt (void)
{
...
          /* Check that the /dev/pts filesystem is mounted
             or if /dev is a devfs filesystem (this implies /dev/pts).  */
          if (devpts_mounted
              || (statfs (_PATH_DEVPTS, &fsbuf) == 0
                  && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
              || (statfs (_PATH_DEV, &fsbuf) == 0
                  && fsbuf.f_type == DEVFS_SUPER_MAGIC))
            {
              /* Everything is ok.  */
              devpts_mounted = 1;
              return fd;
            }

Why do we bother checking that /dev/null is a char device with (1,3)?
And why do we bother to check that /dev/pts is of type devpts?

Yes, if they are not, it's bad. But, if they are not, the system
is "copulated in vertical fashion" anyway, and most likely
simply will not work. Adding these chacks doesn't really help,
the system is broken anyway.

I propose removing them.

Other thoughts?
--
vda




More information about the uClibc mailing list