diff options
author | Daniel P. Berrange <berrange@redhat.com> | 2016-02-10 18:41:03 +0000 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2016-02-16 17:13:49 +0100 |
commit | ae39827802bc2aa781137d2f41bab0b60acd4e63 (patch) | |
tree | b1c586b75f749176595980240686b21c466cb5ed /blockdev-nbd.c | |
parent | d0d6ff584d0f715ae5c9b934b1846c8760b298f0 (diff) |
nbd: convert blockdev NBD server to use I/O channels for connection setup
This converts the blockdev NBD server to use the QIOChannelSocket
class for initial listener socket setup and accepting of client
connections. Actual I/O is still being performed against the
socket file descriptor using the POSIX socket APIs.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Message-Id: <1455129674-17255-6-git-send-email-berrange@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'blockdev-nbd.c')
-rw-r--r-- | blockdev-nbd.c | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/blockdev-nbd.c b/blockdev-nbd.c index efc31a462c..9baf88316c 100644 --- a/blockdev-nbd.c +++ b/blockdev-nbd.c @@ -18,32 +18,48 @@ #include "qmp-commands.h" #include "trace.h" #include "block/nbd.h" -#include "qemu/sockets.h" +#include "io/channel-socket.h" -static int server_fd = -1; +static QIOChannelSocket *server_ioc; +static int server_watch = -1; -static void nbd_accept(void *opaque) +static gboolean nbd_accept(QIOChannel *ioc, GIOCondition condition, + gpointer opaque) { - struct sockaddr_in addr; - socklen_t addr_len = sizeof(addr); + QIOChannelSocket *cioc; + int fd; - int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); + cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc), + NULL); + if (!cioc) { + return TRUE; + } + + fd = dup(cioc->fd); if (fd >= 0) { nbd_client_new(NULL, fd, nbd_client_put); } + object_unref(OBJECT(cioc)); + return TRUE; } void qmp_nbd_server_start(SocketAddress *addr, Error **errp) { - if (server_fd != -1) { + if (server_ioc) { error_setg(errp, "NBD server already running"); return; } - server_fd = socket_listen(addr, errp); - if (server_fd != -1) { - qemu_set_fd_handler(server_fd, nbd_accept, NULL, NULL); + server_ioc = qio_channel_socket_new(); + if (qio_channel_socket_listen_sync(server_ioc, addr, errp) < 0) { + return; } + + server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc), + G_IO_IN, + nbd_accept, + NULL, + NULL); } void qmp_nbd_server_add(const char *device, bool has_writable, bool writable, @@ -52,7 +68,7 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable, BlockBackend *blk; NBDExport *exp; - if (server_fd == -1) { + if (!server_ioc) { error_setg(errp, "NBD server not running"); return; } @@ -98,9 +114,12 @@ void qmp_nbd_server_stop(Error **errp) { nbd_export_close_all(); - if (server_fd != -1) { - qemu_set_fd_handler(server_fd, NULL, NULL, NULL); - close(server_fd); - server_fd = -1; + if (server_watch != -1) { + g_source_remove(server_watch); + server_watch = -1; + } + if (server_ioc) { + object_unref(OBJECT(server_ioc)); + server_ioc = NULL; } } |