From 2a983631b73cd6bc599ec4bc7bfeb672d6126ac8 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Thu, 15 Jun 2017 08:31:49 +0200 Subject: [PATCH] tests: fix subprocess cleanup when dying We need to kill the tool subprocess when bailing out during tests everytime, not only when an error during test_tool_wait() occurs. Also: we need to wait() for killed processes too as otherwise we may end up calling delete_module() before the process actually stops using gpio-mockup giving us a nasty error message from libkmod and leaving the module in memory. Signed-off-by: Bartosz Golaszewski --- tests/gpiod-test.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/gpiod-test.c b/tests/gpiod-test.c index c697184..cd22765 100644 --- a/tests/gpiod-test.c +++ b/tests/gpiod-test.c @@ -166,12 +166,25 @@ static TEST_PRINTF(1, 2) void err(const char *fmt, ...) va_end(va); } -static TEST_PRINTF(1, 2) NORETURN void die(const char *fmt, ...) +static void die_test_cleanup(void) { - va_list va; + struct gpiotool_proc *proc = &globals.test_ctx.tool_proc; + int status; + + if (proc->running) { + kill(proc->pid, SIGKILL); + waitpid(proc->pid, &status, 0); + } if (globals.test_ctx.running) pr_raw("\n"); +} + +static TEST_PRINTF(1, 2) NORETURN void die(const char *fmt, ...) +{ + va_list va; + + die_test_cleanup(); va_start(va, fmt); vmsg("FATAL", CRED, fmt, va); @@ -184,8 +197,7 @@ static TEST_PRINTF(1, 2) NORETURN void die_perr(const char *fmt, ...) { va_list va; - if (globals.test_ctx.running) - pr_raw("\n"); + die_test_cleanup(); va_start(va, fmt); vmsgn("FATAL", CRED, fmt, va); @@ -574,18 +586,10 @@ void test_tool_wait(void) pfd.events = POLLIN | POLLPRI; status = poll(&pfd, 1, 5000); - if (status == 0) { - /* - * If a tool program is taking longer than 5 seconds to - * terminate, then something's wrong. Kill it before dying. - */ - test_tool_signal(SIGKILL); + if (status == 0) die("tool program is taking too long to terminate"); - } else if (status < 0) { - /* Error in poll() - we still need to kill the process. */ - test_tool_signal(SIGKILL); + else if (status < 0) die_perr("error when polling the signalfd"); - } rd = read(proc->sig_fd, &sinfo, sizeof(sinfo)); close(proc->sig_fd); -- 2.30.2