zgrep-signal.sh 2.1 KB

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