diff options
author | Simon Rettberg | 2019-08-28 13:07:13 +0200 |
---|---|---|
committer | Simon Rettberg | 2019-08-28 13:07:13 +0200 |
commit | ac1bf45ebdd630fbc9ad2c1fa3c0ea99f5206799 (patch) | |
tree | 951388f8267c0194a142bf13d99b947ee7f820e6 /src/server/net.c | |
parent | [SERVER] Remove old comments (diff) | |
download | dnbd3-ac1bf45ebdd630fbc9ad2c1fa3c0ea99f5206799.tar.gz dnbd3-ac1bf45ebdd630fbc9ad2c1fa3c0ea99f5206799.tar.xz dnbd3-ac1bf45ebdd630fbc9ad2c1fa3c0ea99f5206799.zip |
[SERVER] Make signal handling more POSIX
According to POSIX, a signal sent to a PID can be delivered to an
arbitrary thread of that process that hasn't the signal blocked. This
seens to never happen on Linux, but would mess things up since the code
expected the main signal handler to only be executed by the main thread.
This should now be fixed by examining the destination PID of the signal
as well as the ID of the thread currently running the signal handler. If
we notice the signal wasn't sent by our own PID and the handler is not
currently run by the main thread, we re-send the signal to the main
thread. Otherwise, if the signal was sent by our own PID but the handler
is not run in the main thread, do nothing. This way we can use
pthread_kill() to wake up threads that might be stuck in a blocking
syscall when it's time to shut down.
Diffstat (limited to 'src/server/net.c')
-rw-r--r-- | src/server/net.c | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/server/net.c b/src/server/net.c index e0b516e..9c855e4 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -44,6 +44,7 @@ #include <jansson.h> #include <inttypes.h> #include <stdatomic.h> +#include <signal.h> static dnbd3_client_t *_clients[SERVER_MAX_CLIENTS]; static int _num_clients = 0; @@ -153,6 +154,7 @@ void* net_handleNewConnection(void *clientPtr) { dnbd3_client_t * const client = (dnbd3_client_t *)clientPtr; dnbd3_request_t request; + client->thread = pthread_self(); // Await data from client. Since this is a fresh connection, we expect data right away sock_setTimeout( client->sock, _clientTimeout ); @@ -631,11 +633,10 @@ void net_disconnectAll() int i; mutex_lock( &_clients_lock ); for (i = 0; i < _num_clients; ++i) { - if ( _clients[i] == NULL ) continue; - dnbd3_client_t * const client = _clients[i]; - mutex_lock( &client->lock ); - if ( client->sock >= 0 ) shutdown( client->sock, SHUT_RDWR ); - mutex_unlock( &client->lock ); + if ( _clients[i] == NULL ) + continue; + shutdown( _clients[i]->sock, SHUT_RDWR ); + pthread_kill( _clients[i]->thread, SIGINT ); } mutex_unlock( &_clients_lock ); } |