Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NvVem-00080G-In for qemu-devel@nongnu.org; Sat, 27 Mar 2010 09:02:28 -0400 Received: from [140.186.70.92] (port=42227 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NvVek-0007ze-Qs for qemu-devel@nongnu.org; Sat, 27 Mar 2010 09:02:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NvVej-0004Qr-Fg for qemu-devel@nongnu.org; Sat, 27 Mar 2010 09:02:26 -0400 Received: from hall.aurel32.net ([88.191.82.174]:40453) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NvVej-0004Qk-9X for qemu-devel@nongnu.org; Sat, 27 Mar 2010 09:02:25 -0400 Date: Sat, 27 Mar 2010 14:02:23 +0100 From: Aurelien Jarno Subject: Re: [Qemu-devel] [PATCH 3/3] qemu-nbd: Improve error reporting Message-ID: <20100327130223.GK15194@volta.aurel32.net> References: <1269066204-4376-1-git-send-email-ozaki.ryota@gmail.com> <1269066204-4376-3-git-send-email-ozaki.ryota@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline In-Reply-To: <1269066204-4376-3-git-send-email-ozaki.ryota@gmail.com> List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Ryota Ozaki Cc: qemu-devel@nongnu.org On Sat, Mar 20, 2010 at 03:23:24PM +0900, Ryota Ozaki wrote: > - use err(3) instead of errx(3) if errno is available > to report why failed > - let fail prior to daemon(3) if opening a nbd file > is likely to fail after daemonizing to avoid silent > failure exit > > Signed-off-by: Ryota Ozaki > --- > qemu-nbd.c | 16 +++++++++++----- > 1 files changed, 11 insertions(+), 5 deletions(-) > > diff --git a/qemu-nbd.c b/qemu-nbd.c > index 6d854d3..8fb8cc3 100644 > --- a/qemu-nbd.c > +++ b/qemu-nbd.c > @@ -316,7 +316,7 @@ int main(int argc, char **argv) > if (disconnect) { > fd = open(argv[optind], O_RDWR); > if (fd == -1) > - errx(EXIT_FAILURE, "Cannot open %s", argv[optind]); > + err(EXIT_FAILURE, "Cannot open %s", argv[optind]); > > nbd_disconnect(fd); > > @@ -333,23 +333,29 @@ int main(int argc, char **argv) > if (bs == NULL) > return 1; > > - if (bdrv_open(bs, argv[optind], flags) < 0) > - return 1; > + if ((ret = bdrv_open(bs, argv[optind], flags)) < 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)) > - errx(EXIT_FAILURE, "Could not find partition %d", partition); > + err(EXIT_FAILURE, "Could not find partition %d", partition); > > if (device) { > pid_t pid; > int sock; > > + /* want to fail before daemonizing */ > + if (access(device, R_OK|W_OK) == -1) > + err(EXIT_FAILURE, "Could not access '%s'", device); > + First of all you need to put this line between curly braces. Secondly, qemu-nbd as a read-only option to export a block device as read-only. The test has to be improved to also take care of that. > if (!verbose) { > /* detach client and server */ > if (daemon(0, 0) == -1) { > - errx(EXIT_FAILURE, "Failed to daemonize"); > + err(EXIT_FAILURE, "Failed to daemonize"); > } > } > > -- > 1.6.5.2 > > > > -- Aurelien Jarno GPG: 1024D/F1BCDB73 aurelien@aurel32.net http://www.aurel32.net