From bf5081b3d9564e456dc1bead73b7e2b3a5939dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 17 Jun 2024 18:11:00 +0200 Subject: [PATCH] Hook SIGUSR2 to requestStop (#842) For macOS this brings up a dialog, asking the user if they are sure they want to shut down, which makes this less useful for automated graceful shutdowns, but it may behave better on Linux, and there might be ways to instruct macOS to not ask the user, so it's still a nice feature, and aligns with the SIGUSR1 for suspend, and SIGINT for non-graceful shutdown. --- Sources/tart/Commands/Run.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sources/tart/Commands/Run.swift b/Sources/tart/Commands/Run.swift index 48bee2a..81686af 100644 --- a/Sources/tart/Commands/Run.swift +++ b/Sources/tart/Commands/Run.swift @@ -359,6 +359,18 @@ struct Run: AsyncParsableCommand { } sigusr1Src.activate() + // Gracefull shutdown support. For macOS this brings up a dialog, + // asking the user if they are sure they want to shut down. + signal(SIGUSR2, SIG_IGN) + let sigusr2Src = DispatchSource.makeSignalSource(signal: SIGUSR2) + sigusr2Src.setEventHandler { + Task { + print("Requesting guest OS to stop...") + try vm!.virtualMachine.requestStop() + } + } + sigusr2Src.activate() + let useVNCWithoutGraphics = (vnc || vncExperimental) && !graphics if noGraphics || useVNCWithoutGraphics { // enter the main even loop, without bringing up any UI,