From fea8af9b11b500a45344f836055d39963204878f Mon Sep 17 00:00:00 2001 From: ln-tech Date: Wed, 13 Nov 2019 07:45:57 +0100 Subject: SIGINT fix, Reader-Writer oroblem fix --- src/fuse/connection.c | 27 ++++++++++++++++----------- src/fuse/connection.h | 4 +++- src/fuse/main.c | 26 ++++++++++++++++++++++++-- src/fuse/main.h | 22 ---------------------- 4 files changed, 43 insertions(+), 36 deletions(-) (limited to 'src/fuse') diff --git a/src/fuse/connection.c b/src/fuse/connection.c index c63fe11..58a7923 100644 --- a/src/fuse/connection.c +++ b/src/fuse/connection.c @@ -31,8 +31,8 @@ static const int FAIL_BACKOFF_START_COUNT = 8; // Init guard static bool connectionInitDone = false; static bool threadInitDone = false; -static pthread_mutex_t mutexInit = PTHREAD_MUTEX_INITIALIZER; -static bool keepRunning = true; +pthread_mutex_t mutexInit = PTHREAD_MUTEX_INITIALIZER; +volatile bool keepRunning = true; static bool learnNewServers; // List of pending requests @@ -113,7 +113,7 @@ bool connection_init(const char *hosts, const char *lowerImage, const uint16_t r timing_setBase(); pthread_mutex_lock( &mutexInit ); - if ( !connectionInitDone && keepRunning ) { + if ( !connectionInitDone ) { dnbd3_host_t tempHosts[MAX_HOSTS_PER_ADDRESS]; const char *current, *end; int altIndex = 0; @@ -209,7 +209,7 @@ bool connection_init(const char *hosts, const char *lowerImage, const uint16_t r bool connection_initThreads() { pthread_mutex_lock( &mutexInit ); - if ( !keepRunning || !connectionInitDone || threadInitDone || connection.sockFd == -1 ) { + if ( !connectionInitDone || threadInitDone || connection.sockFd == -1 ) { pthread_mutex_unlock( &mutexInit ); return false; } @@ -263,11 +263,11 @@ bool connection_read(dnbd3_async_t *request) void connection_close() { - if ( keepRunning ) { + if ( true ) { logadd( LOG_INFO, "Tearing down dnbd3 connections and workers" ); } pthread_mutex_lock( &mutexInit ); - keepRunning = false; + //keepRunning = false; if ( !connectionInitDone ) { pthread_mutex_unlock( &mutexInit ); return; @@ -278,6 +278,7 @@ void connection_close() shutdown( connection.sockFd, SHUT_RDWR ); } pthread_mutex_unlock( &connection.sendMutex ); + logadd( LOG_DEBUG1, "Connection closed." ); } size_t connection_printStats(char *buffer, const size_t len) @@ -348,18 +349,21 @@ static void* connection_receiveThreadMain(void *sockPtr) pthread_detach( pthread_self() ); - while ( keepRunning ) { + while ( true ) { int ret; struct fuse_bufvec splice_buf; do { ret = dnbd3_read_reply( sockFd, &reply, true ); + printf("\n ret %d %d", ret); if ( ret == REPLY_OK ) break; } while ( ret == REPLY_INTR || ret == REPLY_AGAIN ); + //printf("\n hallo %d", keepRunning); if ( ret != REPLY_OK ) { logadd( LOG_DEBUG1, "Error receiving reply on receiveThread (%d)", ret ); + if (ret == REPLY_CLOSED) return NULL; goto fail; } - + printf("\n Rep cmd %d", reply.cmd); if ( reply.cmd == CMD_GET_BLOCK ) { // Get block reply. find matching request dnbd3_async_t *request = removeRequest( (dnbd3_async_t*)reply.handle ); @@ -402,7 +406,7 @@ static void* connection_receiveThreadMain(void *sockPtr) } unlock_rw( &altLock ); } - int rep = -errno; + int rep = -1; if (request->mode == NO_SPLICE) { rep = fuse_reply_buf(request->fuse_req, request->buffer, request->length); } @@ -410,7 +414,7 @@ static void* connection_receiveThreadMain(void *sockPtr) rep = fuse_reply_data(request->fuse_req, &splice_buf, FUSE_BUF_FORCE_SPLICE); // free(splice_buf); } - if (rep != -errno) { + if (rep == 0) { // no error } else { @@ -466,7 +470,8 @@ static void* connection_backgroundThread(void *something UNUSED) timing_get( &nextKeepalive ); nextRttCheck = nextKeepalive; - while ( keepRunning ) { + while ( true) { + //if ( !keepRunning ) break; ticks now; timing_get( &now ); uint32_t wt1 = timing_diffMs( &now, &nextKeepalive ); diff --git a/src/fuse/connection.h b/src/fuse/connection.h index c04d75b..ceb5050 100644 --- a/src/fuse/connection.h +++ b/src/fuse/connection.h @@ -12,7 +12,9 @@ #define NO_SPLICE 0 #define SPLICE 1 +extern volatile bool keepRunning; struct _dnbd3_async; +extern pthread_mutex_t mutexInit; typedef struct _dnbd3_async { struct _dnbd3_async *next; // Next in this linked list (provate field, not set by caller) @@ -21,7 +23,7 @@ typedef struct _dnbd3_async { uint64_t offset; uint32_t length; int mode; // 0 splice off, 1 splice on, ... - fuse_req_t *fuse_req; + fuse_req_t fuse_req; } dnbd3_async_t; bool connection_init(const char *hosts, const char *image, const uint16_t rid, const bool learnNewServers); diff --git a/src/fuse/main.c b/src/fuse/main.c index 666ff6c..175cbb2 100644 --- a/src/fuse/main.c +++ b/src/fuse/main.c @@ -14,7 +14,7 @@ #include "../shared/log.h" #define FUSE_USE_VERSION 30 -#include +#include "../config.h" #include #include #include @@ -48,6 +48,19 @@ static uid_t owner; static void (*fuse_sigIntHandler)(int) = NULL; static void (*fuse_sigTermHandler)(int) = NULL; +static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize); +static int fillStatsFile(char *buf, size_t size, off_t offset); +static void image_destroy(void *private_data); +static void image_ll_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi); +static void image_ll_init(void *userdata, struct fuse_conn_info *conn); +static void image_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name); +static void image_ll_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi); +static void image_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi); +static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, struct fuse_file_info *fi); +static int image_stat(fuse_ino_t ino, struct stat *stbuf); +static void printUsage(char *argv0, int exitCode); +static void printVersion(); + static int image_stat(fuse_ino_t ino, struct stat *stbuf) { stbuf->st_ctim = stbuf->st_atim = stbuf->st_mtim = startupTime; @@ -226,6 +239,7 @@ static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off ++logInfo.blockRequestCount[startBlock]; } } + if (ino == 2 && size != 0) // with size == 0 there is nothing to do { dnbd3_async_t *request = malloc(sizeof(dnbd3_async_t)); @@ -239,7 +253,10 @@ static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off } static void image_sigHandler(int signum) { - //keepRunning = false; + pthread_mutex_lock( &mutexInit ); + keepRunning = false; + pthread_mutex_unlock( &mutexInit ); + //printf("\n hallo222 %d", keepRunning); if ( signum == SIGINT && fuse_sigIntHandler != NULL ) { fuse_sigIntHandler(signum); } @@ -318,8 +335,13 @@ flags contains FUSE_BUF_SPLICE_MOVE struct sigaction newHandler; memset( &newHandler, 0, sizeof(newHandler) ); newHandler.sa_handler = &image_sigHandler; + //newHandler.sa_flags &= ~SA_RESTART; + newHandler.sa_flags |= SA_RESETHAND; sigemptyset( &newHandler.sa_mask ); + //rt_sigaction(sa_flags=SA_RESTORER|SA_ONSTACK|SA_INTERRUPT|SA_NODEFER|SA_RESETHAND|SA_SIGINFO|SA_NOCLDSTOP|SA_NOCLDWAIT|0x3fffff8 struct sigaction oldHandler; + //oldHandler.sa_flags &= ~SA_RESTART; + //oldHandler.sa_flags |= SA_RESETHAND; // Retrieve old handlers when setting sigaction( SIGINT, &newHandler, &oldHandler ); fuse_sigIntHandler = oldHandler.sa_handler; diff --git a/src/fuse/main.h b/src/fuse/main.h index 22ee09e..3586803 100644 --- a/src/fuse/main.h +++ b/src/fuse/main.h @@ -1,27 +1,5 @@ #ifndef _MAIN_H_ #define _MAIN_H_ -#include "../shared/fdsignal.h" -#include "../shared/timing.h" -#include -#include -#include -#include - - -//static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name, fuse_ino_t ino); -static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, off_t off, size_t maxsize); -static int fillStatsFile(char *buf, size_t size, off_t offset); -static void image_destroy(void *private_data); -static void image_ll_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi); -static void image_ll_init(void *userdata, struct fuse_conn_info *conn); -static void image_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name); -static void image_ll_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi); -static void image_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi); -static void image_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, struct fuse_file_info *fi); -static int image_stat(fuse_ino_t ino, struct stat *stbuf); -static void printUsage(char *argv0, int exitCode); -static void printVersion(); -int main(int argc, char *argv[]); #endif /* MAIN_H_ */ -- cgit v1.2.3-55-g7522