r22: Reap zombies; make errormsg bit more helpful when connection is
authoryoe <yoe>
Wed, 11 Sep 2002 15:51:32 +0000 (15:51 +0000)
committeryoe <yoe>
Wed, 11 Sep 2002 15:51:32 +0000 (15:51 +0000)
dropped before anything is sent.

nbd-client.c
nbd-server.c

index 38f8b4e..e9c08c2 100644 (file)
@@ -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 <asm/page.h>
@@ -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(".");
index 904f416..4d8c1e9 100644 (file)
  * Version 1.6 - fix autodetection of block device size and really make 64 bit
  *     clean on 32 bit machines. Anton Altaparmakov <aia21@cam.ac.uk>
  * 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 <sys/types.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/wait.h>          /* wait */
+#include <signal.h>            /* sigaction */
 #include <netinet/tcp.h>
 #include <netinet/in.h>                /* sockaddr_in, htons, in_addr */
 #include <netdb.h>             /* hostent, gethostby*, getservby* */
@@ -37,7 +41,7 @@
 #include <arpa/inet.h>
 #include <strings.h>
 
-#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");