[LTP] [PATCH v2] Terminate leftover subprocesses when main test process crashes
Martin Doucha
mdoucha@suse.cz
Fri Feb 11 12:44:01 CET 2022
When the main test process crashes or gets killed e.g. by OOM killer,
the watchdog process currently does not clean up any remaining child
processes. Fix this by sending SIGKILL to the test process group when
the watchdog process gets notified that the main test process has exited
for any reason.
Fixes #892
Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
Changes since v1:
- The SIGKILL can be sent after SAFE_WAITPID() returns, removed
the unnecessary SIGCHLD handler
- Fixed libtest shell script, calling test_children_cleanup inside backticks
would block the shell script until any leftover child exits, which renders
the test useless. Temp file must be used for passing child PID.
lib/newlib_tests/runtest.sh | 3 +-
lib/newlib_tests/test_children_cleanup.c | 43 +++++++++++++++++++++++
lib/newlib_tests/test_children_cleanup.sh | 19 ++++++++++
lib/tst_test.c | 3 ++
4 files changed, 67 insertions(+), 1 deletion(-)
create mode 100644 lib/newlib_tests/test_children_cleanup.c
create mode 100755 lib/newlib_tests/test_children_cleanup.sh
diff --git a/lib/newlib_tests/runtest.sh b/lib/newlib_tests/runtest.sh
index 92fd3860e..327460e7b 100755
--- a/lib/newlib_tests/runtest.sh
+++ b/lib/newlib_tests/runtest.sh
@@ -4,7 +4,8 @@
LTP_C_API_TESTS="${LTP_C_API_TESTS:-test05 test07 test09 test12 test15 test18
tst_needs_cmds01 tst_needs_cmds02 tst_needs_cmds03 tst_needs_cmds06
tst_needs_cmds07 tst_bool_expr test_exec test_timer tst_res_hexd tst_strstatus
-tst_fuzzy_sync03 test_zero_hugepage.sh test_kconfig.sh}"
+tst_fuzzy_sync03 test_zero_hugepage.sh test_kconfig.sh
+test_children_cleanup.sh}"
LTP_SHELL_API_TESTS="${LTP_SHELL_API_TESTS:-shell/tst_check_driver.sh
shell/tst_check_kconfig0[1-5].sh shell/net/*.sh}"
diff --git a/lib/newlib_tests/test_children_cleanup.c b/lib/newlib_tests/test_children_cleanup.c
new file mode 100644
index 000000000..2b1ca5f9c
--- /dev/null
+++ b/lib/newlib_tests/test_children_cleanup.c
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2022 SUSE LLC <mdoucha@suse.cz>
+ */
+
+/*\
+ * Test whether the LTP library properly reaps any children left over when
+ * the main test process dies. Run using test_children_cleanup.sh.
+ */
+
+#include <unistd.h>
+#include <signal.h>
+
+#include "tst_test.h"
+
+static void run(void)
+{
+ pid_t child_pid, main_pid = getpid();
+
+ tst_res(TINFO, "Main process %d starting", main_pid);
+
+ /* Check that normal child reaping does not disrupt the test */
+ if (!SAFE_FORK())
+ return;
+
+ SAFE_WAIT(NULL);
+ child_pid = SAFE_FORK();
+
+ /* Start child that will outlive the main test process */
+ if (!child_pid) {
+ sleep(30);
+ return;
+ }
+
+ tst_res(TINFO, "Forked child %d", child_pid);
+ kill(main_pid, SIGKILL);
+}
+
+static struct tst_test test = {
+ .test_all = run,
+ .forks_child = 1,
+ .timeout = 10,
+};
diff --git a/lib/newlib_tests/test_children_cleanup.sh b/lib/newlib_tests/test_children_cleanup.sh
new file mode 100755
index 000000000..2a84c3d85
--- /dev/null
+++ b/lib/newlib_tests/test_children_cleanup.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2022 SUSE LLC <mdoucha@suse.cz>
+
+TMPFILE="/tmp/ltp_children_cleanup_$$.log"
+./test_children_cleanup &>"$TMPFILE"
+CHILD_PID=`sed -n 's/^.*Forked child \([0-9]*\)$/\1/p' "$TMPFILE"`
+rm "$TMPFILE"
+
+if [ "x$CHILD_PID" = "x" ]; then
+ echo "TFAIL: Child process was not created"
+ exit 1
+elif ! kill -s 0 $CHILD_PID &>/dev/null; then
+ echo "TPASS: Child process was cleaned up"
+ exit 0
+else
+ echo "TFAIL: Child process was left behind"
+ exit 1
+fi
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 191a9deab..a84d72d53 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -1495,6 +1495,9 @@ static int fork_testrun(void)
return TFAIL;
}
+ if (tst_test->forks_child)
+ kill(-test_pid, SIGKILL);
+
if (WIFEXITED(status) && WEXITSTATUS(status))
return WEXITSTATUS(status);
--
2.34.1
More information about the ltp
mailing list