--- qemu/vl.c 2004-11-26 13:09:07.000000000 -0600 +++ qemu-orig/vl.c 2004-11-26 13:47:37.000000000 -0600 @@ -112,6 +112,7 @@ int bios_size; static DisplayState display_state; int nographic; +char *pidfile; int64_t ticks_per_sec; int boot_device = 'c'; int ram_size; @@ -188,6 +189,12 @@ #endif } +/* Remove PID file. Called on normal exit */ + +static void remove_pidfile (void) { + unlink (pidfile); +} + void init_ioports(void) { int i; @@ -2533,6 +2540,7 @@ "Debug/Expert options:\n" "-monitor dev redirect the monitor to char device 'dev'\n" "-serial dev redirect the serial port to char device 'dev'\n" + "-pidfile file Write PID to 'file'\n" "-S freeze CPU at startup (use 'c' to start execution)\n" "-s wait gdb connection to port %d\n" "-p port change gdb connection port\n" @@ -2590,6 +2598,7 @@ QEMU_OPTION_boot, QEMU_OPTION_snapshot, QEMU_OPTION_m, + QEMU_OPTION_pidfile, QEMU_OPTION_nographic, QEMU_OPTION_enable_audio, @@ -2646,6 +2655,7 @@ { "boot", HAS_ARG, QEMU_OPTION_boot }, { "snapshot", 0, QEMU_OPTION_snapshot }, { "m", HAS_ARG, QEMU_OPTION_m }, + { "pidfile", HAS_ARG, QEMU_OPTION_pidfile }, { "nographic", 0, QEMU_OPTION_nographic }, { "enable-audio", 0, QEMU_OPTION_enable_audio }, @@ -2760,6 +2770,8 @@ int net_if_type, nb_tun_fds, tun_fds[MAX_NICS]; int optind; const char *r, *optarg; + FILE *pidfile_stream; + struct stat pidstat; CharDriverState *monitor_hd; char monitor_device[128]; char serial_devices[MAX_SERIAL_PORTS][128]; @@ -3007,6 +3019,30 @@ exit(1); } break; + case QEMU_OPTION_pidfile: + /* Try to write our PID to the named file */ + if ( stat(optarg, &pidstat) < 0 ) { + if ( errno == ENOENT ) { + if ( (pidfile_stream = fopen (optarg, "w+")) == NULL ) { + perror ("Opening pidfile"); + exit(1); + } + fprintf (pidfile_stream, "%d\n", getpid()); + fclose (pidfile_stream); + pidfile = malloc (strlen(optarg)); + if (!pidfile) { + perror("Saving PID filename"); + exit(1); + } + strncpy (pidfile, optarg, strlen(optarg)); + atexit(remove_pidfile); + } + } + else { + fprintf (stderr, "%s already exists. Remove it and try again.\n", optarg); + exit(1); + } + break; case QEMU_OPTION_d: { int mask;