From c6e99345cd28d8e0ef9256ed826072cbcf7d8cf6 Mon Sep 17 00:00:00 2001 From: Fedor Korotkov Date: Mon, 22 Apr 2024 12:07:01 +0200 Subject: [PATCH] Validate Suspendability (#797) And show "Suspend" menu item based on `--suspnedable` flag Fixes #796 --- Sources/tart/Commands/Run.swift | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Sources/tart/Commands/Run.swift b/Sources/tart/Commands/Run.swift index 510f415..a65e4e9 100644 --- a/Sources/tart/Commands/Run.swift +++ b/Sources/tart/Commands/Run.swift @@ -164,6 +164,10 @@ struct Run: AsyncParsableCommand { } if suspendable { + let config = try VMConfig.init(fromURL: vmDir.configURL) + if !(config.platform is PlatformSuspendable) { + throw ValidationError("You can only suspend macOS VMs") + } if dir.count > 0 { throw ValidationError("Suspending VMs with shared directories is not supported") } @@ -557,7 +561,7 @@ struct Run: AsyncParsableCommand { nsApp.activate(ignoringOtherApps: true) struct MainApp: App { - static var disappearSignal: Int32 = SIGINT + static var suspendable: Bool = false static var capturesSystemKeys: Bool = false @NSApplicationDelegateAdaptor private var appDelegate: MinimalMenuAppDelegate @@ -568,7 +572,7 @@ struct Run: AsyncParsableCommand { VMView(vm: vm!, capturesSystemKeys: MainApp.capturesSystemKeys).onAppear { NSWindow.allowsAutomaticWindowTabbing = false }.onDisappear { - let ret = kill(getpid(), MainApp.disappearSignal) + let ret = kill(getpid(), MainApp.suspendable ? SIGUSR1 : SIGINT) if ret != 0 { // Fallback to the old termination method that doesn't // propagate the cancellation to Task's in case graceful @@ -605,8 +609,10 @@ struct Run: AsyncParsableCommand { Task { try vm!.virtualMachine.requestStop() } } if #available(macOS 14, *) { - Button("Suspend") { - kill(getpid(), SIGUSR1) + if (MainApp.suspendable) { + Button("Suspend") { + kill(getpid(), SIGUSR1) + } } } } @@ -614,7 +620,7 @@ struct Run: AsyncParsableCommand { } } - MainApp.disappearSignal = suspendable ? SIGUSR1 : SIGINT + MainApp.suspendable = suspendable MainApp.capturesSystemKeys = captureSystemKeys MainApp.main() }