Jelajahi Sumber

physfs_platform_posix.c: Use O_CLOEXEC / FD_CLOEXEC

pastdue 5 tahun lalu
induk
melakukan
543a1ae037
1 mengubah file dengan 14 tambahan dan 0 penghapusan
  1. 14 0
      src/physfs_platform_posix.c

+ 14 - 0
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)