]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-imap-client: test-imapc-client - Make sure the child server process is killed.
authorStephan Bosch <stephan.bosch@open-xchange.com>
Sat, 11 Apr 2020 08:02:12 +0000 (10:02 +0200)
committerStephan Bosch <stephan.bosch@open-xchange.com>
Tue, 26 May 2020 09:30:03 +0000 (11:30 +0200)
src/lib-imap-client/test-imapc-client.c

index ff824047dcd2b2be33706c30a015f0d5d27ce8d9..5d31828e3dc3a61178396ebd1c19e69d979b981e 100644 (file)
@@ -1,6 +1,7 @@
 /* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "lib-signals.h"
 #include "array.h"
 #include "hostpid.h"
 #include "net.h"
@@ -200,6 +201,15 @@ static void test_server_kill(void)
        }
 }
 
+static void test_server_kill_forced(void)
+{
+       if (server.pid != (pid_t)-1) {
+               (void)kill(server.pid, SIGKILL);
+               (void)waitpid(server.pid, NULL, 0);
+       }
+       server.pid = (pid_t)-1;
+}
+
 static void
 test_run_client_server(const struct imapc_client_settings *client_set,
                       test_client_init_t *client_test,
@@ -808,9 +818,50 @@ static void test_imapc_client_get_capabilities_disconnected(void)
  * Main
  */
 
+volatile sig_atomic_t terminating = 0;
+
+static void test_signal_handler(const siginfo_t *si, void *context ATTR_UNUSED)
+{
+       int signo = si->si_signo;
+
+       if (terminating != 0)
+               raise(signo);
+       terminating = 1;
+
+       /* make sure we don't leave any pesky children alive */
+       test_server_kill_forced();
+
+       (void)signal(signo, SIG_DFL);
+       raise(signo);
+}
+
+static void test_atexit(void)
+{
+       /* NOTICE: This is also called by children, so be careful. */
+       test_server_kill_forced();
+}
+
+static void main_init(void)
+{
+       atexit(test_atexit);
+
+       lib_signals_ignore(SIGPIPE, TRUE);
+       lib_signals_set_handler(SIGTERM, 0, test_signal_handler, NULL);
+       lib_signals_set_handler(SIGQUIT, 0, test_signal_handler, NULL);
+       lib_signals_set_handler(SIGINT, 0, test_signal_handler, NULL);
+       lib_signals_set_handler(SIGSEGV, 0, test_signal_handler, NULL);
+       lib_signals_set_handler(SIGABRT, 0, test_signal_handler, NULL);
+}
+
+static void main_deinit(void)
+{
+       /* nothing yet */
+}
+
 int main(int argc ATTR_UNUSED, char *argv[])
 {
        int c;
+       int ret;
 
        static void (*const test_functions[])(void) = {
                test_imapc_connect_failed,
@@ -827,6 +878,10 @@ int main(int argc ATTR_UNUSED, char *argv[])
                NULL
        };
 
+       lib_init();
+       lib_signals_init();
+       main_init();
+
        while ((c = getopt(argc, argv, "D")) > 0) {
                switch (c) {
                case 'D':
@@ -844,5 +899,11 @@ int main(int argc ATTR_UNUSED, char *argv[])
        bind_ip.family = AF_INET;
        bind_ip.u.ip4.s_addr = htonl(INADDR_LOOPBACK);
 
-       return test_run(test_functions);
+       ret = test_run(test_functions);
+
+       main_deinit();
+       lib_signals_deinit();
+       lib_deinit();
+
+       return ret;
 }