[PATCH 0/5] A couple of virsocket related cleanups

After I've merged some patches mingw fails to build. The first patch fixes the issue, and the rest is just a cleanup. Michal Prívozník (5): virsocket: Drop unused #include and #define virSocketSendMsgWithFDs: Don't report errors, just set errno virSocketSendMsgWithFDs: Introduce @payload_len argument virsocket: Simplify virSocketSendFD() qemu_monitor: Simplify qemuMonitorIOWriteWithFD() po/POTFILES | 1 - src/ch/ch_process.c | 11 ++++++--- src/qemu/qemu_monitor.c | 27 ++------------------ src/util/virsocket.c | 55 ++++++++++------------------------------- src/util/virsocket.h | 4 +-- 5 files changed, 25 insertions(+), 73 deletions(-) -- 2.43.0

Inside of virsocket.c there is an include of poll.h and PKT_TIMEOUT_MS macro definition. Neither of these is really needed and in fact it's a leftover after I reworked one of previously merged commits during review. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virsocket.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/util/virsocket.c b/src/util/virsocket.c index 3b274a4eec..a4c646e759 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -26,9 +26,6 @@ #include "virlog.h" #include <fcntl.h> -#include <poll.h> - -#define PKT_TIMEOUT_MS 500 /* ms */ #define VIR_FROM_THIS VIR_FROM_NONE -- 2.43.0

On a Friday in 2024, Michal Privoznik wrote:
Inside of virsocket.c there is an include of poll.h and PKT_TIMEOUT_MS macro definition. Neither of these is really needed and in fact it's a leftover after I reworked one of previously merged commits during review.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virsocket.c | 3 --- 1 file changed, 3 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

Currently, virSocketSendMsgWithFDs() reports two errors: 1) if CMSG_FIRSTHDR() fails, 2) if sendmsg() fails. Well, the latter sets an errno, so caller can just use virReportSystemError(). And the former - it is very unlikely to fail because memory for whole control message was allocated just a few lines above. The motivation is to unify behavior of virSocketSendMsgWithFDs() and virSocketSendFD() because the latter is just a subset of the former (will be addressed later). Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- po/POTFILES | 1 - src/ch/ch_process.c | 6 ++++-- src/util/virsocket.c | 14 ++------------ 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/po/POTFILES b/po/POTFILES index e48b9023e2..428919815d 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -327,7 +327,6 @@ 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/ch/ch_process.c b/src/ch/ch_process.c index 86d3190324..3265ae90de 100644 --- a/src/ch/ch_process.c +++ b/src/ch/ch_process.c @@ -558,6 +558,7 @@ chProcessAddNetworkDevices(virCHDriver *driver, g_autofree char *response = NULL; size_t j; size_t tapfd_len; + int saved_errno; int http_res; int rc; @@ -597,6 +598,7 @@ chProcessAddNetworkDevices(virCHDriver *driver, payload = virBufferContentAndReset(&buf); rc = virSocketSendMsgWithFDs(mon_sockfd, payload, tapfds, tapfd_len); + saved_errno = errno; /* Close sent tap fds in Libvirt, as they have been dup()ed in CH */ for (j = 0; j < tapfd_len; j++) { @@ -604,8 +606,8 @@ chProcessAddNetworkDevices(virCHDriver *driver, } if (rc < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Failed to send net-add request to CH")); + virReportSystemError(saved_errno, "%s", + _("Failed to send net-add request to CH")); return -1; } diff --git a/src/util/virsocket.c b/src/util/virsocket.c index a4c646e759..ff06eb15f7 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -19,16 +19,12 @@ #include <config.h> -#include "virerror.h" #include "virsocket.h" #include "virutil.h" #include "virfile.h" -#include "virlog.h" #include <fcntl.h> -#define VIR_FROM_THIS VIR_FROM_NONE - #ifdef WIN32 # define FD2SK(fd) _get_osfhandle(fd) @@ -523,7 +519,7 @@ virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fds_len) cmsg = CMSG_FIRSTHDR(&msg); /* check to eliminate "potential null pointer dereference" errors during build */ if (!cmsg) { - virReportSystemError(EFAULT, "%s", _("Couldn't fit control msg header in msg")); + errno = ENOSPC; return -1; } @@ -536,11 +532,6 @@ virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fds_len) ret = sendmsg(sock, &msg, 0); } while (ret < 0 && errno == EINTR); - if (ret < 0) { - virReportSystemError(errno, "%s", _("sendmsg failed")); - return -1; - } - return ret; } @@ -565,8 +556,7 @@ virSocketSendMsgWithFDs(int sock G_GNUC_UNUSED, int *fds G_GNUC_UNUSED, size_t fds_len G_GNUC_UNUSED) { - virReportSystemError(ENOSYS, "%s", - _("FD passing is not supported on this platform")); + errno = ENOSYS; return -1; } #endif /* WIN32 */ -- 2.43.0

On a Friday in 2024, Michal Privoznik wrote:
Currently, virSocketSendMsgWithFDs() reports two errors:
1) if CMSG_FIRSTHDR() fails, 2) if sendmsg() fails.
Well, the latter sets an errno, so caller can just use virReportSystemError(). And the former - it is very unlikely to fail because memory for whole control message was allocated just a few lines above.
The motivation is to unify behavior of virSocketSendMsgWithFDs() and virSocketSendFD() because the latter is just a subset of the former (will be addressed later).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- po/POTFILES | 1 - src/ch/ch_process.c | 6 ++++-- src/util/virsocket.c | 14 ++------------ 3 files changed, 6 insertions(+), 15 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

Instead of using strlen() to calculate length of payload we're sending, let caller specify the size: they may want to send just a portion of a buffer (even though the only current user doesn't). Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/ch/ch_process.c | 5 ++++- src/util/virsocket.c | 10 ++++++++-- src/util/virsocket.h | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c index 3265ae90de..3bde9d9dcf 100644 --- a/src/ch/ch_process.c +++ b/src/ch/ch_process.c @@ -558,6 +558,7 @@ chProcessAddNetworkDevices(virCHDriver *driver, g_autofree char *response = NULL; size_t j; size_t tapfd_len; + size_t payload_len; int saved_errno; int http_res; int rc; @@ -595,9 +596,11 @@ chProcessAddNetworkDevices(virCHDriver *driver, virBufferAsprintf(&buf, "%s", virBufferCurrentContent(&http_headers)); virBufferAsprintf(&buf, "Content-Length: %ld\r\n\r\n", strlen(payload)); virBufferAsprintf(&buf, "%s", payload); + payload_len = virBufferUse(&buf); payload = virBufferContentAndReset(&buf); - rc = virSocketSendMsgWithFDs(mon_sockfd, payload, tapfds, tapfd_len); + rc = virSocketSendMsgWithFDs(mon_sockfd, payload, payload_len, + tapfds, tapfd_len); saved_errno = errno; /* Close sent tap fds in Libvirt, as they have been dup()ed in CH */ diff --git a/src/util/virsocket.c b/src/util/virsocket.c index ff06eb15f7..a7272a3ec9 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -488,6 +488,7 @@ virSocketRecvFD(int sock, int fdflags) * virSocketSendMsgWithFDs: * @sock: socket to send payload and fds to * @payload: payload to send + * @payload_len: length of @payload * @fds: array of fds to send * @fds_len: len of fds array @@ -496,7 +497,11 @@ virSocketRecvFD(int sock, int fdflags) * On error, set errno and return -1. */ int -virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fds_len) +virSocketSendMsgWithFDs(int sock, + const char *payload, + size_t payload_len, + int *fds, + size_t fds_len) { g_autofree char *control = NULL; const size_t control_size = CMSG_SPACE(sizeof(int) * fds_len); @@ -508,7 +513,7 @@ virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fds_len) control = g_new0(char, control_size); iov[0].iov_base = (void *) payload; - iov[0].iov_len = strlen(payload); + iov[0].iov_len = payload_len; msg.msg_iov = iov; msg.msg_iovlen = 1; @@ -553,6 +558,7 @@ virSocketRecvFD(int sock G_GNUC_UNUSED, int fdflags G_GNUC_UNUSED) int virSocketSendMsgWithFDs(int sock G_GNUC_UNUSED, const char *payload G_GNUC_UNUSED, + size_t payload_len G_GNUC_UNUSED, int *fds G_GNUC_UNUSED, size_t fds_len G_GNUC_UNUSED) { diff --git a/src/util/virsocket.h b/src/util/virsocket.h index 31a31c2378..0e7f9616f2 100644 --- a/src/util/virsocket.h +++ b/src/util/virsocket.h @@ -22,8 +22,8 @@ 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); +int virSocketSendMsgWithFDs(int sock, const char *payload, size_t payload_len, + int *fds, size_t fds_len); #ifdef WIN32 -- 2.43.0

On a Friday in 2024, Michal Privoznik wrote:
Instead of using strlen() to calculate length of payload we're sending, let caller specify the size: they may want to send just a portion of a buffer (even though the only current user doesn't).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/ch/ch_process.c | 5 ++++- src/util/virsocket.c | 10 ++++++++-- src/util/virsocket.h | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

After previous cleanups, virSocketSendFD() is but a thin wrapper over virSocketSendMsgWithFDs(). Replace the body of the former with a call to the latter. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virsocket.c | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/src/util/virsocket.c b/src/util/virsocket.c index a7272a3ec9..c4e214e76a 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -388,32 +388,10 @@ int virSocketSendFD(int sock, int fd) { char byte = 0; - struct iovec iov; - struct msghdr msg = { 0 }; - struct cmsghdr *cmsg; - char buf[CMSG_SPACE(sizeof(fd))]; + int fds[] = { fd }; - /* send at least one char */ - iov.iov_base = &byte; - iov.iov_len = 1; - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_name = NULL; - msg.msg_namelen = 0; - - msg.msg_control = buf; - msg.msg_controllen = sizeof(buf); - cmsg = CMSG_FIRSTHDR(&msg); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); - /* Initialize the payload: */ - memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd)); - msg.msg_controllen = cmsg->cmsg_len; - - if (sendmsg(sock, &msg, 0) != iov.iov_len) - return -1; - return 0; + return virSocketSendMsgWithFDs(sock, &byte, sizeof(byte), + fds, G_N_ELEMENTS(fds)); } -- 2.43.0

On a Friday in 2024, Michal Privoznik wrote:
After previous cleanups, virSocketSendFD() is but a thin wrapper over virSocketSendMsgWithFDs().
Almost. virSocketSendFD is documented to return 0 on success. After this change, it returns the number of bytes sent. None of the callers care, so adjusting/removing the comment is enough. Also, virSocketSendMsgWithFDs retries on EINTR. This should not do any harm when called from virNetSocketSendFD, but you can also delete the while (ret < 0 && errno == EINTR) wrapper from virFileOpenForked.
Replace the body of the former with a call to the latter.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/util/virsocket.c | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-)
With the comment adjusted to reflect the new return value: Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

After previous cleanups, qemuMonitorIOWriteWithFD() is but a thin wrapper over virSocketSendMsgWithFDs(). Replace the body of the former with a call to the latter. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/qemu/qemu_monitor.c | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index a1773d86d4..9471fe711e 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -308,32 +308,9 @@ qemuMonitorIOWriteWithFD(qemuMonitor *mon, size_t len, int fd) { - struct msghdr msg = { 0 }; - struct iovec iov[1]; - int ret; - char control[CMSG_SPACE(sizeof(int))] = { 0 }; - struct cmsghdr *cmsg; + int fds[] = { fd }; - iov[0].iov_base = (void *)data; - iov[0].iov_len = len; - - msg.msg_iov = iov; - msg.msg_iovlen = 1; - - msg.msg_control = control; - msg.msg_controllen = sizeof(control); - - cmsg = CMSG_FIRSTHDR(&msg); - cmsg->cmsg_len = CMSG_LEN(sizeof(int)); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); - - do { - ret = sendmsg(mon->fd, &msg, 0); - } while (ret < 0 && errno == EINTR); - - return ret; + return virSocketSendMsgWithFDs(mon->fd, data, len, fds, G_N_ELEMENTS(fds)); } -- 2.43.0

On a Friday in 2024, Michal Privoznik wrote:
After previous cleanups, qemuMonitorIOWriteWithFD() is but a thin wrapper over virSocketSendMsgWithFDs(). Replace the body of the former with a call to the latter.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- src/qemu/qemu_monitor.c | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano

On Fri, Feb 02, 2024 at 03:20:29PM +0100, Michal Privoznik wrote:
After I've merged some patches mingw fails to build. The first patch fixes the issue, and the rest is just a cleanup.
Michal Pr??vozn??k (5): virsocket: Drop unused #include and #define virSocketSendMsgWithFDs: Don't report errors, just set errno virSocketSendMsgWithFDs: Introduce @payload_len argument virsocket: Simplify virSocketSendFD() qemu_monitor: Simplify qemuMonitorIOWriteWithFD()
po/POTFILES | 1 - src/ch/ch_process.c | 11 ++++++--- src/qemu/qemu_monitor.c | 27 ++------------------ src/util/virsocket.c | 55 ++++++++++------------------------------- src/util/virsocket.h | 4 +-- 5 files changed, 25 insertions(+), 73 deletions(-)
Thanks for the cleanup patches Michal. LGTM! Regards, Praveen
-- 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org
participants (3)
-
Ján Tomko
-
Michal Privoznik
-
Praveen Paladugu