Verified Commit 68fa711b authored by Marc Vef's avatar Marc Vef
Browse files

Temporary fix for ADA-FS and kernel file descriptor value clashes.

parent 6422d40a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -332,6 +332,7 @@ int ftruncate(int fd, off_t length) __THROW {
int dup(int oldfd) __THROW {
    init_passthrough_if_needed();
    if (ld_is_aux_loaded() && file_map.exist(oldfd)) {
        ld_logger->trace("{}() called with oldfd {}", __func__, oldfd);
        return adafs_dup(oldfd);
    }
    return (reinterpret_cast<decltype(&dup)>(libc_dup))(oldfd);
@@ -340,6 +341,7 @@ int dup(int oldfd) __THROW {
int dup2(int oldfd, int newfd) __THROW {
    init_passthrough_if_needed();
    if (ld_is_aux_loaded() && file_map.exist(oldfd)) {
        ld_logger->trace("{}() called with oldfd {} newfd {}", __func__, oldfd, newfd);
        return adafs_dup2(oldfd, newfd);
    }
    return (reinterpret_cast<decltype(&dup2)>(libc_dup2))(oldfd, newfd);
+11 −3
Original line number Diff line number Diff line
@@ -10,7 +10,15 @@ using namespace std;

static const std::string dentry_val_delim = ","s;

static int fd_idx = 3;
/*
 * TODO: Setting our file descriptor index to a specific value is dangerous because we might clash with the kernel.
 * E.g., if we would passthrough and not intercept and the kernel assigns a file descriptor but we will later use
 * the same fd value, we will intercept calls that were supposed to be going to the kernel. This works the other way around too.
 * To mitigate this issue, we set the initial fd number to a high value. We "hope" that we do not clash but this is no permanent solution.
 * Note: This solution will probably work well already for many cases because kernel fd values are reused, unlike to ours.
 * The only case where we will clash with the kernel is, if one process has more than 100000 files open at the same time.
 */
static int fd_idx = 100000;
static mutex fd_idx_mutex;
std::atomic<bool> fd_validation_needed(false);

@@ -22,13 +30,13 @@ int generate_fd_idx() {
    // We need a mutex here for thread safety
    std::lock_guard<std::mutex> inode_lock(fd_idx_mutex);
    if (fd_idx == std::numeric_limits<int>::max()) {
        ld_logger->info("{}() File descriptor index exceeded ints max value. Setting it back to 3", __func__);
        ld_logger->info("{}() File descriptor index exceeded ints max value. Setting it back to 100000", __func__);
        /*
         * Setting fd_idx back to 3 could have the effect that fd are given twice for different path.
         * This must not happen. Instead a flag is set which tells can tell the OpenFileMap that it should check
         * if this fd is really safe to use.
         */
        fd_idx = 3;
        fd_idx = 100000;
        fd_validation_needed = true;
    }
    return fd_idx++;