tests: fix subprocess cleanup when dying
authorBartosz Golaszewski <bartekgola@gmail.com>
Thu, 15 Jun 2017 06:31:49 +0000 (08:31 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Thu, 15 Jun 2017 06:31:49 +0000 (08:31 +0200)
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 <bartekgola@gmail.com>
tests/gpiod-test.c

index c697184494b874ce170166572dc3523b4b9e36c1..cd22765dc6d97509e7a545874eb5c9fb180dc26e 100644 (file)
@@ -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);