Network Block Device  @PACKAGE_VERSION@
nbd-client.c
Go to the documentation of this file.
1 /*
2  * Open connection for network block device
3  *
4  * Copyright 1997,1998 Pavel Machek, distribute under GPL
5  * <pavel@atrey.karlin.mff.cuni.cz>
6  * Copyright (c) 2002 - 2011 Wouter Verhelst <w@uter.be>
7  *
8  * Version 1.0 - 64bit issues should be fixed, now
9  * Version 1.1 - added bs (blocksize) option (Alexey Guzeev, aga@permonline.ru)
10  * Version 1.2 - I added new option '-d' to send the disconnect request
11  * Version 2.0 - Version synchronised with server
12  * Version 2.1 - Check for disconnection before INIT_PASSWD is received
13  * to make errormsg a bit more helpful in case the server can't
14  * open the exported file.
15  * 16/03/2010 - Add IPv6 support.
16  * Kitt Tientanopajai <kitt@kitty.in.th>
17  * Neutron Soutmun <neo.neutron@gmail.com>
18  * Suriya Soutmun <darksolar@gmail.com>
19  */
20 
21 #include "config.h"
22 #include "lfs.h"
23 
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <netinet/tcp.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include "netdb-compat.h"
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <syslog.h>
36 #include <stdlib.h>
37 #include <sys/mount.h>
38 #include <sys/mman.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include <getopt.h>
42 #include <stdarg.h>
43 
44 #include <linux/ioctl.h>
45 #define MY_NAME "nbd_client"
46 #include "cliserv.h"
47 
48 #ifdef WITH_SDP
49 #include <sdp_inet.h>
50 #endif
51 
52 #define NBDC_DO_LIST 1
53 
54 int check_conn(char* devname, int do_print) {
55  char buf[256];
56  char* p;
57  int fd;
58  int len;
59 
60  if( (p=strrchr(devname, '/')) ) {
61  devname=p+1;
62  }
63  if((p=strchr(devname, 'p'))) {
64  /* We can't do checks on partitions. */
65  *p='\0';
66  }
67  snprintf(buf, 256, "/sys/block/%s/pid", devname);
68  if((fd=open(buf, O_RDONLY))<0) {
69  if(errno==ENOENT) {
70  return 1;
71  } else {
72  return 2;
73  }
74  }
75  len=read(fd, buf, 256);
76  if(len < 0) {
77  perror("could not read from server");
78  return 2;
79  }
80  buf[(len < 256) ? len : 255]='\0';
81  if(do_print) printf("%s\n", buf);
82  close(fd);
83  return 0;
84 }
85 
86 int opennet(char *name, char* portstr, int sdp) {
87  int sock;
88  struct addrinfo hints;
89  struct addrinfo *ai = NULL;
90  struct addrinfo *rp = NULL;
91  int e;
92 
93  memset(&hints,'\0',sizeof(hints));
94  hints.ai_family = AF_UNSPEC;
95  hints.ai_socktype = SOCK_STREAM;
96  hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
97  hints.ai_protocol = IPPROTO_TCP;
98 
99  e = getaddrinfo(name, portstr, &hints, &ai);
100 
101  if(e != 0) {
102  fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(e));
103  freeaddrinfo(ai);
104  return -1;
105  }
106 
107  if(sdp) {
108 #ifdef WITH_SDP
109  if (ai->ai_family == AF_INET)
110  ai->ai_family = AF_INET_SDP;
111  else (ai->ai_family == AF_INET6)
112  ai->ai_family = AF_INET6_SDP;
113 #else
114  err("Can't do SDP: I was not compiled with SDP support!");
115 #endif
116  }
117 
118  for(rp = ai; rp != NULL; rp = rp->ai_next) {
119  sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
120 
121  if(sock == -1)
122  continue; /* error */
123 
124  if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
125  break; /* success */
126 
127  close(sock);
128  }
129 
130  if (rp == NULL) {
131  err_nonfatal("Socket failed: %m");
132  sock = -1;
133  goto err;
134  }
135 
136  setmysockopt(sock);
137 err:
138  freeaddrinfo(ai);
139  return sock;
140 }
141 
142 int openunix(const char *path) {
143  int sock;
144  struct sockaddr_un un_addr;
145  memset(&un_addr, 0, sizeof(un_addr));
146 
147  un_addr.sun_family = AF_UNIX;
148  if (strnlen(path, sizeof(un_addr.sun_path)) == sizeof(un_addr.sun_path)) {
149  err_nonfatal("UNIX socket path too long");
150  return -1;
151  }
152 
153  strncpy(un_addr.sun_path, path, sizeof(un_addr.sun_path) - 1);
154 
155  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
156  err_nonfatal("SOCKET failed");
157  return -1;
158  };
159 
160  if (connect(sock, &un_addr, sizeof(un_addr)) == -1) {
161  err_nonfatal("CONNECT failed");
162  close(sock);
163  return -1;
164  }
165  return sock;
166 }
167 
168 void ask_list(int sock) {
169  uint32_t opt;
170  uint32_t opt_server;
171  uint32_t len;
172  uint32_t reptype;
173  uint64_t magic;
174  const int BUF_SIZE = 1024;
175  char buf[BUF_SIZE];
176 
177  magic = ntohll(opts_magic);
178  if (write(sock, &magic, sizeof(magic)) < 0)
179  err("Failed/2.2: %m");
180 
181  /* Ask for the list */
182  opt = htonl(NBD_OPT_LIST);
183  if(write(sock, &opt, sizeof(opt)) < 0) {
184  err("writing list option failed: %m");
185  }
186  /* Send the length (zero) */
187  len = htonl(0);
188  if(write(sock, &len, sizeof(len)) < 0) {
189  err("writing length failed: %m");
190  }
191  /* newline, move away from the "Negotiation:" line */
192  printf("\n");
193  do {
194  memset(buf, 0, 1024);
195  if(read(sock, &magic, sizeof(magic)) < 0) {
196  err("Reading magic from server: %m");
197  }
198  if(read(sock, &opt_server, sizeof(opt_server)) < 0) {
199  err("Reading option: %m");
200  }
201  if(read(sock, &reptype, sizeof(reptype)) <0) {
202  err("Reading reply from server: %m");
203  }
204  if(read(sock, &len, sizeof(len)) < 0) {
205  err("Reading length from server: %m");
206  }
207  magic=ntohll(magic);
208  len=ntohl(len);
209  reptype=ntohl(reptype);
210  if(magic != rep_magic) {
211  err("Not enough magic from server");
212  }
213  if(reptype & NBD_REP_FLAG_ERROR) {
214  switch(reptype) {
215  case NBD_REP_ERR_POLICY:
216  fprintf(stderr, "\nE: listing not allowed by server.\n");
217  break;
218  default:
219  fprintf(stderr, "\nE: unexpected error from server.\n");
220  break;
221  }
222  if(len > 0 && len < BUF_SIZE) {
223  if(len=read(sock, buf, len) < 0) {
224  fprintf(stderr, "\nE: could not read error message from server\n");
225  }
226  buf[len] = '\0';
227  fprintf(stderr, "Server said: %s\n", buf);
228  }
229  exit(EXIT_FAILURE);
230  } else {
231  if(len) {
232  if(reptype != NBD_REP_SERVER) {
233  err("Server sent us a reply we don't understand!");
234  }
235  if(read(sock, &len, sizeof(len)) < 0) {
236  fprintf(stderr, "\nE: could not read export name length from server\n");
237  exit(EXIT_FAILURE);
238  }
239  len=ntohl(len);
240  if (len >= BUF_SIZE) {
241  fprintf(stderr, "\nE: export name on server too long\n");
242  exit(EXIT_FAILURE);
243  }
244  if(read(sock, buf, len) < 0) {
245  fprintf(stderr, "\nE: could not read export name from server\n");
246  exit(EXIT_FAILURE);
247  }
248  buf[len] = 0;
249  printf("%s\n", buf);
250  }
251  }
252  } while(reptype != NBD_REP_ACK);
253  opt=htonl(NBD_OPT_ABORT);
254  len=htonl(0);
255  magic=htonll(opts_magic);
256  if (write(sock, &magic, sizeof(magic)) < 0)
257  err("Failed/2.2: %m");
258  if (write(sock, &opt, sizeof(opt)) < 0)
259  err("Failed writing abort");
260  if (write(sock, &len, sizeof(len)) < 0)
261  err("Failed writing length");
262 }
263 
264 void negotiate(int sock, u64 *rsize64, uint16_t *flags, char* name, uint32_t needed_flags, uint32_t client_flags, uint32_t do_opts) {
265  u64 magic, size64;
266  uint16_t tmp;
267  uint16_t global_flags;
268  char buf[256] = "\0\0\0\0\0\0\0\0\0";
269  uint32_t opt;
270  uint32_t namesize;
271 
272  printf("Negotiation: ");
273  if (read(sock, buf, 8) < 0)
274  err("Failed/1: %m");
275  if (strlen(buf)==0)
276  err("Server closed connection");
277  if (strcmp(buf, INIT_PASSWD))
278  err("INIT_PASSWD bad");
279  printf(".");
280  if (read(sock, &magic, sizeof(magic)) < 0)
281  err("Failed/2: %m");
282  magic = ntohll(magic);
283  if (magic != opts_magic) {
284  if(magic == cliserv_magic) {
285  err("It looks like you're trying to connect to an oldstyle server. This is no longer supported since nbd 3.10.");
286  }
287  }
288  printf(".");
289  if(read(sock, &tmp, sizeof(uint16_t)) < 0) {
290  err("Failed reading flags: %m");
291  }
292  global_flags = ntohs(tmp);
293  if((needed_flags & global_flags) != needed_flags) {
294  /* There's currently really only one reason why this
295  * check could possibly fail, but we may need to change
296  * this error message in the future... */
297  fprintf(stderr, "\nE: Server does not support listing exports\n");
298  exit(EXIT_FAILURE);
299  }
300 
301  if (global_flags & NBD_FLAG_NO_ZEROES) {
302  client_flags |= NBD_FLAG_C_NO_ZEROES;
303  }
304  client_flags = htonl(client_flags);
305  if (write(sock, &client_flags, sizeof(client_flags)) < 0)
306  err("Failed/2.1: %m");
307 
308  if(do_opts & NBDC_DO_LIST) {
309  ask_list(sock);
310  exit(EXIT_SUCCESS);
311  }
312 
313  /* Write the export name that we're after */
314  magic = htonll(opts_magic);
315  if (write(sock, &magic, sizeof(magic)) < 0)
316  err("Failed/2.2: %m");
317 
318  opt = ntohl(NBD_OPT_EXPORT_NAME);
319  if (write(sock, &opt, sizeof(opt)) < 0)
320  err("Failed/2.3: %m");
321  namesize = (u32)strlen(name);
322  namesize = ntohl(namesize);
323  if (write(sock, &namesize, sizeof(namesize)) < 0)
324  err("Failed/2.4: %m");
325  if (write(sock, name, strlen(name)) < 0)
326  err("Failed/2.4: %m");
327 
328  if (read(sock, &size64, sizeof(size64)) <= 0) {
329  if (!errno)
330  err("Server closed connection");
331  err("Failed/3: %m\n");
332  }
333  size64 = ntohll(size64);
334 
335  if ((size64>>12) > (uint64_t)~0UL) {
336  printf("size = %luMB", (unsigned long)(size64>>20));
337  err("Exported device is too big for me. Get 64-bit machine :-(\n");
338  } else
339  printf("size = %luMB", (unsigned long)(size64>>20));
340 
341  if(read(sock, &tmp, sizeof(tmp)) < 0)
342  err("Failed/4: %m\n");
343  *flags = (uint32_t)ntohs(tmp);
344 
345  if (!(global_flags & NBD_FLAG_NO_ZEROES)) {
346  if (read(sock, &buf, 124) < 0)
347  err("Failed/5: %m\n");
348  }
349  printf("\n");
350 
351  *rsize64 = size64;
352 }
353 
354 void setsizes(int nbd, u64 size64, int blocksize, u32 flags) {
355  unsigned long size;
356  int read_only = (flags & NBD_FLAG_READ_ONLY) ? 1 : 0;
357 
358  if (size64>>12 > (uint64_t)~0UL)
359  err("Device too large.\n");
360  else {
361  if (ioctl(nbd, NBD_SET_BLKSIZE, 4096UL) < 0)
362  err("Ioctl/1.1a failed: %m\n");
363  size = (unsigned long)(size64>>12);
364  if (ioctl(nbd, NBD_SET_SIZE_BLOCKS, size) < 0)
365  err("Ioctl/1.1b failed: %m\n");
366  if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0)
367  err("Ioctl/1.1c failed: %m\n");
368  fprintf(stderr, "bs=%d, sz=%llu bytes\n", blocksize, 4096ULL*size);
369  }
370 
371  ioctl(nbd, NBD_CLEAR_SOCK);
372 
373  /* ignore error as kernel may not support */
374  ioctl(nbd, NBD_SET_FLAGS, (unsigned long) flags);
375 
376  if (ioctl(nbd, BLKROSET, (unsigned long) &read_only) < 0)
377  err("Unable to set read-only attribute for device");
378 }
379 
380 void set_timeout(int nbd, int timeout) {
381  if (timeout) {
382  if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long)timeout) < 0)
383  err("Ioctl NBD_SET_TIMEOUT failed: %m\n");
384  fprintf(stderr, "timeout=%d\n", timeout);
385  }
386 }
387 
388 void finish_sock(int sock, int nbd, int swap) {
389  if (ioctl(nbd, NBD_SET_SOCK, sock) < 0)
390  err("Ioctl NBD_SET_SOCK failed: %m\n");
391 
392 #ifndef __ANDROID__
393  if (swap)
394  mlockall(MCL_CURRENT | MCL_FUTURE);
395 #endif
396 }
397 
398 static int
399 oom_adjust(const char *file, const char *value)
400 {
401  int fd, rc;
402  size_t len;
403 
404  fd = open(file, O_WRONLY);
405  if (fd < 0)
406  return -1;
407  len = strlen(value);
408  rc = write(fd, value, len) != (ssize_t) len;
409  close(fd);
410  return rc ? -1 : 0;
411 }
412 
413 void usage(char* errmsg, ...) {
414  if(errmsg) {
415  char tmp[256];
416  va_list ap;
417  va_start(ap, errmsg);
418  snprintf(tmp, 256, "ERROR: %s\n\n", errmsg);
419  vfprintf(stderr, tmp, ap);
420  va_end(ap);
421  } else {
422  fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
423  }
424  fprintf(stderr, "Usage: nbd-client -name|-N name host [port] nbd_device [-block-size|-b block size] [-timeout|-t timeout] [-swap|-s] [-sdp|-S] [-persist|-p] [-nofork|-n] [-systemd-mark|-m]\n");
425  fprintf(stderr, "Or : nbd-client -d nbd_device\n");
426  fprintf(stderr, "Or : nbd-client -c nbd_device\n");
427  fprintf(stderr, "Or : nbd-client -h|--help\n");
428  fprintf(stderr, "Or : nbd-client -l|--list host\n");
429  fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
430  fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
431  fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
432  fprintf(stderr, "blocksizes other than 1024 without patches\n");
433  fprintf(stderr, "Default value for port with -N is 10809. Note that port must always be numeric\n");
434 }
435 
436 void disconnect(char* device) {
437  int nbd = open(device, O_RDWR);
438 
439  if (nbd < 0)
440  err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
441  printf("disconnect, ");
442  if (ioctl(nbd, NBD_DISCONNECT)<0)
443  err("Ioctl failed: %m\n");
444  printf("sock, ");
445  if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
446  err("Ioctl failed: %m\n");
447  printf("done\n");
448 }
449 
450 int main(int argc, char *argv[]) {
451  char* port=NULL;
452  int sock, nbd;
453  int blocksize=1024;
454  char *hostname=NULL;
455  char *nbddev=NULL;
456  int swap=0;
457  int cont=0;
458  int timeout=0;
459  int sdp=0;
460  int G_GNUC_UNUSED nofork=0; // if -dNOFORK
461  u64 size64;
462  uint16_t flags = 0;
463  int c;
464  int nonspecial=0;
465  int b_unix=0;
466  char* name=NULL;
467  uint16_t needed_flags=0;
468  uint32_t cflags=NBD_FLAG_C_FIXED_NEWSTYLE;
469  uint32_t opts=0;
470  sigset_t block, old;
471  struct sigaction sa;
472  struct option long_options[] = {
473  { "block-size", required_argument, NULL, 'b' },
474  { "check", required_argument, NULL, 'c' },
475  { "disconnect", required_argument, NULL, 'd' },
476  { "help", no_argument, NULL, 'h' },
477  { "list", no_argument, NULL, 'l' },
478  { "name", required_argument, NULL, 'N' },
479  { "nofork", no_argument, NULL, 'n' },
480  { "persist", no_argument, NULL, 'p' },
481  { "sdp", no_argument, NULL, 'S' },
482  { "swap", no_argument, NULL, 's' },
483  { "systemd-mark", no_argument, NULL, 'm' },
484  { "timeout", required_argument, NULL, 't' },
485  { "unix", no_argument, NULL, 'u' },
486  { 0, 0, 0, 0 },
487  };
488 
489  logging(MY_NAME);
490 
491  while((c=getopt_long_only(argc, argv, "-b:c:d:hlnN:pSst:u", long_options, NULL))>=0) {
492  switch(c) {
493  case 1:
494  // non-option argument
495  if(strchr(optarg, '=')) {
496  // old-style 'bs=' or 'timeout='
497  // argument
498  fprintf(stderr, "WARNING: old-style command-line argument encountered. This is deprecated.\n");
499  if(!strncmp(optarg, "bs=", 3)) {
500  optarg+=3;
501  goto blocksize;
502  }
503  if(!strncmp(optarg, "timeout=", 8)) {
504  optarg+=8;
505  goto timeout;
506  }
507  usage("unknown option %s encountered", optarg);
508  exit(EXIT_FAILURE);
509  }
510  switch(nonspecial++) {
511  case 0:
512  // host
513  hostname=optarg;
514  break;
515  case 1:
516  // port
517  if(!strtol(optarg, NULL, 0)) {
518  // not parseable as a number, assume it's the device
519  nbddev = optarg;
520  nonspecial++;
521  } else {
522  port = optarg;
523  }
524  break;
525  case 2:
526  // device
527  nbddev = optarg;
528  break;
529  default:
530  usage("too many non-option arguments specified");
531  exit(EXIT_FAILURE);
532  }
533  break;
534  case 'b':
535  blocksize:
536  blocksize=(int)strtol(optarg, NULL, 0);
537  break;
538  case 'c':
539  return check_conn(optarg, 1);
540  case 'd':
541  disconnect(optarg);
542  exit(EXIT_SUCCESS);
543  case 'h':
544  usage(NULL);
545  exit(EXIT_SUCCESS);
546  case 'l':
547  needed_flags |= NBD_FLAG_FIXED_NEWSTYLE;
548  opts |= NBDC_DO_LIST;
549  name="";
550  nbddev="";
551  port = NBD_DEFAULT_PORT;
552  break;
553  case 'm':
554  argv[0][0] = '@';
555  break;
556  case 'n':
557  nofork=1;
558  break;
559  case 'N':
560  name=optarg;
561  if(!port) {
562  port = NBD_DEFAULT_PORT;
563  }
564  break;
565  case 'p':
566  cont=1;
567  break;
568  case 's':
569  swap=1;
570  break;
571  case 'S':
572  sdp=1;
573  break;
574  case 't':
575  timeout:
576  timeout=strtol(optarg, NULL, 0);
577  break;
578  case 'u':
579  b_unix = 1;
580  break;
581  default:
582  fprintf(stderr, "E: option eaten by 42 mice\n");
583  exit(EXIT_FAILURE);
584  }
585  }
586 
587 #ifdef __ANDROID__
588  if (swap)
589  err("swap option unsupported on Android because mlockall is unsupported.");
590 #endif
591 
592  if(!hostname || ((!name || !nbddev) && !(opts & NBDC_DO_LIST))) {
593  usage("not enough information specified");
594  exit(EXIT_FAILURE);
595  }
596 
597  if (b_unix)
598  sock = openunix(hostname);
599  else
600  sock = opennet(hostname, port, sdp);
601  if (sock < 0)
602  exit(EXIT_FAILURE);
603 
604  negotiate(sock, &size64, &flags, name, needed_flags, cflags, opts);
605 
606  nbd = open(nbddev, O_RDWR);
607  if (nbd < 0)
608  err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
609 
610  setsizes(nbd, size64, blocksize, flags);
611  set_timeout(nbd, timeout);
612  finish_sock(sock, nbd, swap);
613  if (swap) {
614  /* try linux >= 2.6.36 interface first */
615  if (oom_adjust("/proc/self/oom_score_adj", "-1000")) {
616  /* fall back to linux <= 2.6.35 interface */
617  oom_adjust("/proc/self/oom_adj", "-17");
618  }
619  }
620 
621  /* Go daemon */
622 
623 #ifndef NOFORK
624  if(!nofork) {
625  if (daemon(0,0) < 0)
626  err("Cannot detach from terminal");
627  }
628 
629  memset(&sa, 0, sizeof(sa));
630  sa.sa_handler = SIG_IGN;
631  sigaction(SIGCHLD, &sa, NULL);
632 #endif
633  do {
634 #ifndef NOFORK
635 
636  sigfillset(&block);
637  sigdelset(&block, SIGKILL);
638  sigdelset(&block, SIGTERM);
639  sigdelset(&block, SIGPIPE);
640  sigprocmask(SIG_SETMASK, &block, &old);
641 
642  if (!fork()) {
643  /* Due to a race, the kernel NBD driver cannot
644  * call for a reread of the partition table
645  * in the handling of the NBD_DO_IT ioctl().
646  * Therefore, this is done in the first open()
647  * of the device. We therefore make sure that
648  * the device is opened at least once after the
649  * connection was made. This has to be done in a
650  * separate process, since the NBD_DO_IT ioctl()
651  * does not return until the NBD device has
652  * disconnected.
653  */
654  while(check_conn(nbddev, 0)) {
655  sleep(1);
656  }
657  open(nbddev, O_RDONLY);
658  exit(0);
659  }
660 #endif
661 
662  if (ioctl(nbd, NBD_DO_IT) < 0) {
663  int error = errno;
664  fprintf(stderr, "nbd,%d: Kernel call returned: %d", getpid(), error);
665  if(error==EBADR) {
666  /* The user probably did 'nbd-client -d' on us.
667  * quit */
668  cont=0;
669  } else {
670  if(cont) {
671  u64 new_size;
672  uint16_t new_flags;
673 
674  close(sock); close(nbd);
675  for (;;) {
676  fprintf(stderr, " Reconnecting\n");
677  if (b_unix)
678  sock = openunix(hostname);
679  else
680  sock = opennet(hostname, port, sdp);
681  if (sock >= 0)
682  break;
683  sleep (1);
684  }
685  nbd = open(nbddev, O_RDWR);
686  if (nbd < 0)
687  err("Cannot open NBD: %m");
688  negotiate(sock, &new_size, &new_flags, name, needed_flags, cflags, opts);
689  if (size64 != new_size) {
690  err("Size of the device changed. Bye");
691  }
692  setsizes(nbd, size64, blocksize,
693  new_flags);
694 
695  set_timeout(nbd, timeout);
696  finish_sock(sock,nbd,swap);
697  }
698  }
699  } else {
700  /* We're on 2.4. It's not clearly defined what exactly
701  * happened at this point. Probably best to quit, now
702  */
703  fprintf(stderr, "Kernel call returned.");
704  cont=0;
705  }
706  } while(cont);
707  printf("sock, ");
708  ioctl(nbd, NBD_CLEAR_SOCK);
709  printf("done\n");
710  return 0;
711 }
void setsizes(int nbd, u64 size64, int blocksize, u32 flags)
Definition: nbd-client.c:354
void disconnect(char *device)
Definition: nbd-client.c:436
#define NBD_REP_FLAG_ERROR
Definition: cliserv.h:96
u64 ntohll(u64 a)
Definition: cliserv.c:74
#define NBD_OPT_LIST
Definition: cliserv.h:91
void negotiate(int sock, u64 *rsize64, uint16_t *flags, char *name, uint32_t needed_flags, uint32_t client_flags, uint32_t do_opts)
Definition: nbd-client.c:264
void err(const char *s)
Definition: cliserv.c:56
#define NBD_CLEAR_SOCK
Definition: nbd.h:24
#define NBD_FLAG_C_NO_ZEROES
Definition: cliserv.h:107
#define NBD_FLAG_NO_ZEROES
Definition: cliserv.h:104
#define MY_NAME
Definition: nbd-client.c:45
#define NBD_SET_SOCK
Definition: nbd.h:20
void finish_sock(int sock, int nbd, int swap)
Definition: nbd-client.c:388
#define NBD_REP_ERR_POLICY
Definition: cliserv.h:98
#define NBD_SET_SIZE_BLOCKS
Definition: nbd.h:27
void usage(char *errmsg,...)
Definition: nbd-client.c:413
#define NBD_OPT_EXPORT_NAME
Definition: cliserv.h:89
void setmysockopt(int sock)
Definition: cliserv.c:12
#define NBD_REP_ACK
Definition: cliserv.h:94
void err_nonfatal(const char *s)
Definition: cliserv.c:30
const u64 cliserv_magic
Definition: cliserv.c:8
#define NBD_OPT_ABORT
Definition: cliserv.h:90
#define NBDC_DO_LIST
Definition: nbd-client.c:52
#define AI_NUMERICSERV
Definition: netdb-compat.h:21
#define NBD_DISCONNECT
Definition: nbd.h:28
#define NBD_SET_TIMEOUT
Definition: nbd.h:29
const u64 rep_magic
Definition: cliserv.c:10
int check_conn(char *devname, int do_print)
Definition: nbd-client.c:54
#define NBD_FLAG_C_FIXED_NEWSTYLE
Definition: cliserv.h:106
const u64 opts_magic
Definition: cliserv.c:9
void ask_list(int sock)
Definition: nbd-client.c:168
#define NBD_FLAG_READ_ONLY
Definition: nbd.h:45
#define G_GNUC_UNUSED
Definition: cliserv.h:63
#define NBD_SET_FLAGS
Definition: nbd.h:30
void set_timeout(int nbd, int timeout)
Definition: nbd-client.c:380
#define NBD_SET_BLKSIZE
Definition: nbd.h:21
int openunix(const char *path)
Definition: nbd-client.c:142
int main(int argc, char *argv[])
Definition: nbd-client.c:450
#define htonll
Definition: cliserv.h:83
static int oom_adjust(const char *file, const char *value)
Definition: nbd-client.c:399
#define NBD_REP_SERVER
Definition: cliserv.h:95
#define NBD_DEFAULT_PORT
Definition: cliserv.h:85
#define PACKAGE_VERSION
Definition: config.h:175
int opennet(char *name, char *portstr, int sdp)
Definition: nbd-client.c:86
#define NBD_FLAG_FIXED_NEWSTYLE
Definition: cliserv.h:103
#define INIT_PASSWD
Definition: cliserv.h:71
__be32 magic
Definition: nbd.h:37
#define NBD_DO_IT
Definition: nbd.h:23
__be32 len
Definition: nbd.h:41
void logging(const char *name)
Definition: cliserv.c:61