c - Perl, how do I create a pipe to my exec'd child? -


i trying pass data perl script c program using pipe (uni-directional). need find way to without messing child programs stdin or stdout, try creating new handle , passing fd.

i create 2 io::handles , create pipe. write 1 end of pipe , attempt pass file descriptor of other end of pipe child program being execed. pass file descriptor setting env variable. why not work? (it not print out 'hello world'). far know, file descriptors , pipes inherited child when exec'd.

perl script:

#!/opt/local/bin/perl use io::pipe; use io::handle;  $reader = io::handle->new(); $writer = io::handle->new(); $reader->autoflush(1); $writer->autoflush(1); $pipe = io::pipe->new($reader, $writer); print $writer "hello world"; $fh = $reader->fileno; $env{'my_fd'} = $fh; exec('./child') or print "error opening app\n"; # no more code after since exec replaces current process 

c program, app.c (compiled 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); } 

output:

fd: 3 message: 

the message never passed through pipe c program. suggestions?

your pipe file descriptors set fd_cloexec, , closed upon exec().

perl's $^f controls behavior. try this, before call io::pipe->new:

$^f = 10;  # assumes don't have zillion fds open 

alternatively, can fcntl clear fd_cloexec flag after creating pipe.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -