| 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 | =cut | ||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | 3 | our $VERSION = '0.02-TRIAL3'; | |||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||
| 78 | #Set this in lieu of using Time::HiRes or built-in time(). | ||||||||||||||||||||||||||||||||||||
| 79 | 3 | our $TIME_CR; | |||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | #As light as possible … | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | 3 | my $read; | |||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||
| 85 | sub sysread { | ||||||||||||||||||||||||||||||||||||
| 86 | READ: { | ||||||||||||||||||||||||||||||||||||
| 87 | 670 | ▶ | $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
| |||||||||||||||||||||||||||||||||||||
| 88 | 5 | ▶ | if ($!) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 89 | 5 | ▶ | redo READ if $!{'EINTR'}; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | 670 | return $read; | |||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | my $wrote; | ||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||
| 99 | sub syswrite { | ||||||||||||||||||||||||||||||||||||
| 100 | 768749 | ▶ | $wrote = 0; | ||||||||||||||||||||||||||||||||||
|
Subroutine
syswrite: called CC=7 SCAR=32.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||
| 102 | WRITE: { | ||||||||||||||||||||||||||||||||||||
| 103 | 768749 | ▶ | $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
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??'
}
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??'
}
| |||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($!) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | redo WRITE if $!{'EINTR'}; #EINTR => file pointer unchanged | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 106 | 0 | return undef; | |||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||
| 109 | 0 | die "empty write without error??"; #unexpected! | |||||||||||||||||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | 768749 | return $wrote; | |||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | 3 | my ($start, $last_loop_time, $os_error, $nfound, $timeleft, $timer_cr); | |||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||
| 118 | #pre-5.16 didn’t have \&CORE::time. | ||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub _time { time } | ||||||||||||||||||||||||||||||||||
|
Subroutine
_time: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||
| 121 | sub select { | ||||||||||||||||||||||||||||||||||||
| 122 | 6 | ▶ | die( (caller 0)[3] . ' must have 4 arguments!' ) if @_ != 4; | ||||||||||||||||||||||||||||||||||
|
Subroutine
select: called CC=7 SCAR=19.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||
| 124 | 6 | $os_error = $!; | |||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||
| 126 | 6 | ▶ | $timer_cr = $TIME_CR || Time::HiRes->can('time') || \&_time; | ||||||||||||||||||||||||||||||||||
|
Condition40%2/5
$IO::SigGuard::TIME_CR || 'Time::HiRes'->can('time') || \&IO::SigGuard::_time
$IO::SigGuard::TIME_CR or "Time::HiRes"->can("time")
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")
| |||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||
| 128 | 6 | $start = $timer_cr->(); | |||||||||||||||||||||||||||||||||||
| 129 | 6 | $last_loop_time = $start; | |||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | SELECT: { | ||||||||||||||||||||||||||||||||||||
| 132 | 6 | ($nfound, $timeleft) = CORE::select( $_[0], $_[1], $_[2], $_[3] - $last_loop_time + $start ); | |||||||||||||||||||||||||||||||||||
| 133 | 26 | ▶ | if ($nfound == -1) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 134 | 20 | ▶ | if ($!{'EINTR'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 135 | 20 | $last_loop_time = $timer_cr->(); | |||||||||||||||||||||||||||||||||||
| 136 | 20 | redo SELECT; | |||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||
| 141 | #select() doesn’t set $! on success, so let’s not clobber what | ||||||||||||||||||||||||||||||||||||
| 142 | #value was there before. | ||||||||||||||||||||||||||||||||||||
| 143 | 6 | $! = $os_error; | |||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | 6 | ▶ | return wantarray ? ($nfound, $timeleft) : $nfound; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||
| 150 | =head1 REPOSITORY | ||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||
| 152 | L<https://github.com/FGasper/p5-IO-SigGuard> | ||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||
| 154 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | Felipe Gasper (FELIPE) | ||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||
| 158 | … with special thanks to Mario Roy (MARIOROY) for extra testing | ||||||||||||||||||||||||||||||||||||
| 159 | and a few fixes/improvements. | ||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||
| 161 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | Copyright 2017 by L<Gasper Software Consulting, LLC|http://gaspersoftware.com> | ||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||
| 165 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||
| 167 | This distribution is released under the same license as Perl. | ||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||
| 171 | 3 | 1; | |||||||||||||||||||||||||||||||||||