Fix: Disable cancellation around fd tracker lock
[lttng-ust.git] / liblttng-ust-comm / lttng-ust-fd-tracker.c
index 329339f98a038736fe528bc856bdeff1737b4c2b..1118163a201af456ac6ed28d490e09f458af08ef 100644 (file)
  * Protect the lttng_fd_set. Nests within the ust_lock, and therefore
  * within the libc dl lock. Therefore, we need to fixup the TLS before
  * nesting into this lock.
+ *
+ * The ust_safe_guard_fd_mutex nests within the ust_mutex. This mutex
+ * is also held across fork.
  */
 static pthread_mutex_t ust_safe_guard_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+/*
+ * Cancel state when grabbing the ust_safe_guard_fd_mutex. Saved when
+ * locking, restored on unlock. Protected by ust_safe_guard_fd_mutex.
+ */
+static int ust_safe_guard_saved_cancelstate;
+
 /*
  * Track whether we are within lttng-ust or application, for close
  * system call override by LD_PRELOAD library.
@@ -124,6 +134,12 @@ void lttng_ust_init_fd_tracker(void)
 
 void lttng_ust_lock_fd_tracker(void)
 {
+       int ret, oldstate;
+
+       ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
+       if (ret) {
+               ERR("pthread_setcancelstate: %s", strerror(ret));
+       }
        URCU_TLS(thread_fd_tracking) = 1;
        /*
         * Ensure the compiler don't move the store after the close()
@@ -131,10 +147,14 @@ void lttng_ust_lock_fd_tracker(void)
         */
        cmm_barrier();
        pthread_mutex_lock(&ust_safe_guard_fd_mutex);
+       ust_safe_guard_saved_cancelstate = oldstate;
 }
 
 void lttng_ust_unlock_fd_tracker(void)
 {
+       int ret, newstate, oldstate;
+
+       newstate = ust_safe_guard_saved_cancelstate;
        pthread_mutex_unlock(&ust_safe_guard_fd_mutex);
        /*
         * Ensure the compiler don't move the store before the close()
@@ -142,11 +162,15 @@ void lttng_ust_unlock_fd_tracker(void)
         */
        cmm_barrier();
        URCU_TLS(thread_fd_tracking) = 0;
+       ret = pthread_setcancelstate(newstate, &oldstate);
+       if (ret) {
+               ERR("pthread_setcancelstate: %s", strerror(ret));
+       }
 }
 
 static int dup_std_fd(int fd)
 {
-       int ret;
+       int ret, i;
        int fd_to_close[STDERR_FILENO + 1];
        int fd_to_close_count = 0;
        int dup_cmd = F_DUPFD; /* Default command */
@@ -171,7 +195,7 @@ static int dup_std_fd(int fd)
        }
 
        /* Perform dup */
-       for (int i = 0; i < STDERR_FILENO + 1; i++) {
+       for (i = 0; i < STDERR_FILENO + 1; i++) {
                ret = fcntl(fd, dup_cmd, 0);
                if (ret < 0) {
                        PERROR("fcntl dup fd");
@@ -192,7 +216,7 @@ static int dup_std_fd(int fd)
        }
 
        /* Close intermediary fds */
-       for (int i = 0; i < fd_to_close_count; i++) {
+       for (i = 0; i < fd_to_close_count; i++) {
                ret = close(fd_to_close[i]);
                if (ret) {
                        PERROR("close on temporary fd: %d.", fd_to_close[i]);
This page took 0.024403 seconds and 4 git commands to generate.