]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsocket: Simplify virSocketSendFD()
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 2 Feb 2024 12:05:20 +0000 (13:05 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 7 Feb 2024 11:34:54 +0000 (12:34 +0100)
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>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/virfile.c
src/util/virsocket.c

index f3108e99cf6bbd459a5416cda605a3ff93b541ba..9463833d31c13a9526b0666c30c73d66fe9680d2 100644 (file)
@@ -2445,9 +2445,7 @@ virFileOpenForked(const char *path, int openflags, mode_t mode,
             goto childerror;
         }
 
-        do {
-            ret = virSocketSendFD(pair[1], fd);
-        } while (ret < 0 && errno == EINTR);
+        ret = virSocketSendFD(pair[1], fd);
 
         if (ret < 0) {
             ret = -errno;
index a7272a3ec9dc0400a2141add3e067b30e9e811c5..01d84ee973af293027b971c62f9a8e4ca8a4e2b6 100644 (file)
@@ -382,38 +382,16 @@ vir_socket(int domain, int type, int protocol)
 /* virSocketSendFD sends the file descriptor fd along the socket
    to a process calling virSocketRecvFD on the other end.
 
-   Return 0 on success, or -1 with errno set in case of error.
+   Return 1 on success, or -1 with errno set in case of error.
 */
 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))];
-
-    /* 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;
+    int fds[] = { fd };
 
-    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));
 }