1#!/bin/sh
2# Check that zgrep is terminated gracefully by signal when
3# its grep/sed pipeline is terminated by a signal.
4
5# Copyright (C) 2010-2016 Free Software Foundation, Inc.
6
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19# limit so don't run it by default.
20
21. "${srcdir=.}/init.sh"; path_prepend_ .
22
23echo a | gzip -c > f.gz || framework_failure_
24
25test "x$PERL" = x && PERL=perl
26("$PERL" -e 'use POSIX qw(dup2)') >/dev/null 2>&1 ||
27   skip_ "no suitable perl found"
28
29# Run the arguments as a command, in a process where stdout is a
30# dangling pipe and SIGPIPE has the default signal-handling action.
31# This can't be done portably in the shell, because if SIGPIPE is
32# ignored when the shell is entered, the shell might refuse to trap
33# it.  Fall back on Perl+POSIX, if available.  Take care to close the
34# pipe's read end before running the program; the equivalent of the
35# shell's "command | :" has a race condition in that COMMAND could
36# write before ":" exits.
37write_to_dangling_pipe () {
38  program=${1?}
39  shift
40  args=
41  for arg; do
42    args="$args, '$arg'"
43  done
44  "$PERL" -e '
45     use POSIX qw(dup2);
46     $SIG{PIPE} = "DEFAULT";
47     pipe my ($read_end, $write_end) or die "pipe: $!\n";
48     dup2 fileno $write_end, 1 or die "dup2: $!\n";
49     close $read_end or die "close: $!\n";
50     exec '"'$program'$args"';
51  '
52}
53
54write_to_dangling_pipe cat f.gz f.gz
55signal_status=$?
56test 128 -lt $signal_status ||
57  framework_failure_ 'signal handling busted on this host'
58
59fail=0
60
61write_to_dangling_pipe zgrep a f.gz f.gz
62test $? -eq $signal_status || fail=1
63
64Exit $fail
65