From: Mathieu Desnoyers Date: Sun, 12 Nov 2017 19:51:24 +0000 (-0500) Subject: Fix: ensure fd tracker is initialized when called from constructors X-Git-Tag: v2.10.1~2 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=d5bfffd2804815b7352f70da6ff17b6db6d72f63;p=lttng-ust.git Fix: ensure fd tracker is initialized when called from constructors Signed-off-by: Mathieu Desnoyers --- diff --git a/liblttng-ust-comm/lttng-ust-fd-tracker.c b/liblttng-ust-comm/lttng-ust-fd-tracker.c index d56288e3..8d2acb69 100644 --- a/liblttng-ust-comm/lttng-ust-fd-tracker.c +++ b/liblttng-ust-comm/lttng-ust-fd-tracker.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -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((<tng_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.