提交 93815bc2 编写于 作者: T ths

Improve -pidfile option, by Anthony Liguori.

git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2506 c046a42c-6fe2-441c-8c8c-71466251a162
上级 c35734b2
master openEuler-20.03-LTS openEuler-20.09 openEuler-RISCV stable-0.10 stable-0.11 stable-0.12 stable-0.13 stable-0.14 stable-0.15 stable-1.0 stable-1.1 stable-1.2 stable-1.3 stable-1.4 stable-1.5 stable-1.6 stable-1.7 stable-2.0 stable-2.1 stable-2.10 stable-2.11 stable-2.12 stable-2.2 stable-2.3 stable-2.4 stable-2.5 stable-2.6 stable-2.7 stable-2.8 stable-2.9 stable-3.0 stable-3.1 stable-4.0 stable-4.1 stable-4.2 staging v5.1.0-rc2 v5.1.0-rc1 v5.1.0-rc0 v5.0.0 v5.0.0-rc4 v5.0.0-rc3 v5.0.0-rc2 v5.0.0-rc1 v5.0.0-rc0 v4.2.1 v4.2.0 v4.2.0-rc5 v4.2.0-rc4 v4.2.0-rc3 v4.2.0-rc2 v4.2.0-rc1 v4.2.0-rc0 v4.1.1 v4.1.0 v4.1.0-rc5 v4.1.0-rc4 v4.1.0-rc3 v4.1.0-rc2 v4.1.0-rc1 v4.1.0-rc0 v4.0.1 v4.0.0 v4.0.0-rc4 v4.0.0-rc3 v4.0.0-rc2 v4.0.0-rc1 v4.0.0-rc0 v3.1.1.1 v3.1.1 v3.1.0 v3.1.0-rc5 v3.1.0-rc4 v3.1.0-rc3 v3.1.0-rc2 v3.1.0-rc1 v3.1.0-rc0 v3.0.1 v3.0.0 v3.0.0-rc4 v3.0.0-rc3 v3.0.0-rc2 v3.0.0-rc1 v3.0.0-rc0 v2.12.1 v2.12.0 v2.12.0-rc4 v2.12.0-rc3 v2.12.0-rc2 v2.12.0-rc1 v2.12.0-rc0 v2.11.2 v2.11.1 v2.11.0 v2.11.0-rc5 v2.11.0-rc4 v2.11.0-rc3 v2.11.0-rc2 v2.11.0-rc1 v2.11.0-rc0 v2.10.2 v2.10.1 v2.10.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1 v1.5.0-rc0 v1.4.2 v1.4.1 v1.4.0 v1.4.0-rc2 v1.4.0-rc1 v1.4.0-rc0 v1.3.1 v1.3.0 v1.3.0-rc2 v1.3.0-rc1 v1.3.0-rc0 v1.2.2 v1.2.1 v1.2.0 v1.2.0-rc3 v1.2.0-rc2 v1.2.0-rc1 v1.2.0-rc0 v1.1.2 v1.1.1 v1.1.0 v1.1.0-rc4 v1.1.0-rc3 v1.1.0-rc2 v1.1-rc2 v1.1-rc1 v1.1-rc0 v1.0.1 v1.0 v1.0-rc4 v1.0-rc3 v1.0-rc2 v1.0-rc1 v1.0-rc0 v0.15.1 v0.15.0 v0.15.0-rc2 v0.15.0-rc1 v0.15.0-rc0 v0.14.1 v0.14.0 v0.14.0-rc2 v0.14.0-rc1 v0.14.0-rc0 v0.13.0 v0.13.0-rc3 v0.13.0-rc2 v0.13.0-rc1 v0.13.0-rc0 v0.12.5 v0.12.4 v0.12.3 v0.12.2 v0.12.1 v0.12.0 v0.12.0-rc2 v0.12.0-rc1 v0.12.0-rc0 v0.11.1 v0.11.0 v0.11.0-rc2 v0.11.0-rc1 v0.11.0-rc0 v0.10.6 v0.10.5 v0.10.4 v0.10.3 v0.10.2 v0.10.1 v0.10.0 v0.9.1 release_0_10_2 release_0_10_1 release_0_10_0 release_0_9_1
无相关合并请求
......@@ -4387,44 +4387,24 @@ void usb_info(void)
}
}
/***********************************************************/
/* pid file */
static char *pid_filename;
static int create_pidfile(const char *filename)
{
int fd;
char buffer[128];
int len;
/* Remove PID file. Called on normal exit */
fd = open(filename, O_RDWR | O_CREAT, 0600);
if (fd == -1)
return -1;
static void remove_pidfile(void)
{
unlink (pid_filename);
}
if (lockf(fd, F_TLOCK, 0) == -1)
return -1;
static void create_pidfile(const char *filename)
{
struct stat pidstat;
FILE *f;
len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
if (write(fd, buffer, len) != len)
return -1;
/* Try to write our PID to the named file */
if (stat(filename, &pidstat) < 0) {
if (errno == ENOENT) {
if ((f = fopen (filename, "w")) == NULL) {
perror("Opening pidfile");
exit(1);
}
fprintf(f, "%d\n", getpid());
fclose(f);
pid_filename = qemu_strdup(filename);
if (!pid_filename) {
fprintf(stderr, "Could not save PID filename");
exit(1);
}
atexit(remove_pidfile);
}
} else {
fprintf(stderr, "%s already exists. Remove it and try again.\n",
filename);
exit(1);
}
return 0;
}
/***********************************************************/
......@@ -6880,6 +6860,7 @@ int main(int argc, char **argv)
char usb_devices[MAX_USB_CMDLINE][128];
int usb_devices_index;
int fds[2];
const char *pid_file = NULL;
LIST_INIT (&vm_change_state_head);
#ifndef _WIN32
......@@ -7285,7 +7266,7 @@ int main(int argc, char **argv)
break;
#endif
case QEMU_OPTION_pidfile:
create_pidfile(optarg);
pid_file = optarg;
break;
#ifdef TARGET_I386
case QEMU_OPTION_win2k_hack:
......@@ -7371,16 +7352,19 @@ int main(int argc, char **argv)
close(fds[1]);
again:
len = read(fds[0], &status, 1);
if (len == -1 && (errno == EINTR))
goto again;
if (len != 1 || status != 0)
exit(1);
else
exit(0);
len = read(fds[0], &status, 1);
if (len == -1 && (errno == EINTR))
goto again;
if (len != 1)
exit(1);
else if (status == 1) {
fprintf(stderr, "Could not acquire pidfile\n");
exit(1);
} else
exit(0);
} else if (pid < 0)
exit(1);
exit(1);
setsid();
......@@ -7399,6 +7383,15 @@ int main(int argc, char **argv)
}
#endif
if (pid_file && create_pidfile(pid_file) != 0) {
if (daemonize) {
uint8_t status = 1;
write(fds[1], &status, 1);
} else
fprintf(stderr, "Could not acquire pid file\n");
exit(1);
}
#ifdef USE_KQEMU
if (smp_cpus > 1)
kqemu_allowed = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册
反馈
建议
客服 返回
顶部