Add recv fds function to lttng-comm API
authorDavid Goulet <david.goulet@polymtl.ca>
Tue, 4 Oct 2011 21:49:27 +0000 (17:49 -0400)
committerDavid Goulet <david.goulet@polymtl.ca>
Tue, 4 Oct 2011 21:49:27 +0000 (17:49 -0400)
Signed-off-by: David Goulet <david.goulet@polymtl.ca>
include/lttng-sessiond-comm.h
liblttng-sessiond-comm/lttng-sessiond-comm.c

index f8308505c712c915f3a2e17119ec2dea5c3c15c6..fcdc5a2cf72331234e11dc161e1179e1bb164630 100644 (file)
@@ -248,6 +248,9 @@ extern int lttcomm_close_unix_sock(int sock);
 /* Send fd(s) over a unix socket. */
 extern ssize_t lttcomm_send_fds_unix_sock(int sock, void *buf, int *fds,
                size_t nb_fd, size_t len);
+/* Recv fd(s) over a unix socket */
+extern ssize_t lttcomm_recv_fds_unix_sock(int sock, void *buf, int *fds,
+               size_t nb_fd, size_t len);
 extern ssize_t lttcomm_recv_unix_sock(int sock, void *buf, size_t len);
 extern ssize_t lttcomm_send_unix_sock(int sock, void *buf, size_t len);
 extern const char *lttcomm_get_readable_code(enum lttcomm_return_code code);
index 3c066cd3b9d5c01eb0b7b3d5f84a81feca4982cf..ff78891aad16805243c7df4dd5768ee4dabc6421 100644 (file)
@@ -343,3 +343,65 @@ ssize_t lttcomm_send_fds_unix_sock(int sock, void *buf, int *fds, size_t nb_fd,
 
        return ret;
 }
+
+/*
+ * Receives a single fd from socket.
+ *
+ * Returns the size of received data
+ */
+ssize_t lttcomm_recv_fds_unix_sock(int sock, void *buf, int *fds,
+               size_t nb_fd, size_t len)
+{
+       struct iovec iov[1];
+       int data_fd, i, ret = 0;
+       struct cmsghdr *cmsg;
+       char recv_fd[CMSG_SPACE(sizeof(int))];
+       struct msghdr msg = { 0 };
+       union {
+               unsigned char vc[4];
+               int vi;
+       } tmp;
+
+       /* Prepare to receive the structures */
+       iov[0].iov_base = &data_fd;
+       iov[0].iov_len = sizeof(data_fd);
+       msg.msg_iov = iov;
+       msg.msg_iovlen = 1;
+       msg.msg_control = recv_fd;
+       msg.msg_controllen = sizeof(recv_fd);
+
+       ret = recvmsg(sock, &msg, 0);
+       if (ret < 0) {
+               perror("recvmsg fds");
+               goto end;
+       }
+
+       if (ret != sizeof(data_fd)) {
+               fprintf(stderr, "Error: Received %d bytes, expected %ld",
+                               ret, sizeof(data_fd));
+               goto end;
+       }
+
+       cmsg = CMSG_FIRSTHDR(&msg);
+       if (!cmsg) {
+               fprintf(stderr, "Error: Invalid control message header");
+               ret = -1;
+               goto end;
+       }
+
+       if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
+               fprintf(stderr, "Didn't received any fd");
+               ret = -1;
+               goto end;
+       }
+
+       /* this is our fd */
+       for (i = 0; i < sizeof(int); i++) {
+               tmp.vc[i] = CMSG_DATA(cmsg)[i];
+       }
+
+       ret = tmp.vi;
+
+end:
+       return ret;
+}
This page took 0.0446 seconds and 4 git commands to generate.