ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-FDPass/FDPass.pm
Revision: 1.1
Committed: Fri Apr 5 04:10:51 2013 UTC (11 years, 1 month ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 IO::FDPass - pass a file descriptor over a socket
4
5 =head1 SYNOPSIS
6
7 use IO::FDPass;
8
9 =head1 DESCRIPTION
10
11 This small low-level module only has one purpose: pass a file descriptor
12 to another process, using a unix domain socket (on POSIX systems) or any
13 socket (on WIN32 systems).
14
15 =head1 FUNCTIONS
16
17 =over 4
18
19 =cut
20
21 package IO::FDPass;
22
23 BEGIN {
24 $VERSION = '0.0';
25
26 require XSLoader;
27 XSLoader::load (__PACKAGE__, $VERSION);
28 }
29
30 =item $bool = IO::FDPass::send $socket_fd, $fd_to_pass
31
32 Sends the file descriptor given by C<$fd_to_pass> over the socket
33 C<$socket_fd>. Return true if it worked, false otherwise.
34
35 Note that I<both> parameters must be file descriptors, not handles.
36
37 When used on non-blocking sockets, this function might fail with C<$!>
38 set to C<EAGAIN> or equivalent, in which case you are free to try. It
39 should succeed if called on a socket that indicates writability (e.g. via
40 C<select>).
41
42 Example: pass a file handle over an open socket.
43
44 IO::FDPass::send fileno $socket, fileno $fh
45 or die "unable to pass file handle: $!";
46
47 =item $fd = IO::FDPass::recv $socket_fd
48
49 Receive a file descriptor from the socket and return it if successful. On
50 errors, return C<-1>.
51
52 Note that I<both> C<$socket_fd> amd the returned file descriptor are, in
53 fact, file descriptors, not handles.
54
55 When used on non-blocking sockets, this function might fail with C<$!>
56 set to C<EAGAIN> or equivalent, in which case you are free to try. It
57 should succeed if called on a socket that indicates readability (e.g. via
58 C<select>).
59
60 Example: receive a file desriptor from a blockign socket and convetr it to
61 a file handle.
62
63 my $fd = IO::FDPass::recv fileno $socket;
64 $fd >= 0 or die "unable to receive file handle: $!";
65 open my $fh, "+<&=$fd"
66 or die "unable to convert file descriptor to handle: $!";
67
68 =back
69
70 =head1 PORTABILITY NOTES
71
72 This module has been tested on NEtBSD 6, OS X 10.5, Windows 2000
73 ActivePerl 5.10, Solaris 10, OpenBSD 4.4, 4.5, 4.8 and 5.0, DragonFly
74 BSD, FreeBSD 7, 8 and 9, Windows 7 + ActivePerl 5.16.3 32 and 64 bit and
75 Strawberry Perl 5.16.3 32 and 64 bit, and found to work.
76
77 However, windows doesn't support asynchronous file descriptor passing, so
78 C<send> and C<recv> will have to "rendezvous", that is, they have to wait
79 for each other. Therefore, on windows, it's advisable to run them at the
80 same time to avoid any unnecessary delays.
81
82 Also, on windows, the passing process must give the receiving process the
83 PROCESS_DUP_HANDLE access right for this module to work.
84
85 =head1 OTHER MODULES
86
87 At the time of this writing, the author of this module was aware of two
88 other file descriptor passing modules on CPAN: L<Linux::FDPasser> and
89 L<AnyEvent::FDPasser>.
90
91 The former hasn't seen any release for over a decade, isn't 64 bit clean
92 and it's author didn't respond to my mail with the fix. It does, however,
93 support a snumber of pre-standard unices.
94
95 The latter seems to have similar support for antique unices, and doesn't
96 seem to suffer from 64 bit bugs, but inexplicably has a large perl part,
97 and requires AnyEvent. Presumably that makes it much more user friendly
98 than this module.
99
100 Neither seems to support native win32 perls.
101
102 =head1 AUTHOR
103
104 Marc Lehmann <schmorp@schmorp.de>
105 http://home.schmorp.de/
106
107 =cut
108
109 1
110