diff options
Diffstat (limited to 'libc/misc')
-rw-r--r-- | libc/misc/dirent/opendir.c | 25 | ||||
-rw-r--r-- | libc/misc/utmp/utent.c | 39 |
2 files changed, 38 insertions, 26 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) { diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c index dc3110fe9..eca2ba14e 100644 --- a/libc/misc/utmp/utent.c +++ b/libc/misc/utmp/utent.c @@ -38,36 +38,43 @@ static const char *static_ut_name = default_file_name; static void __setutent(void) { if (static_fd < 0) { +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + #ifdef __UCLIBC_HAS_THREADS_NATIVE__ - static_fd = open(static_ut_name, O_RDWR); + static_fd = open_not_cancel_2(static_ut_name, O_RDWR | O_CLOEXEC); #else - static_fd = open_not_cancel_2(static_ut_name, O_RDWR); + static_fd = open(static_ut_name, O_RDWR | O_CLOEXEC); #endif if (static_fd < 0) { #ifdef __UCLIBC_HAS_THREADS_NATIVE__ - static_fd = open(static_ut_name, O_RDONLY); + static_fd = open_not_cancel_2(static_ut_name, O_RDONLY | O_CLOEXEC); #else - static_fd = open(static_ut_name, O_RDONLY); + static_fd = open(static_ut_name, O_RDONLY | O_CLOEXEC); #endif if (static_fd < 0) { return; /* static_fd remains < 0 */ } } - /* Make sure the file will be closed on exec() */ + + if (O_CLOEXEC == 0) { + /* Make sure the file will be closed on exec() */ #ifdef __UCLIBC_HAS_THREADS_NATIVE__ - fcntl_not_cancel(static_fd, F_SETFD, FD_CLOEXEC); + fcntl_not_cancel(static_fd, F_SETFD, FD_CLOEXEC); #else - fcntl(static_fd, F_SETFD, FD_CLOEXEC); + fcntl(static_fd, F_SETFD, FD_CLOEXEC); #endif - // 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; - //} + // 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); |