Commit | Line | Data |
378cc40b |
1 | #!./perl |
2 | |
79072805 |
3 | # $RCSfile: pipe.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:31 $ |
378cc40b |
4 | |
774d564b |
5 | BEGIN { |
6 | chdir 't' if -d 't'; |
7 | @INC = '../lib'; |
8 | require Config; import Config; |
9 | unless ($Config{'d_fork'}) { |
10 | print "1..0\n"; |
11 | exit 0; |
12 | } |
13 | } |
14 | |
378cc40b |
15 | $| = 1; |
1d3434b8 |
16 | print "1..12\n"; |
378cc40b |
17 | |
c07a80fd |
18 | open(PIPE, "|-") || (exec 'tr', 'YX', 'ko'); |
19 | print PIPE "Xk 1\n"; |
20 | print PIPE "oY 2\n"; |
378cc40b |
21 | close PIPE; |
22 | |
23 | if (open(PIPE, "-|")) { |
24 | while(<PIPE>) { |
ac58e20f |
25 | s/^not //; |
378cc40b |
26 | print; |
27 | } |
1d3434b8 |
28 | close PIPE; # avoid zombies which disrupt test 12 |
378cc40b |
29 | } |
30 | else { |
ac58e20f |
31 | print STDOUT "not ok 3\n"; |
32 | exec 'echo', 'not ok 4'; |
378cc40b |
33 | } |
ac58e20f |
34 | |
35 | pipe(READER,WRITER) || die "Can't open pipe"; |
36 | |
37 | if ($pid = fork) { |
38 | close WRITER; |
39 | while(<READER>) { |
40 | s/^not //; |
41 | y/A-Z/a-z/; |
42 | print; |
43 | } |
1d3434b8 |
44 | close READER; # avoid zombies which disrupt test 12 |
ac58e20f |
45 | } |
46 | else { |
47 | die "Couldn't fork" unless defined $pid; |
48 | close READER; |
49 | print WRITER "not ok 5\n"; |
50 | open(STDOUT,">&WRITER") || die "Can't dup WRITER to STDOUT"; |
51 | close WRITER; |
52 | exec 'echo', 'not ok 6'; |
53 | } |
54 | |
55 | |
56 | pipe(READER,WRITER) || die "Can't open pipe"; |
57 | close READER; |
58 | |
59 | $SIG{'PIPE'} = 'broken_pipe'; |
60 | |
61 | sub broken_pipe { |
1d2dff63 |
62 | $SIG{'PIPE'} = 'IGNORE'; # loop preventer |
ac58e20f |
63 | print "ok 7\n"; |
64 | } |
65 | |
66 | print WRITER "not ok 7\n"; |
67 | close WRITER; |
3d57aefb |
68 | sleep 1; |
ac58e20f |
69 | print "ok 8\n"; |
03136e13 |
70 | |
71 | # VMS doesn't like spawning subprocesses that are still connected to |
1d3434b8 |
72 | # STDOUT. Someone should modify tests #9 to #12 to work with VMS. |
03136e13 |
73 | |
74 | if ($^O eq 'VMS') { |
75 | print "ok 9\n"; |
76 | print "ok 10\n"; |
1d3434b8 |
77 | print "ok 11\n"; |
78 | print "ok 12\n"; |
03136e13 |
79 | exit; |
80 | } |
81 | |
6ee623d5 |
82 | if ($Config{d_sfio} || $^O eq machten || $^O eq beos) { |
03136e13 |
83 | # Sfio doesn't report failure when closing a broken pipe |
fc261528 |
84 | # that has pending output. Go figure. MachTen doesn't either, |
85 | # but won't write to broken pipes, so nothing's pending at close. |
6ee623d5 |
86 | # BeOS will not write to broken pipes, either. |
03136e13 |
87 | print "ok 9\n"; |
88 | } |
89 | else { |
90 | local $SIG{PIPE} = 'IGNORE'; |
91 | open NIL, '|true' or die "open failed: $!"; |
92 | sleep 2; |
93 | print NIL 'foo' or die "print failed: $!"; |
94 | if (close NIL) { |
95 | print "not ok 9\n"; |
96 | } |
97 | else { |
98 | print "ok 9\n"; |
99 | } |
100 | } |
101 | |
102 | # check that errno gets forced to 0 if the piped program exited non-zero |
103 | open NIL, '|exit 23;' or die "fork failed: $!"; |
104 | $! = 1; |
105 | if (close NIL) { |
106 | print "not ok 10\n# successful close\n"; |
107 | } |
108 | elsif ($! != 0) { |
109 | print "not ok 10\n# errno $!\n"; |
110 | } |
111 | elsif ($? == 0) { |
112 | print "not ok 10\n# status 0\n"; |
113 | } |
114 | else { |
115 | print "ok 10\n"; |
116 | } |
1d3434b8 |
117 | |
118 | # check that status for the correct process is collected |
062dddbe |
119 | wait; # Collect from $pid |
1d3434b8 |
120 | my $zombie = fork or exit 37; |
121 | my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n"; |
122 | $SIG{ALRM} = sub { return }; |
123 | alarm(1); |
124 | my $close = close FH; |
125 | if ($? == 13*256 && ! length $close && ! $!) { |
126 | print "ok 11\n"; |
127 | } else { |
128 | print "not ok 11\n# close $close\$?=$? \$!=", $!+0, ":$!\n"; |
129 | }; |
130 | my $wait = wait; |
131 | if ($? == 37*256 && $wait == $zombie && ! $!) { |
132 | print "ok 12\n"; |
133 | } else { |
062dddbe |
134 | print "not ok 12\n# pid=$wait first=$pid pipe=$pipe zombie=$zombie me=$$ \$?=$? \$!=", $!+0, ":$!\n"; |
1d3434b8 |
135 | } |