Commit f7cce898821ff2a050e3de7317fe05b1a3e155fb

Authored by bellard
1 parent fe2cece6

-pidfile option


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1166 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 2 changed files with 51 additions and 1 deletions
qemu-doc.texi
@@ -202,6 +202,10 @@ Windows. @@ -202,6 +202,10 @@ Windows.
202 @item -full-screen 202 @item -full-screen
203 Start in full screen. 203 Start in full screen.
204 204
  205 +@item -pidfile file
  206 +Store the QEMU process PID in @var{file}. It is useful if you launch QEMU
  207 +from a script.
  208 +
205 @end table 209 @end table
206 210
207 Network options: 211 Network options:
@@ -1667,6 +1667,46 @@ static int net_fd_init(NetDriverState *nd, int fd) @@ -1667,6 +1667,46 @@ static int net_fd_init(NetDriverState *nd, int fd)
1667 #endif /* !_WIN32 */ 1667 #endif /* !_WIN32 */
1668 1668
1669 /***********************************************************/ 1669 /***********************************************************/
  1670 +/* pid file */
  1671 +
  1672 +static char *pid_filename;
  1673 +
  1674 +/* Remove PID file. Called on normal exit */
  1675 +
  1676 +static void remove_pidfile(void)
  1677 +{
  1678 + unlink (pid_filename);
  1679 +}
  1680 +
  1681 +static void create_pidfile(const char *filename)
  1682 +{
  1683 + struct stat pidstat;
  1684 + FILE *f;
  1685 +
  1686 + /* Try to write our PID to the named file */
  1687 + if (stat(filename, &pidstat) < 0) {
  1688 + if (errno == ENOENT) {
  1689 + if ((f = fopen (filename, "w")) == NULL) {
  1690 + perror("Opening pidfile");
  1691 + exit(1);
  1692 + }
  1693 + fprintf(f, "%d\n", getpid());
  1694 + fclose(f);
  1695 + pid_filename = qemu_strdup(filename);
  1696 + if (!pid_filename) {
  1697 + fprintf(stderr, "Could not save PID filename");
  1698 + exit(1);
  1699 + }
  1700 + atexit(remove_pidfile);
  1701 + }
  1702 + } else {
  1703 + fprintf(stderr, "%s already exists. Remove it and try again.\n",
  1704 + filename);
  1705 + exit(1);
  1706 + }
  1707 +}
  1708 +
  1709 +/***********************************************************/
1670 /* dumb display */ 1710 /* dumb display */
1671 1711
1672 static void dumb_update(DisplayState *ds, int x, int y, int w, int h) 1712 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
@@ -2533,6 +2573,7 @@ void help(void) @@ -2533,6 +2573,7 @@ void help(void)
2533 "Debug/Expert options:\n" 2573 "Debug/Expert options:\n"
2534 "-monitor dev redirect the monitor to char device 'dev'\n" 2574 "-monitor dev redirect the monitor to char device 'dev'\n"
2535 "-serial dev redirect the serial port to char device 'dev'\n" 2575 "-serial dev redirect the serial port to char device 'dev'\n"
  2576 + "-pidfile file Write PID to 'file'\n"
2536 "-S freeze CPU at startup (use 'c' to start execution)\n" 2577 "-S freeze CPU at startup (use 'c' to start execution)\n"
2537 "-s wait gdb connection to port %d\n" 2578 "-s wait gdb connection to port %d\n"
2538 "-p port change gdb connection port\n" 2579 "-p port change gdb connection port\n"
@@ -2625,6 +2666,7 @@ enum { @@ -2625,6 +2666,7 @@ enum {
2625 QEMU_OPTION_serial, 2666 QEMU_OPTION_serial,
2626 QEMU_OPTION_loadvm, 2667 QEMU_OPTION_loadvm,
2627 QEMU_OPTION_full_screen, 2668 QEMU_OPTION_full_screen,
  2669 + QEMU_OPTION_pidfile,
2628 }; 2670 };
2629 2671
2630 typedef struct QEMUOption { 2672 typedef struct QEMUOption {
@@ -2685,7 +2727,8 @@ const QEMUOption qemu_options[] = { @@ -2685,7 +2727,8 @@ const QEMUOption qemu_options[] = {
2685 { "serial", 1, QEMU_OPTION_serial }, 2727 { "serial", 1, QEMU_OPTION_serial },
2686 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm }, 2728 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
2687 { "full-screen", 0, QEMU_OPTION_full_screen }, 2729 { "full-screen", 0, QEMU_OPTION_full_screen },
2688 - 2730 + { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
  2731 +
2689 /* temporary options */ 2732 /* temporary options */
2690 { "pci", 0, QEMU_OPTION_pci }, 2733 { "pci", 0, QEMU_OPTION_pci },
2691 { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, 2734 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
@@ -3110,6 +3153,9 @@ int main(int argc, char **argv) @@ -3110,6 +3153,9 @@ int main(int argc, char **argv)
3110 case QEMU_OPTION_full_screen: 3153 case QEMU_OPTION_full_screen:
3111 full_screen = 1; 3154 full_screen = 1;
3112 break; 3155 break;
  3156 + case QEMU_OPTION_pidfile:
  3157 + create_pidfile(optarg);
  3158 + break;
3113 } 3159 }
3114 } 3160 }
3115 } 3161 }