blib/lib/Ereshkigal/Client.pm

filter: Statementstmt 89.5% 180 / 201 Branchbran 69.3% 68 / 98 Conditioncond 47.8% 22 / 46 MC/DCmcdc 26.9% 7 / 26 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 76.0% 298 / 392 CC 70 SCAR 35.6
CC
70
Cov
78%
call_many
63.4
_authenticate
24.8
call
9.0
CRAP 35.0
« blib/lib/Ereshkigal/App/Command/start.pm ↑ Summary blib/lib/Ereshkigal/LogDrek.pm »
1 package Ereshkigal::Client;
2  
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use IO::Select ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use IO::Socket::UNIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use JSON::MaybeXS qw( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Ereshkigal::Client - Small blocking JSON over unix socket client for Ereshkigal.
15  
16 =head1 VERSION
17  
18 Version 0.0.1
19  
20 =cut
21  
22 6 our $VERSION = '0.0.1';
23  
24 =head1 SYNOPSIS
25  
26 use Ereshkigal::Client;
27  
28 my $client = Ereshkigal::Client->new( socket => '/var/run/ereshkigal/socket' );
29  
30 # returns the raw response hash, {status=>..., ...}
31 my $response = $client->call( 'status' );
32  
33 # dies unless status is ok, returning the result
34 my $result = $client->call_ok( 'ban', { ips => ['1.2.3.4'] } );
35  
36 =head1 DESCRIPTION
37  
38 Connects to a unix socket speaking the newline delimited JSON protocol of
39 L<POE::Component::Server::JSONUnix>, sends a single request, and reads back
40 the response. Used by the C<ereshkigal> CLI for talking to the manager and by
41 the manager for talking to kur instances.
42  
43 =head1 METHODS
44  
45 =head2 new
46  
47 Initiates the object. Will die on errors.
48  
49 - socket :: Path of the unix socket to connect to. Must be specified.
50 Default :: undef
51  
52 - timeout :: Timeout in seconds for a call. 0 means no timeout.
53 Default :: 30
54  
55 =cut
56  
57 41 ▶sub new {
Subroutine new: called CC=5 SCAR=16.4
Pod documented
58 41 my ( $blank, %opts ) = @_;
59  
60 41 ▶ if ( !defined( $opts{socket} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"socket"}) 1 40 41
61 1 die('No socket specified');
62 }
63  
64 # fractional seconds would silently truncate going through alarm
65 40 ▶ if ( defined( $opts{timeout} ) && $opts{timeout} !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"timeout"} and not $opts{"timeout"} =~ /^\d+$/) 0 40 40
Condition66%2/3
defined $opts{"timeout"} and not $opts{"timeout"} =~ /^\d+$/
!ll&&!rl&&r
3820
MC/DC0%0/2
defined $opts{"timeout"} and not $opts{"timeout"} =~ /^\d+$/
defined $opts{"timeout"} not $opts{"timeout"} =~ /^\d+$/
Truth table66%2/3
A and not(B)
A= defined $opts{"timeout"}B= not $opts{"timeout"} =~ /^\d+$/
ABresult
0X0
100
111
66 0 die('timeout must be a non-negative integer');
67 }
68  
69 my $self = {
70 socket => $opts{socket},
71 40 ▶ timeout => defined( $opts{timeout} ) ? $opts{timeout} : 30,
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'timeout'} ? : 2 38 40
72 };
73 40 bless $self;
74  
75 40 return $self;
76 } ## end sub new
77  
78 =head2 call
79  
80 Sends a single request and returns the decoded response hash. Will die on
81 connect failure, timeout, or an undecodable response.
82  
83 my $response = $client->call( $command, $args );
84  
85 C<$args>, if defined, is sent as the args value of the request.
86  
87 If the server answers with the L<POE::Component::Server::JSONUnix>
88 authentication required error, the unix ownership challenge is completed
89 transparently on the same connection and the request is resent, so no
90 special handling is needed for talking to a manager with enable_auth on.
91 The timeout wraps the whole exchange.
92  
93 =cut
94  
95 46 ▶sub call {
Subroutine call: called CC=9 SCAR=22.0
Pod documented
96 46 my ( $self, $command, $args ) = @_;
97  
98 46 ▶ if ( !defined($command) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 1 45 46
99 1 die('No command specified');
100 }
101  
102 45 my $response;
103 45 eval {
104 # a write to a peer that died would otherwise raise SIGPIPE and kill
105 # the whole process... ignored, the write fails with EPIPE and that
106 # surfaces through the normal error paths
107 45 local $SIG{PIPE} = 'IGNORE';
108 45 ▶ local $SIG{ALRM} = sub { die( 'timed out after ' . $self->{timeout} . " seconds\n" ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
109 45 alarm( $self->{timeout} );
110  
111 my $sock = IO::Socket::UNIX->new(
112 'Type' => IO::Socket::UNIX::SOCK_STREAM(),
113 'Peer' => $self->{socket},
114 45 ▶ ) || die( 'Failed to connect to "' . $self->{socket} . '"... ' . $! . "\n" );
Condition100%2/2
'IO::Socket::UNIX'->new('Type', 1, 'Peer', $self->{'socket'}) || die('Failed to connect to "' . $self->{'socket'} . '"... ' . $! . "\n")
l!l
3411
MC/DC100%1/1
'IO::Socket::UNIX'->new('Type', 1, 'Peer', $self->{'socket'}) || die('Failed to connect to "' . $self->{'socket'} . '"... ' . $! . "\n")
'IO::Socket::UNIX'->new('Type', 1, 'Peer', $self->{'socket'})
Truth table100%2/2
A
A= 'IO::Socket::UNIX'->new('Type', 1, 'Peer', $self->{'socket'})
Aresult
11
00
115  
116 34 my $request = { 'command' => $command };
117 34 ▶ if ( defined($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) 18 16 34
118 18 $request->{args} = $args;
119 }
120  
121 34 $response = $self->_send_request( $sock, $request );
122  
123 # auth state is per connection, so complete the ownership challenge
124 # on this same connection and resend
125 #
126 # matched against the error prose as the JSONUnix auth_required gate
127 # answers with no code field... only its permission policy denials
128 # and Ereshkigal's own _authorize carry a machine readable code, and
129 # neither of those is the response being caught here
130 31 ▶ if ( defined( $response->{status} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->{"status"} and $response->{"status"} eq "error" and defined $response->{"error"} and $response->{"error"} =~ /^authentication required/) 2 29 31
Condition77%7/9
defined $response->{"status"} and $response->{"status"} eq "error" and defined $response->{"error"} and $response->{"error"} =~ /^authentication required/
!ll&&!rl&&r
2632
defined $response->{"status"} and $response->{"status"} eq "error" and defined $response->{"error"}
!ll&&!rl&&r
2605
defined $response->{"status"} and $response->{"status"} eq "error"
!ll&&!rl&&r
0265
MC/DC50%2/4
defined $response->{"status"} and $response->{"status"} eq "error" and defined $response->{"error"} and $response->{"error"} =~ /^authentication required/
defined $response->{"status"} $response->{"status"} eq "error" defined $response->{"error"} $response->{"error"} =~ /^authentication required/
Truth table60%3/5
A and B and C and D
A= defined $response->{"status"}B= $response->{"status"} eq "error"C= defined $response->{"error"}D= $response->{"error"} =~ /^authentication required/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
131 && $response->{status} eq 'error'
132 && defined( $response->{error} )
133 && $response->{error} =~ /^authentication required/ )
134 {
135 2 $self->_authenticate($sock);
136 1 $response = $self->_send_request( $sock, $request );
137 }
138  
139 30 close($sock);
140 30 alarm(0);
141 };
142 45 my $call_error = $@;
143 45 alarm(0);
144 45 ▶ if ($call_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_error) 15 30 45
145 15 die($call_error);
146 }
147  
148 30 return $response;
149 } ## end sub call
150  
151 # Sends one request over an already connected socket and reads back the one
152 # line answer to it, which is the whole of the newline delimited JSON
153 # protocol POE::Component::Server::JSONUnix speaks. Used by call for both the
154 # initial request and the resend after an auth challenge, and by _authenticate
155 # for the challenge itself, so it deliberately does no connecting, no timing
156 # out, and no auth handling of its own... the caller owns all three.
157 #
158 # The request is JSON encoded, a newline appended, and written to the socket;
159 # the answer is read to the first newline and JSON decoded.
160 #
161 # Args, both required and positional...
162 #
163 # $sock :: An open IO::Socket::UNIX already connected to the server.
164 # Left open on return, as the auth challenge and the resend
165 # that follows it have to happen on this same connection.
166 # $request :: A hash ref of the request to send, which the server expects
167 # to carry a 'command' key naming the command and, optionally,
168 # a 'args' key holding that command's arguments hash ref. It
169 # is encoded as given, so anything JSON::MaybeXS cannot
170 # encode dies here.
171 #
172 # Returns the decoded response as a hash ref. The server's own shape is
173 # { status => 'ok', result => ... } on success and
174 # { status => 'error', error => '...' } on failure, but no checking of that
175 # is done here... an error response comes back as a plain hash ref just like a
176 # successful one, for the caller to judge. call_ok is what turns an error
177 # status into a die.
178 #
179 # Dies, with a trailing newline so the message reads cleanly when it reaches
180 # a user, if the socket gives EOF rather than a line, if the line will not
181 # JSON decode, or if it decodes to anything other than a hash ref.
182 #
183 # my $response = $self->_send_request( $sock, { 'command' => 'status' } );
184 # # $response is { status => 'ok', result => { pid => 42, ... } }
185 #
186 # my $response = $self->_send_request(
187 # $sock,
188 # { 'command' => 'ban', 'args' => { 'ips' => ['1.2.3.4'] } }
189 # );
190 39 ▶sub _send_request {
Subroutine _send_request: called CC=3 SCAR=11.1
191 39 my ( $self, $sock, $request ) = @_;
192  
193 39 print $sock encode_json($request) . "\n";
194  
195 39 my $line = <$sock>;
196 38 ▶ if ( !defined($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 38 38
197 0 die( 'No response read from "' . $self->{socket} . '"' . "\n" );
198 }
199  
200 38 my $response = decode_json($line);
201 37 ▶ if ( ref($response) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response ne "HASH") 1 36 37
202 1 die( 'Response is not a JSON object' . "\n" );
203 }
204  
205 36 return $response;
206 } ## end sub _send_request
207  
208 # Completes the POE::Component::Server::JSONUnix unix ownership challenge on
209 # an already connected socket, which is how a manager with enable_auth on
210 # learns who is talking to it. Called by call when a request comes back
211 # refused with the authentication required error, after which that request is
212 # resent on the same connection.
213 #
214 # The challenge runs in two commands. auth_start hands back a random cookie
215 # and a temp dir the server can see; the cookie is written to a fresh file in
216 # that dir, which by being owned by this process is the actual proof of
217 # identity; auth_verify is then pointed at that file, and the server stats it
218 # to learn the uid and username. The cookie file is removed either way, the
219 # server doing it on success and this doing it otherwise, so a rejected
220 # challenge leaves nothing behind.
221 #
222 # Args, required and positional...
223 #
224 # $sock :: An open IO::Socket::UNIX already connected to the server. Auth
225 # state is per connection, so this must be the same socket the
226 # refused request went over and the one the resend will go over.
227 # Left open on return.
228 #
229 # Returns nothing meaningful, an empty return. Success is the absence of a
230 # die... on return the connection is authenticated and the caller may resend.
231 #
232 # Dies, with a trailing newline, if auth_start answers anything but ok or
233 # without both a cookie and a temp_dir, if the cookie file cannot be
234 # written (a full or unwritable temp dir surfaces here rather than as a
235 # confusing auth_verify failure later), or if auth_verify answers anything
236 # but ok, which is what a genuine refusal looks like.
237 #
238 # eval { $self->_authenticate($sock); };
239 # if ($@) { die( 'could not authenticate... ' . $@ ); }
240 # my $response = $self->_send_request( $sock, $request );
241 2 ▶sub _authenticate {
Subroutine _authenticate: called CC=13 SCAR=32.1
242 2 my ( $self, $sock ) = @_;
243  
244 2 my $start = $self->_send_request( $sock, { 'command' => 'auth_start' } );
245 2 ▶ if ( !defined( $start->{status} ) || $start->{status} ne 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $start->{"status"} or $start->{"status"} ne "ok") 0 2 2
Condition33%1/3
not defined $start->{"status"} or $start->{"status"} ne "ok"
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $start->{"status"} or $start->{"status"} ne "ok"
not defined $start->{"status"} $start->{"status"} ne "ok"
Truth table33%1/3
not(A) or B
A= not defined $start->{"status"}B= $start->{"status"} ne "ok"
ABresult
1X1
011
000
246 0 ▶ die( 'auth_start failed... ' . ( defined( $start->{error} ) ? $start->{error} : 'unknown error' ) . "\n" );
Branch: 1 branch
BranchTrueFalseTotal
defined $start->{'error'} ? : 0 0 0
247 }
248 2 my $cookie = $start->{result}{cookie};
249 2 my $temp_dir = $start->{result}{temp_dir};
250 2 ▶ if ( !defined($cookie) || !defined($temp_dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cookie and defined $temp_dir) 0 2 2
Condition33%1/3
defined $cookie and defined $temp_dir
!ll&&!rl&&r
002
MC/DC0%0/2
defined $cookie and defined $temp_dir
defined $cookie defined $temp_dir
Truth table33%1/3
A and B
A= defined $cookieB= defined $temp_dir
ABresult
0X0
100
111
251 0 die( 'auth_start did not return a cookie and temp_dir' . "\n" );
252 }
253  
254 2 my ( $cookie_fh, $cookie_file )
255 = File::Temp::tempfile( 'ereshkigal-auth-XXXXXXXX', 'DIR' => $temp_dir, 'UNLINK' => 0 );
256 # checked so a full filesystem or unwritable temp_dir surfaces here as
257 # the actual cause rather than as a confusing auth_verify failure
258 2 ▶ if ( !print( $cookie_fh $cookie ) || !close($cookie_fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $cookie_fh $cookie and close $cookie_fh) 0 2 2
Condition33%1/3
print $cookie_fh $cookie and close $cookie_fh
!ll&&!rl&&r
002
MC/DC0%0/2
print $cookie_fh $cookie and close $cookie_fh
print $cookie_fh $cookie close $cookie_fh
Truth table33%1/3
A and B
A= print $cookie_fh $cookieB= close $cookie_fh
ABresult
0X0
100
111
259 0 my $write_error = $!;
260 0 unlink($cookie_file);
261 0 die( 'Failed to write the auth cookie to "' . $cookie_file . '"... ' . $write_error . "\n" );
262 }
263  
264 2 my $verify;
265 2 eval {
266 2 $verify
267 = $self->_send_request( $sock, { 'command' => 'auth_verify', 'args' => { 'path' => $cookie_file } } );
268 };
269 2 my $verify_error = $@;
270 # the server removes it on success, but make sure it is gone either way
271 2 ▶ unlink($cookie_file) if -e $cookie_file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $cookie_file 2 0 2
272 2 ▶ if ($verify_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verify_error) 0 2 2
273 0 die($verify_error);
274 }
275 2 ▶ if ( !defined( $verify->{status} ) || $verify->{status} ne 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $verify->{"status"} or $verify->{"status"} ne "ok") 1 1 2
Condition66%2/3
not defined $verify->{"status"} or $verify->{"status"} ne "ok"
l!l&&r!l&&!r
011
MC/DC50%1/2
not defined $verify->{"status"} or $verify->{"status"} ne "ok"
not defined $verify->{"status"} $verify->{"status"} ne "ok"
Truth table66%2/3
not(A) or B
A= not defined $verify->{"status"}B= $verify->{"status"} ne "ok"
ABresult
1X1
011
000
276 1 ▶ die( 'auth_verify failed... ' . ( defined( $verify->{error} ) ? $verify->{error} : 'unknown error' ) . "\n" );
Branch: 1 branch
BranchTrueFalseTotal
defined $verify->{'error'} ? : 1 0 1
277 }
278  
279 1 return;
280 } ## end sub _authenticate
281  
282 =head2 call_many
283  
284 Sends the same single request to multiple sockets concurrently. This is for
285 the manager fanning a command out to its kurs, which is why it is a class
286 method taking a whole hash of sockets rather than using a per socket object.
287  
288 my $per_name = Ereshkigal::Client->call_many(
289 sockets => { sshd => '/var/run/ereshkigal/kur/sshd.sock' },
290 command => 'ban',
291 args => { ips => [ '1.2.3.4' ] },
292 timeout => 30,
293 );
294  
295 - sockets :: A hash of name to unix socket path. Must be specified.
296 Default :: undef
297  
298 - command :: The command to send to every socket. Must be specified.
299 Default :: undef
300  
301 - args :: Sent as the args value of the request, if defined.
302 Default :: undef
303  
304 - timeout :: Timeout in seconds for the whole fan out, bounding it as a
305 whole rather than each socket individually, so the wall time is
306 the slowest socket capped at one timeout instead of the sum. This
307 covers the connects as well, each of which is bounded by an even
308 share of what is left of it, so a socket whose listener never
309 accepts cannot hang the fan out or starve the rest of it. 0
310 means no timeout.
311 Default :: 30
312  
313 The return is a hash keyed the same as sockets, each value being either
314 C<{ result =E<gt> ... }> for a response with an ok status or
315 C<{ error =E<gt> "..." }> for anything else... connect failure, EOF, an
316 undecodable response, an error status response, or the deadline passing. A
317 failure with one socket never disturbs the others.
318  
319 Unlike L</call>, no authentication challenge handling happens... kur
320 sockets never challenge and this is not meant for talking to the manager
321 socket.
322  
323 =cut
324  
325 12 ▶sub call_many {
Subroutine call_many: called CC=40 SCAR=41.5
Pod documented
326 12 my ( $blank, %opts ) = @_;
327  
328 12 ▶ if ( ref( $opts{sockets} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"sockets"} ne "HASH") 1 11 12
329 1 die('sockets must be a hash of name to socket path');
330 }
331 11 ▶ if ( !defined( $opts{command} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"command"}) 1 10 11
332 1 die('No command specified');
333 }
334 10 ▶ my $timeout = defined( $opts{timeout} ) ? $opts{timeout} : 30;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'timeout'} ? : 3 7 10
335  
336 # a write to a peer that died would otherwise raise SIGPIPE and kill
337 # the whole process... ignored, the write fails with EPIPE and that
338 # lands in that socket's error answer like any other failure
339 10 local $SIG{PIPE} = 'IGNORE';
340  
341 10 my $request = { 'command' => $opts{command} };
342 10 ▶ if ( defined( $opts{args} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"args"}) 1 9 10
343 1 $request->{args} = $opts{args};
344 }
345 10 my $request_line = encode_json($request) . "\n";
346  
347 10 my $answers = {};
348  
349 # one shared deadline over the whole fan out, the connects included... a
350 # timeout of 0 means no deadline at all
351 10 ▶ my $deadline = $timeout ? time + $timeout : undef;
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 10 0 10
352  
353 # each connection is a little state machine... writing till the request
354 # line is fully sent, then reading till a newline shows up
355 10 my %conns;
356 10 my $writers = IO::Select->new;
357 10 my $readers = IO::Select->new;
358 10 my @names = sort( keys( %{ $opts{sockets} } ) );
359 10 my $left_to_try = scalar(@names);
360 10 foreach my $name (@names) {
361 # a unix stream connect to a live listener normally completes at once
362 # and a dead socket refuses at once, but a listener whose accept queue
363 # has filled blocks, so each connect is bounded rather than being left
364 # to wedge the whole fan out... the bound is an even share of what is
365 # left of the deadline, so one wedged listener cannot eat the budget
366 # the rest need, and never less than a second as alarm(0) would mean
367 # no alarm at all
368 16 my $remaining;
369 16 ▶ if ( defined($deadline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deadline) 16 0 16
370 16 my $budget = $deadline - time;
371 16 ▶ if ( $budget <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($budget <= 0) 0 16 16
372 0 $left_to_try--;
373 0 $answers->{$name} = { 'error' => 'Timed out before connecting to "' . $opts{sockets}{$name} . '"' };
374 0 next;
375 }
376 16 ▶ $remaining = int( $budget / $left_to_try ) || 1;
Condition100%2/2
int $budget / $left_to_try || 1
l!l
151
MC/DC100%1/1
int $budget / $left_to_try || 1
int $budget / $left_to_try
Truth table100%2/2
A
A= int $budget / $left_to_try
Aresult
11
00
377 }
378 16 $left_to_try--;
379  
380 16 my $sock;
381 my $connect_error;
382 eval {
383 16 ▶ local $SIG{ALRM} = sub { die("timed out\n"); };
Subroutine __ANON__: called CC=1 SCAR=0.0
384 16 ▶ alarm($remaining) if defined($remaining);
Branch: 1 branch
BranchTrueFalseTotal
if defined $remaining 16 0 16
385 $sock = IO::Socket::UNIX->new(
386 'Type' => IO::Socket::UNIX::SOCK_STREAM(),
387 16 'Peer' => $opts{sockets}{$name},
388 );
389 15 ▶ $connect_error = $! if !$sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 1 14 15
390 15 alarm(0);
391 15 1;
392 16 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"ALRM"} = sub { die "timed out\n"; } ; alarm $remaining if defined $remaining; $sock = "IO::Socket::UNIX"->new("Type", 1, "Peer", $opts{"sockets"}{$name}); $connect_error = $! unless $sock; alarm 0; 1 } } or do { alarm 0; $sock = undef; $connect_error = "timed out" } 15 1 16
393 1 alarm(0);
394 1 $sock = undef;
395 1 $connect_error = 'timed out';
396 };
397 16 ▶ if ( !$sock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 2 14 16
398 $answers->{$name}
399 2 = { 'error' => 'Failed to connect to "' . $opts{sockets}{$name} . '"... ' . $connect_error };
400 2 next;
401 }
402 14 $sock->blocking(0);
403 14 $conns{$sock} = { 'name' => $name, 'sock' => $sock, 'sent' => 0, 'buf' => '' };
404 14 $writers->add($sock);
405 } ## end foreach my $name (@names)
406  
407 11 ▶ my $finish = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
408 11 my ( $conn, $answer ) = @_;
409 11 $writers->remove( $conn->{sock} );
410 11 $readers->remove( $conn->{sock} );
411 11 close( $conn->{sock} );
412 11 delete( $conns{ $conn->{sock} } );
413 11 $answers->{ $conn->{name} } = $answer;
414 10 };
415  
416 # one select loop over all the connections against that same deadline...
417 # no SIGALRM here as one alarm can't supervise N conversations, which is
418 # why only the sequential connects above use one
419 10 ▶ while (%conns) {
Branch: 1 branch
BranchTrueFalseTotal
if (%conns) 24 8 32
420 24 my $remaining;
421 24 ▶ if ( defined($deadline) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deadline) 24 0 24
422 24 $remaining = $deadline - time;
423 24 ▶ last if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 2 22 24
424 }
425  
426 22 my ( $readable, $writable ) = IO::Select->select( $readers, $writers, undef, $remaining );
427  
428 22 ▶ foreach my $sock ( defined($writable) ? @{$writable} : () ) {
Branch: 1 branch
BranchTrueFalseTotal
defined $writable ? : 20 2 22
429 14 my $conn = $conns{$sock};
430 14 ▶ next if !defined($conn);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 0 14 14
431 14 my $wrote = syswrite( $sock, $request_line, length($request_line) - $conn->{sent}, $conn->{sent} );
432 14 ▶ if ( !defined($wrote) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wrote) 0 14 14
433 0 ▶ next if $!{EAGAIN} || $!{EWOULDBLOCK} || $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} or $!{"EINTR"} 0 0 0
Condition0%0/6
$!{"EAGAIN"} or $!{"EWOULDBLOCK"} or $!{"EINTR"}
l!l&&r!l&&!r
000
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"} or $!{"EINTR"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"} $!{"EINTR"}
Truth table0%0/4
A or B or C
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}C= $!{"EINTR"}
ABCresult
1XX1
01X1
0011
0000
434 0 $finish->( $conn, { 'error' => 'Failed to write the request... ' . $! } );
435 0 next;
436 }
437 14 $conn->{sent} += $wrote;
438 14 ▶ if ( $conn->{sent} >= length($request_line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn->{"sent"} >= length $request_line) 14 0 14
439 14 $writers->remove($sock);
440 14 $readers->add($sock);
441 }
442 } ## end foreach my $sock ( defined($writable) ? @{$writable...})
443  
444 22 ▶ foreach my $sock ( defined($readable) ? @{$readable} : () ) {
Branch: 1 branch
BranchTrueFalseTotal
defined $readable ? : 20 2 22
445 11 my $conn = $conns{$sock};
446 11 ▶ next if !defined($conn);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 0 11 11
447 11 my $read = sysread( $sock, my $chunk, 4096 );
448 11 ▶ if ( !defined($read) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $read) 0 11 11
449 0 ▶ next if $!{EAGAIN} || $!{EWOULDBLOCK} || $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EWOULDBLOCK"} or $!{"EINTR"} 0 0 0
Condition0%0/6
$!{"EAGAIN"} or $!{"EWOULDBLOCK"} or $!{"EINTR"}
l!l&&r!l&&!r
000
$!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$!{"EAGAIN"} or $!{"EWOULDBLOCK"} or $!{"EINTR"}
$!{"EAGAIN"} $!{"EWOULDBLOCK"} $!{"EINTR"}
Truth table0%0/4
A or B or C
A= $!{"EAGAIN"}B= $!{"EWOULDBLOCK"}C= $!{"EINTR"}
ABCresult
1XX1
01X1
0011
0000
450 0 $finish->( $conn, { 'error' => 'Failed to read the response... ' . $! } );
451 0 next;
452 }
453 11 ▶ if ( $read == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 0) 0 11 11
454 0 $finish->( $conn, { 'error' => 'No response read from "' . $opts{sockets}{ $conn->{name} } . '"' } );
455 0 next;
456 }
457 11 $conn->{buf} .= $chunk;
458 11 ▶ if ( $conn->{buf} =~ /\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn->{"buf"} =~ /\n/) 11 0 11
459 11 my ($line) = split( /\n/, $conn->{buf}, 2 );
460 11 my $response;
461 11 eval { $response = decode_json($line); };
462 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 10 11
463 1 $finish->( $conn, { 'error' => 'Undecodable response... ' . $@ } );
464 1 next;
465 }
466 10 ▶ if ( ref($response) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response ne "HASH") 0 10 10
467 0 $finish->( $conn, { 'error' => 'Response is not a JSON object' } );
468 0 next;
469 }
470 # the call_ok check, applied per connection
471 10 ▶ if ( !defined( $response->{status} ) || $response->{status} ne 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $response->{"status"} or $response->{"status"} ne "ok") 2 8 10
Condition66%2/3
not defined $response->{"status"} or $response->{"status"} ne "ok"
l!l&&r!l&&!r
028
MC/DC50%1/2
not defined $response->{"status"} or $response->{"status"} ne "ok"
not defined $response->{"status"} $response->{"status"} ne "ok"
Truth table66%2/3
not(A) or B
A= not defined $response->{"status"}B= $response->{"status"} ne "ok"
ABresult
1X1
011
000
472 $finish->(
473 2 ▶ $conn, { 'error' => defined( $response->{error} ) ? $response->{error} : 'unknown error' }
Branch: 1 branch
BranchTrueFalseTotal
defined $response->{'error'} ? : 2 0 2
474 );
475 2 next;
476 }
477 8 $finish->( $conn, { 'result' => $response->{result} } );
478 } ## end if ( $conn->{buf} =~ /\n/ )
479 } ## end foreach my $sock ( defined($readable) ? @{$readable...})
480 } ## end while (%conns)
481  
482 # whatever had not finished when the deadline passed
483 10 foreach my $conn ( values(%conns) ) {
484 3 close( $conn->{sock} );
485 3 $answers->{ $conn->{name} } = { 'error' => 'timed out after ' . $timeout . ' seconds' };
486 }
487  
488 10 return $answers;
489 } ## end sub call_many
490  
491 =head2 call_ok
492  
493 Like L</call>, but dies if the response status is not ok, and returns the
494 result value of the response instead of the whole response.
495  
496 my $result = $client->call_ok( $command, $args );
497  
498 =cut
499  
500 37 ▶sub call_ok {
Subroutine call_ok: called CC=5 SCAR=16.4
Pod documented
501 37 my ( $self, $command, $args ) = @_;
502  
503 37 my $response = $self->call( $command, $args );
504  
505 27 ▶ if ( !defined( $response->{status} ) || $response->{status} ne 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $response->{"status"} or $response->{"status"} ne "ok") 2 25 27
Condition66%2/3
not defined $response->{"status"} or $response->{"status"} ne "ok"
l!l&&r!l&&!r
0225
MC/DC50%1/2
not defined $response->{"status"} or $response->{"status"} ne "ok"
not defined $response->{"status"} $response->{"status"} ne "ok"
Truth table66%2/3
not(A) or B
A= not defined $response->{"status"}B= $response->{"status"} ne "ok"
ABresult
1X1
011
000
506 2 ▶ my $error = defined( $response->{error} ) ? $response->{error} : 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
defined $response->{'error'} ? : 2 0 2
507 2 ▶ $error .= "\n" if $error !~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $error =~ /\n$/ 2 0 2
508 2 die($error);
509 }
510  
511 25 return $response->{result};
512 } ## end sub call_ok
513  
514 6 1;
« blib/lib/Ereshkigal/App/Command/start.pm ↑ Summary blib/lib/Ereshkigal/LogDrek.pm »