1 |
root |
1.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 |
root |
1.5 |
IO::FDPass::send fileno $socket, fileno $fh_to_pass |
10 |
|
|
or die "send failed: $!"; |
11 |
|
|
|
12 |
|
|
my $fd = IO::FDPass::recv fileno $socket; |
13 |
|
|
$fd >= 0 or die "recv failed: $!"; |
14 |
|
|
|
15 |
root |
1.1 |
=head1 DESCRIPTION |
16 |
|
|
|
17 |
|
|
This small low-level module only has one purpose: pass a file descriptor |
18 |
root |
1.3 |
to another process, using a (streaming) unix domain socket (on POSIX |
19 |
|
|
systems) or any (streaming) socket (on WIN32 systems). |
20 |
root |
1.1 |
|
21 |
|
|
=head1 FUNCTIONS |
22 |
|
|
|
23 |
|
|
=over 4 |
24 |
|
|
|
25 |
|
|
=cut |
26 |
|
|
|
27 |
|
|
package IO::FDPass; |
28 |
|
|
|
29 |
|
|
BEGIN { |
30 |
root |
1.4 |
$VERSION = 0.2; |
31 |
root |
1.1 |
|
32 |
|
|
require XSLoader; |
33 |
|
|
XSLoader::load (__PACKAGE__, $VERSION); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
=item $bool = IO::FDPass::send $socket_fd, $fd_to_pass |
37 |
|
|
|
38 |
|
|
Sends the file descriptor given by C<$fd_to_pass> over the socket |
39 |
|
|
C<$socket_fd>. Return true if it worked, false otherwise. |
40 |
|
|
|
41 |
|
|
Note that I<both> parameters must be file descriptors, not handles. |
42 |
|
|
|
43 |
|
|
When used on non-blocking sockets, this function might fail with C<$!> |
44 |
|
|
set to C<EAGAIN> or equivalent, in which case you are free to try. It |
45 |
|
|
should succeed if called on a socket that indicates writability (e.g. via |
46 |
|
|
C<select>). |
47 |
|
|
|
48 |
|
|
Example: pass a file handle over an open socket. |
49 |
|
|
|
50 |
|
|
IO::FDPass::send fileno $socket, fileno $fh |
51 |
|
|
or die "unable to pass file handle: $!"; |
52 |
|
|
|
53 |
|
|
=item $fd = IO::FDPass::recv $socket_fd |
54 |
|
|
|
55 |
|
|
Receive a file descriptor from the socket and return it if successful. On |
56 |
|
|
errors, return C<-1>. |
57 |
|
|
|
58 |
|
|
Note that I<both> C<$socket_fd> amd the returned file descriptor are, in |
59 |
|
|
fact, file descriptors, not handles. |
60 |
|
|
|
61 |
|
|
When used on non-blocking sockets, this function might fail with C<$!> |
62 |
|
|
set to C<EAGAIN> or equivalent, in which case you are free to try. It |
63 |
|
|
should succeed if called on a socket that indicates readability (e.g. via |
64 |
|
|
C<select>). |
65 |
|
|
|
66 |
|
|
Example: receive a file desriptor from a blockign socket and convetr it to |
67 |
|
|
a file handle. |
68 |
|
|
|
69 |
|
|
my $fd = IO::FDPass::recv fileno $socket; |
70 |
|
|
$fd >= 0 or die "unable to receive file handle: $!"; |
71 |
|
|
open my $fh, "+<&=$fd" |
72 |
|
|
or die "unable to convert file descriptor to handle: $!"; |
73 |
|
|
|
74 |
|
|
=back |
75 |
|
|
|
76 |
|
|
=head1 PORTABILITY NOTES |
77 |
|
|
|
78 |
root |
1.3 |
This module has been tested on GNU/Linux x86 and amd64, NetBSD 6, OS X |
79 |
|
|
10.5, Windows 2000 ActivePerl 5.10, Solaris 10, OpenBSD 4.4, 4.5, 4.8 and |
80 |
|
|
5.0, DragonFly BSD, FreeBSD 7, 8 and 9, Windows 7 + ActivePerl 5.16.3 32 |
81 |
|
|
and 64 bit and Strawberry Perl 5.16.3 32 and 64 bit, and found to work, |
82 |
|
|
although ActivePerl 32 bit needed a newer MinGW version (that supports XP |
83 |
|
|
and higher). |
84 |
root |
1.1 |
|
85 |
|
|
However, windows doesn't support asynchronous file descriptor passing, so |
86 |
root |
1.4 |
the source process must still be around when the destination process wants |
87 |
|
|
to receive the file handle. Also, if the target process fails to fetch the |
88 |
|
|
handle, the handle will leak, so never do that. |
89 |
root |
1.1 |
|
90 |
root |
1.4 |
Also, on windows, the receiving process must have the PROCESS_DUP_HANDLE |
91 |
|
|
access right on the sender process for this module to work. |
92 |
root |
1.1 |
|
93 |
root |
1.3 |
Cygwin is not supported at the moment, as file descriptor passing in |
94 |
|
|
cygwin is not supported, and cannot be rolled on your own as cygwin has no |
95 |
|
|
(working) method of opening a handle as fd. That is, it has one, but that |
96 |
|
|
one isn't exposed to programs, and only used for stdin/out/err. Sigh. |
97 |
|
|
|
98 |
root |
1.1 |
=head1 OTHER MODULES |
99 |
|
|
|
100 |
|
|
At the time of this writing, the author of this module was aware of two |
101 |
|
|
other file descriptor passing modules on CPAN: L<Linux::FDPasser> and |
102 |
|
|
L<AnyEvent::FDPasser>. |
103 |
|
|
|
104 |
|
|
The former hasn't seen any release for over a decade, isn't 64 bit clean |
105 |
|
|
and it's author didn't respond to my mail with the fix. It does, however, |
106 |
root |
1.6 |
support a number of pre-standard unices. |
107 |
root |
1.1 |
|
108 |
|
|
The latter seems to have similar support for antique unices, and doesn't |
109 |
|
|
seem to suffer from 64 bit bugs, but inexplicably has a large perl part, |
110 |
|
|
and requires AnyEvent. Presumably that makes it much more user friendly |
111 |
|
|
than this module. |
112 |
|
|
|
113 |
|
|
Neither seems to support native win32 perls. |
114 |
|
|
|
115 |
|
|
=head1 AUTHOR |
116 |
|
|
|
117 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
118 |
|
|
http://home.schmorp.de/ |
119 |
|
|
|
120 |
|
|
=cut |
121 |
|
|
|
122 |
|
|
1 |
123 |
|
|
|