Fix: Block all signals in listener thread
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
index fa184ad74270d50dfe29e45c3c0984a2b2d6c4b5..0d4f5ef9e9c389656b23016c8e63191ce9dce923 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 ?
@@ -324,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));
@@ -339,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
@@ -395,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");
                }
@@ -675,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);
                }
@@ -826,6 +837,7 @@ int get_timeout(struct timespec *constructor_timeout)
 void __attribute__((constructor)) lttng_ust_init(void)
 {
        struct timespec constructor_timeout;
+       sigset_t sig_all_blocked, orig_parent_mask;
        int timeout_mode;
        int ret;
 
@@ -838,6 +850,8 @@ void __attribute__((constructor)) lttng_ust_init(void)
         * 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
@@ -860,6 +874,18 @@ void __attribute__((constructor)) lttng_ust_init(void)
        if (ret) {
                ERR("Error setting up to local apps");
        }
+
+       /* A new thread created by pthread_create inherits the signal mask
+        * from the parent. To avoid any signal being received by the
+        * listener thread, we block all signals temporarily in the parent,
+        * while we create the listener thread.
+        */
+       sigfillset(&sig_all_blocked);
+       ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
+       if (ret) {
+               PERROR("pthread_sigmask: %s", strerror(ret));
+       }
+
        ret = pthread_create(&local_apps.ust_listener, NULL,
                        ust_listener_thread, &local_apps);
 
@@ -870,6 +896,12 @@ void __attribute__((constructor)) lttng_ust_init(void)
                handle_register_done(&local_apps);
        }
 
+       /* Restore original signal mask in parent */
+       ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
+       if (ret) {
+               PERROR("pthread_sigmask: %s", strerror(ret));
+       }
+
        switch (timeout_mode) {
        case 1: /* timeout wait */
                do {
This page took 0.025247 seconds and 4 git commands to generate.