From ac0ac2397bec859c50f869d38fb07718e2dbb360 Mon Sep 17 00:00:00 2001 From: TomiEckert Date: Sat, 28 Feb 2026 14:47:17 +0100 Subject: [PATCH] feat: Add daemon single-instance lock and graceful shutdown to systemd service. --- Core/DaemonService.cs | 14 ++++++++++++++ toak.service | 1 + 2 files changed, 15 insertions(+) diff --git a/Core/DaemonService.cs b/Core/DaemonService.cs index 2020675..1350b0d 100644 --- a/Core/DaemonService.cs +++ b/Core/DaemonService.cs @@ -25,8 +25,22 @@ public static class DaemonService return Path.Combine(runtimeDir, "toak.sock"); } + private static FileStream? _lockFile; + public static async Task StartAsync(bool verbose) { + var lockPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "toak", "daemon.lock"); + try + { + Directory.CreateDirectory(Path.GetDirectoryName(lockPath)!); + _lockFile = new FileStream(lockPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); + } + catch (IOException) + { + Console.WriteLine("Toak daemon is already running."); + return; + } + Logger.Verbose = verbose; var socketPath = GetSocketPath(); diff --git a/toak.service b/toak.service index e062f34..00e3b7a 100644 --- a/toak.service +++ b/toak.service @@ -5,6 +5,7 @@ After=network.target [Service] Type=simple ExecStart=/usr/bin/toak daemon +ExecStop=/bin/kill -INT $MAINPID Restart=always RestartSec=3