summaryrefslogtreecommitdiffstats
path: root/libc/misc/dirent/opendir.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-09-05 22:04:36 +0200
committerAustin Foxley <austinf@cetoncorp.com>2009-09-18 11:21:25 -0700
commit12b4c6b14c197397b47122c1a6c60e179d83376f (patch)
tree2a9021b36d03c76c55bfced8fbb221fa814dbf08 /libc/misc/dirent/opendir.c
parent2b5155d358188649cc7a6653b5968b514fbfe9cf (diff)
downloaduClibc-alpine-12b4c6b14c197397b47122c1a6c60e179d83376f.tar.bz2
uClibc-alpine-12b4c6b14c197397b47122c1a6c60e179d83376f.tar.xz
CLOEXEC: use open(CLOEXEC) if exist; do not check fcntl(FD_CLOEXEC) failure
text data bss dec hex filename - 370 0 0 370 172 libc/misc/dirent/opendir.o + 366 0 0 366 16e libc/misc/dirent/opendir.o - 375 4 0 379 17b libc/pwd_grp/lckpwdf.o + 356 4 0 360 168 libc/pwd_grp/lckpwdf.o - 248 0 0 248 f8 librt/shm.o + 209 0 0 209 d1 librt/shm.o Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com> Signed-off-by: Austin Foxley <austinf@cetoncorp.com>
Diffstat (limited to 'libc/misc/dirent/opendir.c')
-rw-r--r--libc/misc/dirent/opendir.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/libc/misc/dirent/opendir.c b/libc/misc/dirent/opendir.c
index 5f9d26e02..29e28b187 100644
--- a/libc/misc/dirent/opendir.c
+++ b/libc/misc/dirent/opendir.c
@@ -84,26 +84,25 @@ DIR *opendir(const char *name)
}
# define O_DIRECTORY 0
#endif
+
+#ifndef O_CLOEXEC
+# define O_CLOEXEC 0
+#endif
+
#ifdef __UCLIBC_HAS_THREADS_NATIVE__
- if ((fd = open_not_cancel_2(name, O_RDONLY|O_NDELAY|O_DIRECTORY)) < 0)
+ fd = open_not_cancel_2(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
#else
- if ((fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY)) < 0)
+ fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
#endif
+ if (fd < 0)
return NULL;
/* Note: we should check to make sure that between the stat() and open()
* call, 'name' didnt change on us, but that's only if O_DIRECTORY isnt
* defined and since Linux has supported it for like ever, i'm not going
* to worry about it right now (if ever). */
- if (fstat(fd, &statbuf) < 0)
- goto close_and_ret;
-
- /* According to POSIX, directory streams should be closed when
- * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
- */
- if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
+ if (fstat(fd, &statbuf) < 0) {
int saved_errno;
-close_and_ret:
saved_errno = errno;
#ifdef __UCLIBC_HAS_THREADS_NATIVE__
close_not_cancel_no_status(fd);
@@ -114,6 +113,12 @@ close_and_ret:
return NULL;
}
+ /* According to POSIX, directory streams should be closed when
+ * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
+ */
+ if (O_CLOEXEC == 0)
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
+
ptr = fd_to_DIR(fd, statbuf.st_blksize);
if (!ptr) {