Browse Source

Merge pull request #32617 from Faless/fix/fopen_close_exec

Disable file descriptor sharing with subprocs.
Rémi Verschelde 5 years ago
parent
commit
ad685960da
1 changed files with 23 additions and 4 deletions
  1. 23 4
      drivers/unix/file_access_unix.cpp

+ 23 - 4
drivers/unix/file_access_unix.cpp

@@ -56,6 +56,12 @@
 #define S_ISREG(m) ((m)&S_IFREG)
 #define S_ISREG(m) ((m)&S_IFREG)
 #endif
 #endif
 
 
+#ifndef NO_FCNTL
+#include <fcntl.h>
+#else
+#include <sys/ioctl.h>
+#endif
+
 void FileAccessUnix::check_errors() const {
 void FileAccessUnix::check_errors() const {
 
 
 	ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
 	ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
@@ -123,11 +129,24 @@ Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) {
 			} break;
 			} break;
 		}
 		}
 		return last_error;
 		return last_error;
-	} else {
-		last_error = OK;
-		flags = p_mode_flags;
-		return OK;
 	}
 	}
+
+	// Set close on exec to avoid leaking it to subprocesses.
+	int fd = fileno(f);
+
+	if (fd != -1) {
+#if defined(NO_FCNTL)
+		unsigned long par = 0;
+		ioctl(fd, FIOCLEX, &par);
+#else
+		int opts = fcntl(fd, F_GETFD);
+		fcntl(fd, F_SETFD, opts | FD_CLOEXEC);
+#endif
+	}
+
+	last_error = OK;
+	flags = p_mode_flags;
+	return OK;
 }
 }
 
 
 void FileAccessUnix::close() {
 void FileAccessUnix::close() {