From 2498bc49cb112ab9c727d415d29b39bb6cc71cde Mon Sep 17 00:00:00 2001 From: Tom Deseyn Date: Sat, 18 Mar 2017 00:54:45 +0100 Subject: [PATCH] FileStream.Unix: open with CLOEXEC unless FileShare.Inheritable set (dotnet/coreclr#10251) * FileStream.Unix: open with CLOEXEC unless FileShare.Inheritable set * PR feedback Commit migrated from https://github.com/dotnet/coreclr/commit/f29a051ee1829937cde011ee28bbd17557d35e0b --- src/coreclr/src/mscorlib/shared/System/IO/FileStream.Unix.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/coreclr/src/mscorlib/shared/System/IO/FileStream.Unix.cs b/src/coreclr/src/mscorlib/shared/System/IO/FileStream.Unix.cs index ef186ff..7d860ac 100644 --- a/src/coreclr/src/mscorlib/shared/System/IO/FileStream.Unix.cs +++ b/src/coreclr/src/mscorlib/shared/System/IO/FileStream.Unix.cs @@ -45,7 +45,7 @@ namespace System.IO _asyncState = new AsyncState(); // Translate the arguments into arguments for an open call. - Interop.Sys.OpenFlags openFlags = PreOpenConfigurationFromOptions(mode, _access, options); // FileShare currently ignored + Interop.Sys.OpenFlags openFlags = PreOpenConfigurationFromOptions(mode, _access, share, options); // If the file gets created a new, we'll select the permissions for it. Most Unix utilities by default use 666 (read and // write for all), so we do the same (even though this doesn't match Windows, where by default it's possible to write out @@ -120,9 +120,10 @@ namespace System.IO /// Translates the FileMode, FileAccess, and FileOptions values into flags to be passed when opening the file. /// The FileMode provided to the stream's constructor. /// The FileAccess provided to the stream's constructor + /// The FileShare provided to the stream's constructor /// The FileOptions provided to the stream's constructor /// The flags value to be passed to the open system call. - private static Interop.Sys.OpenFlags PreOpenConfigurationFromOptions(FileMode mode, FileAccess access, FileOptions options) + private static Interop.Sys.OpenFlags PreOpenConfigurationFromOptions(FileMode mode, FileAccess access, FileShare share, FileOptions options) { // Translate FileMode. Most of the values map cleanly to one or more options for open. Interop.Sys.OpenFlags flags = default(Interop.Sys.OpenFlags); @@ -166,6 +167,12 @@ namespace System.IO break; } + // Handle Inheritable, other FileShare flags are handled by Init + if ((share & FileShare.Inheritable) == 0) + { + flags |= Interop.Sys.OpenFlags.O_CLOEXEC; + } + // Translate some FileOptions; some just aren't supported, and others will be handled after calling open. // - Asynchronous: Handled in ctor, setting _useAsync and SafeFileHandle.IsAsync to true // - DeleteOnClose: Doesn't have a Unix equivalent, but we approximate it in Dispose -- 2.7.4