Use imsg_get_fd() in vmd.
authorclaudio <claudio@openbsd.org>
Thu, 18 Jan 2024 14:49:59 +0000 (14:49 +0000)
committerclaudio <claudio@openbsd.org>
Thu, 18 Jan 2024 14:49:59 +0000 (14:49 +0000)
vmd uses a lot of fd passing and does it sometimes via extra abstraction
so this just tries to convert the code without any optimisations.

ok dv@

usr.sbin/vmd/config.c
usr.sbin/vmd/control.c
usr.sbin/vmd/priv.c
usr.sbin/vmd/proc.c
usr.sbin/vmd/vm.c
usr.sbin/vmd/vmd.c
usr.sbin/vmd/vmm.c

index e2f3679..0022ce2 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: config.c,v 1.73 2024/01/03 22:34:39 dv Exp $  */
+/*     $OpenBSD: config.c,v 1.74 2024/01/18 14:49:59 claudio Exp $     */
 
 /*
  * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
@@ -552,10 +552,12 @@ config_getvm(struct privsep *ps, struct imsg *imsg)
 {
        struct vmop_create_params        vmc;
        struct vmd_vm                   *vm = NULL;
+       int                              fd;
 
        IMSG_SIZE_CHECK(imsg, &vmc);
        memcpy(&vmc, imsg->data, sizeof(vmc));
-       vmc.vmc_kernel = imsg->fd;
+       fd = imsg_get_fd(imsg);
+       vmc.vmc_kernel = fd;
 
        errno = 0;
        if (vm_register(ps, &vmc, &vm, imsg->hdr.peerid, 0) == -1)
@@ -563,14 +565,12 @@ config_getvm(struct privsep *ps, struct imsg *imsg)
 
        vm->vm_state |= VM_STATE_RUNNING;
        vm->vm_peerid = (uint32_t)-1;
-       vm->vm_kernel = imsg->fd;
+       vm->vm_kernel = fd;
        return (0);
 
  fail:
-       if (imsg->fd != -1) {
-               close(imsg->fd);
-               imsg->fd = -1;
-       }
+       if (fd != -1)
+               close(fd);
 
        vm_remove(vm, __func__);
        if (errno == 0)
@@ -584,6 +584,7 @@ config_getdisk(struct privsep *ps, struct imsg *imsg)
 {
        struct vmd_vm   *vm;
        unsigned int     n, idx;
+       int              fd;
 
        errno = 0;
        if ((vm = vm_getbyvmid(imsg->hdr.peerid)) == NULL) {
@@ -593,8 +594,9 @@ config_getdisk(struct privsep *ps, struct imsg *imsg)
 
        IMSG_SIZE_CHECK(imsg, &n);
        memcpy(&n, imsg->data, sizeof(n));
+       fd = imsg_get_fd(imsg);
 
-       if (n >= vm->vm_params.vmc_ndisks || imsg->fd == -1) {
+       if (n >= vm->vm_params.vmc_ndisks || fd == -1) {
                log_warnx("invalid disk id");
                errno = EINVAL;
                return (-1);
@@ -605,7 +607,7 @@ config_getdisk(struct privsep *ps, struct imsg *imsg)
                errno = EINVAL;
                return (-1);
        }
-       vm->vm_disks[n][idx] = imsg->fd;
+       vm->vm_disks[n][idx] = fd;
        return (0);
 }
 
@@ -614,6 +616,7 @@ config_getif(struct privsep *ps, struct imsg *imsg)
 {
        struct vmd_vm   *vm;
        unsigned int     n;
+       int              fd;
 
        errno = 0;
        if ((vm = vm_getbyvmid(imsg->hdr.peerid)) == NULL) {
@@ -623,16 +626,18 @@ config_getif(struct privsep *ps, struct imsg *imsg)
 
        IMSG_SIZE_CHECK(imsg, &n);
        memcpy(&n, imsg->data, sizeof(n));
+       fd = imsg_get_fd(imsg);
+
        if (n >= vm->vm_params.vmc_nnics ||
-           vm->vm_ifs[n].vif_fd != -1 || imsg->fd == -1) {
+           vm->vm_ifs[n].vif_fd != -1 || fd == -1) {
                log_warnx("invalid interface id");
                goto fail;
        }
-       vm->vm_ifs[n].vif_fd = imsg->fd;
+       vm->vm_ifs[n].vif_fd = fd;
        return (0);
  fail:
-       if (imsg->fd != -1)
-               close(imsg->fd);
+       if (fd != -1)
+               close(fd);
        errno = EINVAL;
        return (-1);
 }
@@ -641,6 +646,7 @@ int
 config_getcdrom(struct privsep *ps, struct imsg *imsg)
 {
        struct vmd_vm   *vm;
+       int              fd;
 
        errno = 0;
        if ((vm = vm_getbyvmid(imsg->hdr.peerid)) == NULL) {
@@ -648,16 +654,15 @@ config_getcdrom(struct privsep *ps, struct imsg *imsg)
                return (-1);
        }
 
-       if (imsg->fd == -1) {
+       fd = imsg_get_fd(imsg);
+       if (fd == -1) {
                log_warnx("invalid cdrom id");
                goto fail;
        }
 
-       vm->vm_cdrom = imsg->fd;
+       vm->vm_cdrom = fd;
        return (0);
  fail:
-       if (imsg->fd != -1)
-               close(imsg->fd);
        errno = EINVAL;
        return (-1);
 }
index ef4d0f0..e465ff1 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: control.c,v 1.41 2023/04/28 19:46:42 dv Exp $ */
+/*     $OpenBSD: control.c,v 1.42 2024/01/18 14:49:59 claudio Exp $    */
 
 /*
  * Copyright (c) 2010-2015 Reyk Floeter <reyk@openbsd.org>
@@ -440,7 +440,7 @@ control_dispatch_imsg(int fd, short event, void *arg)
                case IMSG_VMDOP_RELOAD:
                case IMSG_CTL_RESET:
                        if (proc_compose_imsg(ps, PROC_PARENT, -1,
-                           imsg.hdr.type, fd, imsg.fd,
+                           imsg.hdr.type, fd, imsg_get_fd(&imsg),
                            imsg.data, IMSG_DATA_SIZE(&imsg)) == -1)
                                goto fail;
                        break;
@@ -453,7 +453,7 @@ control_dispatch_imsg(int fd, short event, void *arg)
 
                        /* imsg.fd may contain kernel image fd. */
                        if (proc_compose_imsg(ps, PROC_PARENT, -1,
-                           imsg.hdr.type, fd, imsg.fd, &vmc,
+                           imsg.hdr.type, fd, imsg_get_fd(&imsg), &vmc,
                            sizeof(vmc)) == -1) {
                                control_close(fd, cs);
                                return;
@@ -508,7 +508,7 @@ control_dispatch_imsg(int fd, short event, void *arg)
                            vid.vid_uid);
 
                        if (proc_compose_imsg(ps, PROC_PARENT, -1,
-                           imsg.hdr.type, fd, imsg.fd,
+                           imsg.hdr.type, fd, imsg_get_fd(&imsg),
                            &vid, sizeof(vid)) == -1)
                                goto fail;
                        break;
index bfb1508..744f9c5 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: priv.c,v 1.23 2023/07/13 18:31:59 dv Exp $    */
+/*     $OpenBSD: priv.c,v 1.24 2024/01/18 14:49:59 claudio Exp $       */
 
 /*
  * Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org>
@@ -94,6 +94,7 @@ priv_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
        struct vmop_addr_req     vareq;
        struct vmop_addr_result  varesult;
        char                     type[IF_NAMESIZE];
+       int                      ifd;
 
        switch (imsg->hdr.type) {
        case IMSG_VMDOP_PRIV_IFDESCR:
@@ -254,14 +255,15 @@ priv_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
                varesult.var_vmid = vareq.var_vmid;
                varesult.var_nic_idx = vareq.var_nic_idx;
 
+               ifd = imsg_get_fd(imsg);
                /* resolve lladdr for the tap(4) and send back to parent */
-               if (ioctl(imsg->fd, SIOCGIFADDR, &varesult.var_addr) != 0)
+               if (ioctl(ifd, SIOCGIFADDR, &varesult.var_addr) != 0)
                        log_warn("SIOCGIFADDR");
                else
                        proc_compose_imsg(ps, PROC_PARENT, -1,
                            IMSG_VMDOP_PRIV_GET_ADDR_RESPONSE, imsg->hdr.peerid,
                            -1, &varesult, sizeof(varesult));
-               close(imsg->fd);
+               close(ifd);
                break;
        case IMSG_VMDOP_CONFIG:
                config_getconfig(env, imsg);
index 115a5dd..380b354 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: proc.c,v 1.21 2023/09/26 01:53:54 dv Exp $    */
+/*     $OpenBSD: proc.c,v 1.22 2024/01/18 14:49:59 claudio Exp $       */
 
 /*
  * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
@@ -661,7 +661,7 @@ proc_dispatch(int fd, short event, void *arg)
                case IMSG_CTL_PROCFD:
                        IMSG_SIZE_CHECK(&imsg, &pf);
                        memcpy(&pf, imsg.data, sizeof(pf));
-                       proc_accept(ps, imsg.fd, pf.pf_procid,
+                       proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid,
                            pf.pf_instance);
                        break;
                default:
@@ -792,7 +792,8 @@ proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
     enum privsep_procid id, int n)
 {
        return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
-           imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
+           imsg->hdr.peerid, imsg_get_fd(imsg), imsg->data,
+           IMSG_DATA_SIZE(imsg)));
 }
 
 struct imsgbuf *
index cc2b4fc..8d20b4c 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: vm.c,v 1.95 2024/01/10 04:13:59 dv Exp $      */
+/*     $OpenBSD: vm.c,v 1.96 2024/01/18 14:49:59 claudio Exp $ */
 
 /*
  * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -596,7 +596,7 @@ vm_dispatch_vmm(int fd, short event, void *arg)
                        break;
                case IMSG_VMDOP_SEND_VM_REQUEST:
                        vmr.vmr_id = vm->vm_vmid;
-                       vmr.vmr_result = send_vm(imsg.fd, vm);
+                       vmr.vmr_result = send_vm(imsg_get_fd(&imsg), vm);
                        imsg_compose_event(&vm->vm_iev,
                            IMSG_VMDOP_SEND_VM_RESPONSE,
                            imsg.hdr.peerid, imsg.hdr.pid, -1, &vmr,
index a12b984..613cdde 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: vmd.c,v 1.152 2023/09/26 01:53:54 dv Exp $    */
+/*     $OpenBSD: vmd.c,v 1.153 2024/01/18 14:49:59 claudio Exp $       */
 
 /*
  * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
@@ -97,6 +97,7 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
 {
        struct privsep                  *ps = p->p_ps;
        int                              res = 0, ret = 0, cmd = 0, verbose;
+       int                              ifd;
        unsigned int                     v = 0, flags;
        struct vmop_create_params        vmc;
        struct vmop_id                   vid;
@@ -111,7 +112,7 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
        case IMSG_VMDOP_START_VM_REQUEST:
                IMSG_SIZE_CHECK(imsg, &vmc);
                memcpy(&vmc, imsg->data, sizeof(vmc));
-               vmc.vmc_kernel = imsg->fd;
+               vmc.vmc_kernel = imsg_get_fd(imsg);
 
                /* Try registering our VM in our list of known VMs. */
                if (vm_register(ps, &vmc, &vm, 0, vmc.vmc_owner.uid)) {
@@ -257,11 +258,12 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
                IMSG_SIZE_CHECK(imsg, &vid);
                memcpy(&vid, imsg->data, sizeof(vid));
                id = vid.vid_id;
+               ifd = imsg_get_fd(imsg);
                if (vid.vid_id == 0) {
                        if ((vm = vm_getbyname(vid.vid_name)) == NULL) {
                                res = ENOENT;
                                cmd = IMSG_VMDOP_SEND_VM_RESPONSE;
-                               close(imsg->fd);
+                               close(ifd);
                                break;
                        } else {
                                vid.vid_id = vm->vm_vmid;
@@ -269,43 +271,42 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
                } else if ((vm = vm_getbyvmid(vid.vid_id)) == NULL) {
                        res = ENOENT;
                        cmd = IMSG_VMDOP_SEND_VM_RESPONSE;
-                       close(imsg->fd);
+                       close(ifd);
                        break;
                }
                vmr.vmr_id = vid.vid_id;
                log_debug("%s: sending fd to vmm", __func__);
                proc_compose_imsg(ps, PROC_VMM, -1, imsg->hdr.type,
-                   imsg->hdr.peerid, imsg->fd, &vid, sizeof(vid));
+                   imsg->hdr.peerid, ifd, &vid, sizeof(vid));
                break;
        case IMSG_VMDOP_RECEIVE_VM_REQUEST:
                IMSG_SIZE_CHECK(imsg, &vid);
                memcpy(&vid, imsg->data, sizeof(vid));
-               if (imsg->fd == -1) {
+               ifd = imsg_get_fd(imsg);
+               if (ifd == -1) {
                        log_warnx("%s: invalid fd", __func__);
                        return (-1);
                }
-               if (atomicio(read, imsg->fd, &vmh, sizeof(vmh)) !=
-                   sizeof(vmh)) {
+               if (atomicio(read, ifd, &vmh, sizeof(vmh)) != sizeof(vmh)) {
                        log_warnx("%s: error reading vmh from received vm",
                            __func__);
                        res = EIO;
-                       close(imsg->fd);
+                       close(ifd);
                        cmd = IMSG_VMDOP_START_VM_RESPONSE;
                        break;
                }
 
                if (vmd_check_vmh(&vmh)) {
                        res = ENOENT;
-                       close(imsg->fd);
+                       close(ifd);
                        cmd = IMSG_VMDOP_START_VM_RESPONSE;
                        break;
                }
-               if (atomicio(read, imsg->fd, &vmc, sizeof(vmc)) !=
-                   sizeof(vmc)) {
+               if (atomicio(read, ifd, &vmc, sizeof(vmc)) != sizeof(vmc)) {
                        log_warnx("%s: error reading vmc from received vm",
                            __func__);
                        res = EIO;
-                       close(imsg->fd);
+                       close(ifd);
                        cmd = IMSG_VMDOP_START_VM_RESPONSE;
                        break;
                }
@@ -317,14 +318,14 @@ vmd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
                if (ret != 0) {
                        res = errno;
                        cmd = IMSG_VMDOP_START_VM_RESPONSE;
-                       close(imsg->fd);
+                       close(ifd);
                } else {
                        vm->vm_state |= VM_STATE_RECEIVED;
                        config_setvm(ps, vm, imsg->hdr.peerid,
                            vmc.vmc_owner.uid);
                        log_debug("%s: sending fd to vmm", __func__);
                        proc_compose_imsg(ps, PROC_VMM, -1,
-                           IMSG_VMDOP_RECEIVE_VM_END, vm->vm_vmid, imsg->fd,
+                           IMSG_VMDOP_RECEIVE_VM_END, vm->vm_vmid, ifd,
                            NULL, 0);
                }
                break;
index 87d200c..917c7a2 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: vmm.c,v 1.116 2024/01/03 22:34:39 dv Exp $    */
+/*     $OpenBSD: vmm.c,v 1.117 2024/01/18 14:49:59 claudio Exp $       */
 
 /*
  * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
@@ -252,7 +252,7 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
                }
                imsg_compose_event(&vm->vm_iev,
                    imsg->hdr.type, imsg->hdr.peerid, imsg->hdr.pid,
-                   imsg->fd, &vid, sizeof(vid));
+                   imsg_get_fd(imsg), &vid, sizeof(vid));
                break;
        case IMSG_VMDOP_UNPAUSE_VM:
                IMSG_SIZE_CHECK(imsg, &vid);
@@ -265,7 +265,7 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
                }
                imsg_compose_event(&vm->vm_iev,
                    imsg->hdr.type, imsg->hdr.peerid, imsg->hdr.pid,
-                   imsg->fd, &vid, sizeof(vid));
+                   imsg_get_fd(imsg), &vid, sizeof(vid));
                break;
        case IMSG_VMDOP_SEND_VM_REQUEST:
                IMSG_SIZE_CHECK(imsg, &vid);
@@ -273,13 +273,13 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
                id = vid.vid_id;
                if ((vm = vm_getbyvmid(id)) == NULL) {
                        res = ENOENT;
-                       close(imsg->fd);
+                       close(imsg_get_fd(imsg));       /* XXX */
                        cmd = IMSG_VMDOP_START_VM_RESPONSE;
                        break;
                }
                imsg_compose_event(&vm->vm_iev,
                    imsg->hdr.type, imsg->hdr.peerid, imsg->hdr.pid,
-                   imsg->fd, &vid, sizeof(vid));
+                   imsg_get_fd(imsg), &vid, sizeof(vid));
                break;
        case IMSG_VMDOP_RECEIVE_VM_REQUEST:
                IMSG_SIZE_CHECK(imsg, &vmc);
@@ -290,18 +290,18 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
                        cmd = IMSG_VMDOP_START_VM_RESPONSE;
                        break;
                }
-               vm->vm_tty = imsg->fd;
+               vm->vm_tty = imsg_get_fd(imsg);
                vm->vm_state |= VM_STATE_RECEIVED;
                vm->vm_state |= VM_STATE_PAUSED;
                break;
        case IMSG_VMDOP_RECEIVE_VM_END:
                if ((vm = vm_getbyvmid(imsg->hdr.peerid)) == NULL) {
                        res = ENOENT;
-                       close(imsg->fd);
+                       close(imsg_get_fd(imsg));       /* XXX */
                        cmd = IMSG_VMDOP_START_VM_RESPONSE;
                        break;
                }
-               vm->vm_receive_fd = imsg->fd;
+               vm->vm_receive_fd = imsg_get_fd(imsg);
                res = vmm_start_vm(imsg, &id, &pid);
                /* Check if the ID can be mapped correctly */
                if ((id = vm_id2vmid(id, NULL)) == 0)
@@ -318,12 +318,12 @@ vmm_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
                /* Forward hardware address details to the guest vm */
                imsg_compose_event(&vm->vm_iev,
                    imsg->hdr.type, imsg->hdr.peerid, imsg->hdr.pid,
-                   imsg->fd, &var, sizeof(var));
+                   imsg_get_fd(imsg), &var, sizeof(var));
                break;
        case IMSG_VMDOP_RECEIVE_VMM_FD:
                if (env->vmd_fd > -1)
                        fatalx("already received vmm fd");
-               env->vmd_fd = imsg->fd;
+               env->vmd_fd = imsg_get_fd(imsg);
 
                /* Get and terminate all running VMs */
                get_info_vm(ps, NULL, 1);
@@ -656,7 +656,7 @@ vmm_start_vm(struct imsg *imsg, uint32_t *id, pid_t *pid)
        vcp = &vm->vm_params.vmc_params;
 
        if (!(vm->vm_state & VM_STATE_RECEIVED)) {
-               if ((vm->vm_tty = imsg->fd) == -1) {
+               if ((vm->vm_tty = imsg_get_fd(imsg)) == -1) {
                        log_warnx("%s: can't get tty", __func__);
                        goto err;
                }