Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Mar 1996 10:23:22 -0800 (PST)
From:      Bruce Evans <bde>
To:        bugs@freebsd.org
Subject:   opendir() broken for special files
Message-ID:  <199603271823.KAA15122@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
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;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199603271823.KAA15122>