ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-FDPass/FDPass.pm
Revision: 1.7
Committed: Sat Apr 6 22:10:56 2013 UTC (11 years, 1 month ago) by root
Branch: MAIN
Changes since 1.6: +9 -5 lines
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 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 =head1 DESCRIPTION
16
17 This small low-level module only has one purpose: pass a file descriptor
18 to another process, using a (streaming) unix domain socket (on POSIX
19 systems) or any (streaming) socket (on WIN32 systems).
20
21 =head1 FUNCTIONS
22
23 =over 4
24
25 =cut
26
27 package IO::FDPass;
28
29 BEGIN {
30 $VERSION = 0.2;
31
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 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
85 However, windows doesn't support asynchronous file descriptor passing, so
86 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 for any reason (crashes, fails to call C<recv> etc.), the handle
89 will leak, so never do that.
90
91 Also, on windows, the receiving process must have the PROCESS_DUP_HANDLE
92 access right on the sender process for this module to work.
93
94 Cygwin is not supported at the moment, as file descriptor passing in
95 cygwin is not supported, and cannot be rolled on your own as cygwin has no
96 (working) method of opening a handle as fd. That is, it has one, but that
97 one isn't exposed to programs, and only used for stdin/out/err. Sigh.
98
99 =head1 OTHER MODULES
100
101 At the time of this writing, the author of this module was aware of two
102 other file descriptor passing modules on CPAN: L<File::FDPasser> and
103 L<AnyEvent::FDPasser>.
104
105 The former hasn't seen any release for over a decade, isn't 64 bit clean
106 and it's author didn't respond to my mail with the fix, so doesn't work on
107 many 64 bit machines. It does, however, support a number of pre-standard
108 unices, basically everything of relevance at the time it was written.
109
110 The latter seems to have similar support for antique unices, and doesn't
111 seem to suffer from 64 bit bugs, but inexplicably has a large perl part,
112 and requires AnyEvent. Presumably that makes it much more user friendly
113 than this module (skimming the manpage shows that a lot of thought has
114 gone into it, and you are well advised to read it and maybe use it before
115 trying a low-level module such as this one).
116
117 Neither seems to support native win32 perls.
118
119 =head1 AUTHOR
120
121 Marc Lehmann <schmorp@schmorp.de>
122 http://home.schmorp.de/
123
124 =cut
125
126 1
127