Open a directory with the usual flags

Use O_RDONLY since you are not supposed to write to a directory.

Use O_DIRECTORY as mandated by POSIX (The Open Group Base Specifications
Issue 7, 2018 edition IEEE Std 1003.1-2017):

"If the type DIR is implemented using a file descriptor, the descriptor
shall be obtained as if the O_DIRECTORY flag was passed to open()."

Use O_CLOEXEC as mandated by POSIX:

"When a file descriptor is used to implement the directory stream, it
behaves as if the FD_CLOEXEC had been set for the file descriptor."

Drop the fcntl() call in favour of O_CLOEXEC.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
This commit is contained in:
Sebastian Huber 2018-10-08 08:56:09 +02:00
parent d3d838cc26
commit 61fc64ed97
1 changed files with 4 additions and 9 deletions

View File

@ -49,17 +49,12 @@ static char sccsid[] = "@(#)opendir.c 5.11 (Berkeley) 2/23/91";
DIR *
opendir (const char *name)
{
register DIR *dirp;
register int fd;
int rc = 0;
DIR *dirp;
int fd;
if ((fd = open(name, 0)) == -1)
if ((fd = open(name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)) == -1)
return NULL;
#ifdef HAVE_FCNTL
rc = fcntl(fd, F_SETFD, 1);
#endif
if (rc == -1 ||
(dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
close (fd);
return NULL;
}