Fix: ustctl need to send the second fd upon error of 1st fd
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
index b3567dda36f0c14042546f41d62656fcbadf79f0..0116b5f744160cab7d4077146d4a532f606359d8 100644 (file)
@@ -46,6 +46,7 @@
 #include <usterr-signal-safe.h>
 #include "tracepoint-internal.h"
 #include "ltt-tracer-core.h"
+#include "../libringbuffer/tlsfixup.h"
 
 /*
  * Has lttng ust comm constructor been called ?
@@ -289,28 +290,30 @@ end:
                //lur.ret_code = USTCOMM_SESSION_FAIL;
                lur.ret_code = ret;
        }
-       switch (lum->cmd) {
-       case LTTNG_UST_STREAM:
-               /*
-                * Special-case reply to send stream info.
-                * Use lum.u output.
-                */
-               lur.u.stream.memory_map_size = *args.stream.memory_map_size;
-               shm_fd = *args.stream.shm_fd;
-               wait_fd = *args.stream.wait_fd;
-               break;
-       case LTTNG_UST_METADATA:
-       case LTTNG_UST_CHANNEL:
-               lur.u.channel.memory_map_size = *args.channel.memory_map_size;
-               shm_fd = *args.channel.shm_fd;
-               wait_fd = *args.channel.wait_fd;
-               break;
-       case LTTNG_UST_TRACER_VERSION:
-               lur.u.version = lum->u.version;
-               break;
-       case LTTNG_UST_TRACEPOINT_LIST_GET:
-               memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
-               break;
+       if (ret >= 0) {
+               switch (lum->cmd) {
+               case LTTNG_UST_STREAM:
+                       /*
+                        * Special-case reply to send stream info.
+                        * Use lum.u output.
+                        */
+                       lur.u.stream.memory_map_size = *args.stream.memory_map_size;
+                       shm_fd = *args.stream.shm_fd;
+                       wait_fd = *args.stream.wait_fd;
+                       break;
+               case LTTNG_UST_METADATA:
+               case LTTNG_UST_CHANNEL:
+                       lur.u.channel.memory_map_size = *args.channel.memory_map_size;
+                       shm_fd = *args.channel.shm_fd;
+                       wait_fd = *args.channel.wait_fd;
+                       break;
+               case LTTNG_UST_TRACER_VERSION:
+                       lur.u.version = lum->u.version;
+                       break;
+               case LTTNG_UST_TRACEPOINT_LIST_GET:
+                       memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
+                       break;
+               }
        }
        ret = send_reply(sock, &lur);
        if (ret < 0) {
@@ -322,14 +325,20 @@ end:
             || lum->cmd == LTTNG_UST_CHANNEL
             || lum->cmd == LTTNG_UST_METADATA)
                        && lur.ret_code == USTCOMM_OK) {
+               int sendret = 0;
+
                /* we also need to send the file descriptors. */
                ret = ustcomm_send_fds_unix_sock(sock,
                        &shm_fd, &shm_fd,
                        1, sizeof(int));
                if (ret < 0) {
                        perror("send shm_fd");
-                       goto error;
+                       sendret = ret;
                }
+               /*
+                * The sessiond expects 2 file descriptors, even upon
+                * error.
+                */
                ret = ustcomm_send_fds_unix_sock(sock,
                        &wait_fd, &wait_fd,
                        1, sizeof(int));
@@ -337,6 +346,10 @@ end:
                        perror("send wait_fd");
                        goto error;
                }
+               if (sendret) {
+                       ret = sendret;
+                       goto error;
+               }
        }
        /*
         * We still have the memory map reference, and the fds have been
@@ -393,7 +406,7 @@ void cleanup_sock_info(struct sock_info *sock_info, int exiting)
        int ret;
 
        if (sock_info->socket != -1) {
-               ret = close(sock_info->socket);
+               ret = ustcomm_close_unix_sock(sock_info->socket);
                if (ret) {
                        ERR("Error closing apps socket");
                }
@@ -673,7 +686,7 @@ restart:
        }
 
        if (sock_info->socket != -1) {
-               ret = close(sock_info->socket);
+               ret = ustcomm_close_unix_sock(sock_info->socket);
                if (ret) {
                        ERR("Error closing %s apps socket", sock_info->name);
                }
@@ -830,6 +843,15 @@ void __attribute__((constructor)) lttng_ust_init(void)
        if (uatomic_xchg(&initialized, 1) == 1)
                return;
 
+       /*
+        * Fixup interdependency between TLS fixup mutex (which happens
+        * to be the dynamic linker mutex) and ust_lock, taken within
+        * the ust lock.
+        */
+       lttng_fixup_event_tls();
+       lttng_fixup_ringbuffer_tls();
+       lttng_fixup_vtid_tls();
+
        /*
         * We want precise control over the order in which we construct
         * our sub-libraries vs starting to receive commands from
This page took 0.024694 seconds and 4 git commands to generate.