git: b35e955333ef - stable/14 - bhyvectl: improve options error handling

From: Roman Bogorodskiy <novel_at_FreeBSD.org>
Date: Sun, 04 Jan 2026 10:29:00 UTC
The branch stable/14 has been updated by novel:

URL: https://cgit.FreeBSD.org/src/commit/?id=b35e955333efdd97a3a369740d2b2038fb86826c

commit b35e955333efdd97a3a369740d2b2038fb86826c
Author:     Roman Bogorodskiy <novel@FreeBSD.org>
AuthorDate: 2025-12-05 18:45:03 +0000
Commit:     Roman Bogorodskiy <novel@FreeBSD.org>
CommitDate: 2026-01-04 10:24:47 +0000

    bhyvectl: improve options error handling
    
    Currently, it is possible to execute bhyvectl(8) with mutually exclusive
    options, such as "--destroy" and "--suspend", and it will print out
    obscure errors, e.g.:
    
     bhyvectl --suspend=/var/run/vms/my_vm --destroy --vm my_vm
     connect() failed: Connection refused
    
    Address that by failing early if mutually exclusive options were
    specified.
    
    Additionally, move the BHYVE_SNAPSHOT block before the errors are
    printed, so its errors are also displayed.
    
    Approved by:            markj
    Sponsored by:           The FreeBSD Foundation
    MFC after:              2 weeks
    Differential Revision:  https://reviews.freebsd.org/D54092
    
    (cherry picked from commit 148111d3775eb159d71a36e3a8b4c5c1bf53392c)
---
 usr.sbin/bhyvectl/bhyvectl.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c
index 050d72f013df..acd4950e10ca 100644
--- a/usr.sbin/bhyvectl/bhyvectl.c
+++ b/usr.sbin/bhyvectl/bhyvectl.c
@@ -1628,7 +1628,7 @@ int
 main(int argc, char *argv[])
 {
 	char *vmname;
-	int error, ch, vcpuid, ptenum;
+	int action_opts, error, ch, vcpuid, ptenum;
 	vm_paddr_t gpa_pmap;
 	struct vm_run vmrun;
 	uint64_t rax, cr0, cr2, cr3, cr4, dr0, dr1, dr2, dr3, dr6, dr7;
@@ -1648,6 +1648,7 @@ main(int argc, char *argv[])
 	cpu_intel = cpu_vendor_intel();
 	opts = setup_options(cpu_intel);
 
+	action_opts = 0;
 	vcpuid = 0;
 	vmname = NULL;
 	assert_lapic_lvt = -1;
@@ -1822,6 +1823,17 @@ main(int argc, char *argv[])
 	if (vmname == NULL)
 		usage(opts);
 
+	action_opts = create + destroy + force_reset + force_poweroff;
+#ifdef BHYVE_SNAPSHOT
+	if (checkpoint_file)
+		action_opts++;
+#endif
+
+	if (action_opts > 1) {
+		fprintf(stderr, "mutually exclusive actions specified\n");
+		exit(1);
+	}
+
 	error = 0;
 
 	if (!error && create)
@@ -2290,17 +2302,17 @@ main(int argc, char *argv[])
 	if (!error && force_poweroff)
 		error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);
 
+#ifdef BHYVE_SNAPSHOT
+	if (!error && checkpoint_file)
+		error = snapshot_request(vmname, checkpoint_file, vm_suspend_opt);
+#endif
+
 	if (error)
 		printf("errno = %d\n", errno);
 
 	if (!error && destroy)
 		vm_destroy(ctx);
 
-#ifdef BHYVE_SNAPSHOT
-	if (!error && checkpoint_file)
-		error = snapshot_request(vmname, checkpoint_file, vm_suspend_opt);
-#endif
-
 	free (opts);
 	exit(error);
 }