This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add EventWaitHandle creation extension method that takes an ACL #42213
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7573706
Add EventWaitHandle creation extension method that takes an ACL
carlossanlop 9b0c0c0
Call OpenExisting, add basic unit tests, move MAX_PATH in csproj for …
carlossanlop 2984404
Address suggestions: using for the created handle, simplify try final…
carlossanlop ff6391e
simplify using, reorganize PInvokes a bit, let VS format resx as it e…
carlossanlop b518de9
Add more unit tests
carlossanlop 240bbc7
Save basic access rights in a constant, generate name randomly
carlossanlop ce079a5
Use FullControl (EVENT_ALL_ACCESS); update unit tests to use EventWai…
carlossanlop e0cf7cc
Apply suggestion of creating an EWH, then replacing its SWH.
carlossanlop 09f5a7b
Remove using causing chaos, add more unit tests, remove null string u…
carlossanlop 7fb95e3
spacing
carlossanlop 66b1d7a
Address comments
carlossanlop 42adc30
Remove unnecessary checks/exceptions, update unit tests
carlossanlop 6b6daa2
Fix netfx x86 ut failure - different exception for mode validation.
carlossanlop 166da5c
Small documentation fix
carlossanlop f49035e
Remove documentation for exception thrown with null security
carlossanlop 0b0a5b6
more documentation fixes
carlossanlop aba8320
New file for unit tests
carlossanlop 95af0e1
Dispose and remove windows only attributes in tests
carlossanlop be24175
suggestion to not use var
carlossanlop 0e4eb4c
Remove duplicate resx after merge
carlossanlop 3d62b9a
Readd cs files lost during merge
carlossanlop 0cf8663
Remove resx modification since it was untouched for this PR
carlossanlop bd2680f
Address test comments
carlossanlop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/System.Threading.AccessControl/src/System/Threading/EventWaitHandleAcl.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Security.AccessControl; | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
namespace System.Threading | ||
{ | ||
public static class EventWaitHandleAcl | ||
{ | ||
/// <summary>Gets or creates an <see cref="EventWaitHandle" /> instance, allowing a <see cref="EventWaitHandleSecurity " /> instance to be optionally specified to set it during the event creation.</summary> | ||
/// <param name="initialState"><see langword="true" /> to set the initial state to signaled if the named event is created as a result of this call; <see langword="false" /> to set it to non-signaled.</param> | ||
/// <param name="mode">One of the enum values that determines whether the event resets automatically or manually.</param> | ||
/// <param name="name">The name, if the event is a system-wide synchronization event; otherwise, <see langword="null" /> or an empty string.</param> | ||
/// <param name="createdNew">When this method returns, this argument is always set to <see langword="true" /> if a local event is created; that is, when <paramref name="name" /> is <see langword="null" /> or <see cref="string.Empty" />. If <paramref name="name" /> has a valid, non-empty value, this argument is set to <see langword="true" /> when the system event is created, or it is set to <see langword="false" /> if an existing system event is found with that name. This parameter is passed uninitialized.</param> | ||
/// <param name="eventSecurity">The optional Windows access control security to apply.</param> | ||
/// <returns>An object that represents a system event wait handle, if named, or a local event wait handle, if nameless.</returns> | ||
/// <exception cref="ArgumentNullException">.NET Framework only: The <paramref name="name" /> length is beyond MAX_PATH (260 characters).</exception> | ||
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="mode" /> enum value was out of legal range.</exception> | ||
/// <exception cref="DirectoryNotFoundException">Could not find a part of the path specified in <paramref name="name" />.</exception> | ||
/// <exception cref="WaitHandleCannotBeOpenedException">A system-wide synchronization event with the provided <paramref name="name" /> was not found. | ||
/// -or- | ||
/// An <see cref="EventWaitHandle" /> with system-wide name <paramref name="name" /> cannot be created. An <see cref="EventWaitHandle" /> of a different type might have the same name.</exception> | ||
/// <remarks>If a `name` is passed and the system event already exists, the existing event is returned. If `name` is `null` or <see cref="string.Empty" />, a new local event is always created.</remarks> | ||
public static unsafe EventWaitHandle Create(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity) | ||
{ | ||
if (eventSecurity == null) | ||
carlossanlop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return new EventWaitHandle(initialState, mode, name, out createdNew); | ||
} | ||
|
||
if (mode != EventResetMode.AutoReset && mode != EventResetMode.ManualReset) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(mode)); | ||
} | ||
|
||
uint eventFlags = initialState ? Interop.Kernel32.CREATE_EVENT_INITIAL_SET : 0; | ||
if (mode == EventResetMode.ManualReset) | ||
{ | ||
eventFlags |= Interop.Kernel32.CREATE_EVENT_MANUAL_RESET; | ||
} | ||
|
||
fixed (byte* pSecurityDescriptor = eventSecurity.GetSecurityDescriptorBinaryForm()) | ||
{ | ||
var secAttrs = new Interop.Kernel32.SECURITY_ATTRIBUTES | ||
{ | ||
nLength = (uint)sizeof(Interop.Kernel32.SECURITY_ATTRIBUTES), | ||
lpSecurityDescriptor = (IntPtr)pSecurityDescriptor | ||
}; | ||
|
||
SafeWaitHandle handle = Interop.Kernel32.CreateEventEx( | ||
(IntPtr)(&secAttrs), | ||
name, | ||
eventFlags, | ||
(uint)EventWaitHandleRights.FullControl); | ||
|
||
ValidateHandle(handle, name, out createdNew); | ||
|
||
EventWaitHandle ewh = new EventWaitHandle(initialState, mode); | ||
SafeWaitHandle old = ewh.SafeWaitHandle; | ||
ewh.SafeWaitHandle = handle; | ||
old.Dispose(); | ||
|
||
return ewh; | ||
} | ||
} | ||
|
||
private static void ValidateHandle(SafeWaitHandle handle, string name, out bool createdNew) | ||
{ | ||
int errorCode = Marshal.GetLastWin32Error(); | ||
|
||
if (handle.IsInvalid) | ||
{ | ||
handle.SetHandleAsInvalid(); | ||
|
||
if (!string.IsNullOrEmpty(name) && errorCode == Interop.Errors.ERROR_INVALID_HANDLE) | ||
throw new WaitHandleCannotBeOpenedException(SR.Format(SR.WaitHandleCannotBeOpenedException_InvalidHandle, name)); | ||
|
||
throw Win32Marshal.GetExceptionForWin32Error(errorCode, name); | ||
} | ||
|
||
createdNew = (errorCode != Interop.Errors.ERROR_ALREADY_EXISTS); | ||
carlossanlop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/System.Threading.AccessControl/src/System/Threading/EventWaitHandleAcl.net46.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Security.AccessControl; | ||
|
||
namespace System.Threading | ||
{ | ||
public static class EventWaitHandleAcl | ||
{ | ||
public static EventWaitHandle Create( | ||
bool initialState, | ||
EventResetMode mode, | ||
string name, | ||
out bool createdNew, | ||
EventWaitHandleSecurity eventSecurity) | ||
{ | ||
return new EventWaitHandle(initialState, mode, name, out createdNew, eventSecurity); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.