Fix: perform TLS fixup in all UST entry points from each thread
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
index cc9b6b5e1e8706a1b941de1b0d09ea37dddaffd3..1720bca9cdbbe07f457ee34f22b6e69a5dfd7f2b 100644 (file)
@@ -106,6 +106,14 @@ static pthread_mutex_t ust_fork_mutex = PTHREAD_MUTEX_INITIALIZER;
 /* Should the ust comm thread quit ? */
 static int lttng_ust_comm_should_quit;
 
+/*
+ * This variable can be tested by applications to check whether
+ * lttng-ust is loaded. They simply have to define their own
+ * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
+ * library constructor.
+ */
+int lttng_ust_loaded __attribute__((weak));
+
 /*
  * Return 0 on success, -1 if should quit.
  * The lock is taken in both cases.
@@ -388,6 +396,16 @@ void lttng_fixup_urcu_bp_tls(void)
        rcu_read_unlock();
 }
 
+void lttng_ust_fixup_tls(void)
+{
+       lttng_fixup_urcu_bp_tls();
+       lttng_fixup_ringbuffer_tls();
+       lttng_fixup_vtid_tls();
+       lttng_fixup_nest_count_tls();
+       lttng_fixup_procname_tls();
+       lttng_fixup_ust_mutex_nest_tls();
+}
+
 int lttng_get_notify_socket(void *owner)
 {
        struct sock_info *info = owner;
@@ -1295,6 +1313,7 @@ void *ust_listener_thread(void *arg)
        int sock, ret, prev_connect_failed = 0, has_waited = 0;
        long timeout;
 
+       lttng_ust_fixup_tls();
        /*
         * If available, add '-ust' to the end of this thread's
         * process name
@@ -1547,13 +1566,6 @@ void lttng_ust_malloc_wrapper_init(void)
 {
 }
 
-static
-void init_ust_env(void)
-{
-       if (putenv("LTTNG_UST_LOADED=1"))
-               DBG("Error setting LTTNG_UST_LOADED environment variable");
-}
-
 /*
  * sessiond monitoring thread: monitor presence of global and per-user
  * sessiond by polling the application common named pipe.
@@ -1574,14 +1586,9 @@ void __attribute__((constructor)) lttng_ust_init(void)
         * to be the dynamic linker mutex) and ust_lock, taken within
         * the ust lock.
         */
-       lttng_fixup_urcu_bp_tls();
-       lttng_fixup_ringbuffer_tls();
-       lttng_fixup_vtid_tls();
-       lttng_fixup_nest_count_tls();
-       lttng_fixup_procname_tls();
-       lttng_fixup_ust_mutex_nest_tls();
+       lttng_ust_fixup_tls();
 
-       init_ust_env();
+       lttng_ust_loaded = 1;
 
        /*
         * We want precise control over the order in which we construct
@@ -1714,6 +1721,7 @@ void lttng_ust_cleanup(int exiting)
 {
        cleanup_sock_info(&global_apps, exiting);
        cleanup_sock_info(&local_apps, exiting);
+       local_apps.allowed = 0;
        /*
         * The teardown in this function all affect data structures
         * accessed under the UST lock by the listener thread. This
@@ -1809,6 +1817,9 @@ void ust_before_fork(sigset_t *save_sigset)
        sigset_t all_sigs;
        int ret;
 
+       /* Fixup lttng-ust TLS. */
+       lttng_ust_fixup_tls();
+
        if (URCU_TLS(lttng_ust_nest_count))
                return;
        /* Disable signals */
@@ -1863,11 +1874,11 @@ void ust_after_fork_child(sigset_t *restore_sigset)
 {
        if (URCU_TLS(lttng_ust_nest_count))
                return;
+       lttng_context_vtid_reset();
        DBG("process %d", getpid());
        /* Release urcu mutexes */
        rcu_bp_after_fork_child();
        lttng_ust_cleanup(0);
-       lttng_context_vtid_reset();
        /* Release mutexes and reenable signals */
        ust_after_fork_common(restore_sigset);
        lttng_ust_init();
This page took 0.023661 seconds and 4 git commands to generate.