2 # Test env --default-signal=PIPE feature.
4 # Copyright (C) 2019-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
20 print_ver_ env
seq test timeout
printf
23 # /bin/sh has an intermittent failure in ignoring SIGPIPE on OpenIndiana 11
24 # so we require bash as discussed at:
25 # https://lists.gnu.org/archive/html/coreutils/2020-03/msg00004.html
26 require_bash_as_SHELL_
28 # Paraphrasing https://bugs.gnu.org/34488#8:
29 # POSIX requires that sh started with an inherited ignored SIGPIPE must
30 # silently ignore all attempts from within the shell to restore SIGPIPE
31 # handling to child processes of the shell:
33 # $ (trap '' PIPE; bash -c 'trap - PIPE; seq inf | head -n1')
35 # seq: write error: Broken pipe
37 # With 'env --default-signal=PIPE', the signal handler can be reset to its
40 # Baseline Test - default signal handler
41 # --------------------------------------
42 # Ensure this results in a "broken pipe" error (the first 'trap'
43 # sets SIGPIPE to ignore, and the second 'trap' becomes a no-op instead
44 # of resetting SIGPIPE to its default). Upon a SIGPIPE 'seq' will not be
45 # terminated, instead its write(2) call will return an error.
46 (trap '' PIPE
; $SHELL -c 'trap - PIPE; seq 999999 2>err1t | head -n1 > out1')
48 # The exact broken pipe message depends on the operating system, just ensure
49 # there was a 'write error' message in stderr:
50 sed 's/^\(seq: write error:\) .*/\1/' err1t
> err1 || framework_failure_
52 printf "1\n" > exp-out || framework_failure_
53 printf "seq: write error:\n" > exp-err1 || framework_failure_
55 compare exp-out out1 || framework_failure_
56 compare exp-err1 err1 || framework_failure_
59 # env test - default signal handler
60 # ---------------------------------
61 # With env resetting the signal handler to its defaults, there should be no
62 # error message (because the default SIGPIPE action is to terminate the
65 env
--default-signal=PIPE \
66 $SHELL -c 'trap - PIPE; seq 999999 2>err2 | head -n1 > out2')
68 compare exp-out out2 || fail
=1
69 compare
/dev
/null err2 || fail
=1
71 # env test - default signal handler (3)
72 # -------------------------------------
73 # Repeat the previous test, using --default-signal with no signal names,
76 env
--default-signal \
77 $SHELL -c 'trap - PIPE; seq 999999 2>err4 | head -n1 > out4')
79 compare exp-out out4 || fail
=1
80 compare
/dev
/null err4 || fail
=1
82 # env test - block signal handler
83 env
--block-signal true || fail
=1
89 # The first 'env' is just to ensure timeout is not a shell built-in.
90 env timeout
--verbose --kill-after=.1 --signal=INT
$delay \
91 env
$env_opt sleep 10 > /dev
/null
2>outt
92 # check only the first two lines from stderr, which are printed by timeout.
93 # (operating systems might add more messages, like "killed").
94 sed -n '1,2p' outt
> out || framework_failure_
98 # Baseline test - ignore signal handler
99 # -------------------------------------
100 # Terminate 'sleep' with SIGINT
101 # (SIGINT's default action is to terminate a program).
102 cat <<\EOF
>exp || framework_failure_
103 timeout
: sending signal INT to
command 'env'
105 env_opt
='' retry_delay_ env_ignore_delay_
.1 6 || fail
=1
107 # env test - ignore signal handler
108 # --------------------------------
109 # Use env to ignore SIGINT - "sleep" should continue running
110 # after timeout sends SIGINT, and be killed using SIGKILL.
111 cat <<\EOF
>exp || framework_failure_
112 timeout
: sending signal INT to
command 'env'
113 timeout
: sending signal KILL to
command 'env'
115 env_opt
='--ignore-signal=INT' retry_delay_ env_ignore_delay_
.1 6 || fail
=1
116 env_opt
='--ignore-signal' retry_delay_ env_ignore_delay_
.1 6 || fail
=1
118 # env test --list-signal-handling
119 env
--default-signal --ignore-signal=INT
--list-signal-handling true \
121 sed 's/(.*)/()/' err8t
> err8 || framework_failure_
122 env
printf 'INT (): IGNORE\n' > exp-err8 || framework_failure_
123 compare exp-err8 err8 || fail
=1