--- qemu-nbd.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) Index: qemu/qemu-nbd.c =================================================================== --- qemu.orig/qemu-nbd.c 2008-09-11 17:06:05.000000000 +0200 +++ qemu/qemu-nbd.c 2008-09-15 16:10:37.000000000 +0200 @@ -57,6 +57,7 @@ static void usage(const char *name) " -d, --disconnect disconnect the specified device\n" " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" " -t, --persistent don't exit on the last connection\n" +" -i, --inetd inetd interface: use stdin/stdout instead of a socke\n" " -v, --verbose display extra debugging information\n" " -h, --help display this help and exit\n" " -V, --version output version information and exit\n" @@ -183,14 +184,14 @@ int main(int argc, char **argv) bool readonly = false; bool disconnect = false; const char *bindto = "0.0.0.0"; - int port = 1024; + int port = 0; struct sockaddr_in addr; socklen_t addr_len = sizeof(addr); off_t fd_size; char *device = NULL; char *socket = NULL; char sockpath[128]; - const char *sopt = "hVbo:p:rsnP:c:dvk:e:t"; + const char *sopt = "hVbo:p:rsnP:c:dvk:e:ti"; struct option lopt[] = { { "help", 0, 0, 'h' }, { "version", 0, 0, 'V' }, @@ -207,6 +208,7 @@ int main(int argc, char **argv) { "shared", 1, 0, 'e' }, { "persistent", 0, 0, 't' }, { "verbose", 0, 0, 'v' }, + { "inetd", 0, 0, 'i' }, { NULL, 0, 0, 0 } }; int ch; @@ -225,6 +227,7 @@ int main(int argc, char **argv) int nb_fds = 0; int max_fd; int persistent = 0; + int inetd = 0; while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { switch (ch) { @@ -289,6 +292,9 @@ int main(int argc, char **argv) case 't': persistent = 1; break; + case 'i': + inetd = 1; + break; case 'v': verbose = 1; break; @@ -326,6 +332,18 @@ int main(int argc, char **argv) return 0; } + if (inetd) { + if (shared != 1) + errx(EINVAL, "You cannot use inetd and shared"); + if (socket) + errx(EINVAL, "You cannot use inetd and socket"); + if (port) + errx(EINVAL, "You cannot use inetd and port"); + } else { + if (!socket) + port = 1024; + } + bdrv_init(); bs = bdrv_new("hda"); @@ -412,9 +430,24 @@ int main(int argc, char **argv) if (sharing_fds == NULL) errx(ENOMEM, "Cannot allocate sharing fds"); + data = qemu_memalign(512, NBD_BUFFER_SIZE); + if (data == NULL) + errx(ENOMEM, "Cannot allocate data buffer"); + if (socket) { sharing_fds[0] = unix_socket_incoming(socket); } else { + if (inetd) { + /* read and write on stdin/stdout */ + ret = nbd_negotiate(STDIN_FILENO, fd_size); + while (ret != -1) { + ret = nbd_trip(bs, STDIN_FILENO, fd_size, dev_offset, + &offset, readonly, data, NBD_BUFFER_SIZE); + } + qemu_free(data); + bdrv_close(bs); + return 0; + } sharing_fds[0] = tcp_socket_incoming(bindto, port); } @@ -423,10 +456,6 @@ int main(int argc, char **argv) max_fd = sharing_fds[0]; nb_fds++; - data = qemu_memalign(512, NBD_BUFFER_SIZE); - if (data == NULL) - errx(ENOMEM, "Cannot allocate data buffer"); - do { FD_ZERO(&fds);