From b8fa8fdcac75d055059ac2f6d31292e419c8c98c Mon Sep 17 00:00:00 2001 From: pastdue <30942300+past-due@users.noreply.github.com> Date: Sat, 30 Jan 2021 13:44:14 -0500 Subject: [PATCH] physfs_platform_posix.c: Use O_CLOEXEC / FD_CLOEXEC --- src/physfs_platform_posix.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/physfs_platform_posix.c b/src/physfs_platform_posix.c index 0125953..24ff594 100644 --- a/src/physfs_platform_posix.c +++ b/src/physfs_platform_posix.c @@ -160,16 +160,30 @@ static void *doOpen(const char *filename, int mode) const int appending = (mode & O_APPEND); int fd; int *retval; + int flags; + flags = -1; errno = 0; /* O_APPEND doesn't actually behave as we'd like. */ mode &= ~O_APPEND; + +#ifdef O_CLOEXEC + /* Add O_CLOEXEC if defined */ + mode |= O_CLOEXEC; +#endif do { fd = open(filename, mode, S_IRUSR | S_IWUSR); } while ((fd < 0) && (errno == EINTR)); BAIL_IF(fd < 0, errcodeFromErrno(), NULL); +#if !defined(O_CLOEXEC) && defined(FD_CLOEXEC) + flags = fcntl(fd, F_GETFD); + if (flags != -1) { + fcntl(fd, F_SETFD, flags | FD_CLOEXEC); + } +#endif + if (appending) { if (lseek(fd, 0, SEEK_END) < 0)