X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=liblttng-ust%2Flttng-ust-comm.c;h=b61833155425988d261e9e584247e3b76ebf9c90;hb=2eb235ecd28828cef90ff371a96cd532e92ba9f6;hp=eec049b31fb2a09de4add833adac706e2e4168ce;hpb=553bbf7f38652084ed7966c7817b8ccb372b14e1;p=lttng-ust.git diff --git a/liblttng-ust/lttng-ust-comm.c b/liblttng-ust/lttng-ust-comm.c index eec049b3..b6183315 100644 --- a/liblttng-ust/lttng-ust-comm.c +++ b/liblttng-ust/lttng-ust-comm.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,9 @@ #include "../libringbuffer/getcpu.h" #include "getenv.h" +/* Concatenate lttng ust shared library name with its major version number. */ +#define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." LTTNG_UST_LIBRARY_VERSION_MAJOR + /* * Has lttng ust comm constructor been called ? */ @@ -86,7 +90,7 @@ static int initialized; static pthread_mutex_t ust_mutex = PTHREAD_MUTEX_INITIALIZER; /* Allow nesting the ust_mutex within the same thread. */ -static DEFINE_URCU_TLS_IE(int, ust_mutex_nest); +static DEFINE_URCU_TLS(int, ust_mutex_nest); /* * ust_exit_mutex protects thread_active variable wrt thread exit. It @@ -1261,7 +1265,18 @@ char *get_map_shm(struct sock_info *sock_info) lttng_ust_unlock_fd_tracker(); goto error; } - lttng_ust_add_fd_to_tracker(wait_shm_fd); + + ret = lttng_ust_add_fd_to_tracker(wait_shm_fd); + if (ret < 0) { + ret = close(wait_shm_fd); + if (!ret) { + PERROR("Error closing fd"); + } + lttng_ust_unlock_fd_tracker(); + goto error; + } + + wait_shm_fd = ret; lttng_ust_unlock_fd_tracker(); wait_shm_mmap = mmap(NULL, page_size, PROT_READ, @@ -1353,7 +1368,7 @@ static void *ust_listener_thread(void *arg) { struct sock_info *sock_info = arg; - int sock, ret, prev_connect_failed = 0, has_waited = 0; + int sock, ret, prev_connect_failed = 0, has_waited = 0, fd; long timeout; lttng_ust_fixup_tls(); @@ -1385,6 +1400,10 @@ restart: prev_connect_failed = 0; } + if (ust_lock()) { + goto quit; + } + if (sock_info->socket != -1) { /* FD tracker is updated by ustcomm_close_unix_sock() */ ret = ustcomm_close_unix_sock(sock_info->socket); @@ -1404,9 +1423,6 @@ restart: sock_info->notify_socket = -1; } - if (ust_lock()) { - goto quit; - } /* * Register. We need to perform both connect and sending @@ -1433,9 +1449,21 @@ restart: ust_unlock(); goto restart; } - lttng_ust_add_fd_to_tracker(ret); - lttng_ust_unlock_fd_tracker(); + fd = ret; + ret = lttng_ust_add_fd_to_tracker(fd); + if (ret < 0) { + ret = close(fd); + if (ret) { + PERROR("close on sock_info->socket"); + } + ret = -1; + lttng_ust_unlock_fd_tracker(); + ust_unlock(); + goto quit; + } + sock_info->socket = ret; + lttng_ust_unlock_fd_tracker(); ust_unlock(); /* @@ -1504,9 +1532,22 @@ restart: ust_unlock(); goto restart; } - lttng_ust_add_fd_to_tracker(ret); - lttng_ust_unlock_fd_tracker(); + + fd = ret; + ret = lttng_ust_add_fd_to_tracker(fd); + if (ret < 0) { + ret = close(fd); + if (ret) { + PERROR("close on sock_info->notify_socket"); + } + ret = -1; + lttng_ust_unlock_fd_tracker(); + ust_unlock(); + goto quit; + } + sock_info->notify_socket = ret; + lttng_ust_unlock_fd_tracker(); ust_unlock(); /* @@ -1648,6 +1689,7 @@ void __attribute__((constructor)) lttng_ust_init(void) pthread_attr_t thread_attr; int timeout_mode; int ret; + void *handle; if (uatomic_xchg(&initialized, 1) == 1) return; @@ -1661,6 +1703,26 @@ void __attribute__((constructor)) lttng_ust_init(void) lttng_ust_loaded = 1; + /* + * We need to ensure that the liblttng-ust library is not unloaded to avoid + * the unloading of code used by the ust_listener_threads as we can not + * reliably know when they exited. To do that, manually load + * liblttng-ust.so to increment the dynamic loader's internal refcount for + * this library so it never becomes zero, thus never gets unloaded from the + * address space of the process. Since we are already running in the + * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will + * simply increment the refcount and no additionnal work is needed by the + * dynamic loader as the shared library is already loaded in the address + * space. As a safe guard, we use the RTLD_NODELETE flag to prevent + * unloading of the UST library if its refcount becomes zero (which should + * never happen). Do the return value check but discard the handle at the + * end of the function as it's not needed. + */ + handle = dlopen(LTTNG_UST_LIB_SO_NAME, RTLD_LAZY | RTLD_NODELETE); + if (!handle) { + ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME); + } + /* * We want precise control over the order in which we construct * our sub-libraries vs starting to receive commands from @@ -1949,7 +2011,9 @@ void ust_after_fork_child(sigset_t *restore_sigset) { if (URCU_TLS(lttng_ust_nest_count)) return; + lttng_context_vpid_reset(); lttng_context_vtid_reset(); + lttng_context_procname_reset(); DBG("process %d", getpid()); /* Release urcu mutexes */ rcu_bp_after_fork_child();