From nobody Wed Jan 15 09:15:49 2025 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linux.microsoft.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1705440736309470.2942953640661; Tue, 16 Jan 2024 13:32:16 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 39D201E25; Tue, 16 Jan 2024 16:32:15 -0500 (EST) Received: from lists.libvirt.org.85.43.8.in-addr.arpa (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 92EA41DE3; Tue, 16 Jan 2024 16:26:19 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 817231D54; Tue, 16 Jan 2024 16:25:58 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 613AA1D6A for ; Tue, 16 Jan 2024 16:25:55 -0500 (EST) Received: from home.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 46BF120DF584; Tue, 16 Jan 2024 13:25:54 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 46BF120DF584 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [PATCH v2 3/5] util: Add util methods required by ch networking Date: Tue, 16 Jan 2024 15:25:41 -0600 Message-ID: <20240116212543.24149-4-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116212543.24149-1-prapal@linux.microsoft.com> References: <20240116212543.24149-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: JCCHVAB7CGK2E4ZJO5C5R3MUL7HJVVAC X-Message-ID-Hash: JCCHVAB7CGK2E4ZJO5C5R3MUL7HJVVAC X-MailFrom: prapal@linux.microsoft.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: liuwe@microsoft.com X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1705440736750100001 virSocketSendMsgWithFDs method send fds along with payload using SCM_RIGHTS. virSocketRecv method polls, receives and sends the response to callers. These methods are required to add network suppport in ch driver. Signed-off-by: Praveen K Paladugu --- po/POTFILES | 1 + src/libvirt_private.syms | 2 + src/util/virsocket.c | 116 +++++++++++++++++++++++++++++++++++++++ src/util/virsocket.h | 3 + 4 files changed, 122 insertions(+) diff --git a/po/POTFILES b/po/POTFILES index 023c041f61..b594a8dd39 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -326,6 +326,7 @@ src/util/virscsi.c src/util/virscsihost.c src/util/virscsivhost.c src/util/virsecret.c +src/util/virsocket.c src/util/virsocketaddr.c src/util/virstoragefile.c src/util/virstring.c diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 80d43ed15d..2c9b2635cd 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3371,8 +3371,10 @@ virSecureEraseString; =20 =20 # util/virsocket.h +virSocketRecv; virSocketRecvFD; virSocketSendFD; +virSocketSendMsgWithFDs; =20 =20 # util/virsocketaddr.h diff --git a/src/util/virsocket.c b/src/util/virsocket.c index cd6f7ecd1b..8019fd1199 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -19,11 +19,18 @@ =20 #include =20 +#include "virerror.h" #include "virsocket.h" #include "virutil.h" #include "virfile.h" +#include "virlog.h" =20 #include +#include + +#define PKT_TIMEOUT_MS 500 /* ms */ + +#define VIR_FROM_THIS VIR_FROM_NONE =20 #ifdef WIN32 =20 @@ -482,6 +489,108 @@ virSocketRecvFD(int sock, int fdflags) =20 return fd; } + +/** + * virSocketSendMsgWithFDs: + * @sock: socket to send payload and fds to + * @payload: payload to send + * @fds: array of fds to send + * @fds_len: len of fds array + + * Send @fds along with @payload to @sock using SCM_RIGHTS. + * Return number of bytes sent on success. + * On error, set errno and return -1. + */ +int +virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fd= s_len) +{ + struct msghdr msg; + struct iovec iov[1]; /* Send a single payload, so set vector len to 1 = */ + int ret; + char control[CMSG_SPACE(sizeof(int)*fds_len)]; + struct cmsghdr *cmsg; + + memset(&msg, 0, sizeof(msg)); + memset(control, 0, sizeof(control)); + + iov[0].iov_base =3D (void *) payload; + iov[0].iov_len =3D strlen(payload); + + msg.msg_iov =3D iov; + msg.msg_iovlen =3D 1; + + msg.msg_control =3D control; + msg.msg_controllen =3D sizeof(control); + + cmsg =3D CMSG_FIRSTHDR(&msg); + /* check to eliminate "potential null pointer dereference" errors duri= ng build */ + if (!cmsg) { + virReportSystemError(EFAULT, "%s", _("Couldn't fit control msg hea= der in msg") ); + return -1; + } + + cmsg->cmsg_len =3D CMSG_LEN(sizeof(int) * fds_len); + cmsg->cmsg_level =3D SOL_SOCKET; + cmsg->cmsg_type =3D SCM_RIGHTS; + memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * fds_len); + + do { + ret =3D sendmsg(sock, &msg, 0); + } while (ret < 0 && errno =3D=3D EINTR); + + if (ret < 0) { + virReportSystemError(errno, "%s", _("sendmsg failed") ); + return -1; + } + + return ret; +} + +/** + * virSocketRecv: + * @sock: socket to poll and receive response on + * + * This function polls @sock for response + * Returns received response or NULL on error. + */ +char * +virSocketRecv(int sock) +{ + struct pollfd pfds[1]; + char *buf =3D NULL; + size_t buf_len =3D 1024; + int ret; + + buf =3D g_new0(char, buf_len); + + pfds[0].fd =3D sock; + pfds[0].events =3D POLLIN; + + do { + ret =3D poll(pfds, G_N_ELEMENTS(pfds), PKT_TIMEOUT_MS); + } while (ret < 0 && errno =3D=3D EINTR); + + if (ret <=3D 0) { + if (ret < 0) { + virReportSystemError(errno, _("Poll on sock %1$d failed"), soc= k); + } else if (ret =3D=3D 0) { + virReportSystemError(errno, _("Poll on sock %1$d timed out"), = sock); + } + return NULL; + } + + do { + ret =3D recv(sock, buf, buf_len-1, 0); + } while (ret < 0 && errno =3D=3D EINTR); + + if (ret < 0) { + virReportSystemError(errno, _("recv on sock %1$d failed"), sock); + return NULL; + } + + return g_steal_pointer(&buf); +} + #else /* WIN32 */ int virSocketSendFD(int sock G_GNUC_UNUSED, int fd G_GNUC_UNUSED) @@ -496,4 +605,11 @@ virSocketRecvFD(int sock G_GNUC_UNUSED, int fdflags G_= GNUC_UNUSED) errno =3D ENOSYS; return -1; } + +int +virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fd= s_len) +{ + errno =3D ENOSYS; + return -1; +} #endif /* WIN32 */ diff --git a/src/util/virsocket.h b/src/util/virsocket.h index 419da8b3ae..00fbf52603 100644 --- a/src/util/virsocket.h +++ b/src/util/virsocket.h @@ -22,6 +22,9 @@ =20 int virSocketSendFD(int sock, int fd); int virSocketRecvFD(int sock, int fdflags); +int virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, + size_t fd_len); +char * virSocketRecv(int sock); =20 #ifdef WIN32 =20 --=20 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org