git: f0880ab791a5 - main - libvmmapi: Add vm_close()

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 30 Jun 2022 21:50:06 UTC
The branch main has been updated by jhb:

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

commit f0880ab791a510391a12f2ab7b01889b6774bca0
Author:     Vitaliy Gusev <gusev.vitaliy@gmail.com>
AuthorDate: 2022-06-30 21:21:57 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2022-06-30 21:21:57 +0000

    libvmmapi: Add vm_close()
    
    Currently there is no way to safely free a vm structure without
    leaking the fd.  vm_destroy() closes the fd but also destroys the VM
    whereas in some cases a VM needs to be opened (vm_open) and then
    closed (vm_close).
    
    Reviewed by:    jhb
    Sponsored by:   vStack
    Differential Revision:  https://reviews.freebsd.org/D35073
---
 lib/libvmmapi/vmmapi.c | 9 +++++++++
 lib/libvmmapi/vmmapi.h | 2 ++
 2 files changed, 11 insertions(+)

diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
index e92b3199381f..260c2a2439ff 100644
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -140,6 +140,15 @@ err:
 	return (NULL);
 }
 
+void
+vm_close(struct vmctx *vm)
+{
+	assert(vm != NULL);
+
+	close(vm->fd);
+	free(vm);
+}
+
 void
 vm_destroy(struct vmctx *vm)
 {
diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h
index 87051973225a..438fd582db0e 100644
--- a/lib/libvmmapi/vmmapi.h
+++ b/lib/libvmmapi/vmmapi.h
@@ -33,6 +33,7 @@
 
 #include <sys/param.h>
 #include <sys/cpuset.h>
+#include <machine/vmm.h>
 #include <machine/vmm_dev.h>
 
 #include <stdbool.h>
@@ -117,6 +118,7 @@ int	vm_munmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, size_t len);
 int	vm_create(const char *name);
 int	vm_get_device_fd(struct vmctx *ctx);
 struct vmctx *vm_open(const char *name);
+void	vm_close(struct vmctx *ctx);
 void	vm_destroy(struct vmctx *ctx);
 int	vm_parse_memsize(const char *optarg, size_t *memsize);
 int	vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s);