xref: /openbsd-src/gnu/usr.bin/perl/t/io/openpid.t (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1#!./perl
2
3#####################################################################
4#
5# Test for process id return value from open
6# Ronald Schmidt (The Software Path) RonaldWS@software-path.com
7#
8#####################################################################
9
10BEGIN {
11    chdir 't' if -d 't';
12    require './test.pl';
13    set_up_inc('../lib');
14}
15
16if ($^O eq 'dos') {
17    skip_all("no multitasking");
18}
19
20plan tests => 10;
21watchdog(15, $^O eq 'MSWin32' ? "alarm" : '');
22
23use Config;
24$| = 1;
25$SIG{PIPE} = 'IGNORE';
26# reset the handler in case the shell has set a broken default
27$SIG{HUP} = 'DEFAULT';
28$SIG{HUP} = 'IGNORE' if $^O eq 'interix';
29
30my $perl = which_perl();
31$perl .= qq[ "-I../lib"];
32
33my @perl = ( which_perl(), "-I../lib" );
34
35#
36# commands run 4 perl programs.  Two of these programs write a
37# short message to STDOUT and exit.  Two of these programs
38# read from STDIN.  One reader never exits and must be killed.
39# the other reader reads one line, waits a few seconds and then
40# exits to test the waitpid function.
41#
42# Using 4+ arg open for the children that sleep so that that we're
43# killing the perl process instead of an intermediate shell, this
44# allows harness to see the file handles closed sooner.  I didn't
45# convert them all since I wanted 3-arg open to continue to be
46# exercised here.
47#
48# VMS does not have the list form of piped open, but it also would
49# not create a separate process for an intermediate shell.
50if ($^O eq 'VMS') {
51    $cmd1 = qq/$perl -e "\$|=1; print qq[first process\\n]; sleep 30;"/;
52    $cmd2 = qq/$perl -e "\$|=1; print qq[second process\\n]; sleep 30;"/;
53}
54else {
55    @cmd1 = ( @perl, "-e", "\$|=1; print qq[first process\\n]; sleep 30;" );
56    @cmd2 = ( @perl, "-e", "\$|=1; print qq[second process\\n]; sleep 30;" );
57}
58$cmd3 = qq/$perl -e "print <>;"/; # hangs waiting for end of STDIN
59$cmd4 = qq/$perl -e "print scalar <>;"/;
60
61#warn "#@cmd1\n#@cmd2\n#$cmd3\n#$cmd4\n";
62
63# start the processes
64if ($^O eq 'VMS') {
65    ok( $pid1 = open(FH1, "$cmd1 |"), 'first process started');
66    ok( $pid2 = open(FH2, "$cmd2 |"), '    second' );
67}
68else {
69    ok( $pid1 = open(FH1, "-|", @cmd1), 'first process started');
70    ok( $pid2 = open(FH2, "-|", @cmd2), '    second' );
71}
72{
73    no warnings 'once';
74    ok( $pid3 = open(FH3, "| $cmd3"), '    third'  );
75}
76ok( $pid4 = open(FH4, "| $cmd4"), '    fourth' );
77
78print "# pids were $pid1, $pid2, $pid3, $pid4\n";
79
80my $killsig = 'HUP';
81$killsig = 1 unless $Config{sig_name} =~ /\bHUP\b/;
82
83# get message from first process and kill it
84chomp($from_pid1 = scalar(<FH1>));
85is( $from_pid1, 'first process',    'message from first process' );
86
87$kill_cnt = kill $killsig, $pid1;
88is( $kill_cnt, 1,   'first process killed' ) ||
89  print "# errno == $!\n";
90
91# get message from second process and kill second process and reader process
92chomp($from_pid2 = scalar(<FH2>));
93is( $from_pid2, 'second process',   'message from second process' );
94
95$kill_cnt = kill $killsig, $pid2, $pid3;
96is( $kill_cnt, 2,   'killing procs 2 & 3' ) ||
97  print "# errno == $!\n";
98
99
100# send one expected line of text to child process and then wait for it
101select(FH4); $| = 1; select(STDOUT);
102
103printf FH4 "ok %d - text sent to fourth process\n", curr_test();
104next_test();
105print "# waiting for process $pid4 to exit\n";
106$reap_pid = waitpid $pid4, 0;
107is( $reap_pid, $pid4, 'fourth process reaped' );
108
109