xref: /aosp_15_r20/external/ltp/lib/newlib_tests/test_children_cleanup.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2022 SUSE LLC <[email protected]>
4
5TMPFILE="/tmp/ltp_children_cleanup_$$.log"
6./test_children_cleanup 1>$TMPFILE 2>&1
7CHILD_PID=`sed -n 's/^.*Forked child \([0-9]*\)$/\1/p' "$TMPFILE"`
8rm "$TMPFILE"
9
10if [ "x$CHILD_PID" = "x" ]; then
11	echo "TFAIL: Child process was not created"
12	exit 1
13fi
14
15# The child process can stay alive for a short while even after receiving
16# SIGKILL, especially if the system is under heavy load. Wait up to 5 seconds
17# for it to fully exit.
18for i in `seq 6`; do
19	CHILD_STATE=`sed -ne 's/^State:\s*\([A-Z]\).*$/\1/p' "/proc/$CHILD_PID/status" 2>/dev/null`
20
21	if [ ! -e "/proc/$CHILD_PID" ] || [ "$CHILD_STATE" = "Z" ]; then
22		echo "TPASS: Child process was cleaned up"
23		exit 0
24	fi
25
26	sleep 1
27done
28
29echo "TFAIL: Child process was left behind"
30exit 1
31