1#!/bin/sh 2# Test env --default-signal=PIPE feature. 3 4# Copyright (C) 2019-2023 Free Software Foundation, Inc. 5 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. 10 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. 15 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/>. 18 19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src 20print_ver_ env seq test timeout printf 21trap_sigpipe_or_skip_ 22 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 26require_bash_as_SHELL_ 27 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: 32# 33# $ (trap '' PIPE; bash -c 'trap - PIPE; seq inf | head -n1') 34# 1 35# seq: write error: Broken pipe 36# 37# With 'env --default-signal=PIPE', the signal handler can be reset to its 38# default. 39 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') 47 48# The exact broken pipe message depends on the operating system, just ensure 49# there was a 'write error' message in stderr: 50sed 's/^\(seq: write error:\) .*/\1/' err1t > err1 || framework_failure_ 51 52printf "1\n" > exp-out || framework_failure_ 53printf "seq: write error:\n" > exp-err1 || framework_failure_ 54 55compare exp-out out1 || framework_failure_ 56compare exp-err1 err1 || framework_failure_ 57 58 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 63# 'seq' program): 64(trap '' PIPE; 65 env --default-signal=PIPE \ 66 $SHELL -c 'trap - PIPE; seq 999999 2>err2 | head -n1 > out2') 67 68compare exp-out out2 || fail=1 69compare /dev/null err2 || fail=1 70 71# env test - default signal handler (3) 72# ------------------------------------- 73# Repeat the previous test, using --default-signal with no signal names, 74# i.e., all signals. 75(trap '' PIPE; 76 env --default-signal \ 77 $SHELL -c 'trap - PIPE; seq 999999 2>err4 | head -n1 > out4') 78 79compare exp-out out4 || fail=1 80compare /dev/null err4 || fail=1 81 82# env test - block signal handler 83env --block-signal true || fail=1 84 85env_ignore_delay_() 86{ 87 local delay="$1" 88 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_ 95 compare exp out 96} 97 98# Baseline test - ignore signal handler 99# ------------------------------------- 100# Terminate 'sleep' with SIGINT 101# (SIGINT's default action is to terminate a program). 102cat <<\EOF >exp || framework_failure_ 103timeout: sending signal INT to command 'env' 104EOF 105env_opt='' retry_delay_ env_ignore_delay_ .1 6 || fail=1 106 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. 111cat <<\EOF >exp || framework_failure_ 112timeout: sending signal INT to command 'env' 113timeout: sending signal KILL to command 'env' 114EOF 115env_opt='--ignore-signal=INT' retry_delay_ env_ignore_delay_ .1 6 || fail=1 116env_opt='--ignore-signal' retry_delay_ env_ignore_delay_ .1 6 || fail=1 117 118# env test --list-signal-handling 119env --default-signal --ignore-signal=INT --list-signal-handling true \ 120 2> err8t || fail=1 121sed 's/(.*)/()/' err8t > err8 || framework_failure_ 122env printf 'INT (): IGNORE\n' > exp-err8 || framework_failure_ 123compare exp-err8 err8 || fail=1 124 125 126Exit $fail 127