From 2c7dc98337086ab3b17a8e0c96a2826f038e7b37 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 13 Apr 2019 12:22:46 -0700 Subject: [PATCH] Revert "fcntl a little less" This reverts commits: e5362a4ae57addc82e052dd8ff3040afd38dbe8a. dd9a26715dc212a02c8aa8637067f7585c3f12e4. These commits were incorrect because they stomped other flags, such as O_NONBLOCK. --- src/fish.cpp | 7 +++---- src/wutil.cpp | 13 ++++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/fish.cpp b/src/fish.cpp index bf4b1006b..3e25c5bd6 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -441,14 +441,13 @@ int main(int argc, char **argv) { res = reader_read(STDIN_FILENO, {}); } else { char *file = *(argv + (my_optind++)); -#if defined(O_CLOEXEC) - int fd = open(file, O_RDONLY | O_CLOEXEC); -#else int fd = open(file, O_RDONLY); -#endif if (fd == -1) { perror(file); } else { + // OK to not do this atomically since we cannot have gone multithreaded yet. + set_cloexec(fd); + wcstring_list_t list; for (char **ptr = argv + my_optind; *ptr; ptr++) { list.push_back(str2wcstring(*ptr)); diff --git a/src/wutil.cpp b/src/wutil.cpp index 98a49251a..8ccb9362e 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -176,9 +176,16 @@ FILE *wfopen(const wcstring &path, const char *mode) { } bool set_cloexec(int fd) { - int flags = fcntl(fd, F_SETFD, FD_CLOEXEC); - if (flags == -1) return false; - return true; + // Note we don't want to overwrite existing flags like O_NONBLOCK which may be set. So fetch the + // existing flags and OR in our new one. + int flags = fcntl(fd, F_GETFD, 0); + if (flags < 0) { + return false; + } + if (flags & FD_CLOEXEC) { + return true; + } + return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0; } static int wopen_internal(const wcstring &pathname, int flags, mode_t mode, bool cloexec) {