[LTP] [PATCH 1/1] netstress: Workaround race between SETSID() and exit(0)

Petr Vorel pvorel@suse.cz
Fri Feb 18 17:00:35 CET 2022


There is a race between the SETSID() and exit(0) in move_to_background()
caused by "Killed the leftover descendant processes" introduced in
72b172867 ("Terminate leftover subprocesses when main test process
crashes").

If the main test process calls exit(0) before the newly forked child
managed to do SETSID() it's killed by the test library because it's
still in the old process group. Therefore kill mask SIGUSR1 with dummy
handler to avoid heartbeat_handler() doing the cleanup.

Link: https://lore.kernel.org/ltp/Yg+RXbUTOxK56iZa@pevik/

Suggested-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 testcases/network/netstress/netstress.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/testcases/network/netstress/netstress.c b/testcases/network/netstress/netstress.c
index 0914c65bd4..51daa72c6d 100644
--- a/testcases/network/netstress/netstress.c
+++ b/testcases/network/netstress/netstress.c
@@ -38,6 +38,10 @@ static int rand_r(LTP_ATTRIBUTE_UNUSED unsigned int *seed)
 }
 #endif
 
+static void sig_handler(int sig LTP_ATTRIBUTE_UNUSED)
+{
+}
+
 static const int max_msg_len = (1 << 16) - 1;
 static const int min_msg_len = 5;
 
@@ -713,11 +717,15 @@ static void server_cleanup(void)
 
 static void move_to_background(void)
 {
-	if (SAFE_FORK())
+	if (SAFE_FORK()) {
+		pause();
 		exit(0);
+	}
 
 	SAFE_SETSID();
 
+	SAFE_KILL(getppid(), SIGUSR1);
+
 	close(STDIN_FILENO);
 	SAFE_OPEN("/dev/null", O_RDONLY);
 	close(STDOUT_FILENO);
@@ -843,6 +851,8 @@ static void set_protocol_type(void)
 
 static void setup(void)
 {
+	SAFE_SIGNAL(SIGUSR1, sig_handler);
+
 	if (tst_parse_int(aarg, &clients_num, 1, INT_MAX))
 		tst_brk(TBROK, "Invalid client number '%s'", aarg);
 	if (tst_parse_int(rarg, &client_max_requests, 1, INT_MAX))
-- 
2.35.1



More information about the ltp mailing list