From nobody Mon Feb 9 19:52:50 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1627481637400503.99436834050346; Wed, 28 Jul 2021 07:13:57 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-122-6Phv3zpPPcmocNvNmXrqZw-1; Wed, 28 Jul 2021 10:13:55 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9AD3294EE9; Wed, 28 Jul 2021 14:13:45 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 76AC760C0F; Wed, 28 Jul 2021 14:13:45 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 3C0BF180BAC0; Wed, 28 Jul 2021 14:13:45 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 16S8IJ36013790 for ; Wed, 28 Jul 2021 04:18:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id B54B6200AD0C; Wed, 28 Jul 2021 08:18:19 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast05.extmail.prod.ext.rdu2.redhat.com [10.11.55.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B08D0200E641 for ; Wed, 28 Jul 2021 08:18:19 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 99112801E80 for ; Wed, 28 Jul 2021 08:18:19 +0000 (UTC) Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-278-PMcJtE9EMuKomzQlN-khJA-1; Wed, 28 Jul 2021 04:18:17 -0400 Received: by mail-lj1-f170.google.com with SMTP id h9so2063694ljq.8 for ; Wed, 28 Jul 2021 01:18:17 -0700 (PDT) Received: from navi.cosmonova.net.ua ([95.67.24.131]) by smtp.gmail.com with ESMTPSA id p5sm571964lfd.240.2021.07.28.01.18.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 01:18:15 -0700 (PDT) X-MC-Unique: 6Phv3zpPPcmocNvNmXrqZw-1 X-MC-Unique: PMcJtE9EMuKomzQlN-khJA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Cy3NRNKh+kvApJkg5MlcuPeF5PzXTnzHYlYAopWIvbE=; b=egrYPb28+L8ShqEYTawR4AsSRXqY88XG/IhqFg+uG0tD3OYbhOyn7+5hYa0eEtRN+g XRzQZa6Mi7PS9CUuah7rp8/+U/N5kJCaZhvpn/J+3YalKE+ZnzIIqgQq8nMNuE3AZuey qbfUR/R6B8MhEFGOlZj8+ZAASa+4keoNzXkUredA/ZV9JJKxOT/Dx3VGxr5U972PkgD4 g3FYkdHRMEyiM5RNQ59vifzpNZ41SQDgAtxxtfbwL2s8IKVmLdr0CUZVxYuNQiAIpi3v OV1jaNJAxDW9xD/oDCuChLOubQBux1FT3TtqHLTa/K1e8fHSGlaGVRcY6nvDefBsJCzL 2VjQ== X-Gm-Message-State: AOAM533npkY/a5rfIiORt+h5VHz3+2m3o2DmaYbsQw6sdjpMKyuJ0mwk f7fopi6pYpm1kBJAqr9fd4NPTONmdzK339AM X-Google-Smtp-Source: ABdhPJxgwt+fjjG9NYp9Fhj+UEtzqiMrlyEiZG1i5KQs78CZxB9K8ZY4JtBvQVpQ+IPtDZ5DhDob5w== X-Received: by 2002:a2e:bd85:: with SMTP id o5mr18124413ljq.313.1627460295595; Wed, 28 Jul 2021 01:18:15 -0700 (PDT) From: Andrew Melnychenko To: libvir-list@redhat.com Subject: [RFC PATCH 04/10] virsocket: Added receive for multiple fds. Date: Wed, 28 Jul 2021 11:17:08 +0300 Message-Id: <20210728081714.715287-5-andrew@daynix.com> In-Reply-To: <20210728081714.715287-1-andrew@daynix.com> References: <20210728081714.715287-1-andrew@daynix.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-loop: libvir-list@redhat.com X-Mailman-Approved-At: Wed, 28 Jul 2021 10:13:24 -0400 Cc: yan@daynix.com, yuri.benditovich@daynix.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1627481639643100001 Content-Type: text/plain; charset="utf-8" Similar to virSocketRecvFD() added virSocketRecvMultipleFDs(). This function returns multiple fds through unix socket. New function is required for "qemu-ebpf-rss-helper" program. The helper may pass few file descriptors - eBPF program and maps. Signed-off-by: Andrew Melnychenko --- src/libvirt_private.syms | 1 + src/util/virsocket.c | 83 ++++++++++++++++++++++++++++++++++++++++ src/util/virsocket.h | 2 + 3 files changed, 86 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 43493ea76e..6987ff00c2 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3226,6 +3226,7 @@ virSecureEraseString; # util/virsocket.h virSocketRecvFD; virSocketSendFD; +virSocketRecvMultipleFDs; =20 =20 # util/virsocketaddr.h diff --git a/src/util/virsocket.c b/src/util/virsocket.c index b971da16e3..da8af42e72 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -486,6 +486,82 @@ virSocketRecvFD(int sock, int fdflags) =20 return fd; } + + +/* virSocketRecvMultipleFDs receives few file descriptors through the sock= et. + The flags are a bitmask, possibly including O_CLOEXEC (defined in ). + + Return the number of recived file descriptors on success, + or -1 with errno set in case of error. +*/ +int +virSocketRecvMultipleFDs(int sock, int *fds, size_t nfds, int fdflags) +{ + char byte =3D 0; + struct iovec iov; + struct msghdr msg; + int ret =3D -1; + ssize_t len; + struct cmsghdr *cmsg; + char buf[CMSG_SPACE(sizeof(int) * nfds)]; + int fdflags_recvmsg =3D fdflags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0; + int fdSize =3D -1; + int i =3D 0; + int saved_errno =3D 0; + + if ((fdflags & ~O_CLOEXEC) !=3D 0) { + errno =3D EINVAL; + return -1; + } + + /* send at least one char */ + memset(&msg, 0, sizeof(msg)); + iov.iov_base =3D &byte; + iov.iov_len =3D 1; + msg.msg_iov =3D &iov; + msg.msg_iovlen =3D 1; + msg.msg_name =3D NULL; + msg.msg_namelen =3D 0; + + msg.msg_control =3D buf; + msg.msg_controllen =3D sizeof(buf); + + len =3D recvmsg(sock, &msg, fdflags_recvmsg); + if (len < 0) { + return -1; + } + + cmsg =3D CMSG_FIRSTHDR(&msg); + /* be paranoiac */ + if (len =3D=3D 0 || cmsg =3D=3D NULL || cmsg->cmsg_len < CMSG_LEN(size= of(int)) + || cmsg->cmsg_level !=3D SOL_SOCKET || cmsg->cmsg_type !=3D SCM_RI= GHTS) { + /* fake errno: at end the file is not available */ + errno =3D len ? EACCES : ENOTCONN; + return -1; + } + + fdSize =3D cmsg->cmsg_len - CMSG_LEN(0); + memcpy(fds, CMSG_DATA(cmsg), fdSize); + ret =3D fdSize/sizeof(int); + + /* set close-on-exec flag */ + if (!MSG_CMSG_CLOEXEC && (fdflags & O_CLOEXEC)) { + for (i =3D 0; i < ret; ++i) { + if (virSetCloseExec(fds[i]) < 0) { + saved_errno =3D errno; + goto error; + } + } + } + + return ret; +error: + for (i =3D 0; i < ret; ++i) { + VIR_FORCE_CLOSE(fds[i]); + } + errno =3D saved_errno; + return -1; +} #else /* WIN32 */ int virSocketSendFD(int sock G_GNUC_UNUSED, int fd G_GNUC_UNUSED) @@ -500,4 +576,11 @@ virSocketRecvFD(int sock G_GNUC_UNUSED, int fdflags G_= GNUC_UNUSED) errno =3D ENOSYS; return -1; } + +int +virSocketRecvMultipleFDs(int sock, int *fds, size_t nfds, int fdflags) +{ + errno =3D ENOSYS; + return -1; +} #endif /* WIN32 */ diff --git a/src/util/virsocket.h b/src/util/virsocket.h index 419da8b3ae..c926effbc3 100644 --- a/src/util/virsocket.h +++ b/src/util/virsocket.h @@ -22,6 +22,8 @@ =20 int virSocketSendFD(int sock, int fd); int virSocketRecvFD(int sock, int fdflags); +int +virSocketRecvMultipleFDs(int sock, int *fds, size_t nfds, int fdflags); =20 #ifdef WIN32 =20 --=20 2.31.1