blib/lib/IO/SigGuard.pm

filter: Statementstmt 86.0% 37 / 43 Branchbran 42.8% 12 / 28 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.7% 6 / 7 Podpod 33.3% 1 / 3 totaltotal 63.4% 59 / 93 SCAR 34.3
CC
18
Cov
66%
syswrite
24.9
select
7.5
sysread
6.8
CRAP 30.9
↑ Summary
1 package IO::SigGuard;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 IO::SigGuard - Signal protection for sysread/syswrite
8  
9 =head1 SYNOPSIS
10  
11 IO::SigGuard::sysread( $fh, $buf, $size );
12 IO::SigGuard::sysread( $fh, $buf, $size, $offset );
13  
14 IO::SigGuard::syswrite( $fh, $buf );
15 IO::SigGuard::syswrite( $fh, $buf, $len );
16 IO::SigGuard::syswrite( $fh, $buf, $len, $offset );
17  
18 IO::SigGuard::select( $read, $write, $exc, $timeout );
19  
20 =head1 DESCRIPTION
21  
22 C<perldoc perlipc> describes how Perl versions from 5.8.0 onward disable
23 the OS’s SA_RESTART flag when installing Perl signal handlers.
24  
25 This module restores that pattern: it does an automatic restart
26 when a signal interrupts an operation, so you can entirely avoid
27 the generally-useless EINTR error when using
28 C<sysread()>, C<syswrite()>, and C<select()>.
29  
30 =head1 ABOUT C<sysread()> and C<syswrite()>
31  
32 Other than that you’ll never see EINTR and that
33 there are no function prototypes used (i.e., you need parentheses on
34 all invocations), C<sysread()> and C<syswrite()>
35 work exactly the same as Perl’s equivalent built-ins.
36  
37 =head1 ABOUT C<select()>
38  
39 To handle EINTR, C<IO::SigGuard::select()> has to subtract the elapsed time
40 from the given timeout then repeat the internal C<select()>. Because
41 the C<select()> built-in’s C<$timeleft> return is not reliable across
42 all platforms, we have to compute the elapsed time ourselves. By default the
43 only means of doing this is the C<time()> built-in, which can only measure
44 individual seconds.
45  
46 This works, but there are two ways to make it more accurate:
47  
48 =over
49  
50 =item * Have L<Time::HiRes> loaded, and C<IO::SigGuard::select()> will use that
51 module rather than the C<time()> built-in.
52  
53 =item * Set C<$IO::SigGuard::TIME_CR> to a compatible code reference. This is
54 useful, e.g., if you have your own logic to do the equivalent of
55 L<Time::HiRes>—for example, in Linux you may prefer to call the C<gettimeofday>
56 system call directly from Perl to avoid L<Time::HiRes>’s XS overhead.
57  
58 =back
59  
60 In scalar contact, C<IO::SigGuard::select()> is a drop-in replacement
61 for Perl’s 4-argument built-in.
62  
63 In list context, there may be discrepancies re the C<$timeleft> value
64 that Perl returns from a call to C<select>. As per Perl’s documentation
65 this value is generally not reliable anyway, though, so that shouldn’t be a
66 big deal. In fact, on systems (e.g., MacOS) where the built-in’s C<$timeleft>
67 is completely useless, IO::SigGuard’s return is actually B<better> since it
68 does provide at least a rough estimate of how much of the given timeout value
69 is left.
70  
71 See C<perlport> for portability notes for C<select>.
72  
73 =cut
74  
75 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 3 our $VERSION = '0.02-TRIAL7';
79  
80 #Set this in lieu of using Time::HiRes or built-in time().
81 3 our $TIME_CR;
82  
83 #As light as possible …
84  
85 3 my $read;
86  
87 sub sysread {
88 READ: {
89 687 ▶ $read = ( (@_ == 3) ? CORE::sysread( $_[0], $_[1], $_[2] ) : (@_ == 4) ? CORE::sysread( $_[0], $_[1], $_[2], $_[3] ) : die "Wrong args count! (@_)" ) or do {
Subroutine sysread: called CC=6 SCAR=19.1
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
$read = @_ == 3 ? CORE::sysread($_[0], $_[1], $_[2]) : (@_ == 4 ? CORE::sysread($_[0], $_[1], $_[2], $_[3]) : die("Wrong args count! (@_)")) or do { if ($!) { redo READ if $!{"EINTR"}; } } 5 687 692
@_ == 3 ? : 692 0 692
@_ == 4 ? : 0 0 0
90 5 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 5 0 5
91 5 ▶ redo READ if $!{'EINTR'};
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 5 0 5
92 }
93 };
94 }
95  
96 687 return $read;
97 }
98  
99 my $wrote;
100  
101 sub syswrite {
102 744661 ▶ $wrote = 0;
Subroutine syswrite: called CC=7 SCAR=32.1
Pod undocumented
103  
104 WRITE: {
105 744661 ▶ $wrote += ( (@_ == 2) ? CORE::syswrite( $_[0], $_[1], length($_[1]) - $wrote, $wrote ) : (@_ == 3) ? CORE::syswrite( $_[0], $_[1], $_[2] - $wrote, $wrote ) : (@_ == 4) ? CORE::syswrite( $_[0], $_[1], $_[2] - $wrote, $_[3] + $wrote ) : die "Wrong args count! (@_)" ) || do {
Branch: 3 branches
BranchTrueFalseTotal
@_ == 2 ? : 744661 0 744661
@_ == 3 ? : 0 0 0
@_ == 4 ? : 0 0 0
Condition33%1/3
(@_ == 2 ? CORE::syswrite($_[0], $_[1], length($_[1]) - $wrote, $wrote) : (@_ == 3 ? CORE::syswrite($_[0], $_[1], $_[2] - $wrote, $wrote) : (@_ == 4 ? CORE::syswrite($_[0], $_[1], $_[2] - $wrote, $_[3] + $wrote) : die("Wrong args count! (@_)")))) || do { if ($!) { redo WRITE if $!{'EINTR'}; return undef; } ; die 'empty write without error??' }
l!l&&r!l&&!r
74466100
MC/DC0%0/2
(@_ == 2 ? CORE::syswrite($_[0], $_[1], length($_[1]) - $wrote, $wrote) : (@_ == 3 ? CORE::syswrite($_[0], $_[1], $_[2] - $wrote, $wrote) : (@_ == 4 ? CORE::syswrite($_[0], $_[1], $_[2] - $wrote, $_[3] + $wrote) : die("Wrong args count! (@_)")))) || do { if ($!) { redo WRITE if $!{'EINTR'}; return undef; } ; die 'empty write without error??' }
(@_ == 2 ? CORE::syswrite($_[0], $_[1], length($_[1]) - $wrote, $wrote) : (@_ == 3 ? CORE::syswrite($_[0], $_[1], $_[2] - $wrote, $wrote) : (@_ == 4 ? CORE::syswrite($_[0], $_[1], $_[2] - $wrote, $_[3] + $wrote) : die("Wrong args count! (@_)")))) do { if ($!) { redo WRITE if $!{'EINTR'}; return undef; } ; die 'empty write without error??' }
Truth table33%1/3
A || B
A= (@_ == 2 ? CORE::syswrite($_[0], $_[1], length($_[1]) - $wrote, $wrote) : (@_ == 3 ? CORE::syswrite($_[0], $_[1], $_[2] - $wrote, $wrote) : (@_ == 4 ? CORE::syswrite($_[0], $_[1], $_[2] - $wrote, $_[3] + $wrote) : die("Wrong args count! (@_)"))))B= do { if ($!) { redo WRITE if $!{'EINTR'}; return undef; } ; die 'empty write without error??' }
ABresult
1X1
011
000
106 0 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 0 0
107 0 ▶ redo WRITE if $!{'EINTR'}; #EINTR => file pointer unchanged
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 0 0
108 0 return undef;
109 }
110  
111 0 die "empty write without error??"; #unexpected!
112 };
113 }
114  
115 744661 return $wrote;
116 }
117  
118 3 my ($start, $last_loop_time, $os_error, $nfound, $timeleft, $timer_cr);
119  
120 #pre-5.16 didn’t have \&CORE::time.
121 0 ▶sub _time { time }
Subroutine _time: not called CC=1 SCAR=6.9
122  
123 sub select {
124 6 ▶ die( (caller 0)[3] . ' must have 4 arguments!' ) if @_ != 4;
Subroutine select: called CC=7 SCAR=20.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 6 6
125  
126 6 $os_error = $!;
127  
128 6 ▶ $timer_cr = $TIME_CR || Time::HiRes->can('time') || \&_time;
Condition40%2/5
$IO::SigGuard::TIME_CR || 'Time::HiRes'->can('time') || \&IO::SigGuard::_time
l!l
60
$IO::SigGuard::TIME_CR or "Time::HiRes"->can("time")
l!l&&r!l&&!r
060
MC/DC0%0/2
$IO::SigGuard::TIME_CR || 'Time::HiRes'->can('time') || \&IO::SigGuard::_time
$IO::SigGuard::TIME_CR "Time::HiRes"->can("time")
Truth table33%1/3
A or B
A= $IO::SigGuard::TIME_CRB= "Time::HiRes"->can("time")
ABresult
1X1
011
000
129  
130 6 $start = $timer_cr->();
131 6 $last_loop_time = $start;
132  
133 SELECT: {
134 6 ($nfound, $timeleft) = CORE::select( $_[0], $_[1], $_[2], $_[3] - $last_loop_time + $start );
135 26 ▶ if ($nfound == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == -1) { } 20 6 26
136  
137 #Use of %! will autoload Errno.pm,
138 #which can affect the value of $!.
139 20 my $select_error = $!;
140  
141 20 ▶ if ($!{'EINTR'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"EINTR"}) 20 0 20
142 20 $last_loop_time = $timer_cr->();
143 20 redo SELECT;
144 }
145  
146 0 $! = $select_error;
147 }
148 else {
149  
150 #select() doesn’t set $! on success, so let’s not clobber what
151 #value was there before.
152 6 $! = $os_error;
153 }
154  
155 6 ▶ return wantarray ? ($nfound, $timeleft) : $nfound;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 3 6
156 }
157 }
158  
159 =head1 REPOSITORY
160  
161 L<https://github.com/FGasper/p5-IO-SigGuard>
162  
163 =head1 AUTHOR
164  
165 Felipe Gasper (FELIPE)
166  
167 … with special thanks to Mario Roy (MARIOROY) for extra testing
168 and a few fixes/improvements.
169  
170 =head1 COPYRIGHT
171  
172 Copyright 2017 by L<Gasper Software Consulting, LLC|http://gaspersoftware.com>
173  
174 =head1 LICENSE
175  
176 This distribution is released under the same license as Perl.
177  
178 =cut
179  
180 3 1;
↑ Summary