-static int send_app_msg(int sock, struct lttcomm_ust_msg *lum)
-{
- ssize_t len;
-
- len = lttcomm_send_unix_sock(sock, lum, sizeof(*lum));
- switch (len) {
- case sizeof(*lum):
- printf("message successfully sent\n");
- break;
- case -1:
- if (errno == ECONNRESET) {
- printf("remote end closed connection\n");
- return 0;
- }
- return -1;
- default:
- printf("incorrect message size: %zd\n", len);
- return -1;
- }
- return 0;
-}
-
-static int recv_app_reply(int sock, struct lttcomm_ust_reply *lur,
- uint32_t expected_handle, uint32_t expected_cmd)
-{
- ssize_t len;
-
- memset(lur, 0, sizeof(*lur));
- len = lttcomm_recv_unix_sock(sock, lur, sizeof(*lur));
- switch (len) {
- case 0: /* orderly shutdown */
- printf("Application has performed an orderly shutdown\n");
- return -EINVAL;
- case sizeof(*lur):
- printf("result message received\n");
- if (lur->handle != expected_handle) {
- printf("Unexpected result message handle\n");
- return -EINVAL;
- }
-
- if (lur->cmd != expected_cmd) {
- printf("Unexpected result message command\n");
- return -EINVAL;
- }
- if (lur->ret_code != LTTCOMM_OK) {
- printf("remote operation failed with code %d.\n",
- lur->ret_code);
- return lur->ret_code;
- }
- return 0;
- case -1:
- if (errno == ECONNRESET) {
- printf("remote end closed connection\n");
- return -EINVAL;
- }
- return -1;
- default:
- printf("incorrect message size: %zd\n", len);
- return len > 0 ? -1 : len;
- }
-}
-
-static int send_app_cmd(int sock,
- struct lttcomm_ust_msg *lum,
- struct lttcomm_ust_reply *lur)
-{
- int ret;
-
- ret = send_app_msg(sock, lum);
- if (ret)
- return ret;
- ret = recv_app_reply(sock, lur, lum->handle, lum->cmd);
- if (ret)
- return ret;
- return 0;
-}
-
-
-/*
- * Receives a single fd from socket.
- *
- * Returns the size of received data
- */
-static int lttcomm_recv_fd(int sock)
-{
- struct iovec iov[1];
- int ret = 0;
- int data_fd;
- struct cmsghdr *cmsg;
- char recv_fd[CMSG_SPACE(sizeof(int))];
- struct msghdr msg = { 0 };
-
- /* 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);
-
- printf("Waiting to receive fd\n");
- if ((ret = recvmsg(sock, &msg, 0)) < 0) {
- perror("recvmsg");
- goto end;
- }
- if (ret != sizeof(data_fd)) {
- printf("Received %d bytes, expected %ld", ret, sizeof(data_fd));
- goto end;
- }
- cmsg = CMSG_FIRSTHDR(&msg);
- if (!cmsg) {
- printf("Invalid control message header\n");
- ret = -1;
- goto end;
- }
- if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
- printf("Didn't received any fd\n");
- ret = -1;
- goto end;
- }
- /* this is our fd */
- ret = ((int *) CMSG_DATA(cmsg))[0];
- printf("received fd %d\n", ret);
-end:
- return ret;
-}
-
-