#!/bin/sh
#
# Ensure that strace does not retain stdin and stdout descriptors.
#
# Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
# Copyright (c) 2016-2018 The strace developers.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

. "./init.sh"

run_prog_skip_if_failed \
	kill -0 $$

check_prog sleep
check_prog yes

> "$OUT"
> "$LOG"
(
	while [ -f "$LOG" ]; do
		:
	done
	$SLEEP_A_BIT &
	yes
	if kill -0 $! 2> /dev/null; then
		wait
	else
		echo TIMEOUT >> "$OUT"
	fi
) | $STRACE -qq -enone -esignal=none \
	sh -c "exec <&- >&-; rm -f -- $LOG; $SLEEP_A_BIT; $SLEEP_A_BIT"

if [ -s "$OUT" ]; then
	fail_ "$STRACE failed to redirect standard input"
fi

> "$LOG"
$STRACE -qq -enone -esignal=none \
	sh -c "exec <&- >&-; rm -f -- $LOG; $SLEEP_A_BIT; $SLEEP_A_BIT" |
(
	while [ -f "$LOG" ]; do
		:
	done
	$SLEEP_A_BIT &
	cat > /dev/null
	if kill -0 $! 2> /dev/null; then
		wait
	else
		echo TIMEOUT >> "$OUT"
	fi
)

if [ -s "$OUT" ]; then
	fail_ "$STRACE failed to redirect standard output"
fi
