From f9f4bb026603738dda0367a3eb4cfb7289cf0211 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 20 Jul 2016 08:43:05 +1000 Subject: [PATCH] test: make sure we remove all udev rules when we SIGINT the test Signed-off-by: Peter Hutterer --- test/litest.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/test/litest.c b/test/litest.c index 7c185682..492aa3ac 100644 --- a/test/litest.c +++ b/test/litest.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -73,6 +74,9 @@ struct created_file { char *path; }; +struct list created_files_list; /* list of all files to remove at the end of + the test run */ + static void litest_init_udev_rules(struct list *created_files_list); static void litest_remove_udev_rules(struct list *created_files_list); @@ -837,13 +841,40 @@ struct libinput_interface interface = { .close_restricted = close_restricted, }; +static void +litest_signal(int sig) +{ + struct created_file *f, *tmp; + + list_for_each_safe(f, tmp, &created_files_list, link) { + list_remove(&f->link); + unlink(f->path); + /* in the sighandler, we can't free */ + } + + exit(1); +} + +static inline void +litest_setup_sighandler(int sig) +{ + struct sigaction act, oact; + int rc; + + sigemptyset(&act.sa_mask); + sigaddset(&act.sa_mask, sig); + act.sa_flags = 0; + act.sa_handler = litest_signal; + rc = sigaction(sig, &act, &oact); + litest_assert_int_ne(rc, -1); +} + static inline int litest_run(int argc, char **argv) { struct suite *s, *snext; int failed; SRunner *sr = NULL; - struct list created_files_list; list_init(&created_files_list); @@ -871,6 +902,8 @@ litest_run(int argc, char **argv) litest_init_udev_rules(&created_files_list); + litest_setup_sighandler(SIGINT); + srunner_run_all(sr, CK_ENV); failed = srunner_ntests_failed(sr); srunner_free(sr);