Fix: ensure fd tracker is initialized when called from constructors
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 12 Nov 2017 19:51:24 +0000 (14:51 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 12 Nov 2017 19:52:53 +0000 (14:52 -0500)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust-comm/lttng-ust-fd-tracker.c

index d56288e370470e1deb94a25dc5603048c75f03aa..8d2acb69f55f5cb32b7841ba7dafcdb96064326b 100644 (file)
@@ -34,6 +34,7 @@
 #include <pthread.h>
 #include <urcu/compiler.h>
 #include <urcu/tls-compat.h>
+#include <urcu/system.h>
 
 #include <ust-fd.h>
 #include <helper.h>
@@ -71,6 +72,7 @@ static DEFINE_URCU_TLS(int, thread_fd_tracking);
 static fd_set *lttng_fd_set;
 static int lttng_ust_max_fd;
 static int num_fd_sets;
+static int init_done;
 
 /*
  * Force a read (imply TLS fixup for dlopen) of TLS variables.
@@ -90,6 +92,9 @@ void lttng_ust_init_fd_tracker(void)
        struct rlimit rlim;
        int i;
 
+       if (CMM_LOAD_SHARED(init_done))
+               return;
+
        memset(&rlim, 0, sizeof(rlim));
        /* Get the current possible max number of fd for this process. */
        if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
@@ -113,6 +118,7 @@ void lttng_ust_init_fd_tracker(void)
                abort();
        for (i = 0; i < num_fd_sets; i++)
                FD_ZERO((&lttng_fd_set[i]));
+       CMM_STORE_SHARED(init_done, 1);
 }
 
 void lttng_ust_lock_fd_tracker(void)
@@ -143,6 +149,12 @@ void lttng_ust_unlock_fd_tracker(void)
  */
 void lttng_ust_add_fd_to_tracker(int fd)
 {
+       /*
+        * Ensure the tracker is initialized when called from
+        * constructors.
+        */
+       lttng_ust_init_fd_tracker();
+
        assert(URCU_TLS(thread_fd_tracking));
        /* Trying to add an fd which we can not accommodate. */
        assert(IS_FD_VALID(fd));
@@ -158,6 +170,12 @@ void lttng_ust_add_fd_to_tracker(int fd)
  */
 void lttng_ust_delete_fd_from_tracker(int fd)
 {
+       /*
+        * Ensure the tracker is initialized when called from
+        * constructors.
+        */
+       lttng_ust_init_fd_tracker();
+
        assert(URCU_TLS(thread_fd_tracking));
        /* Not a valid fd. */
        assert(IS_FD_VALID(fd));
@@ -178,6 +196,12 @@ int lttng_ust_safe_close_fd(int fd, int (*close_cb)(int fd))
 
        lttng_ust_fixup_fd_tracker_tls();
 
+       /*
+        * Ensure the tracker is initialized when called from
+        * constructors.
+        */
+       lttng_ust_init_fd_tracker();
+
        /*
         * If called from lttng-ust, we directly call close without
         * validating whether the FD is part of the tracked set.
@@ -208,6 +232,12 @@ int lttng_ust_safe_fclose_stream(FILE *stream, int (*fclose_cb)(FILE *stream))
 
        lttng_ust_fixup_fd_tracker_tls();
 
+       /*
+        * Ensure the tracker is initialized when called from
+        * constructors.
+        */
+       lttng_ust_init_fd_tracker();
+
        /*
         * If called from lttng-ust, we directly call fclose without
         * validating whether the FD is part of the tracked set.
@@ -257,6 +287,12 @@ int lttng_ust_safe_closefrom_fd(int lowfd, int (*close_cb)(int fd))
 
        lttng_ust_fixup_fd_tracker_tls();
 
+       /*
+        * Ensure the tracker is initialized when called from
+        * constructors.
+        */
+       lttng_ust_init_fd_tracker();
+
        if (lowfd < 0) {
                /*
                 * NetBSD return EBADF if fd is invalid.
This page took 0.025374 seconds and 4 git commands to generate.