aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Program.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2023-01-02 13:55:58 -0600
committerJP Dillingham <jpdillingham@gmail.com>2023-01-02 13:55:58 -0600
commit7832e867ee393dd8e00ff3c3c83e272185597d7d (patch)
tree8fc15093a36672e77dfa4910bd7beb28d63d7b28 /src/slskd/Program.cs
parent12bbbffbe9c6fe764cc9161e40419e0a7902fa05 (diff)
downloadslskd-7832e867ee393dd8e00ff3c3c83e272185597d7d.tar.gz
refactor mutex
Diffstat (limited to 'src/slskd/Program.cs')
-rw-r--r--src/slskd/Program.cs50
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)