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
This commit is contained in:
Vitaliy Gusev 2022-06-30 14:21:57 -07:00 committed by John Baldwin
parent 87f49967d3
commit f0880ab791
2 changed files with 11 additions and 0 deletions

View file

@ -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)
{

View file

@ -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);