From: yoe Date: Wed, 11 Sep 2002 15:51:32 +0000 (+0000) Subject: r22: Reap zombies; make errormsg bit more helpful when connection is X-Git-Url: http://git.alex.org.uk r22: Reap zombies; make errormsg bit more helpful when connection is dropped before anything is sent. --- diff --git a/nbd-client.c b/nbd-client.c index 38f8b4e..e9c08c2 100644 --- a/nbd-client.c +++ b/nbd-client.c @@ -8,6 +8,9 @@ * Version 1.1 - added bs (blocksize) option (Alexey Guzeev, aga@permonline.ru) * Version 1.2 - I added new option '-d' to send the disconnect request * Version 2.0 - Version synchronised with server + * Version 2.1 - Check for disconnection before INIT_PASSWD is received + * to make errormsg a bit more helpful in case the server can't + * open the exported file. */ #include @@ -129,6 +132,8 @@ int main(int argc, char *argv[]) printf("Negotiation: "); if (read(sock, buf, 8) < 0) err("Failed/1: %m"); + if (strlen(buf)==0) + err("Server closed connection"); if (strcmp(buf, INIT_PASSWD)) err("INIT_PASSWD bad"); printf("."); diff --git a/nbd-server.c b/nbd-server.c index 904f416..4d8c1e9 100644 --- a/nbd-server.c +++ b/nbd-server.c @@ -17,14 +17,18 @@ * Version 1.6 - fix autodetection of block device size and really make 64 bit * clean on 32 bit machines. Anton Altaparmakov * Version 2.0 - Version synchronised with client + * Version 2.1 - Reap zombie client processes when they exit. Removed + * (uncommented) the _IO magic, it's no longer necessary. */ -#define VERSION "2.0" +#define VERSION "2.1" #define GIGA (1*1024*1024*1024) #include #include #include +#include /* wait */ +#include /* sigaction */ #include #include /* sockaddr_in, htons, in_addr */ #include /* hostent, gethostby*, getservby* */ @@ -37,7 +41,7 @@ #include #include -#define _IO(a,b) +//#define _IO(a,b) // #define ISSERVER #define MY_NAME "nbd_server" @@ -47,7 +51,7 @@ #define AUTH_FILE "nbd_server.allow" #include "cliserv.h" -#undef _IO +//#undef _IO /* Deep magic: ioctl.h defines _IO macro (at least on linux) */ @@ -222,9 +226,15 @@ void cmdline(int argc, char *argv[]) exportname = argv[2]; } +void sigchld_handler(int s) +{ + while(wait(NULL) > 0); +} + void connectme(int port) { struct sockaddr_in addrin; + struct sigaction sa; int addrinlen = sizeof(addrin); int net, sock, newpid; #ifndef sun @@ -251,6 +261,11 @@ void connectme(int port) if (listen(sock, 1) < 0) err("listen: %m"); DEBUG("accept, "); + sa.sa_handler = sigchld_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_RESTART; + if(sigaction(SIGCHLD, &sa, NULL) == -1) + err("sigaction: %m"); for(;;) { /* infinite loop */ if ((net = accept(sock, (struct sockaddr *) &addrin, &addrinlen)) < 0) err("accept: %m");