Re: [PATCH 2/6] Open a directory with the usual flags

Corinna Vinschen <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
On Oct  8 15:38, Sebastian Huber wrote:
> 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.

Yeah, that really makes sense, but what about targets not (yet)
implementing O_CLOEXEC???  I'm not sure how to handle this, if
we have to handle that at all.


Thoughts?
Corinna


> Signed-off-by: Sebastian Huber <[email protected]>
> ---
>  newlib/libc/posix/opendir.c | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/newlib/libc/posix/opendir.c b/newlib/libc/posix/opendir.c
> index 1416f1053..650cbfe8d 100644
> --- a/newlib/libc/posix/opendir.c
> +++ b/newlib/libc/posix/opendir.c
> @@ -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;
>  	}
> -- 
> 2.16.4

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEoVYPmneWZnwT6kwF9TYGna5ET6AFAlu9y1oACgkQ9TYGna5E
T6AQew//Zp62yTLcILKeUhXc8lccp5/DysM8Q11sd+yYF7L07VVrInXaWORR6c15
+ABY2Z0k+n2LRGZrtUXgE3qUcL95ZIxoda66RVEeHj7jv5yW33auykVrGBm8QrUA
Xa/Nrne6R2YRWrSZFbu47bAyZrNLblK2st/D2WWcPjb4Z+s/dMno/961pVMejIZs
9123d2sVCiyM1rM2VskX1m6EhVFrvzxz3vIGL2sZqVTwD3AlAgmYoDmMr8FEHUmF
jkergQ7lNyetCtdGSRqKZBTLGGU3OFdVINHjtOyTALm877bn3Fo4miLrwo4AWXG2
eynSfgW0zzCO6RMKH4j72MVER/f2fz1b+o4zceH5e+aGMQ1YSVpwACURM8YKuGTf
zQhor8lXsgzwMhyN+/0a2lDLL//fPz1Vnu1V6dc7drThF072p+z6K7i+z7Txst3V
fEMZPg4IYHAJcXgVFBlj+LsNCa2VDJ8vu1/TYnxq/TQlbPnTzisWs5oxKhPffKtG
oF5KPWViG8LJv1PhVc6yC5yWvE2YlnsvJmzzI/65HnAGjP2S5P1mpg49KDMx0jTq
ATQTVyzGNhdWi78Wnnc/Bj63DtlfeLjNb5XBriIoQZUfJdhJww4knWvH8+tlzieV
38WRruDTRkQ2hWyfrARkKUP1lV9kTeewwq05joUiZC/7P+9vtTg=
=xoSS
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.