I recently looked again at PPerl, because I wanted to fix some other behaviour, and found that it doesn't build on Windows. After a quick converstation with the author, Matt Sergeant, I found that PPerl doesn't cater for Windows because the functionality it needs, sendfd() and SOCK_PERM, only exists on Linux and some other unixish OSes and not on Windows.
Almost by chance, I happened over the DuplicateHandle() API of Windows, which allows to duplicate a file handle for use in another process. My test file implementing this is below.
I use a child process, to which I print the handle number on STDIN. The child process then reads from that filehandle which it knows no name of. With a child process, the same could well have been done using input/output redirection, but the use of a child process is only to alleviate permission issues and general coordination issues, like how the two processes find each other.
#!perl -w use strict; use Win32::API; use Win32; use Win32API::File qw(FdGetOsFHandle); Win32::API->Import( 'kernel32', 'DuplicateHandle', 'NNNPNNN', 'N', ); Win32::API->Import( 'kernel32', 'OpenProcess', 'NNN', 'N', ); # This is the child process that reads the number from STDIN, opens th +at handle, and reads from it my $cmd = q(| perl -MWin32API::File=OsFHandleOpen -e "$fileno=0+<> or +die;OsFHandleOpen(FH, $fileno,'r') or die $!; print for <FH>"); my $child = open my $child_fh, $cmd or die "Can't spawn child [$cmd]: $! / $? / $^E"; print "1..2\n"; print "1 "; print $child ? "" : "not "; print "ok # spawned child\n"; # 0x400 for PROCESS_QUERY_INFORMATION # 0x040 for PROCESS_DUP_HANDLE my $childHandle = OpenProcess(0x440,0,$child) or die $^E; my $buf = "\0\0\0\0"; my $fhandle = FdGetOsFHandle( fileno *DATA ); DuplicateHandle(-1, $fhandle, $childHandle, $buf, 0, 1, 3 ) or die $^E; $buf = unpack "V", $buf; # Now tell our child from what handle it should read print {$child_fh} "$buf\n"; # The parent is done close $child_fh; __DATA__ 2 ok # output read from child process
As is the case on Windows, sockets are outside of this scheme, unfortunately. If you want to pass around sockets between processes, you need to look at WSADuplicateSocket
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |