#!/bin/sh
#
# Check tracing of looping threads.
#
# Copyright (c) 2009-2024 The strace developers.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

. "${srcdir=.}/init.sh"
. "${srcdir=.}/PTRACE_SEIZE.sh"

run_prog ../orphaned_process_group > /dev/null

run_prog_skip_if_failed date +%s > /dev/null
s0="$(date +%s)"

check_prog nproc
inc="$(nproc)"
[ "$inc" -ge 1 ] || inc=1

[ 4 -le "$TIMEOUT_DURATION" ] || TIMEOUT_DURATION=4
timeout_2="$((TIMEOUT_DURATION/2))"
timeout_4_sqr=$(sq_root "$((TIMEOUT_DURATION/4))")
nproc=1

run_prog "../$NAME" "$timeout_4_sqr" "$nproc"

while :; do
	run_strace --follow-forks -qq -enone -esignal=none "../$NAME" "$timeout_2" "$nproc"

	s1="$(date +%s)"
	[ "$((s1-s0))" -lt "$timeout_4_sqr" ] ||
		break

	nproc="$((nproc+inc))"
done

warn_ "$ME_: nproc=$nproc elapsed=$((s1-s0))"
