git: 2ba5f9560750 - stable/13 - bhyvectl: reduce code duplication

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 26 Jan 2023 18:54:18 UTC
The branch stable/13 has been updated by jhb:

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

commit 2ba5f95607505eccfd8dd3271463fa769809480b
Author:     Robert Wing <rew@FreeBSD.org>
AuthorDate: 2021-02-27 21:05:52 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-01-26 18:45:40 +0000

    bhyvectl: reduce code duplication
    
    Combine send_start_checkpoint() and send_start_suspend() into a
    single function named snapshot_request().
    
    snapshot_request() is equivalent to send_start_checkpoint() and
    send_start_suspend() except that it takes an additional argument. The
    additional argument, enum ipc_opcode, is used to determine the type of
    snapshot request being performed. Also, switch to using strlcpy instead
    of strncpy.
    
    Reviewed by:    jhb
    Differential Revision:  https://reviews.freebsd.org/D28878
    
    (cherry picked from commit da9713917eb26b67bafc740384ccd44f7dff09f2)
---
 usr.sbin/bhyvectl/bhyvectl.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)

diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c
index 6cf5db0ef42f..5683a84c0242 100644
--- a/usr.sbin/bhyvectl/bhyvectl.c
+++ b/usr.sbin/bhyvectl/bhyvectl.c
@@ -1734,25 +1734,12 @@ done:
 }
 
 static int
-send_start_checkpoint(struct vmctx *ctx, const char *checkpoint_file)
+snapshot_request(struct vmctx *ctx, const char *file, enum ipc_opcode code)
 {
 	struct checkpoint_op op;
 
-	op.op = START_CHECKPOINT;
-	strncpy(op.snapshot_filename, checkpoint_file, MAX_SNAPSHOT_VMNAME);
-	op.snapshot_filename[MAX_SNAPSHOT_VMNAME - 1] = 0;
-
-	return (send_checkpoint_op_req(ctx, &op));
-}
-
-static int
-send_start_suspend(struct vmctx *ctx, const char *suspend_file)
-{
-	struct checkpoint_op op;
-
-	op.op = START_SUSPEND;
-	strncpy(op.snapshot_filename, suspend_file, MAX_SNAPSHOT_VMNAME);
-	op.snapshot_filename[MAX_SNAPSHOT_VMNAME - 1] = 0;
+	op.op = code;
+	strlcpy(op.snapshot_filename, file, MAX_SNAPSHOT_VMNAME);
 
 	return (send_checkpoint_op_req(ctx, &op));
 }
@@ -2418,10 +2405,10 @@ main(int argc, char *argv[])
 
 #ifdef BHYVE_SNAPSHOT
 	if (!error && vm_checkpoint_opt)
-		error = send_start_checkpoint(ctx, checkpoint_file);
+		error = snapshot_request(ctx, checkpoint_file, START_CHECKPOINT);
 
 	if (!error && vm_suspend_opt)
-		error = send_start_suspend(ctx, suspend_file);
+		error = snapshot_request(ctx, suspend_file, START_SUSPEND);
 #endif
 
 	free (opts);