From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: mjt@tls.msk.ru, Chunyan Liu <cyliu@suse.com>
Subject: [Qemu-devel] [PATCH] open /dev/nbd in nbd_client_thread
Date: Wed, 18 Jan 2012 12:47:00 +0100 [thread overview]
Message-ID: <1326887220-1439-1-git-send-email-pbonzini@redhat.com> (raw)
Commit a61c678 (qemu-nbd: use common main loop, 2011-09-12)
changed code to use local variable "fd" in qemu-nbd.c:main()
in two places: for /dev/nbd device and for control socket.
The result is that qemu-nbd -c $device does not work anymore.
Opening the device in the client thread fixes the bug.
Reported-by: Michael Tokarev <mjt@tls.msk.ru>
Cc: Chunyan Liu <cyliu@suse.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
Michael, can you check that this fixes the bug too?
It also modifies the code in a way that is similar to
what Chunyan is doing.
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));
--
1.7.7.1
reply other threads:[~2012-01-18 11:47 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1326887220-1439-1-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=cyliu@suse.com \
--cc=mjt@tls.msk.ru \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).