-/* $OpenBSD: vmd.c,v 1.146 2023/04/28 19:46:42 dv Exp $ */
+/* $OpenBSD: vmd.c,v 1.147 2023/05/12 14:42:30 dv Exp $ */
/*
* Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
memcpy(&vmc, imsg->data, sizeof(vmc));
vmc.vmc_kernel = imsg->fd;
- ret = vm_register(ps, &vmc, &vm, 0, vmc.vmc_owner.uid);
- if (vmc.vmc_flags == 0 || vmc.vmc_flags == VMOP_CREATE_KERNEL) {
- /* start an existing VM with pre-configured options */
- if (!(ret == -1 && errno == EALREADY &&
- !(vm->vm_state & VM_STATE_RUNNING))) {
- res = errno;
- cmd = IMSG_VMDOP_START_VM_RESPONSE;
- }
- } else if (ret != 0) {
+ /* Try registering our VM in our list of known VMs. */
+ if (vm_register(ps, &vmc, &vm, 0, vmc.vmc_owner.uid)) {
res = errno;
- cmd = IMSG_VMDOP_START_VM_RESPONSE;
- }
- if (res == 0) {
- res = config_setvm(ps, vm, imsg->hdr.peerid,
- vm->vm_params.vmc_owner.uid);
- if (res)
+ /* Did we have a failure during lookup of a parent? */
+ if (vm == NULL) {
cmd = IMSG_VMDOP_START_VM_RESPONSE;
+ break;
+ }
+
+ /* Does the VM already exist? */
+ if (res == EALREADY) {
+ /* Is it already running? */
+ if (vm->vm_state & VM_STATE_RUNNING) {
+ cmd = IMSG_VMDOP_START_VM_RESPONSE;
+ break;
+ }
+
+ /* If not running, are our flags ok? */
+ if (vmc.vmc_flags &&
+ vmc.vmc_flags != VMOP_CREATE_KERNEL) {
+ cmd = IMSG_VMDOP_START_VM_RESPONSE;
+ break;
+ }
+ }
+ res = 0;
}
+
+ /* Try to start the launch of the VM. */
+ res = config_setvm(ps, vm, imsg->hdr.peerid,
+ vm->vm_params.vmc_owner.uid);
+ if (res)
+ cmd = IMSG_VMDOP_START_VM_RESPONSE;
break;
case IMSG_VMDOP_WAIT_VM_REQUEST:
case IMSG_VMDOP_TERMINATE_VM_REQUEST: