123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #include "libbb.h"
- #include <netinet/tcp.h>
- #include <linux/fs.h>
- #define NBD_SET_SOCK _IO(0xab, 0)
- #define NBD_SET_BLKSIZE _IO(0xab, 1)
- #define NBD_SET_SIZE _IO(0xab, 2)
- #define NBD_DO_IT _IO(0xab, 3)
- #define NBD_CLEAR_SOCK _IO(0xab, 4)
- #define NBD_CLEAR_QUEUE _IO(0xab, 5)
- #define NBD_PRINT_DEBUG _IO(0xab, 6)
- #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
- #define NBD_DISCONNECT _IO(0xab, 8)
- #define NBD_SET_TIMEOUT _IO(0xab, 9)
- int nbdclient_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int nbdclient_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned long timeout = 0;
- #if BB_MMU
- int nofork = 0;
- #endif
- char *host, *port, *device;
- struct nbd_header_t {
- uint64_t magic1;
- uint64_t magic2;
- uint64_t devsize;
- uint32_t flags;
- char data[124];
- } nbd_header;
- BUILD_BUG_ON(offsetof(struct nbd_header_t, data) != 8+8+8+4);
-
- if (!argv[1] || !argv[2] || !argv[3] || argv[4])
- bb_show_usage();
- #if !BB_MMU
- bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
- #endif
- host = argv[1];
- port = argv[2];
- device = argv[3];
-
- for (;;) {
- int sock, nbd;
- int ro;
-
- nbd = xopen(device, O_RDWR);
-
- sock = create_and_connect_stream_or_die(host, xatou16(port));
- setsockopt_1(sock, IPPROTO_TCP, TCP_NODELAY);
-
- xread(sock, &nbd_header, 8+8+8+4 + 124);
- if (memcmp(&nbd_header.magic1, "NBDMAGIC""\x00\x00\x42\x02\x81\x86\x12\x53", 16) != 0)
- bb_error_msg_and_die("login failed");
-
- ioctl(nbd, NBD_SET_BLKSIZE, 4096);
- ioctl(nbd, NBD_SET_SIZE_BLOCKS, SWAP_BE64(nbd_header.devsize) / 4096);
- ioctl(nbd, NBD_CLEAR_SOCK);
-
- ro = (nbd_header.flags & SWAP_BE32(2)) / SWAP_BE32(2);
- if (ioctl(nbd, BLKROSET, &ro) < 0)
- bb_perror_msg_and_die("BLKROSET");
- if (timeout)
- if (ioctl(nbd, NBD_SET_TIMEOUT, timeout))
- bb_perror_msg_and_die("NBD_SET_TIMEOUT");
- if (ioctl(nbd, NBD_SET_SOCK, sock))
- bb_perror_msg_and_die("NBD_SET_SOCK");
-
- #if BB_MMU
-
-
-
- if (fork() == 0) {
- char *s = strrchr(device, '/');
- sprintf(nbd_header.data, "/sys/block/%.32s/pid", s ? s + 1 : device);
-
- for (;;) {
- int fd = open(nbd_header.data, O_RDONLY);
- if (fd >= 0) {
-
- break;
- }
- sleep(1);
- }
- open(device, O_RDONLY);
- return 0;
- }
-
- if (!nofork) {
- daemon(0, 0);
- nofork = 1;
- }
- #endif
-
-
-
-
-
- if (ioctl(nbd, NBD_DO_IT) >= 0 || errno == EBADR) {
-
- ioctl(nbd, NBD_CLEAR_QUEUE);
- ioctl(nbd, NBD_CLEAR_SOCK);
- break;
- }
- close(sock);
- close(nbd);
- }
- return 0;
- }
|