summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Schmelzer2008-08-29 20:55:31 +0200
committerSebastian Schmelzer2008-08-29 20:55:31 +0200
commit4bff8e9bba4bcb09f210ea2fb11c5beda6f8ebf4 (patch)
tree153e90a2f089f4705cd7d334e9b37e8c826c9b47
downloadnbd-client-4bff8e9bba4bcb09f210ea2fb11c5beda6f8ebf4.tar.gz
nbd-client-4bff8e9bba4bcb09f210ea2fb11c5beda6f8ebf4.tar.xz
nbd-client-4bff8e9bba4bcb09f210ea2fb11c5beda6f8ebf4.zip
* added nbd-client (extracted from original client/server bundle)
see also #271 git-svn-id: http://svn.openslx.org/svn/openslx/openslx-src-tools/nbd-client/trunk@2139 95ad53e4-c205-0410-b2fa-d234c58c8868
-rw-r--r--Makefile8
-rw-r--r--README1
-rw-r--r--VERSION1
-rw-r--r--cliserv.h139
-rw-r--r--config.h187
-rw-r--r--lfs.h10
-rw-r--r--nbd-client.c358
7 files changed, 704 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..4374f9d
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+CC = /usr/bin/gcc
+OPTS = -g -O2
+
+all:
+ ${CC} ${OPTS} nbd-client.c -o nbd-client
+
+clean:
+ rm nbd-client
diff --git a/README b/README
new file mode 100644
index 0000000..5fad862
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+nbd-client extracted from original nbd client/server bundle
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..c8e03fd
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+nbd-client 2.9.11
diff --git a/cliserv.h b/cliserv.h
new file mode 100644
index 0000000..5685528
--- /dev/null
+++ b/cliserv.h
@@ -0,0 +1,139 @@
+/* This header file is shared by client & server. They really have
+ * something to share...
+ * */
+
+/* Client/server protocol is as follows:
+ Send INIT_PASSWD
+ Send 64-bit cliserv_magic
+ Send 64-bit size of exported device
+ Send 128 bytes of zeros (reserved for future use)
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <netdb.h>
+#include <netinet/tcp.h>
+#include <stdlib.h>
+
+#if SIZEOF_UNSIGNED_SHORT_INT==4
+typedef unsigned short u32;
+#elif SIZEOF_UNSIGNED_INT==4
+typedef unsigned int u32;
+#elif SIZEOF_UNSIGNED_LONG_INT==4
+typedef unsigned long u32;
+#else
+#error I need at least some 32-bit type
+#endif
+
+#if SIZEOF_UNSIGNED_INT==8
+typedef unsigned int u64;
+#elif SIZEOF_UNSIGNED_LONG_INT==8
+typedef unsigned long u64;
+#elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
+typedef unsigned long long u64;
+#else
+#error I need at least some 64-bit type
+#endif
+
+#ifdef NBD_H_LOCAL
+/* 2.6.18 and above use __be* rather than u* */
+#define __be32 u32
+#define __be64 u64
+#include "nbd.h"
+#endif
+#ifdef NBD_H_LINUX
+#include <linux/types.h>
+#include <linux/nbd.h>
+#endif
+
+#if NBD_LFS==1
+#define _LARGEFILE_SOURCE
+#define _FILE_OFFSET_BITS 64
+#endif
+
+u64 cliserv_magic = 0x00420281861253LL;
+#define INIT_PASSWD "NBDMAGIC"
+
+#define INFO(a) do { } while(0)
+
+void setmysockopt(int sock) {
+ int size = 1;
+#if 0
+ if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
+ INFO("(no sockopt/1: %m)");
+#endif
+#ifdef IPPROTO_TCP
+ size = 1;
+ if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
+ INFO("(no sockopt/2: %m)");
+#endif
+#if 0
+ size = 1024;
+ if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
+ INFO("(no sockopt/3: %m)");
+#endif
+}
+
+#ifndef G_GNUC_NORETURN
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
+#define G_GNUC_NORETURN __attribute__((__noreturn__))
+#else
+#define G_GNUC_NORETURN
+#endif
+#endif
+
+void err(const char *s) G_GNUC_NORETURN;
+
+void err(const char *s) {
+ const int maxlen = 150;
+ char s1[maxlen], *s2;
+
+ strncpy(s1, s, maxlen);
+ if ((s2 = strstr(s, "%m"))) {
+ strcpy(s1 + (s2 - s), strerror(errno));
+ s2 += 2;
+ strcpy(s1 + strlen(s1), s2);
+ }
+#ifndef sun
+ /* Solaris doesn't have %h in syslog */
+ else if ((s2 = strstr(s, "%h"))) {
+ strcpy(s1 + (s2 - s), hstrerror(h_errno));
+ s2 += 2;
+ strcpy(s1 + strlen(s1), s2);
+ }
+#endif
+
+ s1[maxlen-1] = '\0';
+#ifdef ISSERVER
+ syslog(LOG_ERR, "%s", s1);
+#endif
+ fprintf(stderr, "Error: %s\n", s1);
+ exit(1);
+}
+
+void logging(void) {
+#ifdef ISSERVER
+ openlog(MY_NAME, LOG_PID, LOG_DAEMON);
+#endif
+ setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stderr, NULL, _IONBF, 0);
+}
+
+#ifdef WORDS_BIGENDIAN
+u64 ntohll(u64 a) {
+ return a;
+}
+#else
+u64 ntohll(u64 a) {
+ u32 lo = a & 0xffffffff;
+ u32 hi = a >> 32U;
+ lo = ntohl(lo);
+ hi = ntohl(hi);
+ return ((u64) lo) << 32U | hi;
+}
+#endif
+#define htonll ntohll
+
+/* Flags used between the client and server */
+#define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
+#define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */
diff --git a/config.h b/config.h
new file mode 100644
index 0000000..00bff3d
--- /dev/null
+++ b/config.h
@@ -0,0 +1,187 @@
+/* config.h. Generated from config.h.in by configure. */
+/* config.h.in. Generated from configure.ac by autoheader. */
+
+/* Define if you want a debugging version of nbd-server (lots of copious
+ output) */
+/* #undef DODBG */
+
+/* Define to 1 if you have the `alarm' function. */
+#define HAVE_ALARM 1
+
+/* Define to 1 if you have the <arpa/inet.h> header file. */
+#define HAVE_ARPA_INET_H 1
+
+/* Define to 1 if you have the <fcntl.h> header file. */
+#define HAVE_FCNTL_H 1
+
+/* Define to 1 if you have the `fork' function. */
+#define HAVE_FORK 1
+
+/* Define to 1 if you have the `gethostbyname' function. */
+#define HAVE_GETHOSTBYNAME 1
+
+/* Define to 1 if you have the `inet_ntoa' function. */
+#define HAVE_INET_NTOA 1
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#define HAVE_INTTYPES_H 1
+
+/* Define to 1 if you have the `llseek' function. */
+#define HAVE_LLSEEK 1
+
+/* Define to 1 if you have the <memory.h> header file. */
+#define HAVE_MEMORY_H 1
+
+/* Define to 1 if you have the `memset' function. */
+#define HAVE_MEMSET 1
+
+/* Define to 1 if you have the <netdb.h> header file. */
+#define HAVE_NETDB_H 1
+
+/* Define to 1 if you have the <netinet/in.h> header file. */
+#define HAVE_NETINET_IN_H 1
+
+/* Define to 1 if you have the `socket' function. */
+#define HAVE_SOCKET 1
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#define HAVE_STDINT_H 1
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#define HAVE_STDLIB_H 1
+
+/* Define to 1 if you have the `strerror' function. */
+#define HAVE_STRERROR 1
+
+/* Define to 1 if you have the <strings.h> header file. */
+#define HAVE_STRINGS_H 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the `strstr' function. */
+#define HAVE_STRSTR 1
+
+/* Define to 1 if you have the <syslog.h> header file. */
+#define HAVE_SYSLOG_H 1
+
+/* Define to 1 if you have the <sys/ioctl.h> header file. */
+#define HAVE_SYS_IOCTL_H 1
+
+/* Define to 1 if you have the <sys/mount.h> header file. */
+#define HAVE_SYS_MOUNT_H 1
+
+/* Define to 1 if you have the <sys/socket.h> header file. */
+#define HAVE_SYS_SOCKET_H 1
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#define HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have <sys/wait.h> that is POSIX.1 compatible. */
+#define HAVE_SYS_WAIT_H 1
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#define HAVE_UNISTD_H 1
+
+/* Define to 1 if you have the `vfork' function. */
+#define HAVE_VFORK 1
+
+/* Define to 1 if you have the <vfork.h> header file. */
+/* #undef HAVE_VFORK_H */
+
+/* Define to 1 if `fork' works. */
+#define HAVE_WORKING_FORK 1
+
+/* Define to 1 if `vfork' works. */
+#define HAVE_WORKING_VFORK 1
+
+/* Define to 1 if you want nbd-server to log through syslog */
+/* #undef ISSERVER */
+
+/* Set to 1 if a (2.6) nbd.h can be found in the linux directory in the search
+ path */
+#define NBD_H_LINUX 1
+
+/* Set to 1 if a (2.6) nbd.h can be found in the current directory */
+/* #undef NBD_H_LOCAL */
+
+/* Define to 1 if Large File Support should be enabled */
+#define NBD_LFS 1
+
+/* Define if you don't want the nbd-server to fork() */
+/* #undef NOFORK */
+
+/* Name of package */
+#define PACKAGE "nbd"
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT "wouter@debian.org"
+
+/* Define to the full name of this package. */
+#define PACKAGE_NAME "nbd"
+
+/* Define to the full name and version of this package. */
+#define PACKAGE_STRING "nbd 2.9.11"
+
+/* Define to the one symbol short name of this package. */
+#define PACKAGE_TARNAME "nbd"
+
+/* Define to the version of this package. */
+#define PACKAGE_VERSION "2.9.11"
+
+/* Define to 1 if the C compiler supports function prototypes. */
+#define PROTOTYPES 1
+
+/* Define to 1 if the `setvbuf' function takes the buffering type as its
+ second argument and the buffer pointer as the third, as on System V before
+ release 3. */
+/* #undef SETVBUF_REVERSED */
+
+/* The size of `unsigned int', as computed by sizeof. */
+#define SIZEOF_UNSIGNED_INT 4
+
+/* The size of `unsigned long int', as computed by sizeof. */
+#define SIZEOF_UNSIGNED_LONG_INT 4
+
+/* The size of `unsigned long long int', as computed by sizeof. */
+#define SIZEOF_UNSIGNED_LONG_LONG_INT 8
+
+/* The size of `unsigned short int', as computed by sizeof. */
+#define SIZEOF_UNSIGNED_SHORT_INT 2
+
+/* Define to 1 if you have the ANSI C header files. */
+#define STDC_HEADERS 1
+
+/* Version number of package */
+#define VERSION "2.9.11"
+
+/* Define to 1 if you have and want support for the Socket Direct Protocol */
+/* #undef WITH_SDP */
+
+/* Define to 1 if your processor stores words with the most significant byte
+ first (like Motorola and SPARC, unlike Intel and VAX). */
+/* #undef WORDS_BIGENDIAN */
+
+/* Define like PROTOTYPES; this can be used by system headers. */
+#define __PROTOTYPES 1
+
+/* Define to empty if `const' does not conform to ANSI C. */
+/* #undef const */
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+ calls it, or to nothing if 'inline' is not supported under any name. */
+#ifndef __cplusplus
+/* #undef inline */
+#endif
+
+/* Define to `long int' if <sys/types.h> does not define. */
+/* #undef off_t */
+
+/* Define to `int' if <sys/types.h> does not define. */
+/* #undef pid_t */
+
+/* Define as `fork' if `vfork' does not work. */
+/* #undef vfork */
diff --git a/lfs.h b/lfs.h
new file mode 100644
index 0000000..8f90f81
--- /dev/null
+++ b/lfs.h
@@ -0,0 +1,10 @@
+#ifndef LFS_H
+#define LFS_H
+
+#include "config.h"
+#ifdef NBD_LFS
+#define _FILE_OFFSET_BITS 64
+#define _LARGEFILE_SOURCE
+#endif /* NBD_LFS */
+
+#endif /* LFS_H */
diff --git a/nbd-client.c b/nbd-client.c
new file mode 100644
index 0000000..4df2281
--- /dev/null
+++ b/nbd-client.c
@@ -0,0 +1,358 @@
+/*
+ * Open connection for network block device
+ *
+ * Copyright 1997,1998 Pavel Machek, distribute under GPL
+ * <pavel@atrey.karlin.mff.cuni.cz>
+ *
+ * Version 1.0 - 64bit issues should be fixed, now
+ * 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 "config.h"
+#include "lfs.h"
+
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#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 <stdio.h>
+#include <fcntl.h>
+#include <syslog.h>
+#include <stdlib.h>
+#include <sys/mount.h>
+#include <errno.h>
+
+#ifndef __GNUC__
+#error I need GCC to work
+#endif
+
+#include <linux/ioctl.h>
+#define MY_NAME "nbd_client"
+#include "cliserv.h"
+
+int check_conn(char* devname, int do_print) {
+ char buf[256];
+ int fd;
+ int len;
+ if(!strncmp(devname, "/dev/", 5)) {
+ devname+=5;
+ }
+ snprintf(buf, 256, "/sys/block/%s/pid", devname);
+ if((fd=open(buf, O_RDONLY))<0) {
+ if(errno==ENOENT) {
+ return 1;
+ } else {
+ return 2;
+ }
+ }
+ len=read(fd, buf, 256);
+ buf[len-1]='\0';
+ if(do_print) printf("%s\n", buf);
+ return 0;
+}
+
+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");
+
+ af = AF_INET;
+ if(sdp) {
+#ifdef WITH_SDP
+ af = AF_INET_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");
+
+ setmysockopt(sock);
+ return sock;
+}
+
+void negotiate(int sock, u64 *rsize64, u32 *flags) {
+ u64 magic, size64;
+ char buf[256] = "\0\0\0\0\0\0\0\0\0";
+
+ 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(".");
+ if (read(sock, &magic, sizeof(magic)) < 0)
+ err("Failed/2: %m");
+ magic = ntohll(magic);
+ if (magic != cliserv_magic)
+ err("Not enough cliserv_magic");
+ printf(".");
+
+ if (read(sock, &size64, sizeof(size64)) < 0)
+ err("Failed/3: %m\n");
+ size64 = ntohll(size64);
+
+#ifdef NBD_SET_SIZE_BLOCKS
+ if ((size64>>10) > (~0UL >> 1)) {
+ printf("size = %luMB", (unsigned long)(size64>>20));
+ err("Exported device is too big for me. Get 64-bit machine :-(\n");
+ } else
+ printf("size = %luKB", (unsigned long)(size64>>10));
+#else
+ if (size64 > (~0UL >> 1)) {
+ printf("size = %luKB", (unsigned long)(size64>>10));
+ err("Exported device is too big. Get 64-bit machine or newer kernel :-(\n");
+ } else
+ printf("size = %lu", (unsigned long)(size64));
+#endif
+
+ if (read(sock, flags, sizeof(*flags)) < 0)
+ err("Failed/4: %m\n");
+ *flags = ntohl(*flags);
+
+ if (read(sock, &buf, 124) < 0)
+ err("Failed/5: %m\n");
+ printf("\n");
+
+ *rsize64 = size64;
+}
+
+void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
+ unsigned long size;
+ int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
+
+#ifdef NBD_SET_SIZE_BLOCKS
+ if (size64/blocksize > (~0UL >> 1))
+ err("Device too large.\n");
+ else {
+ int er;
+ if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
+ err("Ioctl/1.1a failed: %m\n");
+ size = (unsigned long)(size64/blocksize);
+ if ((er = ioctl(nbd, NBD_SET_SIZE_BLOCKS, size)) < 0)
+ err("Ioctl/1.1b failed: %m\n");
+ fprintf(stderr, "bs=%d, sz=%lu\n", blocksize, size);
+ }
+#else
+ if (size64 > (~0UL >> 1)) {
+ err("Device too large.\n");
+ } else {
+ size = (unsigned long)size64;
+ if (ioctl(nbd, NBD_SET_SIZE, size) < 0)
+ err("Ioctl NBD_SET_SIZE failed: %m\n");
+ }
+#endif
+
+ ioctl(nbd, NBD_CLEAR_SOCK);
+
+ if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
+ err("Unable to set read-only attribute for device");
+}
+
+void set_timeout(int nbd, int timeout) {
+#ifdef NBD_SET_TIMEOUT
+ if (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);
+ }
+#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");
+
+#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
+}
+
+int main(int argc, char *argv[]) {
+ int port, sock, nbd;
+ int blocksize=1024;
+ char *hostname, *nbddev;
+ int swap=0;
+ int cont=0;
+ int timeout=0;
+ int sdp=0;
+ u64 size64;
+ u32 flags;
+
+ 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, "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");
+ fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
+ fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
+ fprintf(stderr, "blocksizes other than 1024 without patches\n");
+ return 1;
+ }
+
+ ++argv; --argc; /* skip programname */
+
+ if (strcmp(argv[0], "-d")==0) {
+ nbd = open(argv[1], O_RDWR);
+ if (nbd < 0)
+ err("Can not open NBD: %m");
+ printf("Disconnecting: que, ");
+ if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
+ err("Ioctl failed: %m\n");
+ printf("disconnect, ");
+#ifdef NBD_DISCONNECT
+ if (ioctl(nbd, NBD_DISCONNECT)<0)
+ err("Ioctl failed: %m\n");
+ printf("sock, ");
+#else
+ fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
+ exit(1);
+#endif
+ if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
+ err("Ioctl failed: %m\n");
+ printf("done\n");
+ return 0;
+ }
+ if(strcmp(argv[0], "-c")==0) {
+ return check_conn(argv[1], 1);
+ }
+
+ if (strncmp(argv[0], "bs=", 3)==0) {
+ blocksize=atoi(argv[0]+3);
+ ++argv; --argc; /* skip blocksize */
+ }
+
+ if (strncmp(argv[0], "timeout=", 8)==0) {
+ timeout=atoi(argv[0]+8);
+ ++argv; --argc; /* skip timeout */
+ }
+
+ if (argc==0) goto errmsg;
+ hostname=argv[0];
+ ++argv; --argc; /* skip hostname */
+
+ if (argc==0) goto errmsg;
+ port = atoi(argv[0]);
+ ++argv; --argc; /* skip port */
+
+ if (argc==0) goto errmsg;
+ nbddev = argv[0];
+ nbd = open(nbddev, O_RDWR);
+ if (nbd < 0)
+ err("Can not open NBD: %m");
+ ++argv; --argc; /* skip device */
+
+ if (argc>3) goto errmsg;
+ 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], "-sdp", 4)==0) {
+ sdp=1;
+ ++argv;--argc;
+ }
+ }
+ if(argc) goto errmsg;
+ sock = opennet(hostname, port, sdp);
+ argv=NULL; argc=0; /* don't use it later suddenly */
+
+ negotiate(sock, &size64, &flags);
+ setsizes(nbd, size64, blocksize, flags);
+ set_timeout(nbd, timeout);
+ finish_sock(sock, nbd, swap);
+
+ /* Go daemon */
+
+ chdir("/");
+ do {
+#ifndef NOFORK
+ if (fork()) {
+ while(check_conn(nbddev, 0)) {
+ sleep(1);
+ }
+ open(nbddev, O_RDONLY);
+ exit(0);
+ }
+#endif
+
+ if (ioctl(nbd, NBD_DO_IT) < 0) {
+ fprintf(stderr, "Kernel call returned: %m");
+ if(errno==EBADR) {
+ /* The user probably did 'nbd-client -d' on us.
+ * quit */
+ cont=0;
+ } else {
+ if(cont) {
+ u64 new_size;
+ u32 new_flags;
+
+ fprintf(stderr, " Reconnecting\n");
+ close(sock); close(nbd);
+ sock = opennet(hostname, port, sdp);
+ nbd = open(nbddev, O_RDWR);
+ negotiate(sock, &new_size, &new_flags);
+ if (size64 != new_size) {
+ err("Size of the device changed. Bye");
+ }
+ setsizes(nbd, size64, blocksize,
+ new_flags);
+
+ set_timeout(nbd, timeout);
+ finish_sock(sock,nbd,swap);
+ }
+ }
+ } else {
+ /* We're on 2.4. It's not clearly defined what exactly
+ * happened at this point. Probably best to quit, now
+ */
+ fprintf(stderr, "Kernel call returned.");
+ cont=0;
+ }
+ } while(cont);
+ printf("Closing: que, ");
+ ioctl(nbd, NBD_CLEAR_QUE);
+ printf("sock, ");
+ ioctl(nbd, NBD_CLEAR_SOCK);
+ printf("done\n");
+ return 0;
+}