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