Fix: set FD_CLOEXEC on incoming FDs.
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-comm.c
index 679b40e6e40725a60bc166cfd2e4d2dc88f8d5d6..46f2e9e3343d0b36b45c919bf2755294fd281ff9 100644 (file)
@@ -106,6 +106,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.
+        * SOCK_CLOEXEC is not used since it is linux specific.
         */
        fd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (fd < 0) {
@@ -438,8 +439,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.
@@ -453,6 +452,7 @@ ssize_t ustcomm_recv_fds_unix_sock(int sock, int *fds, size_t nb_fd)
        char recv_fd[CMSG_SPACE(sizeof_fds)];
        struct msghdr msg;
        char dummy;
+       int i;
 
        memset(&msg, 0, sizeof(msg));
 
@@ -508,8 +508,19 @@ 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;
+
+       /* Set FD_CLOEXEC */
+       for (i = 0; i < nb_fd; i++) {
+               ret = fcntl(fds[i], F_SETFD, FD_CLOEXEC);
+               if (ret < 0) {
+                       PERROR("fcntl failed to set FD_CLOEXEC on fd %d",
+                              fds[i]);
+               }
+       }
+
+       ret = nb_fd;
 end:
        return ret;
 }
@@ -853,12 +864,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 +894,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 +1023,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 +1041,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 +1049,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 +1064,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 +1072,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.024772 seconds and 4 git commands to generate.