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