From patchwork Fri Feb 17 11:43:47 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 141792 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4D633B6F9D for ; Fri, 17 Feb 2012 22:44:28 +1100 (EST) Received: from localhost ([::1]:50574 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RyMEj-0002hC-JH for incoming@patchwork.ozlabs.org; Fri, 17 Feb 2012 06:44:25 -0500 Received: from eggs.gnu.org ([140.186.70.92]:53682) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RyMEV-0002I4-0q for qemu-devel@nongnu.org; Fri, 17 Feb 2012 06:44:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RyMEO-0001EN-UA for qemu-devel@nongnu.org; Fri, 17 Feb 2012 06:44:10 -0500 Received: from mail-pw0-f45.google.com ([209.85.160.45]:52895) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RyMEO-0001CH-O7 for qemu-devel@nongnu.org; Fri, 17 Feb 2012 06:44:04 -0500 Received: by mail-pw0-f45.google.com with SMTP id ro12so4414135pbb.4 for ; Fri, 17 Feb 2012 03:44:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; bh=hk1PR7ZiSN3c4PQ0Qh7ndqp+vWoWCGaCCYgfgqqvupI=; b=VwR1CH0o8SGZRp6Vb0cnCk3Snr/r7DWXpQ9XZPF/BNyBREQrauLfVxB4QUhA53ckRB 9YhaHfPB3ciAnKmAF7F45o7VNCBlyY77Qw3J4Slw7aIONxPS6QnlNQuZxVF5wCFCcGwV kMAVSPA73g4aY+hn3QjQufRMNw9lTPlFoxlgQ= Received: by 10.68.138.167 with SMTP id qr7mr24089467pbb.0.1329479044380; Fri, 17 Feb 2012 03:44:04 -0800 (PST) Received: from yakj.usersys.redhat.com (93-34-182-16.ip50.fastwebnet.it. [93.34.182.16]) by mx.google.com with ESMTPS id a6sm4990788pbs.68.2012.02.17.03.44.01 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 17 Feb 2012 03:44:03 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 17 Feb 2012 12:43:47 +0100 Message-Id: <1329479028-27650-3-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.7.6 In-Reply-To: <1329479028-27650-1-git-send-email-pbonzini@redhat.com> References: <1329479028-27650-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.160.45 Subject: [Qemu-devel] [PATCH 2/3] open /dev/nbd in nbd_client_thread X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Paolo Bonzini --- qemu-nbd.c | 34 ++++++++++++++++------------------ 1 files changed, 16 insertions(+), 18 deletions(-) diff --git a/qemu-nbd.c b/qemu-nbd.c index e189cf8..d4e7041 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -37,7 +37,6 @@ static NBDExport *exp; static int verbose; -static char *device; static char *srcpath; static char *sockpath; static bool sigterm_reported; @@ -178,6 +177,7 @@ static void termsig_handler(int signum) static void *show_parts(void *arg) { + char *device = arg; int nbd; /* linux just needs an open() to trigger @@ -194,11 +194,11 @@ static void *show_parts(void *arg) static void *nbd_client_thread(void *arg) { - int fd = *(int *)arg; + char *device = arg; off_t size; size_t blocksize; uint32_t nbdflags; - int sock; + int fd, sock; int ret; pthread_t show_parts_thread; @@ -213,13 +213,20 @@ static void *nbd_client_thread(void *arg) goto out; } + fd = open(device, O_RDWR); + if (fd == -1) { + /* Linux-only, we can use %m in printf. */ + fprintf(stderr, "Failed to open %s: %m", device); + goto out; + } + ret = nbd_init(fd, sock, nbdflags, size, blocksize); if (ret == -1) { goto out; } /* update partition table */ - pthread_create(&show_parts_thread, NULL, show_parts, NULL); + pthread_create(&show_parts_thread, NULL, show_parts, device); if (verbose) { fprintf(stderr, "NBD device %s is now connected to %s\n", @@ -273,6 +280,7 @@ int main(int argc, char **argv) uint32_t nbdflags = 0; bool disconnect = false; const char *bindto = "0.0.0.0"; + char *device = NULL; int port = NBD_DEFAULT_PORT; off_t fd_size; const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; @@ -466,19 +474,9 @@ int main(int argc, char **argv) } } - if (device) { - /* Open before spawning new threads. In the future, we may - * drop privileges after opening. - */ - fd = open(device, O_RDWR); - if (fd == -1) { - err(EXIT_FAILURE, "Failed to open %s", device); - } - - if (sockpath == NULL) { - sockpath = g_malloc(128); - snprintf(sockpath, 128, SOCKET_PATH, basename(device)); - } + if (device != NULL && sockpath == NULL) { + sockpath = g_malloc(128); + snprintf(sockpath, 128, SOCKET_PATH, basename(device)); } bdrv_init(); @@ -513,7 +511,7 @@ int main(int argc, char **argv) if (device) { int ret; - ret = pthread_create(&client_thread, NULL, nbd_client_thread, &fd); + ret = pthread_create(&client_thread, NULL, nbd_client_thread, device); if (ret != 0) { errx(EXIT_FAILURE, "Failed to create client thread: %s", strerror(ret));