From f0880ab791a510391a12f2ab7b01889b6774bca0 Mon Sep 17 00:00:00 2001 From: Vitaliy Gusev Date: Thu, 30 Jun 2022 14:21:57 -0700 Subject: [PATCH] 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 @@ vm_open(const char *name) 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 #include +#include #include #include @@ -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);