git: 3efc45f34e13 - main - libvmm: constify vm_get_name()

From: Robert Wing <rew_at_FreeBSD.org>
Date: Fri, 18 Mar 2022 05:45:37 UTC
The branch main has been updated by rew:

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

commit 3efc45f34e13c63cb37a77a67fea39937004b75a
Author:     Robert Wing <rew@FreeBSD.org>
AuthorDate: 2022-03-18 05:26:54 +0000
Commit:     Robert Wing <rew@FreeBSD.org>
CommitDate: 2022-03-18 05:38:21 +0000

    libvmm: constify vm_get_name()
    
    Allows callers of vm_get_name() to retrieve the vm name without having
    to allocate a buffer.
    
    While in the vicinity, do minor cleanup in vm_snapshot_basic_metadata().
    
    Reviewed by:    jhb
    Differential Revision:  https://reviews.freebsd.org/D34290
---
 lib/libvmmapi/vmmapi.c    |  9 +++------
 lib/libvmmapi/vmmapi.h    |  2 +-
 usr.sbin/bhyve/snapshot.c | 28 ++++------------------------
 3 files changed, 8 insertions(+), 31 deletions(-)

diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
index 0291e4a10c33..e92b3199381f 100644
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -496,14 +496,11 @@ vm_rev_map_gpa(struct vmctx *ctx, void *addr)
 	return ((vm_paddr_t)-1);
 }
 
-/* TODO: maximum size for vmname */
-int
-vm_get_name(struct vmctx *ctx, char *buf, size_t max_len)
+const char *
+vm_get_name(struct vmctx *ctx)
 {
 
-	if (strlcpy(buf, ctx->name, max_len) >= max_len)
-		return (EINVAL);
-	return (0);
+	return (ctx->name);
 }
 
 size_t
diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h
index 08945309cbd5..87051973225a 100644
--- a/lib/libvmmapi/vmmapi.h
+++ b/lib/libvmmapi/vmmapi.h
@@ -133,7 +133,7 @@ uint32_t vm_get_lowmem_limit(struct vmctx *ctx);
 void	vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit);
 void	vm_set_memflags(struct vmctx *ctx, int flags);
 int	vm_get_memflags(struct vmctx *ctx);
-int	vm_get_name(struct vmctx *ctx, char *buffer, size_t max_len);
+const char *vm_get_name(struct vmctx *ctx);
 size_t	vm_get_lowmem_size(struct vmctx *ctx);
 size_t	vm_get_highmem_size(struct vmctx *ctx);
 int	vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
diff --git a/usr.sbin/bhyve/snapshot.c b/usr.sbin/bhyve/snapshot.c
index b0cc8095c3cb..13154c68c3e7 100644
--- a/usr.sbin/bhyve/snapshot.c
+++ b/usr.sbin/bhyve/snapshot.c
@@ -1123,28 +1123,15 @@ err_vm_snapshot_kern_data:
 static int
 vm_snapshot_basic_metadata(struct vmctx *ctx, xo_handle_t *xop, size_t memsz)
 {
-	int error;
-	int memflags;
-	char vmname_buf[MAX_VMNAME];
-
-	memset(vmname_buf, 0, MAX_VMNAME);
-	error = vm_get_name(ctx, vmname_buf, MAX_VMNAME - 1);
-	if (error != 0) {
-		perror("Failed to get VM name");
-		goto err;
-	}
-
-	memflags = vm_get_memflags(ctx);
 
 	xo_open_container_h(xop, JSON_BASIC_METADATA_KEY);
 	xo_emit_h(xop, "{:" JSON_NCPUS_KEY "/%ld}\n", guest_ncpus);
-	xo_emit_h(xop, "{:" JSON_VMNAME_KEY "/%s}\n", vmname_buf);
+	xo_emit_h(xop, "{:" JSON_VMNAME_KEY "/%s}\n", vm_get_name(ctx));
 	xo_emit_h(xop, "{:" JSON_MEMSIZE_KEY "/%lu}\n", memsz);
-	xo_emit_h(xop, "{:" JSON_MEMFLAGS_KEY "/%d}\n", memflags);
+	xo_emit_h(xop, "{:" JSON_MEMFLAGS_KEY "/%d}\n", vm_get_memflags(ctx));
 	xo_close_container_h(xop, JSON_BASIC_METADATA_KEY);
 
-err:
-	return (error);
+	return (0);
 }
 
 static int
@@ -1518,7 +1505,6 @@ init_checkpoint_thread(struct vmctx *ctx)
 	struct sockaddr_un addr;
 	int socket_fd;
 	pthread_t checkpoint_pthread;
-	char vmname_buf[MAX_VMNAME];
 	int err;
 
 	memset(&addr, 0, sizeof(addr));
@@ -1532,14 +1518,8 @@ init_checkpoint_thread(struct vmctx *ctx)
 
 	addr.sun_family = AF_UNIX;
 
-	err = vm_get_name(ctx, vmname_buf, MAX_VMNAME - 1);
-	if (err != 0) {
-		perror("Failed to get VM name");
-		goto fail;
-	}
-
 	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s",
-		 BHYVE_RUN_DIR, vmname_buf);
+		 BHYVE_RUN_DIR, vm_get_name(ctx));
 	addr.sun_len = SUN_LEN(&addr);
 	unlink(addr.sun_path);