Perl/mq.pm

filter: Statementstmt 88.6% 94 / 106 Branchbran 57.1% 16 / 28 Conditioncond 53.8% 7 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 86.3% 19 / 22 Podpod 100% 9 / 9 totaltotal 78.4% 146 / 186 CC 21 SCAR 14.3
CC
21
Cov
80%
_validate_name
9.9
_new
8.1
send
3.2
CRAP 4.2
« Perl/timerfd.pm ↑ Summary Perl/epoll.pm »
1 package Linux::Perl::mq;
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 utf-8
7  
8 =head1 NAME
9  
10 Linux::Perl::mq - POSIX message queue
11  
12 =head1 SYNOPSIS
13  
14 my $mq = Linux::Perl::mq->new(
15 name => 'my_message_queue',
16 flags => ['CREAT'],
17  
18 # Only needed for message creation:
19 msgsize => 16,
20 maxmsg => 5,
21 mode => 0644,
22 );
23  
24 $mq->send( msg => 'Hello, world!' );
25  
26 my $got_msg = $mq->receive( msgsize => 16 );
27  
28 $mq->blocking(0); # sets non-blocking mode
29 $mq->blocking(); # returns 0
30  
31 my $attrs_hr = $mq->getattr();
32  
33 # For select, epoll, or poll:
34 my $fileno = $mq->fileno();
35  
36 =cut
37  
38 3 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Linux::Perl::EasyPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Linux::Perl::Constants::Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Linux::Perl::ParseFlags;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Linux::Perl::TimeSpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 3 *_flag_CLOEXEC = \*Linux::Perl::Constants::Fcntl::flag_CLOEXEC;
45 3 *_flag_NONBLOCK = \*Linux::Perl::Constants::Fcntl::flag_NONBLOCK;
46 3 *_flag_CREAT = \*Linux::Perl::Constants::Fcntl::flag_CREAT;
47 3 *_flag_EXCL = \*Linux::Perl::Constants::Fcntl::flag_EXCL;
48  
49 use constant {
50 3 _ENOENT => 2,
51 _EAGAIN => 11,
52 _ETIMEDOUT => 110,
53  
54 _name_length_max => 254, # NAME_MAX - 1 (for the initial solidus)
55 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 3 my ($mq_attr_keys_ar, $mq_attr_pack);
58  
59 BEGIN {
60 3 ▶ ($mq_attr_keys_ar, $mq_attr_pack) = Linux::Perl::EasyPack::split_pack_list(
Subroutine BEGIN: called CC=1 SCAR=0.0
61 flags => 'L!',
62 maxmsg => 'L!',
63 msgsize => 'L!',
64 curmsgs => 'L!',
65 q<> => '(L!)4',
66 );
67  
68 3 pop @$mq_attr_keys_ar;
69 }
70  
71 #----------------------------------------------------------------------
72  
73 =head1 METHODS
74  
75 =head2 I<CLASS>->unlink( $NAME )
76  
77 Returns truthy if a queue is removed or falsy if no queue
78 with the given $NAME exists.
79  
80 Throws an exception on failure.
81  
82 =cut
83  
84 sub unlink {
85 2 ▶ my ($class, $name) = @_;
Subroutine unlink: called CC=1 SCAR=0.0
Pod documented
86  
87 2 _validate_name($name);
88  
89 2 my $arch_module = $class->_get_arch_module();
90  
91 2 local @Linux::Perl::_TOLERATE_ERRNO = (_ENOENT());
92  
93 2 my $result = Linux::Perl::call(
94 $arch_module->NR_mq_unlink(),
95 $name,
96 );
97  
98 2 return (-1 != $result);
99 }
100  
101 #----------------------------------------------------------------------
102  
103 =head2 I<CLASS>->new( %OPTS )
104  
105 Creates a new read/write message queue object.
106  
107 %OPTS are:
108  
109 =over
110  
111 =item * C<name>
112  
113 =item * C<flags> - Any/all/none of: C<CLOEXEC>, C<NONBLOCK>,
114 C<CREAT>, C<EXCL>.
115  
116 =item * C<mode> - Only relevant if the C<CREAT> flag is given.
117  
118 =item * C<maxmsg> - Only relevant if the C<CREAT> flag is given.
119  
120 =item * C<msgsize> - Only relevant if the C<CREAT> flag is given.
121  
122 =back
123  
124 =cut
125  
126 sub new {
127 2 ▶ my ($class, @opts_kv) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
128  
129 2 return $class->_new(
130 Linux::Perl::Constants::Fcntl::mode_RDWR(),
131 @opts_kv,
132 );
133 }
134  
135 =head2 I<CLASS>->new_wronly( %OPTS )
136  
137 Like C<new()>, but the queue handle is write-only.
138  
139 =cut
140  
141 sub new_wronly {
142 0 ▶ my ($class, @opts_kv) = @_;
Subroutine new_wronly: not called CC=1 SCAR=6.9
Pod documented
143  
144 0 return $class->_new(
145 Linux::Perl::Constants::Fcntl::mode_WRONLY(),
146 @opts_kv,
147 );
148 }
149  
150 =head2 I<CLASS>->new_rdonly( %OPTS )
151  
152 Like C<new()>, but the queue handle is read-only.
153  
154 =cut
155  
156 sub new_rdonly {
157 0 ▶ my ($class, @opts_kv) = @_;
Subroutine new_rdonly: not called CC=1 SCAR=6.9
Pod documented
158  
159 0 return $class->_new(
160 Linux::Perl::Constants::Fcntl::mode_RDONLY(),
161 @opts_kv,
162 );
163 }
164  
165 sub _new {
166 2 ▶ my ($class, $flags, %opts) = @_;
Subroutine _new: called CC=7 SCAR=21.0
167  
168 2 local ($!, $^E);
169  
170 2 my $arch_module = $class->_get_arch_module();
171  
172 2 _validate_name($opts{'name'});
173  
174 2 $flags |= Linux::Perl::ParseFlags::parse( $class, $opts{'flags'} );
175  
176 2 my @creat_args;
177  
178 2 ▶ if ($flags & _flag_CREAT()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & &_flag_CREAT()) { } 2 0 2
179 2 my $mode = $opts{'mode'};
180 2 ▶ die 'Need mode!' if !defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 2 2
181  
182 # TODO: be consistent
183 #die "Non-numeric mode: [$mode]" if tr<0-9><>c;
184 #die "mode exceeds 0777: [$mode]" if $mode > 0777;
185  
186 2 ▶ my $maxmsg = $opts{'maxmsg'} or die 'Need nonzero maxmsg!';
Branch: 1 branch
BranchTrueFalseTotal
my $maxmsg = $opts{"maxmsg"} or die "Need nonzero maxmsg!" 0 2 2
187 2 ▶ my $msgsize = $opts{'msgsize'} or die 'Need nonzero msgsize!';
Branch: 1 branch
BranchTrueFalseTotal
my $msgsize = $opts{"msgsize"} or die "Need nonzero msgsize!" 0 2 2
188  
189 2 my $attr_pack = pack $mq_attr_pack, 0, $maxmsg, $msgsize, 0;
190  
191 2 push @creat_args, 0 + $mode, $attr_pack;
192 }
193 else {
194 0 for my $opt ( qw( mode maxmsg msgsize ) ) {
195 0 ▶ warn "no $opt flag; ignoring '$opt'" if defined $opts{$opt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{$opt} 0 0 0
196 }
197 }
198  
199 my $mqd = Linux::Perl::call(
200 $arch_module->NR_mq_open(),
201 2 $opts{'name'},
202 0 + $flags,
203 @creat_args,
204 );
205  
206 2 open my $mqfh, '+<&=', $mqd;
207  
208 2 return bless [ $mqd, $mqfh ], $arch_module;
209 }
210  
211 #----------------------------------------------------------------------
212  
213 =head2 I<OBJ>->fileno()
214  
215 Returns the file descriptor number. This is useful, e.g., for use with
216 select, L<epoll|Linux::Perl::epoll>, or poll.
217  
218 =cut
219  
220 0 ▶sub fileno { fileno $_[0][0] }
Subroutine fileno: not called CC=1 SCAR=6.9
Pod documented
221  
222 #----------------------------------------------------------------------
223  
224 =head2 I<OBJ>->getattr()
225  
226 Returns a hashref of attributes that corresponds to C<struct mq_attr>.
227 See C<man 3 mq_getattr> for details.
228  
229 =cut
230  
231 sub getattr {
232 6 ▶ my ($self) = @_;
Subroutine getattr: called CC=1 SCAR=0.0
Pod documented
233  
234 # cf. uapi/linux/mqueue.h - there are 4 (undocumented!) longs
235 # after the 4 documented ones.
236 6 my $buf = pack $mq_attr_pack;
237  
238 6 Linux::Perl::call(
239 $self->NR_mq_getsetattr(),
240 0 + $self->[0],
241 undef,
242 $buf,
243 );
244  
245 6 my %ret;
246 6 @ret{ @$mq_attr_keys_ar } = unpack $mq_attr_pack, $buf;
247  
248 6 return \%ret;
249 }
250  
251 #----------------------------------------------------------------------
252  
253 =head2 I<OBJ>->blocking()
254  
255 Imitates L<IO::Handle>’s method of the same name.
256 Returns a boolean that indicates whether the message queue handle
257 is blocking.
258  
259 =head2 I<OBJ>->blocking( $BLOCKING_YN )
260  
261 Sets the message queue handle as blocking or non-blocking.
262  
263 =cut
264  
265 sub blocking {
266 8 ▶ my ($self, $turn_on_yn) = @_;
Subroutine blocking: called CC=3 SCAR=11.0
Pod documented
267  
268 8 ▶ if (!defined $turn_on_yn) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $turn_on_yn) 4 4 8
269 4 return !$self->getattr()->{'flags'};
270 }
271  
272 4 ▶ my $flags = $turn_on_yn ? 0 : _flag_NONBLOCK();
Branch: 1 branch
BranchTrueFalseTotal
$turn_on_yn ? : 2 2 4
273 4 my $buf = pack $mq_attr_pack, $flags;
274  
275 4 Linux::Perl::call(
276 $self->NR_mq_getsetattr(),
277 0 + $self->[0],
278 $buf,
279 undef,
280 );
281  
282 4 return $self;
283 }
284  
285 sub _send_receive_prio_timeout {
286 8 ▶ my ($opts_hr) = @_;
Subroutine _send_receive_prio_timeout: called CC=3 SCAR=11.4
287  
288 8 ▶ my $timeout = Linux::Perl::TimeSpec::from_float($opts_hr->{'timeout'} || 0);
Condition50%1/2
$opts_hr->{'timeout'} || 0
l!l
08
MC/DC0%0/1
$opts_hr->{'timeout'} || 0
$opts_hr->{'timeout'}
Truth table50%1/2
A
A= $opts_hr->{'timeout'}
Aresult
11
00
289  
290 8 ▶ my $prio = $opts_hr->{'prio'} || 0;
Condition50%1/2
$opts_hr->{'prio'} || 0
l!l
08
MC/DC0%0/1
$opts_hr->{'prio'} || 0
$opts_hr->{'prio'}
Truth table50%1/2
A
A= $opts_hr->{'prio'}
Aresult
11
00
291  
292 8 return (0 + $prio, $timeout);
293 }
294  
295 =head2 I<OBJ>->send( %OPTS )
296  
297 Sends a message to the queue. An exception is thrown on failure,
298 e.g., if the queue cannot accommodate another message.
299  
300 =over
301  
302 =item * C<msg> - The message to send.
303  
304 =item * C<prio> - Optional, the message priority. Defaults to 0
305 (highest priority).
306  
307 =item * C<timeout> - Optional, in seconds. (Can be fractional.)
308  
309 =back
310  
311 =cut
312  
313 sub send {
314 2 ▶ my ($self, %opts) = @_;
Subroutine send: called CC=3 SCAR=11.6
Pod documented
315  
316 2 my $msg = $opts{'msg'};
317 2 ▶ if (!defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 2 2
318 0 die "Need msg!";
319 }
320  
321 # local @Linux::Perl::_TOLERATE_ERRNO = (
322 # _EAGAIN(),
323 # _ETIMEDOUT(),
324 # );
325  
326 2 my $ret = Linux::Perl::call(
327 $self->NR_mq_timedsend(),
328 0 + $self->[0],
329 $msg,
330 length($msg),
331 _send_receive_prio_timeout(\%opts),
332 );
333  
334 2 ▶ return undef if $ret == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $ret == -1 0 2 2
335  
336 2 return 1;
337 }
338  
339 =head2 I<OBJ>->receive( \$BUFFER, %OPTS )
340  
341 Attempts to slurp a message from the queue.
342  
343 $BUFFER is a pre-initialized buffer where the message will be stored.
344 It B<must> be at least as long as the message queue’s C<msgsize>.
345  
346 %OPTS are:
347  
348 =over
349  
350 =item * C<prio> - Optional, the receive priority. Defaults to 0
351 (highest priority).
352  
353 =item * C<timeout> - Optional, in seconds. (Can be fractional.)
354  
355 =back
356  
357 Returns the message length on success; if there is no message available,
358 undef is returned. Any other failure prompts an exception.
359  
360 =cut
361  
362 sub receive {
363 6 ▶ my ($self, $buffer_sr, %opts) = @_;
Subroutine receive: called CC=2 SCAR=6.9
Pod documented
364  
365 6 local @Linux::Perl::_TOLERATE_ERRNO = (
366 _EAGAIN(),
367 _ETIMEDOUT(),
368 );
369  
370 6 my $len = Linux::Perl::call(
371 $self->NR_mq_timedreceive(),
372 0 + $self->[0],
373 $$buffer_sr,
374 length $$buffer_sr,
375 _send_receive_prio_timeout(\%opts),
376 );
377  
378 6 ▶ return( ($len != -1) ? $len : undef );
Branch: 1 branch
BranchTrueFalseTotal
$len != -1 ? : 2 4 6
379 }
380  
381 #----------------------------------------------------------------------
382  
383 sub _get_arch_module {
384 4 ▶ my ($class) = @_;
Subroutine _get_arch_module: called CC=3 SCAR=11.2
385  
386 4 ▶ my $arch_module = $class->can('NR_mq_open') && $class;
Condition66%2/3
$class->can('NR_mq_open') && $class
!ll&&!rl&&r
202
MC/DC50%1/2
$class->can('NR_mq_open') && $class
$class->can('NR_mq_open') $class
Truth table66%2/3
A && B
A= $class->can('NR_mq_open')B= $class
ABresult
0X0
100
111
387 4 ▶ return $arch_module ||= do {
Condition66%2/3
$arch_module ||= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
l!l&&r!l&&!r
220
MC/DC0%0/2
$arch_module ||= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
$arch_module do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
Truth table66%2/3
A ||= B
A= $arch_moduleB= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
ABresult
1X1
011
000
388 2 require Linux::Perl::ArchLoader;
389 2 Linux::Perl::ArchLoader::get_arch_module($class);
390 };
391 }
392  
393 sub _validate_name {
394 4 ▶ my ($name) = @_;
Subroutine _validate_name: called CC=6 SCAR=22.9
395  
396 4 ▶ if (!defined $name || !length $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and length $name) 0 4 4
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
004
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
397 0 die 'Need name!';
398 }
399  
400 4 ▶ if (-1 != index($name, '/')) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($name, "/") != -1) 0 4 4
401 0 die "name cannot contain a solidus (/)!";
402 }
403  
404 4 ▶ if (-1 != index($name, "\0")) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($name, "\0") != -1) 0 4 4
405 0 die "name cannot contain a NUL byte!";
406 }
407  
408 4 ▶ if ( length($name) > _name_length_max() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name > 254) 0 4 4
409 0 die sprintf("name ($name) exceeds the length limit (%d)!", _NAME_MAX());;
410 }
411  
412 4 return;
413 }
414  
415 3 1;
« Perl/timerfd.pm ↑ Summary Perl/epoll.pm »