public unsafe Mutex(bool initiallyOwned, String name, out bool createdNew, MutexSecurity mutexSecurity) { if (name != null) { #if PLATFORM_UNIX throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_NamedSynchronizationPrimitives")); #else if (System.IO.Path.MaxPath < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name)); } #endif } Contract.EndContractBlock(); Win32Native.SECURITY_ATTRIBUTES secAttrs = null; #if FEATURE_MACL // For ACL's, get the security descriptor from the MutexSecurity. if (mutexSecurity != null) { secAttrs = new Win32Native.SECURITY_ATTRIBUTES(); secAttrs.nLength = (int)Marshal.SizeOf(secAttrs); byte[] sd = mutexSecurity.GetSecurityDescriptorBinaryForm(); byte* pSecDescriptor = stackalloc byte[sd.Length]; Buffer.Memcpy(pSecDescriptor, 0, sd, 0, sd.Length); secAttrs.pSecurityDescriptor = pSecDescriptor; } #endif CreateMutexWithGuaranteedCleanup(initiallyOwned, name, out createdNew, secAttrs); }
public unsafe Mutex(bool initiallyOwned, String name, out bool createdNew, MutexSecurity mutexSecurity) { if (name == string.Empty) { // Empty name is treated as an unnamed mutex. Set to null, and we will check for null from now on. name = null; } #if !PLATFORM_UNIX if (name != null && System.IO.Path.MaxPath < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", Path.MaxPath), "name"); } #endif Contract.EndContractBlock(); Win32Native.SECURITY_ATTRIBUTES secAttrs = null; #if FEATURE_MACL // For ACL's, get the security descriptor from the MutexSecurity. if (mutexSecurity != null) { secAttrs = new Win32Native.SECURITY_ATTRIBUTES(); secAttrs.nLength = (int)Marshal.SizeOf(secAttrs); byte[] sd = mutexSecurity.GetSecurityDescriptorBinaryForm(); byte* pSecDescriptor = stackalloc byte[sd.Length]; Buffer.Memcpy(pSecDescriptor, 0, sd, 0, sd.Length); secAttrs.pSecurityDescriptor = pSecDescriptor; } #endif CreateMutexWithGuaranteedCleanup(initiallyOwned, name, out createdNew, secAttrs); }
public unsafe Mutex(bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity) { if ((name != null) && (260 < name.Length)) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name })); } Win32Native.SECURITY_ATTRIBUTES secAttrs = null; if (mutexSecurity != null) { secAttrs = new Win32Native.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(secAttrs) }; byte[] securityDescriptorBinaryForm = mutexSecurity.GetSecurityDescriptorBinaryForm(); byte* pDest = stackalloc byte[1 * securityDescriptorBinaryForm.Length]; Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length); secAttrs.pSecurityDescriptor = pDest; } SafeWaitHandle mutexHandle = null; bool newMutex = false; RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.MutexCleanupCode); MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(mutexHandle, false); RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(delegate (object userData) { RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { if (initiallyOwned) { cleanupInfo.inCriticalRegion = true; Thread.BeginThreadAffinity(); Thread.BeginCriticalRegion(); } } int errorCode = 0; RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { errorCode = CreateMutexHandle(initiallyOwned, name, secAttrs, out mutexHandle); } if (mutexHandle.IsInvalid) { mutexHandle.SetHandleAsInvalid(); if (((name != null) && (name.Length != 0)) && (6 == errorCode)) { throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name })); } __Error.WinIOError(errorCode, name); } newMutex = errorCode != 0xb7; this.SetHandleInternal(mutexHandle); this.hasThreadAffinity = true; }, backoutCode, cleanupInfo); createdNew = newMutex; }
public unsafe Mutex(bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity) { if ((name != null) && (260 < name.Length)) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name })); } Win32Native.SECURITY_ATTRIBUTES structure = null; if (mutexSecurity != null) { structure = new Win32Native.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure) }; byte[] securityDescriptorBinaryForm = mutexSecurity.GetSecurityDescriptorBinaryForm(); byte* pDest = stackalloc byte[(IntPtr) securityDescriptorBinaryForm.Length]; Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length); structure.pSecurityDescriptor = pDest; } RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.MutexCleanupCode); MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(null, false); MutexTryCodeHelper helper = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, structure, this); RuntimeHelpers.TryCode code = new RuntimeHelpers.TryCode(helper.MutexTryCode); RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(code, backoutCode, cleanupInfo); createdNew = helper.m_newMutex; }
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 }
public unsafe Mutex(bool initiallyOwned, String name, out bool createdNew, MutexSecurity mutexSecurity) { if(null != name && System.IO.Path.MAX_PATH < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong",name)); } Contract.EndContractBlock(); Win32Native.SECURITY_ATTRIBUTES secAttrs = null; #if !FEATURE_PAL && FEATURE_MACL // For ACL's, get the security descriptor from the MutexSecurity. if (mutexSecurity != null) { secAttrs = new Win32Native.SECURITY_ATTRIBUTES(); secAttrs.nLength = (int)Marshal.SizeOf(secAttrs); byte[] sd = mutexSecurity.GetSecurityDescriptorBinaryForm(); byte* pSecDescriptor = stackalloc byte[sd.Length]; Buffer.memcpy(sd, 0, pSecDescriptor, 0, sd.Length); secAttrs.pSecurityDescriptor = pSecDescriptor; } #endif RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(MutexCleanupCode); MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(null, false); MutexTryCodeHelper tryCodeHelper = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, secAttrs, this); RuntimeHelpers.TryCode tryCode = new RuntimeHelpers.TryCode(tryCodeHelper.MutexTryCode); RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup( tryCode, cleanupCode, cleanupInfo); createdNew = tryCodeHelper.m_newMutex; }
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; } } } }