From owner-freebsd-bugs Wed Mar 27 10:23:24 1996 Return-Path: owner-bugs Received: (from root@localhost) by freefall.freebsd.org (8.7.3/8.7.3) id KAA15131 for bugs-outgoing; Wed, 27 Mar 1996 10:23:24 -0800 (PST) Received: (from bde@localhost) by freefall.freebsd.org (8.7.3/8.7.3) id KAA15122 for bugs@freebsd.org; Wed, 27 Mar 1996 10:23:22 -0800 (PST) Date: Wed, 27 Mar 1996 10:23:22 -0800 (PST) From: Bruce Evans Message-Id: <199603271823.KAA15122@freefall.freebsd.org> To: bugs@freebsd.org Subject: opendir() broken for special files Sender: owner-bugs@freebsd.org X-Loop: FreeBSD.org Precedence: bulk opendir() attempts to open files before [f]stat()'ing them. This causes problems for special files for which open() has side effects. Try something like `echo /dev/nrst0/*' or `echo /dev/*/*' with a shell that tries opendir() on non-directories, e.g., bash (bash is apparently trying to avoid a syscall for the case where the file is a directory). I don't see a better fix than stat()ing the file in opendir(). Bruce diff -c2 src/lib/libc/gen/opendir.c~ src/lib/libc/gen/opendir.c *** src/lib/libc/gen/opendir.c~ Sun Apr 16 20:22:58 1995 --- src/lib/libc/gen/opendir.c Wed Mar 27 17:40:22 1996 *************** *** 57,60 **** --- 57,70 ---- struct stat sb; + /* + * stat() before open() because opening of special files may be + * harmful. fstat() after open because the file may have changed. + */ + if (stat(name, &sb) != 0) + return NULL; + if (!S_ISDIR(sb.st_mode)) { + errno = ENOTDIR; + return NULL; + } if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1) return NULL;