xref: /third_party/toybox/toys/other/nbd_client.c (revision 0f66f451)
10f66f451Sopenharmony_ci/* nbd-client.c - network block device client
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2010 Rob Landley <rob@landley.net>
40f66f451Sopenharmony_ci *
50f66f451Sopenharmony_ci * Not in SUSv4.
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ci// This little dance is because a NEWTOY with - in the name tries to do
80f66f451Sopenharmony_ci// things like prototype "nbd-client_main" which isn't a valid symbol. So
90f66f451Sopenharmony_ci// we hide the underscore name and OLDTOY the name we want.
100f66f451Sopenharmony_ciUSE_NBD_CLIENT(NEWTOY(nbd_client, "<3>3ns", 0))
110f66f451Sopenharmony_ciUSE_NBD_CLIENT(OLDTOY(nbd-client, nbd_client, TOYFLAG_USR|TOYFLAG_BIN))
120f66f451Sopenharmony_ci
130f66f451Sopenharmony_ciconfig NBD_CLIENT
140f66f451Sopenharmony_ci  bool "nbd-client"
150f66f451Sopenharmony_ci  depends on TOYBOX_FORK
160f66f451Sopenharmony_ci  default y
170f66f451Sopenharmony_ci  help
180f66f451Sopenharmony_ci    usage: nbd-client [-ns] HOST PORT DEVICE
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci    -n	Do not fork into background
210f66f451Sopenharmony_ci    -s	nbd swap support (lock server into memory)
220f66f451Sopenharmony_ci*/
230f66f451Sopenharmony_ci
240f66f451Sopenharmony_ci/*  TODO:
250f66f451Sopenharmony_ci    usage: nbd-client [-sSpn] [-b BLKSZ] [-t SECS] [-N name] HOST PORT DEVICE
260f66f451Sopenharmony_ci
270f66f451Sopenharmony_ci    -b	block size
280f66f451Sopenharmony_ci    -t	timeout in seconds
290f66f451Sopenharmony_ci    -S	sdp
300f66f451Sopenharmony_ci    -p	persist
310f66f451Sopenharmony_ci    -n	nofork
320f66f451Sopenharmony_ci    -d	DEVICE
330f66f451Sopenharmony_ci    -c	DEVICE
340f66f451Sopenharmony_ci*/
350f66f451Sopenharmony_ci
360f66f451Sopenharmony_ci#define FOR_nbd_client
370f66f451Sopenharmony_ci#include "toys.h"
380f66f451Sopenharmony_ci#include <linux/nbd.h>
390f66f451Sopenharmony_ci
400f66f451Sopenharmony_civoid nbd_client_main(void)
410f66f451Sopenharmony_ci{
420f66f451Sopenharmony_ci  int sock = -1, nbd, flags;
430f66f451Sopenharmony_ci  unsigned long timeout = 0;
440f66f451Sopenharmony_ci  char *host=toys.optargs[0], *port=toys.optargs[1], *device=toys.optargs[2];
450f66f451Sopenharmony_ci  uint64_t devsize;
460f66f451Sopenharmony_ci
470f66f451Sopenharmony_ci  // Repeat until spanked
480f66f451Sopenharmony_ci
490f66f451Sopenharmony_ci  nbd = xopen(device, O_RDWR);
500f66f451Sopenharmony_ci  for (;;) {
510f66f451Sopenharmony_ci    int temp;
520f66f451Sopenharmony_ci
530f66f451Sopenharmony_ci    // Find and connect to server
540f66f451Sopenharmony_ci
550f66f451Sopenharmony_ci    sock = xconnectany(xgetaddrinfo(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0));
560f66f451Sopenharmony_ci    temp = 1;
570f66f451Sopenharmony_ci    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
580f66f451Sopenharmony_ci
590f66f451Sopenharmony_ci    // Read login data
600f66f451Sopenharmony_ci
610f66f451Sopenharmony_ci    xreadall(sock, toybuf, 152);
620f66f451Sopenharmony_ci    if (memcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16))
630f66f451Sopenharmony_ci      error_exit("bad login %s:%s", host, port);
640f66f451Sopenharmony_ci    devsize = SWAP_BE64(*(uint64_t *)(toybuf+16));
650f66f451Sopenharmony_ci    flags = SWAP_BE32(*(int *)(toybuf+24));
660f66f451Sopenharmony_ci
670f66f451Sopenharmony_ci    // Set 4k block size.  Everything uses that these days.
680f66f451Sopenharmony_ci    ioctl(nbd, NBD_SET_BLKSIZE, 4096);
690f66f451Sopenharmony_ci    ioctl(nbd, NBD_SET_SIZE_BLOCKS, devsize/4096);
700f66f451Sopenharmony_ci    ioctl(nbd, NBD_CLEAR_SOCK);
710f66f451Sopenharmony_ci
720f66f451Sopenharmony_ci    // If the sucker was exported read only, respect that locally.
730f66f451Sopenharmony_ci    temp = (flags & 2) ? 1 : 0;
740f66f451Sopenharmony_ci    xioctl(nbd, BLKROSET, &temp);
750f66f451Sopenharmony_ci
760f66f451Sopenharmony_ci    if (timeout && ioctl(nbd, NBD_SET_TIMEOUT, timeout)<0) break;
770f66f451Sopenharmony_ci    if (ioctl(nbd, NBD_SET_SOCK, sock) < 0) break;
780f66f451Sopenharmony_ci
790f66f451Sopenharmony_ci    if (toys.optflags & FLAG_s) mlockall(MCL_CURRENT|MCL_FUTURE);
800f66f451Sopenharmony_ci
810f66f451Sopenharmony_ci    // Open the device to force reread of the partition table.
820f66f451Sopenharmony_ci    if ((toys.optflags & FLAG_n) || !xfork()) {
830f66f451Sopenharmony_ci      char *s = strrchr(device, '/');
840f66f451Sopenharmony_ci      int i;
850f66f451Sopenharmony_ci
860f66f451Sopenharmony_ci      sprintf(toybuf, "/sys/block/%.32s/pid", s ? s+1 : device);
870f66f451Sopenharmony_ci      // Is it up yet? (Give it 10 seconds.)
880f66f451Sopenharmony_ci      for (i=0; i<100; i++) {
890f66f451Sopenharmony_ci        temp = open(toybuf, O_RDONLY);
900f66f451Sopenharmony_ci        if (temp == -1) msleep(100);
910f66f451Sopenharmony_ci        else {
920f66f451Sopenharmony_ci          close(temp);
930f66f451Sopenharmony_ci          break;
940f66f451Sopenharmony_ci        }
950f66f451Sopenharmony_ci      }
960f66f451Sopenharmony_ci      close(open(device, O_RDONLY));
970f66f451Sopenharmony_ci      if (!(toys.optflags & FLAG_n)) exit(0);
980f66f451Sopenharmony_ci    }
990f66f451Sopenharmony_ci
1000f66f451Sopenharmony_ci    // Daemonize here.
1010f66f451Sopenharmony_ci
1020f66f451Sopenharmony_ci    if (daemon(0,0)) perror_exit("daemonize");
1030f66f451Sopenharmony_ci
1040f66f451Sopenharmony_ci    // Process NBD requests until further notice.
1050f66f451Sopenharmony_ci
1060f66f451Sopenharmony_ci    if (ioctl(nbd, NBD_DO_IT)>=0 || errno==EBADR) break;
1070f66f451Sopenharmony_ci    close(sock);
1080f66f451Sopenharmony_ci  }
1090f66f451Sopenharmony_ci
1100f66f451Sopenharmony_ci  // Flush queue and exit.
1110f66f451Sopenharmony_ci  ioctl(nbd, NBD_CLEAR_QUE);
1120f66f451Sopenharmony_ci  ioctl(nbd, NBD_CLEAR_SOCK);
1130f66f451Sopenharmony_ci  if (CFG_TOYBOX_FREE) close(nbd);
1140f66f451Sopenharmony_ci}
115