Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/stefanha/tags/qtest-monitor-pro…
Browse files Browse the repository at this point in the history
…cess-pull-request' into staging

qtest resource cleanup pull request

# gpg: Signature made Wed 19 Feb 2014 14:46:34 GMT using RSA key ID 81AB73C8
# gpg: Good signature from "Stefan Hajnoczi <stefanha@redhat.com>"
# gpg:                 aka "Stefan Hajnoczi <stefanha@gmail.com>"
# gpg: WARNING: This key is not certified with a trusted signature!
# gpg:          There is no indication that the signature belongs to the owner.
# Primary key fingerprint: 8695 A8BF D3F9 7CDA AC35  775A 9CA4 ABB3 81AB 73C8

* remotes/stefanha/tags/qtest-monitor-process-pull-request:
  qtest: kill QEMU process on g_assert() failure
  qtest: make QEMU our direct child process
  qtest: drop unused child_pid field

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
  • Loading branch information
pm215 committed Feb 21, 2014
2 parents 7a87a7b + b15d422 commit 3e890c7
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions tests/libqtest.c
Expand Up @@ -43,8 +43,8 @@ struct QTestState
int qmp_fd;
bool irq_level[MAX_IRQ];
GString *rx;
int child_pid; /* Child process created to execute QEMU */
pid_t qemu_pid; /* QEMU process spawned by our child */
pid_t qemu_pid; /* our child QEMU process */
struct sigaction sigact_old; /* restored on exit */
};

#define g_assert_no_errno(ret) do { \
Expand Down Expand Up @@ -89,20 +89,17 @@ static int socket_accept(int sock)
return ret;
}

static pid_t read_pid_file(const char *pid_file)
static void kill_qemu(QTestState *s)
{
FILE *f;
char buffer[1024];
pid_t pid = -1;

f = fopen(pid_file, "r");
if (f) {
if (fgets(buffer, sizeof(buffer), f)) {
pid = atoi(buffer);
}
fclose(f);
if (s->qemu_pid != -1) {
kill(s->qemu_pid, SIGTERM);
waitpid(s->qemu_pid, NULL, 0);
}
return pid;
}

static void sigabrt_handler(int signo)
{
kill_qemu(global_qtest);
}

QTestState *qtest_init(const char *extra_args)
Expand All @@ -111,10 +108,9 @@ QTestState *qtest_init(const char *extra_args)
int sock, qmpsock, i;
gchar *socket_path;
gchar *qmp_socket_path;
gchar *pid_file;
gchar *command;
const char *qemu_binary;
pid_t pid;
struct sigaction sigact;

qemu_binary = getenv("QTEST_QEMU_BINARY");
g_assert(qemu_binary != NULL);
Expand All @@ -123,22 +119,28 @@ QTestState *qtest_init(const char *extra_args)

socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());

sock = init_socket(socket_path);
qmpsock = init_socket(qmp_socket_path);

pid = fork();
if (pid == 0) {
command = g_strdup_printf("%s "
/* Catch SIGABRT to clean up on g_assert() failure */
sigact = (struct sigaction){
.sa_handler = sigabrt_handler,
.sa_flags = SA_RESETHAND,
};
sigemptyset(&sigact.sa_mask);
sigaction(SIGABRT, &sigact, &s->sigact_old);

s->qemu_pid = fork();
if (s->qemu_pid == 0) {
command = g_strdup_printf("exec %s "
"-qtest unix:%s,nowait "
"-qtest-log /dev/null "
"-qmp unix:%s,nowait "
"-pidfile %s "
"-machine accel=qtest "
"-display none "
"%s", qemu_binary, socket_path,
qmp_socket_path, pid_file,
qmp_socket_path,
extra_args ?: "");
execlp("/bin/sh", "sh", "-c", command, NULL);
exit(1);
Expand All @@ -152,7 +154,6 @@ QTestState *qtest_init(const char *extra_args)
g_free(qmp_socket_path);

s->rx = g_string_new("");
s->child_pid = pid;
for (i = 0; i < MAX_IRQ; i++) {
s->irq_level[i] = false;
}
Expand All @@ -161,10 +162,6 @@ QTestState *qtest_init(const char *extra_args)
qtest_qmp_discard_response(s, "");
qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");

s->qemu_pid = read_pid_file(pid_file);
unlink(pid_file);
g_free(pid_file);

if (getenv("QTEST_STOP")) {
kill(s->qemu_pid, SIGSTOP);
}
Expand All @@ -174,13 +171,9 @@ QTestState *qtest_init(const char *extra_args)

void qtest_quit(QTestState *s)
{
int status;

if (s->qemu_pid != -1) {
kill(s->qemu_pid, SIGTERM);
waitpid(s->qemu_pid, &status, 0);
}
sigaction(SIGABRT, &s->sigact_old, NULL);

kill_qemu(s);
close(s->fd);
close(s->qmp_fd);
g_string_free(s->rx, true);
Expand Down

0 comments on commit 3e890c7

Please sign in to comment.