Perl, how do I create a channel for my exec'd child?

I am trying to transfer data from my perl script to my c program using a pipe (unidirectional). I need to find a way to do this without going into the STDIN or STDOUT child programs, so I am trying to create a new handle and pass fd.

I create 2 IO :: Handles and create a channel. I am writing to one end of a pipe and trying to pass a file descriptor to the other end of the pipe to my child program that is running. I pass the file descriptor by setting the ENV variable. Why is this not working? (He does not print "hello world"). As far as I know, file descriptors and pipes are inherited by the child when exec'd.

Perl script:

#!/opt/local/bin/perl
use IO::Pipe;
use IO::Handle;

my $reader = IO::Handle->new();
my $writer = IO::Handle->new();
$reader->autoflush(1);
$writer->autoflush(1);
my $pipe = IO::Pipe->new($reader, $writer);
print $writer "hello world";
my $fh = $reader->fileno;
$ENV{'MY_FD'} = $fh;
exec('./child') or print "error opening app\n";
# No more code after this since exec replaces the current process

C Program, .c application (compiled from gcc app.c -o child):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char ** argv) {
  int fd = atoi(getenv("MY_FD"));
  char buf[12];
  read(fd, buf, 11);
  buf[11] = '\0';
  printf("fd: %d\n", fd);
  printf("message: %s\n", buf);
}

Conclusion:

fd: 3
message:

C. ?

+5
4

pipe FD_CLOEXEC exec().

Perl $^F . - , IO:: Pipe- > new:

$^F = 10;  # Assumes we don't already have a zillion FDs open

Fcntl FD_CLOEXEC .

+3

, exec . .

, IO::Pipe , : "ARGS exec". , . IO::Pipe Perl script, fork. , exec .

: , , open :

open my $prog, '|-', './child' or die "can't run program: $!";

print {$prog} "Hello, world!";
0

, , , c-.
, , INVALID, . FD = 3 , .
fd fcntl.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char ** argv) {
  int fd = atoi(getenv("MY_FD"));
  char buf[12];
  read(fd, buf, 11);
  buf[11] = '\0';
  printf("fd: %d, if fd still valid: %d\n", fd, fcntl(fd, F_GETFD));
  printf("strlen %d\n", (int)strlen(buf));
  printf("message: %s\n", buf);
}

, MY_FD = 3 ENV, , fd 3. . fcntl (fd, F_GETFD) -1, , fd, 0.
" ".

, @dan1111 , , .
, , MY_FD = 0,

$ENV{'MY_FD'} = 0;

STDIN/OUT - , , , perl c. , .
hanle, , STDIN.

0

. , exec , , , .

, perl / fd > 2, .

FD ( READ - , NOT STDIN):

my $flags = fcntl(READ, F_GETFD, 0);
fcntl(READ, F_SETFD, $flags & ~FD_CLOEXEC);
0

All Articles