OSDN Git Service

am f5cef31b: (-s ours) am c96e4249: Don\'t add a symlink for killall --- it\'s broken.
[android-x86/external-toybox.git] / toys / other / nbd_client.c
1 /* nbd-client.c - network block device client
2  *
3  * Copyright 2010 Rob Landley <rob@landley.net>
4  *
5  * Not in SUSv4.
6
7 // This little dance is because a NEWTOY with - in the name tries to do
8 // things like prototype "nbd-client_main" which isn't a valid symbol. So
9 // we hide the underscore name and OLDTOY the name we want.
10 USE_NBD_CLIENT(NEWTOY(nbd_client, "<3>3ns", 0))
11 USE_NBD_CLIENT(OLDTOY(nbd-client, nbd_client, TOYFLAG_USR|TOYFLAG_BIN))
12
13 config NBD_CLIENT
14   bool "nbd-client"
15   default y
16   help
17     usage: nbd-client [-ns] HOST PORT DEVICE
18
19     -n  Do not fork into background
20     -s  nbd swap support (lock server into memory)
21 */
22
23 /*  TODO:
24     usage: nbd-client [-sSpn] [-b BLKSZ] [-t SECS] [-N name] HOST PORT DEVICE
25
26     -b  block size
27     -t  timeout in seconds
28     -S  sdp
29     -p  persist
30     -n  nofork
31     -d  DEVICE
32     -c  DEVICE
33 */
34
35 #define FOR_nbd_client
36 #include "toys.h"
37 #include <linux/nbd.h>
38
39 void nbd_client_main(void)
40 {
41   int sock = -1, nbd, flags;
42   unsigned long timeout = 0;
43   char *host=toys.optargs[0], *port=toys.optargs[1], *device=toys.optargs[2];
44   uint64_t devsize;
45
46   // Repeat until spanked
47
48   nbd = xopen(device, O_RDWR);
49   for (;;) {
50     int temp;
51
52     // Find and connect to server
53
54     sock = xconnect(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0);
55     temp = 1;
56     setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
57
58     // Read login data
59
60     xreadall(sock, toybuf, 152);
61     if (memcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16))
62       error_exit("bad login %s:%s", host, port);
63     devsize = SWAP_BE64(*(uint64_t *)(toybuf+16));
64     flags = SWAP_BE32(*(int *)(toybuf+24));
65
66     // Set 4k block size.  Everything uses that these days.
67     ioctl(nbd, NBD_SET_BLKSIZE, 4096);
68     ioctl(nbd, NBD_SET_SIZE_BLOCKS, devsize/4096);
69     ioctl(nbd, NBD_CLEAR_SOCK);
70
71     // If the sucker was exported read only, respect that locally.
72     temp = (flags & 2) ? 1 : 0;
73     xioctl(nbd, BLKROSET, &temp);
74
75     if (timeout && ioctl(nbd, NBD_SET_TIMEOUT, timeout)<0) break;
76     if (ioctl(nbd, NBD_SET_SOCK, sock) < 0) break;
77
78     if (toys.optflags & FLAG_s) mlockall(MCL_CURRENT|MCL_FUTURE);
79
80     // Open the device to force reread of the partition table.
81     if ((toys.optflags & FLAG_n) || !xfork()) {
82       char *s = strrchr(device, '/');
83       int i;
84
85       sprintf(toybuf, "/sys/block/%.32s/pid", s ? s+1 : device);
86       // Is it up yet? (Give it 10 seconds.)
87       for (i=0; i<100; i++) {
88         temp = open(toybuf, O_RDONLY);
89         if (temp == -1) msleep(100);
90         else {
91           close(temp);
92           break;
93         }
94       }
95       close(open(device, O_RDONLY));
96       if (!(toys.optflags & FLAG_n)) exit(0);
97     }
98
99     // Daemonize here.
100
101     if (daemon(0,0)) perror_exit("daemonize");
102
103     // Process NBD requests until further notice.
104
105     if (ioctl(nbd, NBD_DO_IT)>=0 || errno==EBADR) break;
106     close(sock);
107   }
108
109   // Flush queue and exit.
110   ioctl(nbd, NBD_CLEAR_QUE);
111   ioctl(nbd, NBD_CLEAR_SOCK);
112   if (CFG_TOYBOX_FREE) close(nbd);
113 }