Revision a6ac2313

b/qemu-nbd.c
37 37

  
38 38
static NBDExport *exp;
39 39
static int verbose;
40
static char *device;
41 40
static char *srcpath;
42 41
static char *sockpath;
43 42
static bool sigterm_reported;
......
178 177

  
179 178
static void *show_parts(void *arg)
180 179
{
180
    char *device = arg;
181 181
    int nbd;
182 182

  
183 183
    /* linux just needs an open() to trigger
......
194 194

  
195 195
static void *nbd_client_thread(void *arg)
196 196
{
197
    int fd = *(int *)arg;
197
    char *device = arg;
198 198
    off_t size;
199 199
    size_t blocksize;
200 200
    uint32_t nbdflags;
201
    int sock;
201
    int fd, sock;
202 202
    int ret;
203 203
    pthread_t show_parts_thread;
204 204

  
......
213 213
        goto out;
214 214
    }
215 215

  
216
    fd = open(device, O_RDWR);
217
    if (fd == -1) {
218
        /* Linux-only, we can use %m in printf.  */
219
        fprintf(stderr, "Failed to open %s: %m", device);
220
        goto out;
221
    }
222

  
216 223
    ret = nbd_init(fd, sock, nbdflags, size, blocksize);
217 224
    if (ret == -1) {
218 225
        goto out;
219 226
    }
220 227

  
221 228
    /* update partition table */
222
    pthread_create(&show_parts_thread, NULL, show_parts, NULL);
229
    pthread_create(&show_parts_thread, NULL, show_parts, device);
223 230

  
224 231
    if (verbose) {
225 232
        fprintf(stderr, "NBD device %s is now connected to %s\n",
......
273 280
    uint32_t nbdflags = 0;
274 281
    bool disconnect = false;
275 282
    const char *bindto = "0.0.0.0";
283
    char *device = NULL;
276 284
    int port = NBD_DEFAULT_PORT;
277 285
    off_t fd_size;
278 286
    const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
......
466 474
        }
467 475
    }
468 476

  
469
    if (device) {
470
        /* Open before spawning new threads.  In the future, we may
471
         * drop privileges after opening.
472
         */
473
        fd = open(device, O_RDWR);
474
        if (fd == -1) {
475
            err(EXIT_FAILURE, "Failed to open %s", device);
476
        }
477

  
478
        if (sockpath == NULL) {
479
            sockpath = g_malloc(128);
480
            snprintf(sockpath, 128, SOCKET_PATH, basename(device));
481
        }
477
    if (device != NULL && sockpath == NULL) {
478
        sockpath = g_malloc(128);
479
        snprintf(sockpath, 128, SOCKET_PATH, basename(device));
482 480
    }
483 481

  
484 482
    bdrv_init();
......
513 511
    if (device) {
514 512
        int ret;
515 513

  
516
        ret = pthread_create(&client_thread, NULL, nbd_client_thread, &fd);
514
        ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
517 515
        if (ret != 0) {
518 516
            errx(EXIT_FAILURE, "Failed to create client thread: %s",
519 517
                 strerror(ret));

Also available in: Unified diff