common: compile libconfig as C++
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.c
index 88f53c7418be7518c98cea62007fe3eb50f6bcea..a9921fa847eff9d6c361b772ad34b224ac13e932 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2018, 2020 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2018-2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License, version 2 only, as
- * published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <urcu.h>
@@ -220,7 +210,7 @@ static void fs_handle_tracked_log(struct fs_handle_tracked *handle)
        const char *path;
 
        pthread_mutex_lock(&handle->lock);
-       lttng_inode_get_location(handle->inode, NULL, &path);
+       lttng_inode_borrow_location(handle->inode, NULL, &path);
 
        if (handle->fd >= 0) {
                DBG_NO_LOC("    %s [active, fd %d%s]", path, handle->fd,
@@ -240,8 +230,9 @@ static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle)
        const struct lttng_directory_handle *node_directory_handle;
 
        pthread_mutex_lock(&handle->lock);
-       lttng_inode_get_location(handle->inode, &node_directory_handle, &path);
-       assert(handle->fd >= 0);
+       lttng_inode_borrow_location(
+                       handle->inode, &node_directory_handle, &path);
+       LTTNG_ASSERT(handle->fd >= 0);
        if (handle->in_use) {
                /* This handle can't be suspended as it is currently in use. */
                ret = -EAGAIN;
@@ -298,10 +289,11 @@ static int fs_handle_tracked_restore(struct fs_handle_tracked *handle)
        const char *path;
        const struct lttng_directory_handle *node_directory_handle;
 
-       lttng_inode_get_location(handle->inode, &node_directory_handle, &path);
+       lttng_inode_borrow_location(
+                       handle->inode, &node_directory_handle, &path);
 
-       assert(handle->fd == -1);
-       assert(path);
+       LTTNG_ASSERT(handle->fd == -1);
+       LTTNG_ASSERT(path);
        ret = open_from_properties(
                        node_directory_handle, path, &handle->properties);
        if (ret < 0) {
@@ -465,6 +457,9 @@ int fd_tracker_destroy(struct fd_tracker *tracker)
 {
        int ret = 0;
 
+       if (!tracker) {
+               goto end;
+       }
        /*
         * Refuse to destroy the tracker as fs_handles may still old
         * weak references to the tracker.
@@ -482,7 +477,7 @@ int fd_tracker_destroy(struct fd_tracker *tracker)
 
        if (tracker->unsuspendable_fds) {
                ret = cds_lfht_destroy(tracker->unsuspendable_fds, NULL);
-               assert(!ret);
+               LTTNG_ASSERT(!ret);
        }
 
        lttng_inode_registry_destroy(tracker->inode_registry);
@@ -642,7 +637,7 @@ int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
                } else {
                        /*
                         * There are not enough active suspendable file
-                        * descriptors to open a new fd and still accomodate the
+                        * descriptors to open a new fd and still accommodates the
                         * tracker's capacity.
                         */
                        WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
@@ -839,7 +834,7 @@ static int fs_handle_tracked_get_fd(struct fs_handle *_handle)
         */
        pthread_mutex_lock(&handle->tracker->lock);
        pthread_mutex_lock(&handle->lock);
-       assert(!handle->in_use);
+       LTTNG_ASSERT(!handle->in_use);
 
        handle->tracker->stats.uses++;
        if (handle->fd >= 0) {
@@ -892,6 +887,7 @@ static int fs_handle_tracked_close(struct fs_handle *_handle)
        const char *path = NULL;
        struct fs_handle_tracked *handle =
                        container_of(_handle, struct fs_handle_tracked, parent);
+       struct lttng_directory_handle *inode_directory_handle = NULL;
 
        if (!handle) {
                ret = -EINVAL;
@@ -901,7 +897,29 @@ static int fs_handle_tracked_close(struct fs_handle *_handle)
        pthread_mutex_lock(&handle->tracker->lock);
        pthread_mutex_lock(&handle->lock);
        if (handle->inode) {
-               lttng_inode_get_location(handle->inode, NULL, &path);
+               lttng_inode_borrow_location(handle->inode, NULL, &path);
+               /*
+                * Here a reference to the inode's directory handle is acquired
+                * to prevent the last reference to it from being released while
+                * the tracker's lock is taken.
+                *
+                * If this wasn't done, the directory handle could attempt to
+                * close its underlying directory file descriptor, which would
+                * attempt to lock the tracker's lock, resulting in a deadlock.
+                *
+                * Since a new reference to the directory handle is taken within
+                * the scope of this function, it is not possible for the last
+                * reference to the inode's location directory handle to be
+                * released during the call to lttng_inode_put().
+                *
+                * We wait until the tracker's lock is released to release the
+                * reference. Hence, the call to the tracker is delayed just
+                * enough to not attempt to recursively acquire the tracker's
+                * lock twice.
+                */
+               inode_directory_handle =
+                               lttng_inode_get_location_directory_handle(
+                                               handle->inode);
        }
        fd_tracker_untrack(handle->tracker, handle);
        if (handle->fd >= 0) {
@@ -910,7 +928,7 @@ static int fs_handle_tracked_close(struct fs_handle *_handle)
                 * isn't much the user can do about it.
                 */
                if (close(handle->fd)) {
-                       PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
+                       PERROR("Failed to close the file descriptor (%d) of fs handle to %s, close() returned",
                                        handle->fd, path ? path : "Unknown");
                }
                handle->fd = -1;
@@ -922,6 +940,7 @@ static int fs_handle_tracked_close(struct fs_handle *_handle)
        pthread_mutex_destroy(&handle->lock);
        pthread_mutex_unlock(&handle->tracker->lock);
        free(handle);
+       lttng_directory_handle_put(inode_directory_handle);
 end:
        return ret;
 }
This page took 0.025257 seconds and 4 git commands to generate.