in reply to IPC::Open3 not connecting network socket handle

The first three arguments of open3 are outputs. open3 creates pipes and assigns them to the provided variables (causing the handle they previously held to be closed).

That is unless they hold a string that starts with "<&" (1st arg) or ">&" (2nd and 3rd args). When these are used, open3 use the specified handle instead of creating a pipe. Like in the example that worked, you need to use "<&" to tell open3 to use an existing handle.

open3( '<&'.fileno($new_sock), ... )

There is also a special meaning for the third arg when it's undef, but it's unrelated to the issue at hand.

Update: Added explanation.

Replies are listed 'Best First'.
Re^2: IPC::Open3 not connecting network socket handle
by Anonymous Monk on Jul 24, 2009 at 15:08 UTC
    Thanks for the suggestion - I didn't realize it wanted a file descriptor number. I tried "<&".fileno($new_sock) and now (on 5.8.4) it connects but I'm still having an issue:
    open3: close(5) failed: Bad file number at ./s5 line 32
    The cat subprocess actually continues -- but I realized this was actually a side benefit of the bug that salva referenced. I tested on perl 5.8.8 (previously 5.8.4) and indeed the subprocess was killed upon the close(5) failure.
      IPC::Open3 is already closing it for you. Just remove the close.
        I don't think it is - it seems like my script is actually dying at the open3 step, while the child lives on (at least under 5.8.4). I'm dropped back to a shell prompt , even though my telnet session is still open in the other window and cat is still printing the text to my terminal. I removed the redundant close to no effect, it doesn't seem that the script had been getting that far.
        #!/usr/bin/perl use warnings; use strict; use IO::Socket; use IPC::Open3; my $sock = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '1818', Proto => 'tcp', Listen => 1, ReuseAddr => 1, Timeout => 20 ); die "Could not create socket: $!\n" unless $sock; our $new_sock = $sock->accept() or die "No one came!"; my $pid = open3( "<&" . fileno($new_sock), ">&STDOUT", ">&STDERR", "/b +in/cat" ); waitpid( $pid, 0 );
        Here is the result:
        sh-3.2# ./test.pl open3: close(5) failed: Bad file number at ./test.pl line 21 sh-3.2# ps -ef | grep /bin/cat root 6124 6106 1 12:57:29 pts/3 0:00 grep /bin/cat root 6122 1 1 12:57:21 pts/3 0:00 /bin/cat sh-3.2# Blah blah this is text coming from my telnet session