From 5bf9320f3be59ada7ffb316a3d90ea97f969ed38 Mon Sep 17 00:00:00 2001 From: Conrad Micallef Date: Fri, 24 Jun 2022 15:31:45 +0200 Subject: [PATCH 1/3] Changes to properly support Mutexes on NET6.0 Global Mutexes were being created without insufficient rights for a 2nd process running on same machine to use them --- LiteDB/Client/Shared/SharedEngine.cs | 41 ++++++++++++++++++++-------- LiteDB/LiteDB.csproj | 21 ++++++++------ 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/LiteDB/Client/Shared/SharedEngine.cs b/LiteDB/Client/Shared/SharedEngine.cs index 386679cec..25e0b153c 100644 --- a/LiteDB/Client/Shared/SharedEngine.cs +++ b/LiteDB/Client/Shared/SharedEngine.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Linq.Expressions; using System.Threading; -#if NETFRAMEWORK +#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER using System.Security.AccessControl; using System.Security.Principal; #endif @@ -27,16 +27,33 @@ public SharedEngine(EngineSettings settings) try { -#if NETFRAMEWORK - var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), +#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER +#if NET6_0_OR_GREATER + if (!OperatingSystem.IsWindows()) + _mutex = new Mutex(false, "Global\\" + name + ".Mutex"); + else + { +#endif + var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow); - var securitySettings = new MutexSecurity(); - securitySettings.AddAccessRule(allowEveryoneRule); - + var securitySettings = new MutexSecurity(); + securitySettings.AddAccessRule(allowEveryoneRule); +#if NET6_0_OR_GREATER + _mutex = MutexAcl.Create(false, "Global\\" + name + ".Mutex", out _, securitySettings); +#endif +#if NETFRAMEWORK _mutex = new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings); +#endif +#if NETSTANDARD2_0_OR_GREATER + _mutex = new Mutex(false, "Global\\" + name + ".Mutex"); + ThreadingAclExtensions.SetAccessControl(_mutex, securitySettings); +#endif #else _mutex = new Mutex(false, "Global\\" + name + ".Mutex"); +#endif +#if NET6_0_OR_GREATER + } #endif } catch (NotSupportedException ex) @@ -95,7 +112,7 @@ private void CloseDatabase() } } - #region Transaction Operations +#region Transaction Operations public bool BeginTrans() { @@ -147,9 +164,9 @@ public bool Rollback() } } - #endregion +#endregion - #region Read Operation +#region Read Operation public IBsonDataReader Query(string collection, Query query) { @@ -188,9 +205,9 @@ public bool Pragma(string name, BsonValue value) } } - #endregion +#endregion - #region Write Operations +#region Write Operations public int Checkpoint() { @@ -360,7 +377,7 @@ public bool EnsureIndex(string collection, string name, BsonExpression expressio } } - #endregion +#endregion public void Dispose() { diff --git a/LiteDB/LiteDB.csproj b/LiteDB/LiteDB.csproj index cbc9b0446..077c849bb 100644 --- a/LiteDB/LiteDB.csproj +++ b/LiteDB/LiteDB.csproj @@ -1,10 +1,10 @@  - net45;netstandard1.3;netstandard2.0 - 5.0.12 - 5.0.12 - 5.0.12 + netstandard1.3;netstandard2.0;net6.0 + 5.0.14 + 5.0.14 + 5.0.14 Maurício David LiteDB LiteDB - A lightweight embedded .NET NoSQL document store in a single datafile @@ -12,7 +12,7 @@ en-US LiteDB LiteDB - 5.0.12 + 5.0.14 database nosql embedded icon_64x64.png LICENSE @@ -25,9 +25,8 @@ 1.6.1 1701;1702;1705;1591;0618 bin\$(Configuration)\$(TargetFramework)\LiteDB.xml - true - LiteDB.snk true + True From 4a3ae5bd0e62cb779a2fcdb1826bacfc8589f297 Mon Sep 17 00:00:00 2001 From: Jonas Kamsker <11245306+JKamsker@users.noreply.github.com> Date: Sat, 8 Jun 2024 01:24:32 +0200 Subject: [PATCH 2/3] Improve readability of the mutex generating part --- LiteDB/Client/Shared/MutexGenerator.cs | 78 ++++++++++++++++++++++++++ LiteDB/Client/Shared/SharedEngine.cs | 31 +--------- LiteDB/LiteDB.csproj | 3 +- 3 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 LiteDB/Client/Shared/MutexGenerator.cs diff --git a/LiteDB/Client/Shared/MutexGenerator.cs b/LiteDB/Client/Shared/MutexGenerator.cs new file mode 100644 index 000000000..45adf3042 --- /dev/null +++ b/LiteDB/Client/Shared/MutexGenerator.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; + +using System.Text; +using System.Threading; +using System.Diagnostics; + +#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER +using System.Security.AccessControl; +using System.Security.Principal; +#endif + +namespace LiteDB.Client.Shared; + +internal static class MutexGenerator +{ +#if NET6_0_OR_GREATER + private static Mutex CreateMutexForNet6OrGreater(string name) + { + if (!OperatingSystem.IsWindows()) + { + return new Mutex(false, "Global\\" + name + ".Mutex"); + } + + var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), + MutexRights.FullControl, AccessControlType.Allow); + + var securitySettings = new MutexSecurity(); + securitySettings.AddAccessRule(allowEveryoneRule); + + return MutexAcl.Create(false, "Global\\" + name + ".Mutex", out _, securitySettings); + } +#endif + +#if NETSTANDARD2_0_OR_GREATER + private static Mutex CreateMutexForNetStandard(string name) + { + var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), + MutexRights.FullControl, AccessControlType.Allow); + + var securitySettings = new MutexSecurity(); + securitySettings.AddAccessRule(allowEveryoneRule); + + var mutex = new Mutex(false, "Global\\" + name + ".Mutex"); + ThreadingAclExtensions.SetAccessControl(mutex, securitySettings); + + return mutex; + } +#endif + +#if NETFRAMEWORK + private static Mutex CreateMutexForNetFramework(string name) + { + var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), + MutexRights.FullControl, AccessControlType.Allow); + + var securitySettings = new MutexSecurity(); + securitySettings.AddAccessRule(allowEveryoneRule); + + return new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings); + } +#endif + + public static Mutex CreateMutex(string name) + { +#if NET6_0_OR_GREATER + return CreateMutexForNet6OrGreater(name); +#endif +#if NETSTANDARD2_0_OR_GREATER + return CreateMutexForNetStandard(name); +#endif +#if NETFRAMEWORK + return CreateMutexForNetFramework(name); +#endif + + return new Mutex(false, "Global\\" + name + ".Mutex"); + } +} \ No newline at end of file diff --git a/LiteDB/Client/Shared/SharedEngine.cs b/LiteDB/Client/Shared/SharedEngine.cs index 25e0b153c..e3004df9c 100644 --- a/LiteDB/Client/Shared/SharedEngine.cs +++ b/LiteDB/Client/Shared/SharedEngine.cs @@ -5,6 +5,8 @@ using System.Linq; using System.Linq.Expressions; using System.Threading; +using LiteDB.Client.Shared; + #if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER using System.Security.AccessControl; using System.Security.Principal; @@ -27,34 +29,7 @@ public SharedEngine(EngineSettings settings) try { -#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER || NET6_0_OR_GREATER -#if NET6_0_OR_GREATER - if (!OperatingSystem.IsWindows()) - _mutex = new Mutex(false, "Global\\" + name + ".Mutex"); - else - { -#endif - var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), - MutexRights.FullControl, AccessControlType.Allow); - - var securitySettings = new MutexSecurity(); - securitySettings.AddAccessRule(allowEveryoneRule); -#if NET6_0_OR_GREATER - _mutex = MutexAcl.Create(false, "Global\\" + name + ".Mutex", out _, securitySettings); -#endif -#if NETFRAMEWORK - _mutex = new Mutex(false, "Global\\" + name + ".Mutex", out _, securitySettings); -#endif -#if NETSTANDARD2_0_OR_GREATER - _mutex = new Mutex(false, "Global\\" + name + ".Mutex"); - ThreadingAclExtensions.SetAccessControl(_mutex, securitySettings); -#endif -#else - _mutex = new Mutex(false, "Global\\" + name + ".Mutex"); -#endif -#if NET6_0_OR_GREATER - } -#endif + _mutex = MutexGenerator.CreateMutex(name); } catch (NotSupportedException ex) { diff --git a/LiteDB/LiteDB.csproj b/LiteDB/LiteDB.csproj index 077c849bb..59b23b5bd 100644 --- a/LiteDB/LiteDB.csproj +++ b/LiteDB/LiteDB.csproj @@ -1,7 +1,7 @@  - netstandard1.3;netstandard2.0;net6.0 + net45;netstandard1.3;netstandard2.0 5.0.14 5.0.14 5.0.14 @@ -27,6 +27,7 @@ bin\$(Configuration)\$(TargetFramework)\LiteDB.xml true True + latest