blib/lib/Net/Async/TransferFD.pm

filter: Statementstmt 72.7% 72 / 99 Branchbran 31.8% 7 / 22 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 72.7% 16 / 22 Podpod 80.0% 8 / 10 totaltotal 64.0% 105 / 164 CC 19 SCAR 19.9
CC
19
Cov
63%
accept_fds
30.0
send_queued
10.1
on_fh
6.0
CRAP 7.3
↑ Summary
1 package Net::Async::TransferFD;
2 # ABSTRACT: send handles between processes
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.002';
9  
10 =head1 NAME
11  
12 Net::Async::TransferFD - support for transferring handles between
13 processes via socketpair
14  
15 =head1 VERSION
16  
17 version 0.002
18  
19 =head1 SYNOPSIS
20  
21 use feature qw(say);
22 my $loop = IO::Async::Loop->new;
23 my $proc = IO::Async::Process->new(
24 code => sub { ... },
25 fd3 => { via => 'socketpair' },
26 );
27 $loop->add(my $control = Net::Async::TransferFD->new(
28 handle => $proc->fd(3),
29 on_fh => sub {
30 my $h = shift;
31 say "New handle $h - " . join '', <$h>;
32 }
33 ));
34 $control->send(\*STDIN);
35  
36 =head1 DESCRIPTION
37  
38 Uses SCM_RIGHTS to pass an open handle from one process to another.
39 Typically used to hand a network socket off to another process, for
40 example an accept loop in one process dispatching incoming connections
41 to other active processes.
42  
43 =cut
44  
45 2 ▶use IO::Async::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 2 ▶use Socket::MsgHdr qw(sendmsg recvmsg);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC SOL_SOCKET SCM_RIGHTS);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use Variable::Disposition qw(retain_future);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # Not sure of a good value for this but 16 seems low enough
54 # to avoid problems, we'll split into multiple packets if
55 # we have more than this number of pending FDs to send.
56 # On linux the /proc/sys/net/core/optmem_max figure may be
57 # relevant here - it's 10240 on one test system.
58 2 ▶use constant MAX_FD_PER_PACKET => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 =head1 METHODS
61  
62 =cut
63  
64 =head2 outgoing_packet
65  
66 Convert a list of handles to a cmsghdr struct suitable for
67 transferring to another process.
68  
69 Returns the encoded cmsghdr struct.
70  
71 =cut
72  
73 1 ▶sub outgoing_packet {
Subroutine outgoing_packet: called CC=1 SCAR=0.0
Pod documented
74 1 my $self = shift;
75 # FIXME presumably this 512 figure should really be calculated,
76 # also surely it'd be controllen rather than buflen?
77 1 my $data = pack "i" x @_, map $_->fileno, @_;
78 1 my $hdr = Socket::MsgHdr->new(buflen => length($data));
79 1 $hdr->cmsghdr(SOL_SOCKET, SCM_RIGHTS, $data);
80 1 $hdr
81 }
82  
83 =head2 recv_fds
84  
85 Receive packet containing FDs.
86  
87 Takes a single coderef which will be called with two
88 parameters.
89  
90 Returns $self.
91  
92 =cut
93  
94 0 ▶sub recv_fds {
Subroutine recv_fds: not called CC=1 SCAR=6.9
Pod documented
95 0 my $self = shift;
96 0 my $handler = shift;
97 # FIXME more magic numbers
98 0 my $inHdr = Socket::MsgHdr->new(buflen => 512, controllen => 512);
99 0 ▶ $handler->($inHdr, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 my ($level, $type, $data) = $inHdr->cmsghdr();
101 0 unpack('i*', $data);
102 0 });
103 0 $self
104 }
105  
106 8 ▶sub handle { shift->{handle} }
Subroutine handle: called CC=1 SCAR=0.0
Pod undocumented
107  
108 =head2 send_queued
109  
110 If we have any FDs queued for sending, bundle them into a packet
111 and send them over. Will close the FDs once the send is complete.
112  
113 Returns $self.
114  
115 =cut
116  
117 1 ▶sub send_queued {
Subroutine send_queued: called CC=8 SCAR=23.1
Pod documented
118 1 my $self = shift;
119 # Send a single batch at a time
120 1 ▶ if(@{$self->{pending} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"pending"} or [];}) 1 0 1
Condition50%1/2
$self->{'pending'} || []
l!l
10
MC/DC0%0/1
$self->{'pending'} || []
$self->{'pending'}
Truth table50%1/2
A
A= $self->{'pending'}
Aresult
11
00
121 1 my @chunk = splice @{$self->{pending}}, 0, MAX_FD_PER_PACKET;
122 1 my @fd = map $_->[0], @chunk;
123 1 my @future = map $_->[1], @chunk;
124 1 $self->debug_printf("Sending %d FDs - %s", scalar(@fd), join ',', map $_->fileno, @fd);
125 1 sendmsg $self->handle->write_handle, $self->outgoing_packet(
126 @fd
127 );
128 1 $_->close for @fd;
129 1 $_->done for @future;
130 }
131 # If we have any leftovers, we hope to be called next time around
132 0 ▶ $self->handle->want_writeready(0) if $self->handle && ! @{$self->{pending}||[]};
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle and not @{$self->{"pending"} or [];} 0 1 1
Condition20%1/5
$self->handle and not @{$self->{"pending"} or [];}
!ll&&!rl&&r
100
$self->{'pending'} || []
l!l
00
MC/DC0%0/2
$self->handle and not @{$self->{"pending"} or [];}
$self->handle not @{$self->{"pending"} or [];}
MC/DC0%0/1
$self->{'pending'} || []
$self->{'pending'}
Truth table33%1/3
A and not(B)
A= $self->handleB= not @{$self->{"pending"} or [];}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->{'pending'}
Aresult
11
00
133 1 $self
134 }
135  
136 =head2 read_pending
137  
138 Reads any pending messages, converting to FDs
139 as appropriate and calling the on_fh callback.
140  
141 Returns $self.
142  
143 =cut
144  
145 0 ▶sub read_pending {
Subroutine read_pending: not called CC=1 SCAR=6.9
Pod documented
146 0 my $self = shift;
147 0 $self->recv_fds($self->curry::accept_fds);
148 }
149  
150 =head2 accept_fds
151  
152 Attempts to accept the given FDs from the remote.
153  
154 Will call L</on_fh> for each received file descriptor after reopening.
155  
156 =cut
157  
158 0 ▶sub accept_fds {
Subroutine accept_fds: not called CC=5 SCAR=34.0
Pod documented
159 0 my ($self, $hdr, $code) = @_;
160 0 ▶ defined(recvmsg $self->handle->write_handle, $hdr, 0)
Branch: 1 branch
BranchTrueFalseTotal
defined &recvmsg(scalar $self->handle->write_handle, $hdr, 0) or $self->debug_printf("Failed to recvmsg - %s", $!) 0 0 0
161 or $self->debug_printf("Failed to recvmsg - %s", $!);
162 0 ▶ unless(length $hdr->{control}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $hdr->{"control"}) 0 0 0
163 0 $self->debug_printf("No control data, remote has probably gone away - closing");
164 0 $self->handle->want_readready(0);
165 0 return;
166 }
167  
168 0 my @fd = $code->();
169 0 foreach my $fileno (@fd) {
170 0 $self->debug_printf("Opening handle for %d", $fileno);
171 0 ▶ open my $fh, '+<&=', $fileno or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<&=", $fileno or die $! 0 0 0
172 0 $self->on_fh($fh);
173 }
174 }
175  
176 =head2 on_fh
177  
178 Calls the configured filehandle method if provided (via L</configure>(C<on_fh>)).
179 =cut
180  
181 0 ▶sub on_fh {
Subroutine on_fh: not called CC=2 SCAR=17.9
Pod documented
182 0 my ($self, $fh) = @_;
183 0 ▶ $self->{on_fh}->($fh) if $self->{on_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_fh"} 0 0 0
184 0 $self
185 }
186  
187 1 ▶sub configure {
Subroutine configure: called CC=4 SCAR=14.3
Pod documented
188 1 my $self = shift;
189 1 my %args = @_;
190  
191 1 ▶ $self->{on_fh} = delete $args{on_fh} if exists $args{on_fh};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_fh"} 1 0 1
192  
193 1 ▶ if(exists $args{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"handle"}) 1 0 1
194 1 my $h = delete $args{handle};
195 1 ▶ if($h->isa('IO::Async::Handle')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->isa('IO::Async::Handle')) { } 1 0 1
196 1 $self->{handle} = $h;
197 1 $self->handle->configure(
198 on_write_ready => $self->curry::weak::send_queued,
199 on_read_ready => $self->curry::weak::read_pending,
200 );
201 } else {
202 $self->add_child(
203 0 $self->{handle} = IO::Async::Handle->new(
204 handle => $h,
205 on_write_ready => $self->curry::weak::send_queued,
206 on_read_ready => $self->curry::weak::read_pending,
207 )
208 );
209 }
210 1 $self->handle->want_writeready(0);
211 1 $self->handle->want_readready(1);
212 };
213 1 $self->SUPER::configure(%args);
214 }
215  
216 =head2 send
217  
218 Sends the given FDs to the remote, returning a L<Future> which will resolve once
219 all FDs have been transferred.
220  
221 =cut
222  
223 1 ▶sub send {
Subroutine send: called CC=3 SCAR=11.0
Pod documented
224 1 my $self = shift;
225 1 my @future;
226 1 for (@_) {
227 1 push @{$self->{pending}}, [
228 $_,
229 my $f = $self->loop->new_future
230 ];
231 1 push @future, $f;
232 }
233 1 ▶ $self->handle->want_writeready(1) if $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle 1 0 1
234 1 retain_future(
235 Future->wait_all(@future)
236 )
237 }
238  
239 0 ▶sub _remove_from_loop {
Subroutine _remove_from_loop: not called CC=1 SCAR=6.9
240 0 my ($self) = @_;
241 0 $self->stop;
242 }
243  
244 1 ▶sub stop {
Subroutine stop: called CC=2 SCAR=7.2
Pod undocumented
245 1 my ($self) = @_;
246 1 ▶ (delete $self->{handle})->close if $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle 1 0 1
247 }
248  
249  
250 2 1;
251  
252 __END__
↑ Summary