From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:42375) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ROv1s-0006KJ-D0 for qemu-devel@nongnu.org; Fri, 11 Nov 2011 12:36:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ROv1r-0000KF-Ce for qemu-devel@nongnu.org; Fri, 11 Nov 2011 12:36:40 -0500 Received: from mx1.redhat.com ([209.132.183.28]:31499) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ROv1r-0000K6-3U for qemu-devel@nongnu.org; Fri, 11 Nov 2011 12:36:39 -0500 From: Kevin Wolf Date: Fri, 11 Nov 2011 18:39:23 +0100 Message-Id: <1321033168-8739-12-git-send-email-kwolf@redhat.com> In-Reply-To: <1321033168-8739-1-git-send-email-kwolf@redhat.com> References: <1321033168-8739-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH 11/16] qemu-nbd: open the block device after starting the client thread List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: anthony@codemonkey.ws Cc: kwolf@redhat.com, qemu-devel@nongnu.org From: Paolo Bonzini This is cleaner, because we do not need to close the block device when there is an error opening /dev/nbdX. It was done this way only to print errors before daemonizing. At the same time, use atexit to ensure that the block device is closed whenever we exit. Signed-off-by: Paolo Bonzini Signed-off-by: Kevin Wolf --- qemu-nbd.c | 34 +++++++++++++++++----------------- 1 files changed, 17 insertions(+), 17 deletions(-) diff --git a/qemu-nbd.c b/qemu-nbd.c index 6f84ad0..291cba2 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -460,22 +460,6 @@ int main(int argc, char **argv) } } - bdrv_init(); - - bs = bdrv_new("hda"); - - srcpath = argv[optind]; - if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) { - errno = -ret; - err(EXIT_FAILURE, "Failed to bdrv_open '%s'", srcpath); - } - - fd_size = bs->total_sectors * 512; - - if (partition != -1 && - find_partition(bs, partition, &dev_offset, &fd_size)) - err(EXIT_FAILURE, "Could not find partition %d", partition); - if (device) { /* Open before spawning new threads. In the future, we may * drop privileges after opening. @@ -491,6 +475,23 @@ int main(int argc, char **argv) } } + bdrv_init(); + atexit(bdrv_close_all); + + bs = bdrv_new("hda"); + srcpath = argv[optind]; + if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) { + errno = -ret; + err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]); + } + + fd_size = bs->total_sectors * 512; + + if (partition != -1 && + find_partition(bs, partition, &dev_offset, &fd_size)) { + err(EXIT_FAILURE, "Could not find partition %d", partition); + } + sharing_fds = g_malloc((shared + 1) * sizeof(int)); if (sockpath) { @@ -568,7 +569,6 @@ int main(int argc, char **argv) qemu_vfree(data); close(sharing_fds[0]); - bdrv_close(bs); g_free(sharing_fds); if (sockpath) { unlink(sockpath); -- 1.7.6.4