ust-fd: Add close_range declaration
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-comm.c
index 679b40e6e40725a60bc166cfd2e4d2dc88f8d5d6..5976f6336e0dee543900e6c73881776533e698cf 100644 (file)
@@ -71,6 +71,8 @@ static const char *ustcomm_readable_code[] = {
        [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_MAGIC) ] = "Invalid magic number",
        [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_INVAL_SOCKET_TYPE) ] = "Invalid socket type",
        [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_UNSUP_MAJOR) ] = "Unsupported major version",
+       [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PEERCRED) ] = "Cannot get unix socket peer credentials",
+       [ USTCOMM_CODE_OFFSET(LTTNG_UST_ERR_PEERCRED_PID) ] = "Peer credentials PID is invalid. Socket appears to belong to a distinct, non-nested pid namespace.",
 };
 
 /*
@@ -107,7 +109,7 @@ int ustcomm_connect_unix_sock(const char *pathname, long timeout)
         * libust threads require the close-on-exec flag for all
         * resources so it does not leak file descriptors upon exec.
         */
-       fd = socket(PF_UNIX, SOCK_STREAM, 0);
+       fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
        if (fd < 0) {
                PERROR("socket");
                ret = -errno;
@@ -122,12 +124,6 @@ int ustcomm_connect_unix_sock(const char *pathname, long timeout)
                        WARN("Error setting connect socket send timeout");
                }
        }
-       ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
-       if (ret < 0) {
-               PERROR("fcntl");
-               ret = -errno;
-               goto error_fcntl;
-       }
 
        memset(&sun, 0, sizeof(sun));
        sun.sun_family = AF_UNIX;
@@ -155,7 +151,6 @@ int ustcomm_connect_unix_sock(const char *pathname, long timeout)
        return fd;
 
 error_connect:
-error_fcntl:
        {
                int closeret;
 
@@ -438,8 +433,6 @@ ssize_t ustcomm_send_fds_unix_sock(int sock, int *fds, size_t nb_fd)
 /*
  * Recv a message accompanied by fd(s) from a unix socket.
  *
- * Returns the size of received data, or negative error value.
- *
  * Expect at most "nb_fd" file descriptors. Returns the number of fd
  * actually received in nb_fd.
  * Returns -EPIPE on orderly shutdown.
@@ -465,7 +458,7 @@ ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
        msg.msg_controllen = sizeof(recv_fd);
 
        do {
-               ret = recvmsg(sock, &msg, 0);
+               ret = recvmsg(sock, &msg, MSG_CMSG_CLOEXEC);
        } while (ret < 0 && errno == EINTR);
        if (ret < 0) {
                if (errno != EPIPE && errno != ECONNRESET) {
@@ -508,8 +501,10 @@ ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
                ret = -1;
                goto end;
        }
+
        memcpy(fds, CMSG_DATA(cmsg), sizeof_fds);
-       ret = sizeof_fds;
+
+       ret = nb_fd;
 end:
        return ret;
 }
@@ -853,12 +848,15 @@ static
 int serialize_integer_type(struct ustctl_integer_type *uit,
                const struct lttng_integer_type *lit)
 {
+       int32_t encoding;
+
        uit->size = lit->size;
        uit->signedness = lit->signedness;
        uit->reverse_byte_order = lit->reverse_byte_order;
        uit->base = lit->base;
-       if (serialize_string_encoding(&uit->encoding, lit->encoding))
+       if (serialize_string_encoding(&encoding, lit->encoding))
                return -EINVAL;
+       uit->encoding = encoding;
        uit->alignment = lit->alignment;
        return 0;
 }
@@ -880,9 +878,11 @@ int serialize_basic_type(struct lttng_session *session,
        }
        case atype_string:
        {
-               if (serialize_string_encoding(&ubt->string.encoding,
-                               lbt->string.encoding))
+               int32_t encoding;
+
+               if (serialize_string_encoding(&encoding, lbt->string.encoding))
                        return -EINVAL;
+               ubt->string.encoding = encoding;
                *uatype = ustctl_atype_string;
                break;
        }
@@ -1007,13 +1007,15 @@ int serialize_one_field(struct lttng_session *session,
        {
                struct ustctl_field *uf = &fields[*iter_output];
                struct ustctl_type *ut = &uf->type;
+               enum ustctl_abstract_types atype;
 
                strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
                uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
-               ret = serialize_basic_type(session, &ut->atype, lt->atype,
+               ret = serialize_basic_type(session, &atype, lt->atype,
                        &ut->u.basic, &lt->u.basic);
                if (ret)
                        return ret;
+               ut->atype = atype;
                (*iter_output)++;
                break;
        }
@@ -1023,6 +1025,7 @@ int serialize_one_field(struct lttng_session *session,
                struct ustctl_type *ut = &uf->type;
                struct ustctl_basic_type *ubt;
                const struct lttng_basic_type *lbt;
+               enum ustctl_abstract_types atype;
 
                strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
                uf->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
@@ -1030,10 +1033,11 @@ int serialize_one_field(struct lttng_session *session,
                ubt = &ut->u.array.elem_type;
                lbt = &lt->u.array.elem_type;
                ut->u.array.length = lt->u.array.length;
-               ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
+               ret = serialize_basic_type(session, &atype, lbt->atype,
                        &ubt->u.basic, &lbt->u.basic);
                if (ret)
                        return -EINVAL;
+               ubt->atype = atype;
                ut->atype = ustctl_atype_array;
                (*iter_output)++;
                break;
@@ -1044,6 +1048,7 @@ int serialize_one_field(struct lttng_session *session,
                struct ustctl_type *ut = &uf->type;
                struct ustctl_basic_type *ubt;
                const struct lttng_basic_type *lbt;
+               enum ustctl_abstract_types atype;
                int ret;
 
                strncpy(uf->name, lf->name, LTTNG_UST_SYM_NAME_LEN);
@@ -1051,16 +1056,18 @@ int serialize_one_field(struct lttng_session *session,
                uf->type.atype = ustctl_atype_sequence;
                ubt = &ut->u.sequence.length_type;
                lbt = &lt->u.sequence.length_type;
-               ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
+               ret = serialize_basic_type(session, &atype, lbt->atype,
                        &ubt->u.basic, &lbt->u.basic);
                if (ret)
                        return -EINVAL;
+               ubt->atype = atype;
                ubt = &ut->u.sequence.elem_type;
                lbt = &lt->u.sequence.elem_type;
-               ret = serialize_basic_type(session, &ubt->atype, lbt->atype,
+               ret = serialize_basic_type(session, &atype, lbt->atype,
                        &ubt->u.basic, &lbt->u.basic);
                if (ret)
                        return -EINVAL;
+               ubt->atype = atype;
                ut->atype = ustctl_atype_sequence;
                (*iter_output)++;
                break;
This page took 0.026321 seconds and 4 git commands to generate.