diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-01-02 13:59:41 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-02 13:59:41 -0600 |
| commit | 23c9d2fda9125f8bc0b35810521f2d53eead6be6 (patch) | |
| tree | 8fc15093a36672e77dfa4910bd7beb28d63d7b28 /src/slskd/Program.cs | |
| parent | bf65e2c278d3362f46a5aea3d1beedfe2488c718 (diff) | |
| parent | 7832e867ee393dd8e00ff3c3c83e272185597d7d (diff) | |
| download | slskd-23c9d2fda9125f8bc0b35810521f2d53eead6be6.tar.gz | |
Merge pull request #820 from slskd/mutex
Refactor single-instance mutex to use a more specific handle
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 50 |
1 files changed, 15 insertions, 35 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 9cae6e02..d0b670a2 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -214,6 +214,7 @@ namespace slskd private static IConfigurationRoot Configuration { get; set; } private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); private static ILogger Log { get; set; } = new ConsoleWriteLineLogger(); + private static Mutex Mutex { get; } = new Mutex(initiallyOwned: true, Compute.Sha256Hash(AppName)); [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] private static bool GenerateCertificate { get; set; } @@ -271,50 +272,30 @@ namespace slskd return; } - var reqInitialOwnership = false; - - using (var mutex = new Mutex(reqInitialOwnership, AppName)) + if (GenerateCertificate) { - var hasHandle = false; + var (filename, password) = GenerateX509Certificate(password: Cryptography.Random.GetBytes(16).ToBase62(), filename: $"{AppName}.pfx"); - try - { - try - { - if (!mutex.WaitOne(0, false)) - { - Console.WriteLine("Another instance of this program is running"); - Environment.Exit(0); - } - } - catch (AbandonedMutexException) - { - hasHandle = true; - } - } - finally - { - if (hasHandle) - { - mutex.ReleaseMutex(); - } - } + Log.Information($"Certificate exported to {filename}"); + Log.Information($"Password: {password}"); + return; } - if (GenerateCertificate) + if (GenerateSecret > 0) { - GenerateX509Certificate(password: Cryptography.Random.GetBytes(16).ToBase62(), filename: $"{AppName}.pfx"); + Log.Information(Cryptography.Random.GetBytes(GenerateSecret).ToBase62()); return; } - if (GenerateSecret > 0) + // the application isn't being run in command mode. check the mutex to ensure + // only one long-running instance. + if (!Mutex.WaitOne(millisecondsTimeout: 0, exitContext: false)) { - Log.Information(Cryptography.Random.GetBytes(GenerateSecret).ToBase62()); + Log.Fatal($"An instance of {AppName} is already running"); return; } - // the application isn't being run in command mode. derive the application directory value - // and defaults that are dependent upon it + // derive the application directory value and defaults that are dependent upon it AppDirectory ??= DefaultAppDirectory; DataDirectory = Path.Combine(AppDirectory, "data"); @@ -1062,15 +1043,14 @@ namespace slskd } } - private static void GenerateX509Certificate(string password, string filename) + private static (string Filename, string Password) GenerateX509Certificate(string password, string filename) { filename = Path.Combine(AppContext.BaseDirectory, filename); var cert = X509.Generate(subject: AppName, password, X509KeyStorageFlags.Exportable); IOFile.WriteAllBytes(filename, cert.Export(X509ContentType.Pkcs12, password)); - Log.Information($"Certificate exported to {filename}"); - Log.Information($"Password: {password}"); + return (filename, password); } private static void PrintCommandLineArguments(Type targetType) |
