diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 16:58:41 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 16:58:41 +0000 |
commit | e1908ae95dd4c9d19ee4dfabfc8bf8a7f85943fe (patch) | |
tree | f5cc731bedcac0fb7fe14d952e4581e749f8bb87 /tests/timeout | |
parent | Initial commit. (diff) | |
download | coreutils-e1908ae95dd4c9d19ee4dfabfc8bf8a7f85943fe.tar.xz coreutils-e1908ae95dd4c9d19ee4dfabfc8bf8a7f85943fe.zip |
Adding upstream version 9.4.upstream/9.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/timeout')
-rwxr-xr-x | tests/timeout/timeout-blocked.pl | 48 | ||||
-rwxr-xr-x | tests/timeout/timeout-group.sh | 103 | ||||
-rwxr-xr-x | tests/timeout/timeout-large-parameters.sh | 46 | ||||
-rwxr-xr-x | tests/timeout/timeout-parameters.sh | 50 | ||||
-rwxr-xr-x | tests/timeout/timeout.sh | 74 |
5 files changed, 321 insertions, 0 deletions
diff --git a/tests/timeout/timeout-blocked.pl b/tests/timeout/timeout-blocked.pl new file mode 100755 index 0000000..2ec865f --- /dev/null +++ b/tests/timeout/timeout-blocked.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +# Test that timeout handles blocked SIGALRM from its parent. + +# Copyright (C) 2013-2023 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +use strict; + +(my $ME = $0) =~ s|.*/||; + +eval { require POSIX; }; +$@ + and CuSkip::skip "$ME: this script requires Perl's POSIX module\n"; + +use POSIX qw(:signal_h); +my $sigset = POSIX::SigSet->new(SIGALRM); # define the signals to block +my $old_sigset = POSIX::SigSet->new; # where the old sigmask will be kept +unless (defined sigprocmask(SIG_BLOCK, $sigset, $old_sigset)) { + CuSkip::skip "$ME: sigprocmask failed; skipped"; +} + +my @Tests = + ( + # test-name, [option, option, ...] {OUT=>"expected-output"} + # + + ['block-alrm', ".1 sleep 10", {EXIT => 124}], + ); + +my $save_temps = $ENV{DEBUG}; +my $verbose = $ENV{VERBOSE}; + +my $prog = 'timeout'; +my $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose); + +exit $fail; diff --git a/tests/timeout/timeout-group.sh b/tests/timeout/timeout-group.sh new file mode 100755 index 0000000..053abc9 --- /dev/null +++ b/tests/timeout/timeout-group.sh @@ -0,0 +1,103 @@ +#!/bin/sh +# test program group handling + +# Copyright (C) 2011-2023 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ timeout +require_trap_signame_ +require_kill_group_ + +# construct a program group hierarchy as follows: +# timeout-group - foreground group +# group.sh - separate group +# timeout.cmd - same group as group.sh +# +# We then send a SIGINT to the "separate group" +# to simulate what happens when a Ctrl-C +# is sent to the foreground group. + +setsid true || skip_ "setsid required to control groups" + +printf '%s\n' '#!'"$SHELL" > timeout.cmd || framework_failure_ +cat >> timeout.cmd <<\EOF +trap 'touch int.received; exit' INT +touch timeout.running +count=$1 +until test -e int.received || test $count = 0; do + sleep 1 + count=$(expr $count - 1) +done +EOF +chmod a+x timeout.cmd + +cat > group.sh <<EOF +#!$SHELL +trap '' INT +timeout --foreground 25 ./timeout.cmd 20& +wait +EOF +chmod a+x group.sh + +check_timeout_cmd_running() +{ + local delay="$1" + test -e timeout.running || + { sleep $delay; return 1; } +} + +# Terminate any background processes +cleanup_() { kill $pid 2>/dev/null && wait $pid; } + +# Start above script in its own group. +# We could use timeout for this, but that assumes an implementation. +setsid ./group.sh & pid=$! +# Wait 6.3s for timeout.cmd to start +retry_delay_ check_timeout_cmd_running .1 6 || fail=1 +# Simulate a Ctrl-C to the group to test timely exit +kill -INT -- -$pid +wait +test -e int.received || fail=1 + +rm -f int.received timeout.running + + +# Ensure cascaded timeouts work +# or more generally, ensure we timeout +# commands that create their own group +# This didn't work before 8.13. + +start=$(date +%s) + +# Note the first timeout must send a signal that +# the second is handling for it to be propagated to the command. +# SIGINT, SIGTERM, SIGALRM etc. are implicit. +timeout -sALRM 30 timeout -sINT 25 ./timeout.cmd 20 & pid=$! +# Wait 6.3s for timeout.cmd to start +retry_delay_ check_timeout_cmd_running .1 6 || fail=1 +kill -ALRM $pid # trigger the alarm of the first timeout command +wait $pid +ret=$? +test $ret -eq 124 || + skip_ "timeout returned $ret. SIGALRM not handled?" +test -e int.received || fail=1 + +end=$(date +%s) + +test $(expr $end - $start) -lt 20 || + skip_ "timeout.cmd didn't receive a signal until after sleep?" + +Exit $fail diff --git a/tests/timeout/timeout-large-parameters.sh b/tests/timeout/timeout-large-parameters.sh new file mode 100755 index 0000000..510d985 --- /dev/null +++ b/tests/timeout/timeout-large-parameters.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# Validate large timeout parameters +# Separated from standard parameter testing due to kernel overflow bugs. + +# Copyright (C) 2008-2023 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ timeout +getlimits_ + +# It was seen on 32 bit Linux/HPPA and OpenIndiana 11 +# that a kernel time_t overflowing cause the timer to fire immediately. +# This is still racy, but unlikely to cause an issue unless +# timeout can't process the timer firing within 3 seconds. +timeout $TIME_T_OFLOW sleep 3 +if test $? = 124; then + skip_ 'timeout $TIME_T_OFLOW ..., timed out immediately!' +fi + +# timeout overflow +timeout $UINT_OFLOW sleep 0 || fail=1 + +# timeout overflow +timeout ${TIME_T_OFLOW}d sleep 0 || fail=1 + +# floating point notation +timeout 2.34e+5d sleep 0 || fail=1 + +# floating point overflow +timeout $LDBL_MAX sleep 0 || fail=1 +returns_ 125 timeout -- -$LDBL_MAX sleep 0 || fail=1 + +Exit $fail diff --git a/tests/timeout/timeout-parameters.sh b/tests/timeout/timeout-parameters.sh new file mode 100755 index 0000000..8bb74c5 --- /dev/null +++ b/tests/timeout/timeout-parameters.sh @@ -0,0 +1,50 @@ +#!/bin/sh +# Validate timeout parameter combinations + +# Copyright (C) 2008-2023 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ timeout env +getlimits_ + + +# internal errors are 125, distinct from execution failure + +# invalid timeout +returns_ 125 timeout invalid sleep 0 || fail=1 + +# invalid kill delay +returns_ 125 timeout --kill-after=invalid 1 sleep 0 || fail=1 + +# invalid timeout suffix +returns_ 125 timeout 42D sleep 0 || fail=1 + +# floating point notation +timeout 10.34 sleep 0 || fail=1 + +# nanoseconds potentially supported +timeout 9.999999999 sleep 0 || fail=1 + +# invalid signal spec +returns_ 125 timeout --signal=invalid 1 sleep 0 || fail=1 + +# invalid command +returns_ 126 env . && { returns_ 126 timeout 10 . || fail=1; } + +# no such command +returns_ 127 timeout 10 no_such || fail=1 + +Exit $fail diff --git a/tests/timeout/timeout.sh b/tests/timeout/timeout.sh new file mode 100755 index 0000000..0881770 --- /dev/null +++ b/tests/timeout/timeout.sh @@ -0,0 +1,74 @@ +#!/bin/sh +# Validate timeout basic operation + +# Copyright (C) 2008-2023 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ timeout +require_trap_signame_ + +# no timeout +timeout 10 true || fail=1 + +# no timeout (suffix check) +timeout 1d true || fail=1 + +# disabled timeout +timeout 0 true || fail=1 + +# exit status propagation +returns_ 2 timeout 10 sh -c 'exit 2' || fail=1 + +# timeout +returns_ 124 timeout .1 sleep 10 || fail=1 + +# exit status propagation even on timeout +# exit status should be 128+TERM +returns_ 124 timeout --preserve-status .1 sleep 10 && fail=1 + +# kill delay. Note once the initial timeout triggers, +# the exit status will be 124 even if the command +# exits on its own accord. +# exit status should be 128+KILL +returns_ 124 timeout -s0 -k1 .1 sleep 10 && fail=1 +# Ensure a consistent exit status with --foreground +returns_ 124 timeout --foreground -s0 -k1 .1 sleep 10 && fail=1 + +# Ensure 'timeout' is immune to parent's SIGCHLD handler +# Use a subshell and an exec to work around a bug in FreeBSD 5.0 /bin/sh. +( + trap '' CHLD + + exec timeout 10 true +) || fail=1 + +# Don't be confused when starting off with a child (Bug#9098). +out=$(sleep .1 & exec timeout .5 sh -c 'sleep 2; echo foo') +status=$? +test "$out" = "" && test $status = 124 || fail=1 + +# Verify --verbose output +cat > exp <<\EOF +timeout: sending signal EXIT to command 'sleep' +timeout: sending signal KILL to command 'sleep' +EOF +for opt in -v --verbose; do + timeout $opt -s0 -k .1 .1 sleep 10 2> errt + sed '/^Killed/d' < errt > err || framework_failure_ + compare exp err || fail=1 +done + +Exit $fail |