Skip to content

Commit

Permalink
guard hash access with locks
Browse files Browse the repository at this point in the history
  • Loading branch information
i-rinat committed May 13, 2018
1 parent 1e8ad32 commit 12c0fd1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.6)

set(CMAKE_C_STANDARD 99)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=format")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--unresolved-symbols=report-all")
Expand Down
8 changes: 8 additions & 0 deletions autofsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "uthash.h"
#include <dlfcn.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -78,6 +79,7 @@ struct file {
};

static struct file *g_files = NULL;
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;

static bool real_entry_points_initialized = false;

Expand Down Expand Up @@ -127,7 +129,9 @@ account_opened_fd(int fd)
new_file->dirty = 0;

struct file *old_file = NULL;
pthread_mutex_lock(&g_lock);
HASH_REPLACE_INT(g_files, fd, new_file, old_file);
pthread_mutex_unlock(&g_lock);

if (old_file != NULL) {
LOG_(" unexpected old_file");
Expand Down Expand Up @@ -211,6 +215,7 @@ close(int fd)
ensure_entry_points_initialized();

struct file *a_file = NULL;
pthread_mutex_lock(&g_lock);
HASH_FIND_INT(g_files, &fd, a_file);
if (a_file) {
HASH_DEL(g_files, a_file);
Expand All @@ -219,6 +224,7 @@ close(int fd)
} else {
LOG_(" mismatched close");
}
pthread_mutex_unlock(&g_lock);

return real_close(fd);
}
Expand All @@ -227,7 +233,9 @@ static void
write_throttle(int fd, ssize_t bytes_written)
{
struct file *a_file = NULL;
pthread_mutex_lock(&g_lock);
HASH_FIND_INT(g_files, &fd, a_file);
pthread_mutex_unlock(&g_lock);
if (a_file == NULL)
return;

Expand Down

0 comments on commit 12c0fd1

Please sign in to comment.