Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NsqgD-000655-U3 for qemu-devel@nongnu.org; Sat, 20 Mar 2010 00:52:58 -0400 Received: from [199.232.76.173] (port=54202 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NsqgD-00064F-B6 for qemu-devel@nongnu.org; Sat, 20 Mar 2010 00:52:57 -0400 Received: from Debian-exim by monty-python.gnu.org with spam-scanned (Exim 4.60) (envelope-from ) id 1NsqgC-0006wk-HH for qemu-devel@nongnu.org; Sat, 20 Mar 2010 00:52:57 -0400 Received: from mail-yw0-f198.google.com ([209.85.211.198]:65171) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NsqgC-0006wS-9e for qemu-devel@nongnu.org; Sat, 20 Mar 2010 00:52:56 -0400 Received: by mail-yw0-f198.google.com with SMTP id 36so915108ywh.4 for ; Fri, 19 Mar 2010 21:52:56 -0700 (PDT) From: Ryota Ozaki Date: Sat, 20 Mar 2010 13:50:43 +0900 Message-Id: <1269060643-3321-2-git-send-email-ozaki.ryota@gmail.com> In-Reply-To: <1269060643-3321-1-git-send-email-ozaki.ryota@gmail.com> References: <1269060643-3321-1-git-send-email-ozaki.ryota@gmail.com> Subject: [Qemu-devel] [PATCH 2/2] qemu-nbd: Improve error reporting List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org - 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 --- qemu-nbd.c | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-) diff --git a/qemu-nbd.c b/qemu-nbd.c index b89c361..5f10ff0 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(errno, "Cannot open %s", argv[optind]); + err(errno, "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(errno, "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(errno, "Could not find partition %d", partition); + err(errno, "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(errno, "Could not access '%s'", device); + if (!verbose) { /* detach client and server */ if (daemon(0, 0) == -1) { - errx(errno, "Failed to daemonize"); + err(errno, "Failed to daemonize"); } } -- 1.6.5.2