Revision c1f8fdc3 qemu-nbd.c

b/qemu-nbd.c
226 226
    /* update partition table */
227 227
    pthread_create(&show_parts_thread, NULL, show_parts, NULL);
228 228

  
229
    fprintf(stderr, "NBD device %s is now connected to %s\n",
230
            device, srcpath);
229
    if (verbose) {
230
        fprintf(stderr, "NBD device %s is now connected to %s\n",
231
                device, srcpath);
232
    } else {
233
        /* Close stderr so that the qemu-nbd process exits.  */
234
        dup2(STDOUT_FILENO, STDERR_FILENO);
235
    }
231 236

  
232 237
    ret = nbd_client(fd);
233 238
    if (ret) {
......
406 411
	return 0;
407 412
    }
408 413

  
414
    if (device && !verbose) {
415
        int stderr_fd[2];
416
        pid_t pid;
417
        int ret;
418

  
419
        if (qemu_pipe(stderr_fd) == -1) {
420
            err(EXIT_FAILURE, "Error setting up communication pipe");
421
        }
422

  
423
        /* Now daemonize, but keep a communication channel open to
424
         * print errors and exit with the proper status code.
425
         */
426
        pid = fork();
427
        if (pid == 0) {
428
            close(stderr_fd[0]);
429
            ret = qemu_daemon(0, 0);
430

  
431
            /* Temporarily redirect stderr to the parent's pipe...  */
432
            dup2(stderr_fd[1], STDERR_FILENO);
433
            if (ret == -1) {
434
                err(EXIT_FAILURE, "Failed to daemonize");
435
            }
436

  
437
            /* ... close the descriptor we inherited and go on.  */
438
            close(stderr_fd[1]);
439
        } else {
440
            bool errors = false;
441
            char *buf;
442

  
443
            /* In the parent.  Print error messages from the child until
444
             * it closes the pipe.
445
             */
446
            close(stderr_fd[1]);
447
            buf = g_malloc(1024);
448
            while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
449
                errors = true;
450
                ret = qemu_write_full(STDERR_FILENO, buf, ret);
451
                if (ret == -1) {
452
                    exit(EXIT_FAILURE);
453
                }
454
            }
455
            if (ret == -1) {
456
                err(EXIT_FAILURE, "Cannot read from daemon");
457
            }
458

  
459
            /* Usually the daemon should not print any message.
460
             * Exit with zero status in that case.
461
             */
462
            exit(errors);
463
        }
464
    }
465

  
409 466
    bdrv_init();
410 467

  
411 468
    bs = bdrv_new("hda");
......
433 490
            err(EXIT_FAILURE, "Failed to open %s", device);
434 491
        }
435 492

  
436
        if (!verbose) {
437
            /* detach client and server */
438
            if (qemu_daemon(0, 0) == -1) {
439
                err(EXIT_FAILURE, "Failed to daemonize");
440
            }
441
        }
442

  
443 493
        if (sockpath == NULL) {
444 494
            sockpath = g_malloc(128);
445 495
            snprintf(sockpath, 128, SOCKET_PATH, basename(device));

Also available in: Unified diff