Perl/mq.pm

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