Fix: handle pthread errors
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
index 36f57efb25f4d97c31bcb04b7fd207f2807b9a8c..37f21c80d01ea4881e54b74c5b49e998a8588879 100644 (file)
@@ -46,6 +46,7 @@
 #include "tracepoint-internal.h"
 #include "ltt-tracer-core.h"
 #include "compat.h"
+#include "../libringbuffer/tlsfixup.h"
 
 /*
  * Has lttng ust comm constructor been called ?
@@ -235,6 +236,7 @@ int handle_message(struct sock_info *sock_info,
        struct ustcomm_ust_reply lur;
        int shm_fd, wait_fd;
        union ust_args args;
+       ssize_t len;
 
        ust_lock();
 
@@ -319,14 +321,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));
@@ -334,6 +342,26 @@ end:
                        perror("send wait_fd");
                        goto error;
                }
+               if (sendret) {
+                       ret = sendret;
+                       goto error;
+               }
+       }
+       /*
+        * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
+        * after the reply.
+        */
+       if (lur.ret_code == USTCOMM_OK) {
+               switch (lum->cmd) {
+               case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
+                       len = ustcomm_send_unix_sock(sock,
+                               &args.field_list.entry,
+                               sizeof(args.field_list.entry));
+                       if (len != sizeof(args.field_list.entry)) {
+                               ret = -1;
+                               goto error;
+                       }
+               }
        }
        /*
         * We still have the memory map reference, and the fds have been
@@ -390,7 +418,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");
                }
@@ -670,7 +698,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);
                }
@@ -821,12 +849,22 @@ 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;
 
        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
@@ -848,16 +886,39 @@ void __attribute__((constructor)) lttng_ust_init(void)
        if (ret) {
                ERR("Error setting up to local apps");
        }
-       ret = pthread_create(&local_apps.ust_listener, NULL,
-                       ust_listener_thread, &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) {
+               ERR("pthread_sigmask: %s", strerror(ret));
+       }
+
+       ret = pthread_create(&global_apps.ust_listener, NULL,
+                       ust_listener_thread, &global_apps);
+       if (ret) {
+               ERR("pthread_create global: %s", strerror(ret));
+       }
        if (local_apps.allowed) {
-               ret = pthread_create(&global_apps.ust_listener, NULL,
-                               ust_listener_thread, &global_apps);
+               ret = pthread_create(&local_apps.ust_listener, NULL,
+                               ust_listener_thread, &local_apps);
+               if (ret) {
+                       ERR("pthread_create local: %s", strerror(ret));
+               }
        } else {
                handle_register_done(&local_apps);
        }
 
+       /* Restore original signal mask in parent */
+       ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
+       if (ret) {
+               ERR("pthread_sigmask: %s", strerror(ret));
+       }
+
        switch (timeout_mode) {
        case 1: /* timeout wait */
                do {
@@ -931,12 +992,14 @@ void __attribute__((destructor)) lttng_ust_exit(void)
        /* cancel threads */
        ret = pthread_cancel(global_apps.ust_listener);
        if (ret) {
-               ERR("Error cancelling global ust listener thread");
+               ERR("Error cancelling global ust listener thread: %s",
+                       strerror(ret));
        }
        if (local_apps.allowed) {
                ret = pthread_cancel(local_apps.ust_listener);
                if (ret) {
-                       ERR("Error cancelling local ust listener thread");
+                       ERR("Error cancelling local ust listener thread: %s",
+                               strerror(ret));
                }
        }
        /*
This page took 0.025035 seconds and 4 git commands to generate.