physfs_platform_posix.c: Use O_CLOEXEC / FD_CLOEXEC

This commit is contained in:
pastdue 2021-01-30 13:44:14 -05:00 committed by Ryan C. Gordon
parent a9cb20772b
commit b8fa8fdcac
1 changed files with 14 additions and 0 deletions

View File

@ -160,16 +160,30 @@ static void *doOpen(const char *filename, int mode)
const int appending = (mode & O_APPEND); const int appending = (mode & O_APPEND);
int fd; int fd;
int *retval; int *retval;
int flags;
flags = -1;
errno = 0; errno = 0;
/* O_APPEND doesn't actually behave as we'd like. */ /* O_APPEND doesn't actually behave as we'd like. */
mode &= ~O_APPEND; mode &= ~O_APPEND;
#ifdef O_CLOEXEC
/* Add O_CLOEXEC if defined */
mode |= O_CLOEXEC;
#endif
do { do {
fd = open(filename, mode, S_IRUSR | S_IWUSR); fd = open(filename, mode, S_IRUSR | S_IWUSR);
} while ((fd < 0) && (errno == EINTR)); } while ((fd < 0) && (errno == EINTR));
BAIL_IF(fd < 0, errcodeFromErrno(), NULL); 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 (appending)
{ {
if (lseek(fd, 0, SEEK_END) < 0) if (lseek(fd, 0, SEEK_END) < 0)