vscode: Add configurations to run the executables under the debugger
[lttng-tools.git] / src / common / unix.cpp
index 091587f48f0edbd31ebdb4d24f6714abba19b131..11ab18a98567cddcf6ca2cb3513f61009150e6cf 100644 (file)
@@ -7,6 +7,13 @@
  */
 
 #define _LGPL_SOURCE
+#include "unix.hpp"
+
+#include <common/common.hpp>
+#include <common/compat/errno.hpp>
+#include <common/fd-handle.hpp>
+#include <common/sessiond-comm/sessiond-comm.hpp>
+
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <common/common.h>
-#include <common/compat/errno.h>
-#include <common/sessiond-comm/sessiond-comm.h>
-#include <common/fd-handle.h>
-
-#include "unix.h"
-
 /*
  * Connect to unix socket using the path name.
  */
@@ -32,8 +32,9 @@ int lttcomm_connect_unix_sock(const char *pathname)
 
        if (strlen(pathname) >= sizeof(s_un.sun_path)) {
                ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
-                               pathname, strlen(pathname) + 1,
-                               sizeof(s_un.sun_path));
+                   pathname,
+                   strlen(pathname) + 1,
+                   sizeof(s_un.sun_path));
                ret = -ENAMETOOLONG;
                goto error;
        }
@@ -110,8 +111,9 @@ int lttcomm_create_unix_sock(const char *pathname)
 
        if (strlen(pathname) >= sizeof(s_un.sun_path)) {
                ERR("unix socket address (\"%s\") is longer than the platform's limit (%zu > %zu).",
-                               pathname, strlen(pathname) + 1,
-                               sizeof(s_un.sun_path));
+                   pathname,
+                   strlen(pathname) + 1,
+                   sizeof(s_un.sun_path));
                ret = -ENAMETOOLONG;
                goto error;
        }
@@ -239,8 +241,10 @@ retry:
                        /*
                         * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
                         */
-                       if (errno == EAGAIN || errno == EWOULDBLOCK ||
-                                       errno == EPIPE) {
+                       DIAGNOSTIC_PUSH
+                       DIAGNOSTIC_IGNORE_LOGICAL_OP
+                       if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EPIPE) {
+                               DIAGNOSTIC_POP
                                /*
                                 * Nothing was recv.
                                 */
@@ -343,8 +347,10 @@ retry:
                        /*
                         * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
                         */
-                       if (errno == EAGAIN || errno == EWOULDBLOCK ||
-                                       errno == EPIPE) {
+                       DIAGNOSTIC_PUSH
+                       DIAGNOSTIC_IGNORE_LOGICAL_OP
+                       if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EPIPE) {
+                               DIAGNOSTIC_POP
                                /*
                                 * This can happen in non blocking mode.
                                 * Nothing was sent.
@@ -373,7 +379,16 @@ int lttcomm_close_unix_sock(int sock)
        /* Shutdown receptions and transmissions */
        ret = shutdown(sock, SHUT_RDWR);
        if (ret < 0) {
-               PERROR("shutdown");
+               /*
+                * The socket is already disconnected, don't error out.
+                * This doesn't happen on Linux, but it does on FreeBSD, see:
+                * https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=227259
+                */
+               if (errno == ENOTCONN) {
+                       ret = 0;
+               } else {
+                       PERROR("shutdown");
+               }
        }
 
        closeret = close(sock);
@@ -409,7 +424,7 @@ ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd)
        if (nb_fd > LTTCOMM_MAX_SEND_FDS)
                return -EINVAL;
 
-       msg.msg_control = (caddr_t)tmp;
+       msg.msg_control = (caddr_t) tmp;
        msg.msg_controllen = CMSG_LEN(sizeof_fds);
 
        cmptr = CMSG_FIRSTHDR(&msg);
@@ -449,17 +464,15 @@ ssize_t lttcomm_send_fds_unix_sock(int sock, const int *fds, size_t nb_fd)
  *
  * Returns the size of data sent, or negative error value.
  */
-static
-ssize_t _lttcomm_send_payload_view_fds_unix_sock(int sock,
-               struct lttng_payload_view *view,
-               bool blocking)
+static ssize_t
+_lttcomm_send_payload_view_fds_unix_sock(int sock, struct lttng_payload_view *view, bool blocking)
 {
        int i;
        ssize_t ret;
        struct lttng_dynamic_array raw_fds;
        const int fd_count = lttng_payload_view_get_fd_handle_count(view);
 
-       lttng_dynamic_array_init(&raw_fds, sizeof(int), NULL);
+       lttng_dynamic_array_init(&raw_fds, sizeof(int), nullptr);
 
        if (fd_count < 0) {
                ret = -LTTNG_ERR_INVALID;
@@ -476,11 +489,9 @@ ssize_t _lttcomm_send_payload_view_fds_unix_sock(int sock,
         * owns a reference to the fd_handles.
         */
        for (i = 0; i < fd_count; i++) {
-               struct fd_handle *handle =
-                               lttng_payload_view_pop_fd_handle(view);
+               struct fd_handle *handle = lttng_payload_view_pop_fd_handle(view);
                const int raw_fd = fd_handle_get_fd(handle);
-               const int add_ret = lttng_dynamic_array_add_element(
-                               &raw_fds, &raw_fd);
+               const int add_ret = lttng_dynamic_array_add_element(&raw_fds, &raw_fd);
 
                fd_handle_put(handle);
                if (add_ret) {
@@ -490,11 +501,10 @@ ssize_t _lttcomm_send_payload_view_fds_unix_sock(int sock,
        }
 
        if (blocking) {
-               ret = lttcomm_send_fds_unix_sock(sock,
-                               (const int *) raw_fds.buffer.data, fd_count);
+               ret = lttcomm_send_fds_unix_sock(sock, (const int *) raw_fds.buffer.data, fd_count);
        } else {
-               ret = lttcomm_send_fds_unix_sock_non_block(sock,
-                               (const int *) raw_fds.buffer.data, fd_count);
+               ret = lttcomm_send_fds_unix_sock_non_block(
+                       sock, (const int *) raw_fds.buffer.data, fd_count);
        }
 
 end:
@@ -502,14 +512,12 @@ end:
        return ret;
 }
 
-ssize_t lttcomm_send_payload_view_fds_unix_sock(int sock,
-               struct lttng_payload_view *view)
+ssize_t lttcomm_send_payload_view_fds_unix_sock(int sock, struct lttng_payload_view *view)
 {
        return _lttcomm_send_payload_view_fds_unix_sock(sock, view, true);
 }
 
-ssize_t lttcomm_send_payload_view_fds_unix_sock_non_block(int sock,
-               struct lttng_payload_view *view)
+ssize_t lttcomm_send_payload_view_fds_unix_sock_non_block(int sock, struct lttng_payload_view *view)
 {
        return _lttcomm_send_payload_view_fds_unix_sock(sock, view, false);
 }
@@ -540,7 +548,7 @@ ssize_t lttcomm_send_fds_unix_sock_non_block(int sock, const int *fds, size_t nb
        if (nb_fd > LTTCOMM_MAX_SEND_FDS)
                return -EINVAL;
 
-       msg.msg_control = (caddr_t)tmp;
+       msg.msg_control = (caddr_t) tmp;
        msg.msg_controllen = CMSG_LEN(sizeof_fds);
 
        cmptr = CMSG_FIRSTHDR(&msg);
@@ -569,7 +577,10 @@ retry:
                        /*
                         * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
                         */
+                       DIAGNOSTIC_PUSH
+                       DIAGNOSTIC_IGNORE_LOGICAL_OP
                        if (errno == EAGAIN || errno == EWOULDBLOCK) {
+                               DIAGNOSTIC_POP
                                /*
                                 * This can happen in non blocking mode.
                                 * Nothing was sent.
@@ -650,8 +661,7 @@ retry:
                        goto retry;
                } else {
                        /* We consider EPIPE and EAGAIN as expected. */
-                       if (!lttng_opt_quiet &&
-                                       (errno != EPIPE && errno != EAGAIN)) {
+                       if (!lttng_opt_quiet && (errno != EPIPE && errno != EAGAIN)) {
                                PERROR("recvmsg");
                        }
                        goto end;
@@ -659,8 +669,7 @@ retry:
        }
 
        if (ret != 1) {
-               fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
-                               ret, 1);
+               fprintf(stderr, "Error: Received %zd bytes, expected %d\n", ret, 1);
                goto end;
        }
 
@@ -676,7 +685,7 @@ retry:
         * need to expect a cmsg of the SCM_CREDENTIALS as the first control
         * message.
         */
-       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
                if (cmsg->cmsg_level != SOL_SOCKET) {
                        fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
                        ret = -1;
@@ -688,7 +697,8 @@ retry:
                         * now copy the fds to the fds ptr and return success.
                         */
                        if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
-                               fprintf(stderr, "Error: Received %zu bytes of"
+                               fprintf(stderr,
+                                       "Error: Received %zu bytes of"
                                        "ancillary data for FDs, expected %zu\n",
                                        (size_t) cmsg->cmsg_len,
                                        (size_t) CMSG_LEN(sizeof_fds));
@@ -714,8 +724,7 @@ end:
        return ret;
 }
 
-static
-void close_raw_fd(void *ptr)
+static void close_raw_fd(void *ptr)
 {
        const int raw_fd = *((const int *) ptr);
 
@@ -728,9 +737,8 @@ void close_raw_fd(void *ptr)
        }
 }
 
-static
-enum lttng_error_code add_fds_to_payload(struct lttng_dynamic_array *raw_fds,
-               struct lttng_payload *payload)
+static enum lttng_error_code add_fds_to_payload(struct lttng_dynamic_array *raw_fds,
+                                               struct lttng_payload *payload)
 {
        int i;
        enum lttng_error_code ret_code = LTTNG_OK;
@@ -739,8 +747,7 @@ enum lttng_error_code add_fds_to_payload(struct lttng_dynamic_array *raw_fds,
        for (i = 0; i < fd_count; i++) {
                int ret;
                struct fd_handle *handle;
-               int *raw_fd = (int *) lttng_dynamic_array_get_element(
-                       raw_fds, i);
+               int *raw_fd = (int *) lttng_dynamic_array_get_element(raw_fds, i);
 
                LTTNG_ASSERT(*raw_fd != -1);
 
@@ -765,9 +772,10 @@ end:
        return ret_code;
 }
 
-static
-ssize_t _lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
-               struct lttng_payload *payload, bool blocking)
+static ssize_t _lttcomm_recv_payload_fds_unix_sock(int sock,
+                                                  size_t nb_fd,
+                                                  struct lttng_payload *payload,
+                                                  bool blocking)
 {
        int i = 0;
        enum lttng_error_code add_ret;
@@ -789,11 +797,10 @@ ssize_t _lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
        }
 
        if (blocking) {
-               ret = lttcomm_recv_fds_unix_sock(
-                               sock, (int *) raw_fds.buffer.data, nb_fd);
+               ret = lttcomm_recv_fds_unix_sock(sock, (int *) raw_fds.buffer.data, nb_fd);
        } else {
                ret = lttcomm_recv_fds_unix_sock_non_block(
-                               sock, (int *) raw_fds.buffer.data, nb_fd);
+                       sock, (int *) raw_fds.buffer.data, nb_fd);
        }
 
        if (ret <= 0) {
@@ -802,7 +809,7 @@ ssize_t _lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
 
        add_ret = add_fds_to_payload(&raw_fds, payload);
        if (add_ret != LTTNG_OK) {
-               ret = - (int) add_ret;
+               ret = -(int) add_ret;
                goto end;
        }
 
@@ -811,14 +818,13 @@ end:
        return ret;
 }
 
-ssize_t lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
-                          struct lttng_payload *payload)
+ssize_t lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd, struct lttng_payload *payload)
 {
        return _lttcomm_recv_payload_fds_unix_sock(sock, nb_fd, payload, true);
 }
 
-ssize_t lttcomm_recv_payload_fds_unix_sock_non_block(int sock, size_t nb_fd,
-                          struct lttng_payload *payload)
+ssize_t
+lttcomm_recv_payload_fds_unix_sock_non_block(int sock, size_t nb_fd, struct lttng_payload *payload)
 {
        return _lttcomm_recv_payload_fds_unix_sock(sock, nb_fd, payload, false);
 }
@@ -883,7 +889,10 @@ retry:
                        /*
                         * We consider EPIPE and EAGAIN/EWOULDBLOCK as expected.
                         */
+                       DIAGNOSTIC_PUSH
+                       DIAGNOSTIC_IGNORE_LOGICAL_OP
                        if (errno == EAGAIN || errno == EWOULDBLOCK) {
+                               DIAGNOSTIC_POP
                                /*
                                 * This can happen in non blocking mode.
                                 * Nothing was recv.
@@ -907,8 +916,7 @@ retry:
        }
 
        if (ret != 1) {
-               fprintf(stderr, "Error: Received %zd bytes, expected %d\n",
-                               ret, 1);
+               fprintf(stderr, "Error: Received %zd bytes, expected %d\n", ret, 1);
                goto end;
        }
 
@@ -924,7 +932,7 @@ retry:
         * need to expect a cmsg of the SCM_CREDENTIALS as the first control
         * message.
         */
-       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
                if (cmsg->cmsg_level != SOL_SOCKET) {
                        fprintf(stderr, "Error: The socket needs to be of type SOL_SOCKET\n");
                        ret = -1;
@@ -936,7 +944,8 @@ retry:
                         * now copy the fds to the fds ptr and return success.
                         */
                        if (cmsg->cmsg_len != CMSG_LEN(sizeof_fds)) {
-                               fprintf(stderr, "Error: Received %zu bytes of"
+                               fprintf(stderr,
+                                       "Error: Received %zu bytes of"
                                        "ancillary data for FDs, expected %zu\n",
                                        (size_t) cmsg->cmsg_len,
                                        (size_t) CMSG_LEN(sizeof_fds));
@@ -1004,7 +1013,7 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, const void *buf, size_t len)
        cmptr->cmsg_type = LTTNG_SOCK_CREDS;
        cmptr->cmsg_len = CMSG_LEN(sizeof_cred);
 
-       creds = (lttng_sock_cred*) CMSG_DATA(cmptr);
+       creds = (lttng_sock_cred *) CMSG_DATA(cmptr);
 
        LTTNG_SOCK_SET_UID_CRED(creds, geteuid());
        LTTNG_SOCK_SET_GID_CRED(creds, getegid());
@@ -1031,8 +1040,7 @@ ssize_t lttcomm_send_creds_unix_sock(int sock, const void *buf, size_t len)
  *
  * Returns the size of received data, or negative error value.
  */
-ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
-               lttng_sock_cred *creds)
+ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len, lttng_sock_cred *creds)
 {
        struct msghdr msg;
        struct iovec iov[1];
@@ -1042,7 +1050,7 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
        struct cmsghdr *cmptr;
        size_t sizeof_cred = sizeof(lttng_sock_cred);
        char anc_buf[CMSG_SPACE(sizeof_cred)];
-#endif /* __linux__, __CYGWIN__ */
+#endif /* __linux__, __CYGWIN__ */
 
        LTTNG_ASSERT(sock);
        LTTNG_ASSERT(buf);
@@ -1087,22 +1095,23 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
        }
 
        cmptr = CMSG_FIRSTHDR(&msg);
-       if (cmptr == NULL) {
+       if (cmptr == nullptr) {
                fprintf(stderr, "Error: Invalid control message header\n");
                ret = -1;
                goto end;
        }
 
-       if (cmptr->cmsg_level != SOL_SOCKET ||
-                       cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
+       if (cmptr->cmsg_level != SOL_SOCKET || cmptr->cmsg_type != LTTNG_SOCK_CREDS) {
                fprintf(stderr, "Didn't received any credentials\n");
                ret = -1;
                goto end;
        }
 
        if (cmptr->cmsg_len != CMSG_LEN(sizeof_cred)) {
-               fprintf(stderr, "Error: Received %zu bytes of ancillary data, expected %zu\n",
-                               (size_t) cmptr->cmsg_len, (size_t) CMSG_LEN(sizeof_cred));
+               fprintf(stderr,
+                       "Error: Received %zu bytes of ancillary data, expected %zu\n",
+                       (size_t) cmptr->cmsg_len,
+                       (size_t) CMSG_LEN(sizeof_cred));
                ret = -1;
                goto end;
        }
@@ -1116,7 +1125,7 @@ ssize_t lttcomm_recv_creds_unix_sock(int sock, void *buf, size_t len,
        }
 #else
 #error "Please implement credential support for your OS."
-#endif /* __linux__, __CYGWIN__ */
+#endif /* __linux__, __CYGWIN__ */
 
 end:
        return ret;
@@ -1138,7 +1147,7 @@ int lttcomm_setsockopt_creds_unix_sock(int sock)
        return ret;
 }
 #elif (defined(__FreeBSD__) || defined(__sun__) || defined(__APPLE__))
-int lttcomm_setsockopt_creds_unix_sock(int sock)
+int lttcomm_setsockopt_creds_unix_sock(int sock __attribute__((unused)))
 {
        return 0;
 }
This page took 0.029371 seconds and 4 git commands to generate.