internal static unsafe void AcquireReservedMutex(ref bool bHandleObtained) { SafeWaitHandle handle = null; bHandleObtained = false; if (Environment.IsW2k3) { if (s_ReservedMutex == null) { Win32Native.SECURITY_ATTRIBUTES security_attributes; MutexSecurity security = new MutexSecurity(); SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.WorldSid, null); security.AddAccessRule(new MutexAccessRule(identity, MutexRights.FullControl, AccessControlType.Allow)); security_attributes = new Win32Native.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(security_attributes) }; byte[] securityDescriptorBinaryForm = security.GetSecurityDescriptorBinaryForm(); byte * pDest = stackalloc byte[(IntPtr)securityDescriptorBinaryForm.Length]; Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length); security_attributes.pSecurityDescriptor = pDest; RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { handle = Win32Native.CreateMutex(security_attributes, false, @"Global\CLR_RESERVED_MUTEX_NAME"); handle.SetAsReservedMutex(); } int errorCode = Marshal.GetLastWin32Error(); if (handle.IsInvalid) { handle.SetHandleAsInvalid(); __Error.WinIOError(errorCode, @"Global\CLR_RESERVED_MUTEX_NAME"); } Mutex mutex = new Mutex(handle); Interlocked.CompareExchange <Mutex>(ref s_ReservedMutex, mutex, null); } RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { try { s_ReservedMutex.WaitOne(); bHandleObtained = true; } catch (AbandonedMutexException) { bHandleObtained = true; } } } }
private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { bool bHandleObtained = false; bool flag2 = false; Label_0004: mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); int num = Marshal.GetLastWin32Error(); if (mutexHandle.IsInvalid && (num == 5)) { RuntimeHelpers.PrepareConstrainedRegions(); try { try { } finally { Thread.BeginThreadAffinity(); flag2 = true; } AcquireReservedMutex(ref bHandleObtained); mutexHandle = Win32Native.OpenMutex(0x100001, false, name); if (!mutexHandle.IsInvalid) { num = 0xb7; } else { num = Marshal.GetLastWin32Error(); } } finally { if (bHandleObtained) { ReleaseReservedMutex(); } if (flag2) { Thread.EndThreadAffinity(); } } switch (num) { case 2: goto Label_0004; case 0: return(0xb7); } } return(num); }
private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { bool flag = false; int num; do { mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); num = Marshal.GetLastWin32Error(); if (!mutexHandle.IsInvalid || num != 5) { return(num); } RuntimeHelpers.PrepareConstrainedRegions(); try { try { } finally { Thread.BeginThreadAffinity(); flag = true; } mutexHandle = Win32Native.OpenMutex(1048577, false, name); if (!mutexHandle.IsInvalid) { num = 183; } else { num = Marshal.GetLastWin32Error(); } } finally { if (flag) { Thread.EndThreadAffinity(); } } }while (num == 2); if (num == 0) { num = 183; } return(num); }
static int CreateMutexHandle(bool initiallyOwned, String name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { int errorCode; while (true) { mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); errorCode = Marshal.GetLastWin32Error(); if (!mutexHandle.IsInvalid) { break; } if (errorCode != Win32Native.ERROR_ACCESS_DENIED) { break; } // If a mutex with the name already exists, OS will try to open it with FullAccess. // It might fail if we don't have enough access. In that case, we try to open the mutex will modify and synchronize access. RuntimeHelpers.PrepareConstrainedRegions(); mutexHandle = Win32Native.OpenMutex( Win32Native.MUTEX_MODIFY_STATE | Win32Native.SYNCHRONIZE, false, name); errorCode = !mutexHandle.IsInvalid ? Win32Native.ERROR_ALREADY_EXISTS : Marshal.GetLastWin32Error(); // There could be a race condition here, the other owner of the mutex can free the mutex, // We need to retry creation in that case. if (errorCode != Win32Native.ERROR_FILE_NOT_FOUND) { if (errorCode == Win32Native.ERROR_SUCCESS) { errorCode = Win32Native.ERROR_ALREADY_EXISTS; } break; } } return(errorCode); }
internal static unsafe void AcquireReservedMutex(ref bool bHandleObtained) { #if FEATURE_MACL SafeWaitHandle mutexHandle = null; int errorCode; bHandleObtained = false; if (!Environment.IsW2k3) { return; } if (s_ReservedMutex == null) { // Create a maximally-permissive security descriptor, to ensure we never get an // ACCESS_DENIED error when calling CreateMutex MutexSecurity sec = new MutexSecurity(); SecurityIdentifier everyoneSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); sec.AddAccessRule(new MutexAccessRule(everyoneSid, MutexRights.FullControl, AccessControlType.Allow)); // For ACL's, get the security descriptor from the MutexSecurity. Win32Native.SECURITY_ATTRIBUTES secAttrs = new Win32Native.SECURITY_ATTRIBUTES(); secAttrs.nLength = (int)Marshal.SizeOf(secAttrs); byte[] sd = sec.GetSecurityDescriptorBinaryForm(); byte * bytesOnStack = stackalloc byte[sd.Length]; Buffer.Memcpy(bytesOnStack, 0, sd, 0, sd.Length); secAttrs.pSecurityDescriptor = bytesOnStack; RuntimeHelpers.PrepareConstrainedRegions(); try {} finally { mutexHandle = Win32Native.CreateMutex(secAttrs, false, c_ReservedMutexName); // need to set specially, since this mutex cannot lock on itself while closing itself. mutexHandle.SetAsReservedMutex(); } errorCode = Marshal.GetLastWin32Error(); if (mutexHandle.IsInvalid) { mutexHandle.SetHandleAsInvalid(); __Error.WinIOError(errorCode, c_ReservedMutexName); } Mutex m = new Mutex(mutexHandle); Interlocked.CompareExchange(ref s_ReservedMutex, m, null); } RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { try { s_ReservedMutex.WaitOne(); bHandleObtained = true; } catch (AbandonedMutexException) { // we don't care if another process holding the Mutex was killed bHandleObtained = true; } } #else bHandleObtained = true; #endif }
static int CreateMutexHandle(bool initiallyOwned, String name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { int errorCode; bool fReservedMutexObtained = false; bool fAffinity = false; while (true) { mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); errorCode = Marshal.GetLastWin32Error(); if (!mutexHandle.IsInvalid) { break; } if (errorCode == Win32Native.ERROR_ACCESS_DENIED) { // If a mutex with the name already exists, OS will try to open it with FullAccess. // It might fail if we don't have enough access. In that case, we try to open the mutex will modify and synchronize access. // RuntimeHelpers.PrepareConstrainedRegions(); try { try { } finally { #if !FEATURE_CORECLR Thread.BeginThreadAffinity(); #endif fAffinity = true; } AcquireReservedMutex(ref fReservedMutexObtained); mutexHandle = Win32Native.OpenMutex(Win32Native.MUTEX_MODIFY_STATE | Win32Native.SYNCHRONIZE, false, name); if (!mutexHandle.IsInvalid) { errorCode = Win32Native.ERROR_ALREADY_EXISTS; } else { errorCode = Marshal.GetLastWin32Error(); } } finally { if (fReservedMutexObtained) { ReleaseReservedMutex(); } if (fAffinity) { #if !FEATURE_CORECLR Thread.EndThreadAffinity(); #endif } } // There could be a ---- here, the other owner of the mutex can free the mutex, // We need to retry creation in that case. if (errorCode != Win32Native.ERROR_FILE_NOT_FOUND) { if (errorCode == Win32Native.ERROR_SUCCESS) { errorCode = Win32Native.ERROR_ALREADY_EXISTS; } break; } } else { break; } } return(errorCode); }
private static int CreateMutexHandle(bool initiallyOwned, string name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) { bool flag = false; bool flag2 = false; bool flag3 = false; Label_0006: flag2 = false; flag3 = false; mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name); int num = Marshal.GetLastWin32Error(); if (!mutexHandle.IsInvalid || (num != 5)) { return(num); } RuntimeHelpers.PrepareConstrainedRegions(); try { RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { Thread.BeginThreadAffinity(); flag = true; } mutexHandle = Win32Native.OpenMutex(0x100001, false, name); if (!mutexHandle.IsInvalid) { num = 0xb7; if (Environment.IsW2k3) { SafeWaitHandle handle = Win32Native.OpenMutex(0x100001, false, name); if (!handle.IsInvalid) { RuntimeHelpers.PrepareConstrainedRegions(); try { uint num2 = 0; IntPtr ptr = mutexHandle.DangerousGetHandle(); IntPtr ptr2 = handle.DangerousGetHandle(); IntPtr[] handles = new IntPtr[] { ptr, ptr2 }; num2 = Win32Native.WaitForMultipleObjects(2, handles, true, 0); GC.KeepAlive(handles); if (num2 == uint.MaxValue) { if (Marshal.GetLastWin32Error() != 0x57) { mutexHandle.Dispose(); flag3 = true; } } else { flag2 = true; if ((num2 >= 0) && (num2 < 2)) { Win32Native.ReleaseMutex(mutexHandle); Win32Native.ReleaseMutex(handle); } else if ((num2 >= 0x80) && (num2 < 130)) { Win32Native.ReleaseMutex(mutexHandle); Win32Native.ReleaseMutex(handle); } mutexHandle.Dispose(); } goto Label_0166; } finally { handle.Dispose(); } } mutexHandle.Dispose(); flag3 = true; } } else { num = Marshal.GetLastWin32Error(); } } finally { if (flag) { Thread.EndThreadAffinity(); } } Label_0166: if ((flag2 || flag3) || (num == 2)) { goto Label_0006; } if (num == 0) { num = 0xb7; } return(num); }