Perl/signalfd.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod 100% 4 / 4 totaltotal 95.5% 86 / 90 CC 5 SCAR 4.4
CC
5
Cov
96%
read
3.0
new
2.0
fileno
2.0
CRAP 1.6
« Perl/uname/i686.pm ↑ Summary Perl/Base.pm »
1 package Linux::Perl::signalfd;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Linux::Perl::signalfd
9  
10 =head1 SYNOPSIS
11  
12 # One potential way of preventing the signals
13 # from taking down the process.
14 Linux::Perl::sigprocmask->block( 'INT', 'ABRT' );
15  
16 my $sigfd = Linux::Perl::signalfd->new(
17 flags => ['NONBLOCK', 'CLOEXEC'],
18 signals => ['INT', 'ABRT'],
19 );
20  
21 $sigfd->set_signals( 'INT' );
22  
23 my @evts = $sigfd->read();
24  
25 =head1 DESCRIPTION
26  
27 An implementation of Linux’s “signalfd”.
28  
29 Note that you’ll need to ensure that whatever signals you
30 expect to receive don’t take down the process.
31 L<sigprocmask|Linux::Perl::sigprocmask> can help with this.
32  
33 =cut
34  
35 3 ▶use parent qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
36 Linux::Perl::Base
37 Linux::Perl::Base::BitsTest
38 3 );
39  
40 3 ▶use Call::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 3 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use Linux::Perl::EasyPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use Linux::Perl::ParseFlags;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use Linux::Perl::SigSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use Linux::Perl::Constants::Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 3 *_flag_CLOEXEC = \*Linux::Perl::Constants::Fcntl::flag_CLOEXEC;
49 3 *_flag_NONBLOCK = \*Linux::Perl::Constants::Fcntl::flag_NONBLOCK;
50  
51 3 ▶use constant _sfd_siginfo_size => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 #----------------------------------------------------------------------
54  
55 =head1 METHODS
56  
57 =head2 I<CLASS>->new( %OPTS )
58  
59 Creates a signalfd instance. %OPTS are:
60  
61 =over
62  
63 =item * C<signals> - An array reference, each of whose members is either
64 a string (e.g., C<INT>) or a signal number.
65  
66 =item * C<flags> - Optional, an array reference of either/both of:
67 C<NONBLOCK>, C<CLOEXEC>.
68  
69 =back
70  
71 =cut
72  
73 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
74 2 my ($class, %opts) = @_;
75  
76 2 my $arch_module = $class->_get_arch_module();
77  
78 2 my $flags = Linux::Perl::ParseFlags::parse( $class, $opts{'flags'} );
79  
80 2 my $fd = _call_signalfd( $arch_module, -1, $flags, $opts{'signals'} );
81  
82 2 ▶ local $^F = 1000 if $flags & _flag_CLOEXEC();
Branch: 1 branch
BranchTrueFalseTotal
if $flags & &_flag_CLOEXEC() 0 2 2
83  
84 2 open my $fh, '+<&=', $fd;
85  
86 2 return bless [$fd, $fh], $arch_module;
87 }
88  
89 #----------------------------------------------------------------------
90  
91 =head2 $num = I<OBJ>->fileno()
92  
93 Returns the file descriptor, which can be used with, e.g.,
94 C<select()>, L<epoll()|Linux::Perl::epoll>, or C<poll()>.
95  
96 =cut
97  
98 0 ▶sub fileno { return $_[0][0]; }
Subroutine fileno: not called CC=1 SCAR=6.9
Pod documented
99  
100 #----------------------------------------------------------------------
101  
102 3 my ($sfd_siginfo_keys_ar, $sfd_siginfo_pack);
103  
104 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
105 3 ($sfd_siginfo_keys_ar, $sfd_siginfo_pack) = Linux::Perl::EasyPack::split_pack_list(
106 signo => 'L',
107 errno => 'l',
108 code => 'l',
109 pid => 'L',
110 uid => 'L',
111 fd => 'l',
112 tid => 'L',
113 band => 'L',
114 overrun => 'L',
115 trapno => 'L',
116 status => 'l',
117 int => 'l',
118 ptr => __PACKAGE__->_PACK_u64(),
119 utime => __PACKAGE__->_PACK_u64(),
120 stime => __PACKAGE__->_PACK_u64(),
121 addr => __PACKAGE__->_PACK_u64(),
122 addr_lsb => 'S',
123 );
124 }
125  
126 =head2 @signals = I<OBJ>-read()
127  
128 Reads events from the signalfd instance. Each event is a hash reference
129 whose keys and values correspond to C<struct inotify_event>.
130 (cf. C<man 7 inotify>)
131  
132 In scalar context the return is the number of hash references that would
133 be returned in list context.
134  
135 An empty return (0 in scalar context) is an error state, in which case
136 C<$!> will indicate what the error was.
137  
138 =cut
139  
140 4 ▶sub read {
Subroutine read: called CC=3 SCAR=11.0
Pod documented
141 4 my ($self) = @_;
142  
143 4 Call::Context::must_be_list();
144  
145 4 ▶ return if !sysread( $self->[1], my $buf, 65536 );
Branch: 1 branch
BranchTrueFalseTotal
unless sysread $self->[1], my $buf, 65536 0 4 4
146  
147 4 my @sigs;
148  
149 4 ▶ while (length $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf) 4 4 8
150 4 my $bufbuf = substr($buf, 0, _sfd_siginfo_size(), q<>);
151  
152 4 my %result;
153 4 @result{ @$sfd_siginfo_keys_ar } = unpack $sfd_siginfo_pack, $bufbuf;
154  
155 4 push @sigs, \%result;
156 }
157  
158 4 return @sigs;
159 }
160  
161 #----------------------------------------------------------------------
162  
163 =head2 I<OBJ>->set_signals( @SIGNALS )
164  
165 Updates the signalfd instance’s list of signals to listen for.
166 @SIGNALS is a list such as the constructor’s C<signals> argument.
167  
168 This returns the instance.
169  
170 =cut
171  
172 2 ▶sub set_signals {
Subroutine set_signals: called CC=1 SCAR=0.0
Pod documented
173 2 my ($self, @signals) = @_;
174  
175 2 _call_signalfd(
176 $self,
177 $self->[0],
178 0,
179 \@signals,
180 );
181  
182 2 return $self;
183 }
184  
185 4 ▶sub _call_signalfd {
Subroutine _call_signalfd: called CC=2 SCAR=6.9
186 4 my ($arch_module, $fd, $flags, $signals_ar) = @_;
187  
188 4 my $sigmask = Linux::Perl::SigSet::from_list( @$signals_ar );
189  
190 4 my $call_name = 'NR_signalfd';
191 4 ▶ $call_name .= '4' if $flags;
Branch: 1 branch
BranchTrueFalseTotal
if $flags 2 2 4
192  
193 4 $fd = Linux::Perl::call(
194 $arch_module->$call_name(),
195 $fd,
196 $sigmask,
197 length $sigmask,
198 0 + $flags,
199 );
200  
201 4 return $fd;
202 }
203  
204 3 1;
« Perl/uname/i686.pm ↑ Summary Perl/Base.pm »