1#! /usr/bin/perl -w
2
3BEGIN {
4  my $inc = $ENV{PERL5LIB};
5  $inc = $ENV{PERLLIB} unless defined $inc;
6  $inc = '' unless defined $inc;
7  $ENV{PERL5LIB} = join ';', @INC, split /;/, $inc;
8}
9
10use strict;
11use Test::More tests => 11;
12use OS2::Process;
13
14my $cmd = <<'EOA';
15use OS2::Process;
16$| = 1;
17print for $$, ppid, sidOf;
18$SIG{TERM} = $SIG{INT} = sub {exit};
19sleep 10;
20EOA
21
22#my $PID = open my $fh, '-|', $^X, '-wle', $cmd;
23$ENV{CMD_RUN} = $cmd;
24my $PID = open my $fh, '-|', "$^X -wle 'eval \$ENV{CMD_RUN} or die'";
25ok $PID, 'opened a pipe';
26my ($kpid, $kppid, $sid);
27$kpid = <$fh>;
28$kppid = <$fh>;
29$sid = <$fh>;
30chomp ($kpid, $kppid, $sid);
31
32# This does not work with the intervening shell...
33my $extra_fork = $kppid == $PID; # Temporary implementation of multi-arg open()
34
35print "# us=$$, immediate-pid=$PID, parent-of-kid=$kppid, kid=$kpid\n";
36if ($ENV{CMD_RUN}) {	# Two copies of the shell intervene...
37  is( ppidOf($kppid), $PID, 'correct pid of the kid or its parent');
38  is( ppidOf($PID), $$,  'we know our child\'s parent');
39} else {
40  is( ($extra_fork ? $kppid : $kpid), $PID, 'correct pid of the kid');
41  is( $kppid, ($extra_fork ? $PID : $$), 'kid knows its ppid');
42}
43ok $sid >= 0, 'kid got its sid';
44is($sid, sidOf, 'sid of kid same as our');
45is(sidOf($kpid), $sid, 'we know sid of kid');
46is(sidOf($PID), $sid, 'we know sid of inter-kid');
47is(ppidOf($kpid), $kppid, 'we know ppid of kid');
48is(ppidOf($PID), $$, 'we know ppid of inter-kid');
49
50ok kill('TERM', $kpid), 'killed the kid';
51#ok( ($PID == $kpid or kill('TERM', $PID)), 'killed the inter-kid');
52ok close $fh, 'closed the pipe';	# No kid any more
53