From 2780ccdd62f9986bf1aaf015e577e0b4168d436e Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Wed, 16 Jan 2013 15:28:12 +0100 Subject: [PATCH] Properly cleanup on interrupt in api-test. --- tests/api-test.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/api-test.c b/tests/api-test.c index 3ccfa73..15f95ed 100644 --- a/tests/api-test.c +++ b/tests/api-test.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -86,6 +87,8 @@ static int _debug = 0; static int _verbose = 1; static int _fips_mode = 0; +static int _quit = 0; + static char global_log[4096]; static int global_lines = 0; @@ -520,6 +523,12 @@ static void xlog(const char *msg, const char *tst, const char *func, int line, c else printf(" [%s,%s:%d] %s\n", msg, func, line, tst); } + if (_quit) { + if (_verbose) + printf("Interrupted by a signal.\n"); + _cleanup(); + exit(-1); + } } /* crypt_device context must be "cd" to parse error properly here */ @@ -1849,8 +1858,14 @@ static void NonFIPSAlg(void) crypt_free(cd); } +static void int_handler(int sig __attribute__((__unused__))) +{ + _quit++; +} + int main(int argc, char *argv[]) { + struct sigaction sa = { .sa_handler = int_handler }; int i; if (getuid() != 0) { @@ -1865,6 +1880,10 @@ int main(int argc, char *argv[]) _debug = _verbose = 1; } + /* Handle interrupt properly */ + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); + _cleanup(); if (_setup()) goto out; -- 2.7.4