PageRenderTime 34ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/cmd/perl/5.8.4/distrib/t/io/pipe.t

https://bitbucket.org/osunix/osunix-gate
Perl | 198 lines | 170 code | 17 blank | 11 comment | 10 complexity | 65ccd11b57ea358c84dd6ccb71825ea8 MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, MPL-2.0-no-copyleft-exception, BSD-3-Clause, BSD-2-Clause, LGPL-3.0, 0BSD, GPL-2.0, LGPL-2.0, AGPL-1.0, AGPL-3.0, GPL-3.0, LGPL-2.1
  1. #!./perl
  2. BEGIN {
  3. chdir 't' if -d 't';
  4. @INC = '../lib';
  5. require Config; import Config;
  6. require './test.pl';
  7. if (!$Config{'d_fork'}) {
  8. skip_all("fork required to pipe");
  9. }
  10. else {
  11. plan(tests => 22);
  12. }
  13. }
  14. my $Perl = which_perl();
  15. $| = 1;
  16. open(PIPE, "|-") || exec $Perl, '-pe', 'tr/YX/ko/';
  17. printf PIPE "Xk %d - open |- || exec\n", curr_test();
  18. next_test();
  19. printf PIPE "oY %d - again\n", curr_test();
  20. next_test();
  21. close PIPE;
  22. SKIP: {
  23. # Technically this should be TODO. Someone try it if you happen to
  24. # have a vmesa machine.
  25. skip "Doesn't work here yet", 4 if $^O eq 'vmesa';
  26. if (open(PIPE, "-|")) {
  27. while(<PIPE>) {
  28. s/^not //;
  29. print;
  30. }
  31. close PIPE; # avoid zombies
  32. }
  33. else {
  34. printf STDOUT "not ok %d - open -|\n", curr_test();
  35. next_test();
  36. my $tnum = curr_test;
  37. next_test();
  38. exec $Perl, '-le', "print q{not ok $tnum - again}";
  39. }
  40. # This has to be *outside* the fork
  41. next_test() for 1..2;
  42. SKIP: {
  43. skip "fork required", 2 unless $Config{d_fork};
  44. pipe(READER,WRITER) || die "Can't open pipe";
  45. if ($pid = fork) {
  46. close WRITER;
  47. while(<READER>) {
  48. s/^not //;
  49. y/A-Z/a-z/;
  50. print;
  51. }
  52. close READER; # avoid zombies
  53. }
  54. else {
  55. die "Couldn't fork" unless defined $pid;
  56. close READER;
  57. printf WRITER "not ok %d - pipe & fork\n", curr_test;
  58. next_test;
  59. open(STDOUT,">&WRITER") || die "Can't dup WRITER to STDOUT";
  60. close WRITER;
  61. my $tnum = curr_test;
  62. next_test;
  63. exec $Perl, '-le', "print q{not ok $tnum - with fh dup }";
  64. }
  65. # This has to be done *outside* the fork.
  66. next_test() for 1..2;
  67. }
  68. }
  69. wait; # Collect from $pid
  70. pipe(READER,WRITER) || die "Can't open pipe";
  71. close READER;
  72. $SIG{'PIPE'} = 'broken_pipe';
  73. sub broken_pipe {
  74. $SIG{'PIPE'} = 'IGNORE'; # loop preventer
  75. printf "ok %d - SIGPIPE\n", curr_test;
  76. }
  77. printf WRITER "not ok %d - SIGPIPE\n", curr_test;
  78. close WRITER;
  79. sleep 1;
  80. next_test;
  81. pass();
  82. # VMS doesn't like spawning subprocesses that are still connected to
  83. # STDOUT. Someone should modify these tests to work with VMS.
  84. SKIP: {
  85. skip "doesn't like spawning subprocesses that are still connected", 10
  86. if $^O eq 'VMS';
  87. SKIP: {
  88. # Sfio doesn't report failure when closing a broken pipe
  89. # that has pending output. Go figure. MachTen doesn't either,
  90. # but won't write to broken pipes, so nothing's pending at close.
  91. # BeOS will not write to broken pipes, either.
  92. # Nor does POSIX-BC.
  93. skip "Won't report failure on broken pipe", 1
  94. if $Config{d_sfio} || $^O eq 'machten' || $^O eq 'beos' ||
  95. $^O eq 'posix-bc';
  96. local $SIG{PIPE} = 'IGNORE';
  97. open NIL, qq{|$Perl -e "exit 0"} or die "open failed: $!";
  98. sleep 5;
  99. if (print NIL 'foo') {
  100. # If print was allowed we had better get an error on close
  101. ok( !close NIL, 'close error on broken pipe' );
  102. }
  103. else {
  104. ok(close NIL, 'print failed on broken pipe');
  105. }
  106. }
  107. SKIP: {
  108. skip "Don't work yet", 9 if $^O eq 'vmesa';
  109. # check that errno gets forced to 0 if the piped program exited
  110. # non-zero
  111. open NIL, qq{|$Perl -e "exit 23";} or die "fork failed: $!";
  112. $! = 1;
  113. ok(!close NIL, 'close failure on non-zero piped exit');
  114. is($!, '', ' errno');
  115. isnt($?, 0, ' status');
  116. SKIP: {
  117. skip "Don't work yet", 6 if $^O eq 'mpeix';
  118. # check that status for the correct process is collected
  119. my $zombie;
  120. unless( $zombie = fork ) {
  121. $NO_ENDING=1;
  122. exit 37;
  123. }
  124. my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n";
  125. $SIG{ALRM} = sub { return };
  126. alarm(1);
  127. is( close FH, '', 'close failure for... umm, something' );
  128. is( $?, 13*256, ' status' );
  129. is( $!, '', ' errno');
  130. my $wait = wait;
  131. is( $?, 37*256, 'status correct after wait' );
  132. is( $wait, $zombie, ' wait pid' );
  133. is( $!, '', ' errno');
  134. }
  135. }
  136. }
  137. # Test new semantics for missing command in piped open
  138. # 19990114 M-J. Dominus mjd@plover.com
  139. { local *P;
  140. ok( !open(P, "| "), 'missing command in piped open input' );
  141. ok( !open(P, " |"), ' output');
  142. }
  143. # check that status is unaffected by implicit close
  144. {
  145. local(*NIL);
  146. open NIL, qq{|$Perl -e "exit 23"} or die "fork failed: $!";
  147. $? = 42;
  148. # NIL implicitly closed here
  149. }
  150. is($?, 42, 'status unaffected by implicit close');
  151. $? = 0;
  152. # check that child is reaped if the piped program can't be executed
  153. {
  154. open NIL, '/no_such_process |';
  155. close NIL;
  156. my $child = 0;
  157. eval {
  158. local $SIG{ALRM} = sub { die; };
  159. alarm 2;
  160. $child = wait;
  161. alarm 0;
  162. };
  163. is($child, -1, 'child reaped if piped program cannot be executed');
  164. }