virSocketSendMsgWithFDs: Don't report errors, just set errno

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>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2024-02-02 12:56:04 +01:00
parent b051762e3e
commit e82f99283d
3 changed files with 6 additions and 15 deletions

View File

@ -327,7 +327,6 @@ src/util/virscsi.c
src/util/virscsihost.c src/util/virscsihost.c
src/util/virscsivhost.c src/util/virscsivhost.c
src/util/virsecret.c src/util/virsecret.c
src/util/virsocket.c
src/util/virsocketaddr.c src/util/virsocketaddr.c
src/util/virstoragefile.c src/util/virstoragefile.c
src/util/virstring.c src/util/virstring.c

View File

@ -558,6 +558,7 @@ chProcessAddNetworkDevices(virCHDriver *driver,
g_autofree char *response = NULL; g_autofree char *response = NULL;
size_t j; size_t j;
size_t tapfd_len; size_t tapfd_len;
int saved_errno;
int http_res; int http_res;
int rc; int rc;
@ -597,6 +598,7 @@ chProcessAddNetworkDevices(virCHDriver *driver,
payload = virBufferContentAndReset(&buf); payload = virBufferContentAndReset(&buf);
rc = virSocketSendMsgWithFDs(mon_sockfd, payload, tapfds, tapfd_len); 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 */ /* Close sent tap fds in Libvirt, as they have been dup()ed in CH */
for (j = 0; j < tapfd_len; j++) { for (j = 0; j < tapfd_len; j++) {
@ -604,8 +606,8 @@ chProcessAddNetworkDevices(virCHDriver *driver,
} }
if (rc < 0) { if (rc < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportSystemError(saved_errno, "%s",
_("Failed to send net-add request to CH")); _("Failed to send net-add request to CH"));
return -1; return -1;
} }

View File

@ -19,16 +19,12 @@
#include <config.h> #include <config.h>
#include "virerror.h"
#include "virsocket.h" #include "virsocket.h"
#include "virutil.h" #include "virutil.h"
#include "virfile.h" #include "virfile.h"
#include "virlog.h"
#include <fcntl.h> #include <fcntl.h>
#define VIR_FROM_THIS VIR_FROM_NONE
#ifdef WIN32 #ifdef WIN32
# define FD2SK(fd) _get_osfhandle(fd) # 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); cmsg = CMSG_FIRSTHDR(&msg);
/* check to eliminate "potential null pointer dereference" errors during build */ /* check to eliminate "potential null pointer dereference" errors during build */
if (!cmsg) { if (!cmsg) {
virReportSystemError(EFAULT, "%s", _("Couldn't fit control msg header in msg")); errno = ENOSPC;
return -1; return -1;
} }
@ -536,11 +532,6 @@ virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fds_len)
ret = sendmsg(sock, &msg, 0); ret = sendmsg(sock, &msg, 0);
} while (ret < 0 && errno == EINTR); } while (ret < 0 && errno == EINTR);
if (ret < 0) {
virReportSystemError(errno, "%s", _("sendmsg failed"));
return -1;
}
return ret; return ret;
} }
@ -565,8 +556,7 @@ virSocketSendMsgWithFDs(int sock G_GNUC_UNUSED,
int *fds G_GNUC_UNUSED, int *fds G_GNUC_UNUSED,
size_t fds_len G_GNUC_UNUSED) size_t fds_len G_GNUC_UNUSED)
{ {
virReportSystemError(ENOSYS, "%s", errno = ENOSYS;
_("FD passing is not supported on this platform"));
return -1; return -1;
} }
#endif /* WIN32 */ #endif /* WIN32 */