From 379436433bb4c9257a0af96025034065da623a0f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 16 Dec 2015 11:13:39 +0100 Subject: [FUSE] Fix forking mode (not passing -f) by not spawning threads before entering fuse_main --- src/fuse/connection.c | 64 ++++++++++++++++++++++++++++++++------------------- src/fuse/connection.h | 2 ++ src/fuse/main.c | 17 +++++++++----- 3 files changed, 53 insertions(+), 30 deletions(-) (limited to 'src/fuse') diff --git a/src/fuse/connection.c b/src/fuse/connection.c index 8b0cdd1..0a96055 100644 --- a/src/fuse/connection.c +++ b/src/fuse/connection.c @@ -24,7 +24,8 @@ static const int MAX_CONSECUTIVE_FAILURES = 16; /* Module variables */ // Init guard -static bool initDone = false; +static bool connectionInitDone = false; +static bool threadInitDone = false; static pthread_mutex_t mutexInit = PTHREAD_MUTEX_INITIALIZER; static bool keepRunning = true; @@ -90,11 +91,12 @@ bool connection_init(const char *hosts, const char *lowerImage, const uint16_t r uint64_t remoteSize; pthread_mutex_lock( &mutexInit ); - if ( !initDone && keepRunning ) { + if ( !connectionInitDone && keepRunning ) { dnbd3_host_t tempHosts[MAX_HOSTS_PER_ADDRESS]; const char *current, *end; int altIndex = 0; memset( altservers, 0, sizeof altservers ); + connection.sockFd = -1; current = hosts; do { // Get next host from string @@ -148,32 +150,46 @@ bool connection_init(const char *hosts, const char *lowerImage, const uint16_t r } } if ( sock != -1 ) { - pthread_t thread; - logadd( LOG_DEBUG1, "Initializing stuff" ); - if ( pthread_mutex_init( &connection.sendMutex, NULL ) != 0 - || pthread_spin_init( &requests.lock, PTHREAD_PROCESS_PRIVATE ) != 0 - || pthread_spin_init( &altLock, PTHREAD_PROCESS_PRIVATE ) != 0 ) { - logadd( LOG_ERROR, "Mutex or spinlock init failure" ); - close( sock ); - sock = -1; - } else { - if ( pthread_create( &thread, NULL, &connection_receiveThreadMain, (void*)(size_t)sock ) != 0 ) { - logadd( LOG_ERROR, "Could not create receive thread" ); - close( sock ); - sock = -1; - } else if ( pthread_create( &thread, NULL, &connection_backgroundThread, NULL ) != 0 ) { - logadd( LOG_ERROR, "Could not create background thread" ); - shutdown( sock, SHUT_RDWR ); - sock = -1; - } - } - initDone = true; + connectionInitDone = true; } } pthread_mutex_unlock( &mutexInit ); return sock != -1; } +bool connection_initThreads() +{ + pthread_mutex_lock( &mutexInit ); + if ( !keepRunning || !connectionInitDone || threadInitDone || connection.sockFd == -1 ) { + pthread_mutex_unlock( &mutexInit ); + return false; + } + bool success = true; + pthread_t thread; + threadInitDone = true; + logadd( LOG_DEBUG1, "Initializing stuff" ); + if ( pthread_mutex_init( &connection.sendMutex, NULL ) != 0 + || pthread_spin_init( &requests.lock, PTHREAD_PROCESS_PRIVATE ) != 0 + || pthread_spin_init( &altLock, PTHREAD_PROCESS_PRIVATE ) != 0 ) { + logadd( LOG_ERROR, "Mutex or spinlock init failure" ); + success = false; + } else { + if ( pthread_create( &thread, NULL, &connection_receiveThreadMain, (void*)(size_t)connection.sockFd ) != 0 ) { + logadd( LOG_ERROR, "Could not create receive thread" ); + success = false; + } else if ( pthread_create( &thread, NULL, &connection_backgroundThread, NULL ) != 0 ) { + logadd( LOG_ERROR, "Could not create background thread" ); + success = false; + } + } + if ( !success ) { + close( connection.sockFd ); + connection.sockFd = -1; + } + pthread_mutex_unlock( &mutexInit ); + return success; +} + uint64_t connection_getImageSize() { return image.size; @@ -181,7 +197,7 @@ uint64_t connection_getImageSize() bool connection_read(dnbd3_async_t *request) { - if (!initDone) return false; + if (!connectionInitDone) return false; pthread_mutex_lock( &connection.sendMutex ); enqueueRequest( request ); if ( connection.sockFd != -1 ) { @@ -201,7 +217,7 @@ void connection_close() { pthread_mutex_lock( &mutexInit ); keepRunning = false; - if ( !initDone ) { + if ( !connectionInitDone ) { pthread_mutex_unlock( &mutexInit ); return; } diff --git a/src/fuse/connection.h b/src/fuse/connection.h index f59c876..06db6c4 100644 --- a/src/fuse/connection.h +++ b/src/fuse/connection.h @@ -18,6 +18,8 @@ typedef struct _dnbd3_async { bool connection_init(const char *hosts, const char *image, const uint16_t rid); +bool connection_initThreads(); + uint64_t connection_getImageSize(); bool connection_read(dnbd3_async_t *request); diff --git a/src/fuse/main.c b/src/fuse/main.c index eb4566a..c2b07be 100644 --- a/src/fuse/main.c +++ b/src/fuse/main.c @@ -39,11 +39,6 @@ static log_info logInfo; static struct timespec startupTime; static uid_t owner; -void error(const char *msg) -{ - perror( msg ); - exit( 0 ); -} static int image_getattr(const char *path, struct stat *stbuf) { @@ -156,8 +151,17 @@ static int image_read(const char *path, char *buf, size_t size, off_t offset, st } } +static void* image_init(struct fuse_conn_info *conn UNUSED) +{ + if ( !connection_initThreads() ) { + logadd( LOG_ERROR, "Could not initialize threads for dnbd3 connection, exiting..." ); + exit( EXIT_FAILURE ); + } + return NULL; +} + /* close the connection */ -void image_destroy(void *private_data UNUSED) +static void image_destroy(void *private_data UNUSED) { if ( useDebug ) { printLog( &logInfo ); @@ -172,6 +176,7 @@ static struct fuse_operations image_oper = { .readdir = image_readdir, .open = image_open, .read = image_read, + .init = image_init, .destroy = image_destroy, }; -- cgit v1.2.3-55-g7522