From abb3587fe939d469a44f2097bae361aabd269f3f Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 5 Sep 2009 22:04:36 +0200 Subject: 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 --- libc/misc/dirent/opendir.c | 21 ++++++++++++--------- libc/misc/utmp/utent.c | 31 ++++++++++++++++++------------- 2 files changed, 30 insertions(+), 22 deletions(-) (limited to 'libc/misc') diff --git a/libc/misc/dirent/opendir.c b/libc/misc/dirent/opendir.c index fa07d0052..70b30658e 100644 --- a/libc/misc/dirent/opendir.c +++ b/libc/misc/dirent/opendir.c @@ -81,28 +81,31 @@ DIR *opendir(const char *name) } # define O_DIRECTORY 0 #endif - if ((fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY)) < 0) +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC); + 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" . - */ - if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { + if (fstat(fd, &statbuf) < 0) { int saved_errno; -close_and_ret: saved_errno = errno; close(fd); __set_errno(saved_errno); return NULL; } + /* According to POSIX, directory streams should be closed when + * exec. From "Anna Pluzhnikov" . + */ + if (O_CLOEXEC == 0) + fcntl(fd, F_SETFD, FD_CLOEXEC); + ptr = fd_to_DIR(fd, statbuf.st_blksize); if (!ptr) { close(fd); diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c index f0700af21..7d8a2d3c1 100644 --- a/libc/misc/utmp/utent.c +++ b/libc/misc/utmp/utent.c @@ -34,24 +34,29 @@ static const char *static_ut_name = default_file_name; static void __setutent(void) { if (static_fd < 0) { - static_fd = open(static_ut_name, O_RDWR); +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + static_fd = open(static_ut_name, O_RDWR | O_CLOEXEC); if (static_fd < 0) { - static_fd = open(static_ut_name, O_RDONLY); + static_fd = open(static_ut_name, O_RDONLY | O_CLOEXEC); if (static_fd < 0) { return; /* static_fd remains < 0 */ } } - /* Make sure the file will be closed on exec() */ - fcntl(static_fd, F_SETFD, FD_CLOEXEC); - // thus far, {G,S}ETFD only has this single flag, - // and setting it never fails. - //int ret = fcntl(static_fd, F_GETFD, 0); - //if (ret >= 0) { - // ret = fcntl(static_fd, F_SETFD, ret | FD_CLOEXEC); - //} - //if (ret < 0) { - // static_fd = -1; - //} + if (O_CLOEXEC == 0) { + /* Make sure the file will be closed on exec() */ + fcntl(static_fd, F_SETFD, FD_CLOEXEC); + // thus far, {G,S}ETFD only has this single flag, + // and setting it never fails. + //int ret = fcntl(static_fd, F_GETFD, 0); + //if (ret >= 0) { + // ret = fcntl(static_fd, F_SETFD, ret | FD_CLOEXEC); + //} + //if (ret < 0) { + // static_fd = -1; + //} + } return; } lseek(static_fd, 0, SEEK_SET); -- cgit v1.2.3