Commit 93815bc280744c05abab9501eeb53edd01190e1b

Authored by ths
1 parent c35734b2

Improve -pidfile option, by Anthony Liguori.


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2506 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 1 changed file with 37 additions and 44 deletions
... ... @@ -4387,44 +4387,24 @@ void usb_info(void)
4387 4387 }
4388 4388 }
4389 4389  
4390   -/***********************************************************/
4391   -/* pid file */
4392   -
4393   -static char *pid_filename;
  4390 +static int create_pidfile(const char *filename)
  4391 +{
  4392 + int fd;
  4393 + char buffer[128];
  4394 + int len;
4394 4395  
4395   -/* Remove PID file. Called on normal exit */
  4396 + fd = open(filename, O_RDWR | O_CREAT, 0600);
  4397 + if (fd == -1)
  4398 + return -1;
4396 4399  
4397   -static void remove_pidfile(void)
4398   -{
4399   - unlink (pid_filename);
4400   -}
  4400 + if (lockf(fd, F_TLOCK, 0) == -1)
  4401 + return -1;
4401 4402  
4402   -static void create_pidfile(const char *filename)
4403   -{
4404   - struct stat pidstat;
4405   - FILE *f;
  4403 + len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
  4404 + if (write(fd, buffer, len) != len)
  4405 + return -1;
4406 4406  
4407   - /* Try to write our PID to the named file */
4408   - if (stat(filename, &pidstat) < 0) {
4409   - if (errno == ENOENT) {
4410   - if ((f = fopen (filename, "w")) == NULL) {
4411   - perror("Opening pidfile");
4412   - exit(1);
4413   - }
4414   - fprintf(f, "%d\n", getpid());
4415   - fclose(f);
4416   - pid_filename = qemu_strdup(filename);
4417   - if (!pid_filename) {
4418   - fprintf(stderr, "Could not save PID filename");
4419   - exit(1);
4420   - }
4421   - atexit(remove_pidfile);
4422   - }
4423   - } else {
4424   - fprintf(stderr, "%s already exists. Remove it and try again.\n",
4425   - filename);
4426   - exit(1);
4427   - }
  4407 + return 0;
4428 4408 }
4429 4409  
4430 4410 /***********************************************************/
... ... @@ -6880,6 +6860,7 @@ int main(int argc, char **argv)
6880 6860 char usb_devices[MAX_USB_CMDLINE][128];
6881 6861 int usb_devices_index;
6882 6862 int fds[2];
  6863 + const char *pid_file = NULL;
6883 6864  
6884 6865 LIST_INIT (&vm_change_state_head);
6885 6866 #ifndef _WIN32
... ... @@ -7285,7 +7266,7 @@ int main(int argc, char **argv)
7285 7266 break;
7286 7267 #endif
7287 7268 case QEMU_OPTION_pidfile:
7288   - create_pidfile(optarg);
  7269 + pid_file = optarg;
7289 7270 break;
7290 7271 #ifdef TARGET_I386
7291 7272 case QEMU_OPTION_win2k_hack:
... ... @@ -7371,16 +7352,19 @@ int main(int argc, char **argv)
7371 7352 close(fds[1]);
7372 7353  
7373 7354 again:
7374   - len = read(fds[0], &status, 1);
7375   - if (len == -1 && (errno == EINTR))
7376   - goto again;
7377   -
7378   - if (len != 1 || status != 0)
7379   - exit(1);
7380   - else
7381   - exit(0);
  7355 + len = read(fds[0], &status, 1);
  7356 + if (len == -1 && (errno == EINTR))
  7357 + goto again;
  7358 +
  7359 + if (len != 1)
  7360 + exit(1);
  7361 + else if (status == 1) {
  7362 + fprintf(stderr, "Could not acquire pidfile\n");
  7363 + exit(1);
  7364 + } else
  7365 + exit(0);
7382 7366 } else if (pid < 0)
7383   - exit(1);
  7367 + exit(1);
7384 7368  
7385 7369 setsid();
7386 7370  
... ... @@ -7399,6 +7383,15 @@ int main(int argc, char **argv)
7399 7383 }
7400 7384 #endif
7401 7385  
  7386 + if (pid_file && create_pidfile(pid_file) != 0) {
  7387 + if (daemonize) {
  7388 + uint8_t status = 1;
  7389 + write(fds[1], &status, 1);
  7390 + } else
  7391 + fprintf(stderr, "Could not acquire pid file\n");
  7392 + exit(1);
  7393 + }
  7394 +
7402 7395 #ifdef USE_KQEMU
7403 7396 if (smp_cpus > 1)
7404 7397 kqemu_allowed = 0;
... ...