blib/lib/Net/Async/TransferFD.pm

filter: Statementstmt 72.7% 72 / 99 Branchbran 28.5% 8 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 72.7% 16 / 22 Podpod 50.0% 5 / 10 totaltotal 62.2% 102 / 164 SCAR 37.2
CC
20
Cov
62%
accept_fds
30.0
send_queued
10.1
on_fh
6.0
CRAP 41.4
↑ 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.001';
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.001
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  
52 # Not sure of a good value for this but 16 seems low enough
53 # to avoid problems, we'll split into multiple packets if
54 # we have more than this number of pending FDs to send.
55 # On linux the /proc/sys/net/core/optmem_max figure may be
56 # relevant here - it's 10240 on one test system.
57 2 ▶use constant MAX_FD_PER_PACKET => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 =head1 METHODS
60  
61 =cut
62  
63 =head2 outgoing_packet
64  
65 Convert a list of handles to a cmsghdr struct suitable for
66 transferring to another process.
67  
68 Returns the encoded cmsghdr struct.
69  
70 =cut
71  
72 sub outgoing_packet {
73 1 ▶ my $self = shift;
Subroutine outgoing_packet: called CC=1 SCAR=0.0
Pod documented
74 # FIXME presumably this 512 figure should really be calculated,
75 # also surely it'd be controllen rather than buflen?
76 1 my $data = pack "i" x @_, map $_->fileno, @_;
77 1 my $hdr = Socket::MsgHdr->new(buflen => length($data));
78 1 $hdr->cmsghdr(SOL_SOCKET, SCM_RIGHTS, $data);
79 1 $hdr
80 }
81  
82 =head2 recv_fds
83  
84 Receive packet containing FDs.
85  
86 Takes a single coderef which will be called with two
87 parameters.
88  
89 Returns $self.
90  
91 =cut
92  
93 sub recv_fds {
94 0 ▶ my $self = shift;
Subroutine recv_fds: not called CC=1 SCAR=6.9
Pod documented
95 0 my $handler = shift;
96 # FIXME more magic numbers
97 0 my $inHdr = Socket::MsgHdr->new(buflen => 512, controllen => 512);
98 $handler->($inHdr, sub {
99 0 ▶ my ($level, $type, $data) = $inHdr->cmsghdr();
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 unpack('i*', $data);
101 0 });
102 0 $self
103 }
104  
105 8 ▶sub handle { shift->{handle} }
Subroutine handle: called CC=1 SCAR=0.0
Pod undocumented
106  
107 =head2 send_queued
108  
109 If we have any FDs queued for sending, bundle them into a packet
110 and send them over. Will close the FDs once the send is complete.
111  
112 Returns $self.
113  
114 =cut
115  
116 sub send_queued {
117 1 ▶ my $self = shift;
Subroutine send_queued: called CC=8 SCAR=23.1
Pod documented
118 # Send a single batch at a time
119 1 ▶ if(@{$self->{pending} || []}) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{"pending"} or [];}) 1 0 1
$self->{"pending"} or [] 0 1 1
120 1 my @chunk = splice @{$self->{pending}}, 0, MAX_FD_PER_PACKET;
121 1 my @fd = map $_->[0], @chunk;
122 1 my @future = map $_->[1], @chunk;
123 1 $self->debug_printf("Sending %d FDs - %s", scalar(@fd), join ',', map $_->fileno, @fd);
124 1 sendmsg $self->handle->write_handle, $self->outgoing_packet(
125 @fd
126 );
127 1 $_->close for @fd;
128 1 $_->done for @future;
129 }
130 # If we have any leftovers, we hope to be called next time around
131 1 ▶ $self->handle->want_writeready(0) if $self->handle && ! @{$self->{pending}||[]};
Branch: 2 branches
BranchTrueFalseTotal
if $self->handle and not @{$self->{"pending"} or [];} 0 1 1
$self->{"pending"} or [] 0 0 0
Condition33%1/3
$self->handle and not @{$self->{"pending"} or [];}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->handle and not @{$self->{"pending"} or [];}
$self->handle not @{$self->{"pending"} or [];}
Truth table33%1/3
A and not(B)
A= $self->handleB= not @{$self->{"pending"} or [];}
ABresult
0X0
100
111
132 1 $self
133 }
134  
135 =head2 read_pending
136  
137 Reads any pending messages, converting to FDs
138 as appropriate and calling the on_fh callback.
139  
140 Returns $self.
141  
142 =cut
143  
144 sub read_pending {
145 0 ▶ my $self = shift;
Subroutine read_pending: not called CC=1 SCAR=6.9
Pod documented
146 0 $self->recv_fds($self->curry::accept_fds);
147 }
148  
149 sub accept_fds {
150 0 ▶ my ($self, $hdr, $code) = @_;
Subroutine accept_fds: not called CC=5 SCAR=34.0
Pod undocumented
151 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
152 or $self->debug_printf("Failed to recvmsg - %s", $!);
153 0 ▶ unless(length $hdr->{control}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $hdr->{"control"}) 0 0 0
154 0 $self->debug_printf("No control data, remote has probably gone away - closing");
155 0 $self->handle->want_readready(0);
156 0 return;
157 }
158  
159 0 my @fd = $code->();
160 0 foreach my $fileno (@fd) {
161 0 $self->debug_printf("Opening handle for %d", $fileno);
162 0 ▶ open my $fh, '+<&=', $fileno or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<&=", $fileno or die $! 0 0 0
163 0 $self->on_fh($fh);
164 }
165 }
166  
167 sub on_fh {
168 0 ▶ my ($self, $fh) = @_;
Subroutine on_fh: not called CC=2 SCAR=17.9
Pod undocumented
169 0 ▶ $self->{on_fh}->($fh) if $self->{on_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_fh"} 0 0 0
170 0 $self
171 }
172  
173 sub configure {
174 1 ▶ my $self = shift;
Subroutine configure: called CC=4 SCAR=14.3
Pod documented
175 1 my %args = @_;
176  
177 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
178  
179 1 ▶ if(exists $args{handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"handle"}) 1 0 1
180 1 my $h = delete $args{handle};
181 1 ▶ if($h->isa('IO::Async::Handle')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->isa('IO::Async::Handle')) { } 1 0 1
182 1 $self->{handle} = $h;
183 1 $self->handle->configure(
184 on_write_ready => $self->curry::weak::send_queued,
185 on_read_ready => $self->curry::weak::read_pending,
186 );
187 } else {
188 $self->add_child(
189 0 $self->{handle} = IO::Async::Handle->new(
190 handle => $h,
191 on_write_ready => $self->curry::weak::send_queued,
192 on_read_ready => $self->curry::weak::read_pending,
193 )
194 );
195 }
196 1 $self->handle->want_writeready(0);
197 1 $self->handle->want_readready(1);
198 };
199 1 $self->SUPER::configure(%args);
200 }
201  
202 sub send {
203 1 ▶ my $self = shift;
Subroutine send: called CC=3 SCAR=11.0
Pod undocumented
204 1 my @future;
205 1 for (@_) {
206 1 push @{$self->{pending}}, [
207 $_,
208 my $f = $self->loop->new_future
209 ];
210 1 push @future, $f;
211 }
212 1 ▶ $self->handle->want_writeready(1) if $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle 1 0 1
213 1 my $f;
214 1 ▶ $f = Future->wait_all(@future)->on_ready(sub { weaken $f });
Subroutine __ANON__: called CC=1 SCAR=0.0
215 1 $f
216 }
217  
218 sub _remove_from_loop {
219 0 ▶ my ($self) = @_;
Subroutine _remove_from_loop: not called CC=2 SCAR=17.9
220 0 ▶ (delete $self->{handle})->close if $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle 0 0 0
221 }
222  
223 sub stop {
224 1 ▶ my ($self) = @_;
Subroutine stop: called CC=2 SCAR=7.2
Pod undocumented
225 1 ▶ (delete $self->{handle})->close if $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle 1 0 1
226 }
227  
228  
229 2 1;
230  
231 __END__
↑ Summary