Open fstream files in O_CLOEXEC mode when possible.
Reviewers: EricWF, mclow.lists, ldionne
Reviewed By: ldionne
Subscribers: smeenai, dexonsmith, christof, ldionne, libcxx-commits
Tags: #libc
Differential Revision: https://reviews.llvm.org/D59839
llvm-svn: 372027
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: a7e9059967c4bff83ae6c3b595828bbbc8aaf82e
diff --git a/include/__config b/include/__config
index bfbedf6..17e5e04 100644
--- a/include/__config
+++ b/include/__config
@@ -1447,6 +1447,17 @@
#define _LIBCPP_UNUSED_VAR(x) ((void)(x))
+// Configures the fopen close-on-exec mode character, if any. This string will
+// be appended to any mode string used by fstream for fopen/fdopen.
+//
+// Not all platforms support this, but it helps avoid fd-leaks on platforms that
+// do.
+#if defined(__BIONIC__)
+# define _LIBCPP_FOPEN_CLOEXEC_MODE "e"
+#else
+# define _LIBCPP_FOPEN_CLOEXEC_MODE
+#endif
+
#endif // __cplusplus
#endif // _LIBCPP_CONFIG