Revision eb505be1 vl.c

b/vl.c
216 216
int cursor_hide = 1;
217 217
int graphic_rotate = 0;
218 218
uint8_t irq0override = 1;
219
#ifndef _WIN32
220
int daemonize = 0;
221
#endif
222 219
const char *watchdog;
223 220
const char *option_rom[MAX_OPTION_ROMS];
224 221
int nb_option_roms;
......
2301 2298
    const char *loadvm = NULL;
2302 2299
    QEMUMachine *machine;
2303 2300
    const char *cpu_model;
2304
#ifndef _WIN32
2305
    int fds[2];
2306
#endif
2307 2301
    int tb_size;
2308 2302
    const char *pid_file = NULL;
2309 2303
    const char *incoming = NULL;
2310
#ifndef _WIN32
2311
    int fd = 0;
2312
#endif
2313 2304
    int show_vnc_port = 0;
2314 2305
    int defconfig = 1;
2315 2306

  
......
2975 2966
                    exit(1);
2976 2967
                }
2977 2968
                break;
2978
#ifndef _WIN32
2979
	    case QEMU_OPTION_daemonize:
2980
		daemonize = 1;
2981
		break;
2982
#endif
2983 2969
	    case QEMU_OPTION_option_rom:
2984 2970
		if (nb_option_roms >= MAX_OPTION_ROMS) {
2985 2971
		    fprintf(stderr, "Too many option ROMs\n");
......
3194 3180
    }
3195 3181
#endif
3196 3182

  
3197
#ifndef _WIN32
3198
    if (daemonize) {
3199
	pid_t pid;
3200

  
3201
	if (pipe(fds) == -1)
3202
	    exit(1);
3203

  
3204
	pid = fork();
3205
	if (pid > 0) {
3206
	    uint8_t status;
3207
	    ssize_t len;
3208

  
3209
	    close(fds[1]);
3210

  
3211
	again:
3212
            len = read(fds[0], &status, 1);
3213
            if (len == -1 && (errno == EINTR))
3214
                goto again;
3215

  
3216
            if (len != 1)
3217
                exit(1);
3218
            else if (status == 1) {
3219
                fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
3220
                exit(1);
3221
            } else
3222
                exit(0);
3223
	} else if (pid < 0)
3224
            exit(1);
3225

  
3226
	close(fds[0]);
3227
	qemu_set_cloexec(fds[1]);
3228

  
3229
	setsid();
3230

  
3231
	pid = fork();
3232
	if (pid > 0)
3233
	    exit(0);
3234
	else if (pid < 0)
3235
	    exit(1);
3236

  
3237
	umask(027);
3238

  
3239
        signal(SIGTSTP, SIG_IGN);
3240
        signal(SIGTTOU, SIG_IGN);
3241
        signal(SIGTTIN, SIG_IGN);
3242
    }
3243
#endif
3183
    os_daemonize();
3244 3184

  
3245 3185
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3246
#ifndef _WIN32
3247
        if (daemonize) {
3248
            uint8_t status = 1;
3249
            if (write(fds[1], &status, 1) != 1) {
3250
                perror("daemonize. Writing to pipe\n");
3251
            }
3252
        } else
3253
#endif
3254
            fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
3186
        os_pidfile_error();
3255 3187
        exit(1);
3256 3188
    }
3257 3189

  
......
3520 3452
        vm_start();
3521 3453
    }
3522 3454

  
3523
#ifndef _WIN32
3524
    if (daemonize) {
3525
	uint8_t status = 0;
3526
	ssize_t len;
3527

  
3528
    again1:
3529
	len = write(fds[1], &status, 1);
3530
	if (len == -1 && (errno == EINTR))
3531
	    goto again1;
3532

  
3533
	if (len != 1)
3534
	    exit(1);
3535

  
3536
        if (chdir("/")) {
3537
            perror("not able to chdir to /");
3538
            exit(1);
3539
        }
3540
	TFR(fd = qemu_open("/dev/null", O_RDWR));
3541
	if (fd == -1)
3542
	    exit(1);
3543
    }
3544

  
3545
    os_change_root();
3546
    os_change_process_uid();
3547

  
3548
    if (daemonize) {
3549
        dup2(fd, 0);
3550
        dup2(fd, 1);
3551
        dup2(fd, 2);
3552

  
3553
        close(fd);
3554
    }
3555
#endif
3455
    os_setup_post();
3556 3456

  
3557 3457
    main_loop();
3558 3458
    quit_timers();

Also available in: Unified diff