Fix sending fd through sendmsg/recvmsg ancillary data
[lttng-tools.git] / liblttkconsumerd / liblttkconsumerd.c
index e11229abd5dca98661aa653be113c6b74a6e088c..1d69a4c463faffc1fa764141e287f725a625c535 100644 (file)
@@ -28,6 +28,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <urcu/list.h>
+#include <assert.h>
 
 #include "libkernelctl.h"
 #include "liblttkconsumerd.h"
@@ -124,7 +125,6 @@ static int kconsumerd_find_session_fd(int fd)
        cds_list_for_each_entry(iter, &kconsumerd_data.fd_list.head, list) {
                if (iter->sessiond_fd == fd) {
                        DBG("Duplicate session fd %d", fd);
-                       pthread_mutex_unlock(&kconsumerd_data.lock);
                        return 1;
                }
        }
@@ -586,7 +586,6 @@ static int kconsumerd_consumerd_recv_fd(int sfd,
                struct pollfd *kconsumerd_sockpoll, int size,
                enum kconsumerd_command cmd_type)
 {
-       struct msghdr msg;
        struct iovec iov[1];
        int ret = 0, i, tmp2;
        struct cmsghdr *cmsg;
@@ -597,8 +596,15 @@ static int kconsumerd_consumerd_recv_fd(int sfd,
        /* the number of fds we are about to receive */
        nb_fd = size / sizeof(struct lttcomm_kconsumerd_msg);
 
+       /*
+        * Note: only supporting receiving one FD at a time for now.
+        * This code needs fixing if we wish to receive more (a single
+        * receive for the whole fd batch rather than one per fd).
+        */
+       assert(nb_fd == 1);
+
        for (i = 0; i < nb_fd; i++) {
-               memset(&msg, 0, sizeof(msg));
+               struct msghdr msg = { 0 };
 
                /* Prepare to receive the structures */
                iov[0].iov_base = &lkm;
@@ -632,12 +638,13 @@ static int kconsumerd_consumerd_recv_fd(int sfd,
                        kconsumerd_send_error(KCONSUMERD_ERROR_RECV_FD);
                        goto end;
                }
+
                /* if we received fds */
                if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
                        switch (cmd_type) {
                        case ADD_STREAM:
-                               DBG("kconsumerd_add_fd %s (%d)", lkm.path_name, (CMSG_DATA(cmsg)[0]));
-                               ret = kconsumerd_add_fd(&lkm, (CMSG_DATA(cmsg)[0]));
+                               DBG("kconsumerd_add_fd %s (%d)", lkm.path_name, ((int *) CMSG_DATA(cmsg))[0]);
+                               ret = kconsumerd_add_fd(&lkm, ((int *) CMSG_DATA(cmsg))[0]);
                                if (ret < 0) {
                                        kconsumerd_send_error(KCONSUMERD_OUTFD_ERROR);
                                        goto end;
@@ -651,6 +658,9 @@ static int kconsumerd_consumerd_recv_fd(int sfd,
                        }
                        /* signal the poll thread */
                        tmp2 = write(kconsumerd_poll_pipe[1], "4", 1);
+                       if (tmp2 < 0) {
+                               perror("write kconsumerd poll");
+                       }
                } else {
                        ERR("Didn't received any fd");
                        kconsumerd_send_error(KCONSUMERD_ERROR_RECV_FD);
@@ -762,6 +772,9 @@ void *kconsumerd_thread_poll_fds(void *data)
                if (pollfd[nb_fd].revents == POLLIN) {
                        DBG("kconsumerd_poll_pipe wake up");
                        tmp2 = read(kconsumerd_poll_pipe[0], &tmp, 1);
+                       if (tmp2 < 0) {
+                               perror("read kconsumerd poll");
+                       }
                        continue;
                }
 
@@ -1020,6 +1033,9 @@ void kconsumerd_should_exit(void)
        int ret;
        kconsumerd_quit = 1;
        ret = write(kconsumerd_should_quit[1], "4", 1);
+       if (ret < 0) {
+               perror("write kconsumerd quit");
+       }
 }
 
 /*
This page took 0.024283 seconds and 4 git commands to generate.