virSocketSendMsgWithFDs: Introduce @payload_len argument

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>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Michal Privoznik 2024-02-02 13:05:08 +01:00
parent e82f99283d
commit 495a826dbf
3 changed files with 14 additions and 5 deletions

View File

@ -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 */

View File

@ -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)
{

View File

@ -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