From 618db2374c0ca8986bd1913cb2451215691374f0 Mon Sep 17 00:00:00 2001 From: Ben RUBSON Date: Fri, 10 Mar 2017 08:47:04 +0100 Subject: [PATCH] Get idle counters at once (#294) to avoid a race with usage==0 & openCount>0 --- encfs/Context.cpp | 11 +++-------- encfs/Context.h | 5 ++--- encfs/main.cpp | 10 +++++++--- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/encfs/Context.cpp b/encfs/Context.cpp index 4cdcd36..eed9f57 100644 --- a/encfs/Context.cpp +++ b/encfs/Context.cpp @@ -74,20 +74,15 @@ void EncFS_Context::setRoot(const std::shared_ptr &r) { bool EncFS_Context::isMounted() { return root.get() != nullptr; } -int EncFS_Context::getAndResetUsageCounter() { +void EncFS_Context::getAndResetUsageCounter(int *usage, int *openCount) { Lock lock(contextMutex); - int count = usageCount; + *usage = usageCount; usageCount = 0; - return count; + *openCount = openFiles.size(); } -int EncFS_Context::openFileCount() const { - Lock lock(contextMutex); - - return openFiles.size(); -} std::shared_ptr EncFS_Context::lookupNode(const char *path) { Lock lock(contextMutex); diff --git a/encfs/Context.h b/encfs/Context.h index ada1e02..fade081 100644 --- a/encfs/Context.h +++ b/encfs/Context.h @@ -44,9 +44,8 @@ class EncFS_Context { std::shared_ptr lookupNode(const char *path); - int getAndResetUsageCounter(); - int openFileCount() const; - + void getAndResetUsageCounter(int *usage, int *openCount); + FileNode *putNode(const char *path, std::shared_ptr &&node); void eraseNode(const char *path, FileNode *fnode); diff --git a/encfs/main.cpp b/encfs/main.cpp index e56b83a..77242b4 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -718,15 +718,19 @@ static void *idleMonitor(void *_arg) { pthread_mutex_lock(&ctx->wakeupMutex); while (ctx->running) { - int usage = ctx->getAndResetUsageCounter(); + int usage, openCount; + ctx->getAndResetUsageCounter(&usage, &openCount); if (usage == 0 && ctx->isMounted()) ++idleCycles; - else + else { + if (idleCycles >= timeoutCycles) + RLOG(INFO) << "Filesystem no longer inactive: " + << arg->opts->mountPoint; idleCycles = 0; + } if (idleCycles >= timeoutCycles) { - int openCount = ctx->openFileCount(); if (openCount == 0) { if (unmountFS(ctx)) { // wait for main thread to wake us up