Introduce file descriptor tracker
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
index dd8822cf13138a22053a7f04c2a143cbde556ad1..4a21866358b66a4d9f766abdfd47665e2cffe13d 100644 (file)
@@ -46,6 +46,7 @@
 #include <lttng/ust-ctl.h>
 #include <urcu/tls-compat.h>
 #include <ust-comm.h>
+#include <ust-fd.h>
 #include <usterr-signal-safe.h>
 #include <helper.h>
 #include "tracepoint-internal.h"
@@ -404,6 +405,7 @@ void lttng_ust_fixup_tls(void)
        lttng_fixup_nest_count_tls();
        lttng_fixup_procname_tls();
        lttng_fixup_ust_mutex_nest_tls();
+       lttng_ust_fixup_fd_tracker_tls();
 }
 
 int lttng_get_notify_socket(void *owner)
@@ -1231,17 +1233,28 @@ char *get_map_shm(struct sock_info *sock_info)
                goto error;
        }
 
+       lttng_ust_lock_fd_tracker();
        wait_shm_fd = get_wait_shm(sock_info, page_size);
        if (wait_shm_fd < 0) {
+               lttng_ust_unlock_fd_tracker();
                goto error;
        }
+       lttng_ust_add_fd_to_tracker(wait_shm_fd);
+       lttng_ust_unlock_fd_tracker();
+
        wait_shm_mmap = mmap(NULL, page_size, PROT_READ,
                  MAP_SHARED, wait_shm_fd, 0);
+
        /* close shm fd immediately after taking the mmap reference */
+       lttng_ust_lock_fd_tracker();
        ret = close(wait_shm_fd);
-       if (ret) {
+       if (!ret) {
+               lttng_ust_delete_fd_from_tracker(wait_shm_fd);
+       } else {
                PERROR("Error closing fd");
        }
+       lttng_ust_unlock_fd_tracker();
+
        if (wait_shm_mmap == MAP_FAILED) {
                DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
                goto error;
@@ -1351,6 +1364,7 @@ restart:
        }
 
        if (sock_info->socket != -1) {
+               /* FD tracker is updated by ustcomm_close_unix_sock() */
                ret = ustcomm_close_unix_sock(sock_info->socket);
                if (ret) {
                        ERR("Error closing %s ust cmd socket",
@@ -1359,6 +1373,7 @@ restart:
                sock_info->socket = -1;
        }
        if (sock_info->notify_socket != -1) {
+               /* FD tracker is updated by ustcomm_close_unix_sock() */
                ret = ustcomm_close_unix_sock(sock_info->notify_socket);
                if (ret) {
                        ERR("Error closing %s ust notify socket",
@@ -1367,6 +1382,10 @@ restart:
                sock_info->notify_socket = -1;
        }
 
+       if (ust_lock()) {
+               goto quit;
+       }
+
        /*
         * Register. We need to perform both connect and sending
         * registration message before doing the next connect otherwise
@@ -1375,16 +1394,14 @@ restart:
         * first connect registration message.
         */
        /* Connect cmd socket */
+       lttng_ust_lock_fd_tracker();
        ret = ustcomm_connect_unix_sock(sock_info->sock_path,
                get_connect_sock_timeout());
        if (ret < 0) {
+               lttng_ust_unlock_fd_tracker();
                DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
                prev_connect_failed = 1;
 
-               if (ust_lock()) {
-                       goto quit;
-               }
-
                /*
                 * If we cannot find the sessiond daemon, don't delay
                 * constructor execution.
@@ -1394,8 +1411,16 @@ restart:
                ust_unlock();
                goto restart;
        }
+       lttng_ust_add_fd_to_tracker(ret);
+       lttng_ust_unlock_fd_tracker();
        sock_info->socket = ret;
 
+       ust_unlock();
+       /*
+        * Unlock/relock ust lock because connect is blocking (with
+        * timeout). Don't delay constructors on the ust lock for too
+        * long.
+        */
        if (ust_lock()) {
                goto quit;
        }
@@ -1430,18 +1455,24 @@ restart:
        }
 
        ust_unlock();
+       /*
+        * Unlock/relock ust lock because connect is blocking (with
+        * timeout). Don't delay constructors on the ust lock for too
+        * long.
+        */
+       if (ust_lock()) {
+               goto quit;
+       }
 
        /* Connect notify socket */
+       lttng_ust_lock_fd_tracker();
        ret = ustcomm_connect_unix_sock(sock_info->sock_path,
                get_connect_sock_timeout());
        if (ret < 0) {
+               lttng_ust_unlock_fd_tracker();
                DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
                prev_connect_failed = 1;
 
-               if (ust_lock()) {
-                       goto quit;
-               }
-
                /*
                 * If we cannot find the sessiond daemon, don't delay
                 * constructor execution.
@@ -1451,8 +1482,20 @@ restart:
                ust_unlock();
                goto restart;
        }
+       lttng_ust_add_fd_to_tracker(ret);
+       lttng_ust_unlock_fd_tracker();
        sock_info->notify_socket = ret;
 
+       ust_unlock();
+       /*
+        * Unlock/relock ust lock because connect is blocking (with
+        * timeout). Don't delay constructors on the ust lock for too
+        * long.
+        */
+       if (ust_lock()) {
+               goto quit;
+       }
+
        timeout = get_notify_sock_timeout();
        if (timeout >= 0) {
                /*
@@ -1475,10 +1518,6 @@ restart:
                WARN("Unsupported timeout value %ld", timeout);
        }
 
-       if (ust_lock()) {
-               goto quit;
-       }
-
        ret = register_to_sessiond(sock_info->notify_socket,
                        USTCTL_SOCKET_NOTIFY);
        if (ret < 0) {
@@ -1608,6 +1647,7 @@ void __attribute__((constructor)) lttng_ust_init(void)
         */
        init_usterr();
        init_tracepoint();
+       lttng_ust_init_fd_tracker();
        lttng_ust_clock_init();
        lttng_ust_getcpu_init();
        lttng_ust_statedump_init();
This page took 0.025088 seconds and 4 git commands to generate.