Fix compile with -DNOFORK
[nbd.git] / nbd-client.c
index 4156915..7aceb3e 100644 (file)
  * 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.
+ * 16/03/2010 - Add IPv6 support.
+ *     Kitt Tientanopajai <kitt@kitty.in.th>
+ *     Neutron Soutmun <neo.neutron@gmail.com>
+ *     Suriya Soutmun <darksolar@gmail.com>
  */
 
 #include "config.h"
@@ -21,8 +25,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <netinet/tcp.h>
-#include <netinet/in.h>                /* sockaddr_in, htons, in_addr */
-#include <netdb.h>             /* hostent, gethostby*, getservby* */
+#include <netinet/in.h>
+#include <netdb.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <syslog.h>
 
 int check_conn(char* devname, int do_print) {
        char buf[256];
+       char* p;
        int fd;
        int len;
        if(!strncmp(devname, "/dev/", 5)) {
                devname+=5;
        }
+       if((p=strchr(devname, 'p'))) {
+               /* We can't do checks on partitions. */
+               *p='\0';
+       }
        snprintf(buf, 256, "/sys/block/%s/pid", devname);
        if((fd=open(buf, O_RDONLY))<0) {
                if(errno==ENOENT) {
@@ -62,33 +71,55 @@ int check_conn(char* devname, int do_print) {
 
 int opennet(char *name, int port, int sdp) {
        int sock;
-       struct sockaddr_in xaddrin;
-       int xaddrinlen = sizeof(xaddrin);
-       struct hostent *hostn;
-       int af;
-
-       hostn = gethostbyname(name);
-       if (!hostn)
-               err("Gethostname failed: %h\n");
+       char portstr[6];
+       struct addrinfo hints;
+       struct addrinfo *ai = NULL;
+       struct addrinfo *rp = NULL;
+       int e;
+
+       snprintf(portstr, sizeof(portstr), "%d", port);
+
+       memset(&hints,'\0',sizeof(hints));
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_socktype = SOCK_STREAM;
+       hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
+       hints.ai_protocol = IPPROTO_TCP;
+
+       e = getaddrinfo(name, portstr, &hints, &ai);
+
+       if(e != 0) {
+               fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(e));
+               freeaddrinfo(ai);
+               exit(EXIT_FAILURE);
+       }
 
-       af = AF_INET;
        if(sdp) {
 #ifdef WITH_SDP
-               af = AF_INET_SDP;
+               if (ai->ai_family == AF_INET)
+                       ai->ai_family = AF_INET_SDP;
+               else (ai->ai_family == AF_INET6)
+                       ai->ai_family = AF_INET6_SDP;
 #else
                err("Can't do SDP: I was not compiled with SDP support!");
 #endif
        }
-       if ((sock = socket(af, SOCK_STREAM, IPPROTO_TCP)) < 0)
-               err("Socket failed: %m");
 
-       xaddrin.sin_family = af;
-       xaddrin.sin_port = htons(port);
-       xaddrin.sin_addr.s_addr = *((int *) hostn->h_addr);
-       if ((connect(sock, (struct sockaddr *) &xaddrin, xaddrinlen) < 0))
-               err("Connect: %m");
+       for(rp = ai; rp != NULL; rp = rp->ai_next) {
+               sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+
+               if(sock == -1)
+                       continue;       /* error */
+
+               if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
+                       break;          /* success */
+       }
+
+       if (rp == NULL)
+               err("Socket failed: %m");
 
        setmysockopt(sock);
+
+       freeaddrinfo(ai);
        return sock;
 }
 
@@ -173,32 +204,23 @@ void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
 }
 
 void set_timeout(int nbd, int timeout) {
-#ifdef NBD_SET_TIMEOUT
        if (timeout) {
+#ifdef NBD_SET_TIMEOUT
                if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
                        err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
                fprintf(stderr, "timeout=%d\n", timeout);
-       }
+#else
+               err("Ioctl NBD_SET_TIMEOUT cannot be called when compiled on a system that does not support it\n");
 #endif
+       }
 }
 
 void finish_sock(int sock, int nbd, int swap) {
        if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
                err("Ioctl NBD_SET_SOCK failed: %m\n");
 
-/*
- * If anyone ever forward-patches this patch, I'll happily re-enable
- * this code. Until then...
-#ifndef SO_SWAPPING
-       if (swap)
-               err("You have to compile me on machine with swapping patch enabled in order to use it later.");
-#else
        if (swap)
-               if (setsockopt(sock, SOL_SOCKET, SO_SWAPPING, &one, sizeof(int)) < 0)
-                       err("Could not enable swapping: %m");
-#endif
-*/
-       mlockall(MCL_CURRENT | MCL_FUTURE);
+               mlockall(MCL_CURRENT | MCL_FUTURE);
 }
 
 int main(int argc, char *argv[]) {
@@ -209,15 +231,17 @@ int main(int argc, char *argv[]) {
        int cont=0;
        int timeout=0;
        int sdp=0;
+       int nofork=0;
        u64 size64;
        u32 flags;
+       int i;
 
        logging();
 
        if (argc < 3) {
        errmsg:
                fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
-               fprintf(stderr, "Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist]\n");
+               fprintf(stderr, "Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist] [-nofork]\n");
                fprintf(stderr, "Or   : nbd-client -d nbd_device\n");
                fprintf(stderr, "Or   : nbd-client -c nbd_device\n");
                fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
@@ -232,7 +256,7 @@ int main(int argc, char *argv[]) {
        if (strcmp(argv[0], "-d")==0) {
                nbd = open(argv[1], O_RDWR);
                if (nbd < 0)
-                       err("Can not open NBD: %m");
+                       err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
                printf("Disconnecting: que, ");
                if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
                        err("Ioctl failed: %m\n");
@@ -276,26 +300,34 @@ int main(int argc, char *argv[]) {
        nbddev = argv[0];
        nbd = open(nbddev, O_RDWR);
        if (nbd < 0)
-         err("Can not open NBD: %m");
+         err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
        ++argv; --argc; /* skip device */
 
-       if (argc>3) goto errmsg;
-       if (argc) {
-               if(strncmp(argv[0], "-swap", 5)==0) {
-                       swap=1;
-                       ++argv;--argc;
+       if (argc>4) goto errmsg;
+       for(i=0; i<4; i++) {
+               if (argc) {
+                       if(strncmp(argv[0], "-swap", 5)==0) {
+                               swap=1;
+                               ++argv;--argc;
+                       }
                }
-       }
-       if (argc) {
-               if(strncmp(argv[0], "-persist", 8)==0) {
-                       cont=1;
-                       ++argv;--argc;
+               if (argc) {
+                       if(strncmp(argv[0], "-persist", 8)==0) {
+                               cont=1;
+                               ++argv;--argc;
+                       }
                }
-       }
-       if (argc) {
-               if(strncmp(argv[0], "-sdp", 4)==0) {
-                       sdp=1;
-                       ++argv;--argc;
+               if (argc) {
+                       if(strncmp(argv[0], "-sdp", 4)==0) {
+                               sdp=1;
+                               ++argv;--argc;
+                       }
+               }
+               if (argc) {
+                       if(strncmp(argv[0], "-nofork", 7)==0) {
+                               nofork=1;
+                               ++argv;--argc;
+                       }
                }
        }
        if(argc) goto errmsg;
@@ -309,10 +341,23 @@ int main(int argc, char *argv[]) {
 
        /* Go daemon */
        
-       chdir("/");
+#ifndef NOFORK
+       if(!nofork) daemon(0,0);
+#endif
        do {
 #ifndef NOFORK
                if (fork()) {
+                       /* Due to a race, the kernel NBD driver cannot
+                        * call for a reread of the partition table
+                        * in the handling of the NBD_DO_IT ioctl().
+                        * Therefore, this is done in the first open()
+                        * of the device. We therefore make sure that
+                        * the device is opened at least once after the
+                        * connection was made. This has to be done in a
+                        * separate process, since the NBD_DO_IT ioctl()
+                        * does not return until the NBD device has
+                        * disconnected.
+                        */
                        while(check_conn(nbddev, 0)) {
                                sleep(1);
                        }