File: child_fd_inadvertently_closed.t

package info (click to toggle)
libipc-run-perl 20231003.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 844 kB
  • sloc: perl: 6,255; makefile: 5
file content (67 lines) | stat: -rw-r--r-- 1,306 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! perl

use strict;
use warnings;

use Test::More;
use IPC::Run 'run';

plan skip_all => "$^O does not allow redirection of file descriptors > 2"
  if IPC::Run::Win32_MODE();
plan tests => 1;

use File::Temp;
use IO::Handle;

use POSIX ();

# trigger IPC::Run bug where parent has $fd open
# and child needs $fd & $fd+1

my $error;

sub parent {

    # dup stderr so we get some fd
    my $xfd = POSIX::dup( 2 );
    die $! if $xfd == -1;

    my @fds = ( $xfd, $xfd + 1 );

    # create input files to be attached to the fds
    my @tmp;
    @tmp[@fds] = map {
        my $tmp = File::Temp->new;
        $tmp->print( $_ );
        $tmp->close;
        $tmp
    } @fds;


    # child reads from fds and make sure that
    # it can open them and that they're attached
    # to the files it expects.
    my $child = sub {

        for my $fd ( @fds ) {

            my $io = IO::Handle->new_from_fd( $fd, '<' )
              or print( STDERR ( "error fdopening $fd\n" ) ), next;

            my $input = $io->getline;
            print STDERR "expected >$fd<.  got >$input<\n"
              unless $fd eq $input;

        }


    };

    run $child,(  map { $_ . '<', $tmp[$_]->filename } @fds, ), '2>', \$error;

    POSIX::close $xfd;
}

parent;
is ( $error, '', "child fd not closed" )
  or note $error;