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