blib/lib/IPC/Shareable.pm

filter: Statementstmt 95.2% 971 / 1019 Branchbran 80.1% 545 / 680 Conditioncond 65.7% 148 / 225 MC/DCmcdc 43.6% 58 / 133 Subroutinesub 98.8% 87 / 88 Podpod 100% 22 / 22 totaltotal 84.4% 1831 / 2167 CC 453 SCAR 32.6
CC
453
Cov
86%
_shm_data_summary
158.1
shm_segments
73.7
sysv_info
65.7
CRAP 26.1
↑ Summary blib/lib/IPC/Shareable/SharedMem.pm »
1 package IPC::Shareable;
2  
3 83 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 83 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 83 require 5.010;
7  
8 83 ▶use Carp qw(croak confess carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 83 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 83 ▶use Errno qw(ENOMEM ENOSPC);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 83 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 83 ▶use IPC::Semaphore;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 83 ▶use IPC::Shareable::SharedMem;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 83 use IPC::SysV qw(
15 IPC_PRIVATE
16 IPC_CREAT
17 IPC_EXCL
18 IPC_NOWAIT
19 IPC_RMID
20 IPC_STAT
21 SEM_UNDO
22 83 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 83 ▶use JSON qw(-convert_blessed_universally);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 83 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 83 ▶use String::CRC32;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 83 ▶use Storable 0.6 qw(freeze thaw);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 83 our $VERSION = '1.17';
29  
30 # eval() returns 1 on success; // 0 coerces undef (failure) to 0 so callers
31 # can boolean-test cleanly without checking definedness.
32  
33 83 ▶our $_have_xs = ! $ENV{IPC_SHAREABLE_NO_XS} && eval {
Condition40%2/5
!$ENV{"IPC_SHAREABLE_NO_XS"} && eval { do { require XSLoader; XSLoader::load("IPC::Shareable", $IPC::Shareable::VERSION); 1 } } // 0
l!l
830
not $ENV{"IPC_SHAREABLE_NO_XS"} and eval { do { require XSLoader; XSLoader::load("IPC::Shareable", $IPC::Shareable::VERSION); 1 } }
!ll&&!rl&&r
0083
MC/DC0%0/2
!$ENV{"IPC_SHAREABLE_NO_XS"} && eval { do { require XSLoader; XSLoader::load("IPC::Shareable", $IPC::Shareable::VERSION); 1 } } // 0
not $ENV{"IPC_SHAREABLE_NO_XS"} eval { do { require XSLoader; XSLoader::load("IPC::Shareable", $IPC::Shareable::VERSION); 1 } }
Truth table33%1/3
not(A) and B
A= not $ENV{"IPC_SHAREABLE_NO_XS"}B= eval { do { require XSLoader; XSLoader::load("IPC::Shareable", $IPC::Shareable::VERSION); 1 } }
ABresult
111
0X0
100
34 83 require XSLoader;
35 83 XSLoader::load('IPC::Shareable', $VERSION);
36 83 1;
37 } // 0;
38  
39 use constant {
40 # Locking
41  
42 83 LOCK_SH => 1,
43 LOCK_EX => 2,
44 LOCK_NB => 4,
45 LOCK_UN => 8,
46  
47 # SHM parameters
48  
49 SHM_BUFSIZ => 65536,
50 SHMMAX_BYTES => 1073741824, # ~1 GB
51 SHM_EXISTS => 1,
52  
53 # Semaphore slots (4 slots always; 5th slot added when 'testing' is set)
54  
55 SEM_MARKER => 0,
56 SEM_READERS => 1,
57 SEM_WRITERS => 2,
58 SEM_PROTECTED => 3,
59 SEM_TESTING => 4,
60  
61 # Perl sends in a double as opposed to an integer to shmat(), and on some
62 # systems, this causes the IPC system to round down to the maximum integer
63 # size of 0x80000000. We correct that when generating keys with CRC32.
64  
65 MAX_KEY_INT_SIZE => 0x80000000,
66  
67 # Number of times we'll check for existing segs
68  
69 EXCLUSIVE_CHECK_LIMIT => 10,
70  
71 # Struct types
72  
73 TYPE_HASH => 0,
74 TYPE_ARRAY => 1,
75 TYPE_SCALAR => 2,
76 83 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 83 require Exporter;
79 83 our @ISA = 'Exporter';
80 83 our @EXPORT_OK = qw(
81 LOCK_EX
82 LOCK_SH
83 LOCK_NB
84 LOCK_UN
85 SEM_MARKER
86 SEM_READERS
87 SEM_WRITERS
88 SEM_PROTECTED
89 SEM_TESTING
90 );
91 83 our %EXPORT_TAGS = (
92 all => [
93 qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN ),
94 qw( SEM_MARKER SEM_READERS SEM_WRITERS SEM_PROTECTED SEM_TESTING ),
95 ],
96 lock => [qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN )],
97 flock => [qw( LOCK_EX LOCK_SH LOCK_NB LOCK_UN )],
98 semaphores => [qw( SEM_MARKER SEM_READERS SEM_WRITERS SEM_PROTECTED SEM_TESTING )],
99 );
100  
101 # Locking scheme copied from IPC::ShareLite (with minor modifications)
102  
103 83 my %semop_args = (
104 (LOCK_EX),
105 [
106 SEM_READERS, 0, 0, # Wait for readers to finish
107 SEM_WRITERS, 0, 0, # Wait for writers to finish
108 SEM_WRITERS, 1, SEM_UNDO, # Assert write lock
109 ],
110 (LOCK_EX|LOCK_NB),
111 [
112 SEM_READERS, 0, IPC_NOWAIT, # Wait for readers to finish
113 SEM_WRITERS, 0, IPC_NOWAIT, # Wait for writers to finish
114 SEM_WRITERS, 1, (SEM_UNDO | IPC_NOWAIT), # Assert write lock
115 ],
116 (LOCK_EX|LOCK_UN),
117 [
118 SEM_WRITERS, -1, (SEM_UNDO | IPC_NOWAIT),
119 ],
120 (LOCK_SH),
121 [
122 SEM_WRITERS, 0, 0, # Wait for writers to finish
123 SEM_READERS, 1, SEM_UNDO, # Assert shared read lock
124 ],
125 (LOCK_SH|LOCK_NB),
126 [
127 SEM_WRITERS, 0, IPC_NOWAIT, # Wait for writers to finish
128 SEM_READERS, 1, (SEM_UNDO | IPC_NOWAIT), # Assert shared read lock
129 ],
130 (LOCK_SH|LOCK_UN),
131 [
132 SEM_READERS, -1, (SEM_UNDO | IPC_NOWAIT), # Remove shared read lock
133 ],
134 );
135  
136 83 my %default_options = (
137 key => IPC_PRIVATE,
138 create => 0,
139 exclusive => 0,
140 destroy => 0,
141 mode => 0666,
142 size => SHM_BUFSIZ,
143 protected => 0,
144 testing => 0,
145 limit => 1,
146 graceful => 0,
147 warn => 0,
148 serializer => 'json',
149 enforced_write_locking => 1,
150 enforced_read_locking => 1,
151 violated_write_lock_warn => 1,
152 violated_read_lock_warn => 1,
153 );
154  
155 # Class-level variables
156  
157 83 my %global_register;
158 my %process_register;
159 83 my %used_ids;
160 83 my $_testing_dist = '';
161  
162 # "Magic" methods
163  
164 sub TIESCALAR {
165 139 ▶ return _tie('SCALAR', @_);
Subroutine TIESCALAR: called CC=1 SCAR=0.0
166 }
167 sub TIEARRAY {
168 103 ▶ return _tie('ARRAY', @_);
Subroutine TIEARRAY: called CC=1 SCAR=0.0
169 }
170 sub TIEHASH {
171 298 ▶ return _tie('HASH', @_);
Subroutine TIEHASH: called CC=1 SCAR=0.0
172 }
173 sub STORE {
174 735 ▶ my $knot = shift;
Subroutine STORE: called CC=15 SCAR=27.1
175  
176 735 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 7 728 735
177  
178 728 ▶ $knot->{_data} = $knot->_decode($knot->seg) unless ($knot->{_lock});
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 695 33 728
179  
180 728 ▶ if ($knot->{_type_int} == TYPE_HASH) {
Branch: 3 branches
BranchTrueFalseTotal
if ($knot->{'_type_int'} == 0) { } 461 267 728
elsif ($knot->{'_type_int'} == 1) { } 174 93 267
elsif ($knot->{'_type_int'} == 2) { } 93 0 93
181 461 my ($key, $val) = @_;
182 461 _remove_child($knot->{_data}{$key});
183 461 ▶ _magic_tie($knot, $val, $key) if ref($val) && $knot->_need_tie($val, $key);
Branch: 1 branch
BranchTrueFalseTotal
if ref $val and $knot->_need_tie($val, $key) 93 368 461
Condition100%3/3
ref $val and $knot->_need_tie($val, $key)
!ll&&!rl&&r
367193
MC/DC100%2/2
ref $val and $knot->_need_tie($val, $key)
ref $val $knot->_need_tie($val, $key)
Truth table100%3/3
A and B
A= ref $valB= $knot->_need_tie($val, $key)
ABresult
0X0
100
111
184 460 $knot->{_data}{$key} = $val;
185 }
186 elsif ($knot->{_type_int} == TYPE_ARRAY) {
187 174 my ($i, $val) = @_;
188 174 _remove_child($knot->{_data}[$i]);
189 174 ▶ _magic_tie($knot, $val, $i) if ref($val) && $knot->_need_tie($val, $i);
Branch: 1 branch
BranchTrueFalseTotal
if ref $val and $knot->_need_tie($val, $i) 36 138 174
Condition66%2/3
ref $val and $knot->_need_tie($val, $i)
!ll&&!rl&&r
138036
MC/DC50%1/2
ref $val and $knot->_need_tie($val, $i)
ref $val $knot->_need_tie($val, $i)
Truth table66%2/3
A and B
A= ref $valB= $knot->_need_tie($val, $i)
ABresult
0X0
100
111
190 174 $knot->{_data}[$i] = $val;
191 }
192 elsif ($knot->{_type_int} == TYPE_SCALAR) {
193 93 my ($val) = @_;
194 93 ▶ if ($knot->{_data} && ref($knot->{_data})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{"_data"} and ref $knot->{"_data"}) 17 76 93
Condition66%2/3
$knot->{"_data"} and ref $knot->{"_data"}
!ll&&!rl&&r
76017
MC/DC50%1/2
$knot->{"_data"} and ref $knot->{"_data"}
$knot->{"_data"} ref $knot->{"_data"}
Truth table66%2/3
A and B
A= $knot->{"_data"}B= ref $knot->{"_data"}
ABresult
0X0
100
111
195 17 _remove_child(${$knot->{_data}});
196 }
197 93 ▶ _magic_tie($knot, $val) if ref($val) && $knot->_need_tie($val);
Branch: 1 branch
BranchTrueFalseTotal
if ref $val and $knot->_need_tie($val) 15 78 93
Condition100%3/3
ref $val and $knot->_need_tie($val)
!ll&&!rl&&r
77115
MC/DC100%2/2
ref $val and $knot->_need_tie($val)
ref $val $knot->_need_tie($val)
Truth table100%3/3
A and B
A= ref $valB= $knot->_need_tie($val)
ABresult
0X0
100
111
198 93 $knot->{_data} = \$val;
199 }
200  
201 727 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 33 694 727
202 33 $knot->{_was_changed} = 1;
203 }
204 else {
205 694 _write_to_seg($knot);
206 }
207  
208 724 return 1;
209 }
210 sub FETCH {
211 992 ▶ my $knot = shift;
Subroutine FETCH: called CC=10 SCAR=23.0
212  
213 992 my $data;
214 992 ▶ if ($knot->{_lock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'}) { } 30 962 992
215 30 $data = $knot->{_data};
216 }
217 else {
218 962 _read_check($knot);
219 962 $data = $knot->_decode($knot->seg);
220 960 $knot->{_data} = $data;
221 }
222  
223 990 my $val;
224  
225 990 ▶ if ($knot->{_type_int} == TYPE_HASH) {
Branch: 3 branches
BranchTrueFalseTotal
if ($knot->{'_type_int'} == 0) { } 646 344 990
elsif ($knot->{'_type_int'} == 1) { } 182 162 344
elsif ($knot->{'_type_int'} == 2) { } 162 0 162
226 646 my $key = shift;
227 646 $val = $data->{$key};
228 }
229 elsif ($knot->{_type_int} == TYPE_ARRAY) {
230 182 my $i = shift;
231 182 $val = $data->[$i];
232 }
233 elsif ($knot->{_type_int} == TYPE_SCALAR) {
234 162 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 153 9 162
235 153 $val = $$data;
236 }
237 else {
238 9 return;
239 }
240 }
241  
242 981 ▶ if (ref($val) && (my $inner = _is_child($val))) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and my $inner = _is_child($val)) 511 470 981
Condition66%2/3
ref $val and my $inner = _is_child($val)
!ll&&!rl&&r
4700511
MC/DC50%1/2
ref $val and my $inner = _is_child($val)
ref $val my $inner = _is_child($val)
Truth table66%2/3
A and B
A= ref $valB= my $inner = _is_child($val)
ABresult
0X0
100
111
243 # Register the inner knot so clean_up_all() can find it even when it
244 # was created in a forked child process
245  
246 511 ▶ if (! exists $global_register{$inner->seg->id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $global_register{$inner->seg->id}) 13 498 511
247 13 $global_register{$inner->seg->id} = $inner;
248 }
249  
250 511 ▶ unless ($inner->{_lock}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inner->{"_lock"}) 507 4 511
251 507 my $s = $inner->seg;
252 507 $inner->{_data} = $knot->_decode($s);
253 }
254 }
255 981 return $val;
256  
257 }
258 sub CLEAR {
259 170 ▶ my $knot = shift;
Subroutine CLEAR: called CC=8 SCAR=20.8
260  
261 170 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 1 169 170
262  
263 169 ▶ $knot->{_data} = $knot->_decode($knot->seg) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 168 1 169
264  
265 169 ▶ if ($knot->{_type_int} == TYPE_HASH) {
Branch: 2 branches
BranchTrueFalseTotal
if ($knot->{'_type_int'} == 0) { } 111 58 169
elsif ($knot->{'_type_int'} == 1) { } 58 0 58
266 111 for my $val (values %{ $knot->{_data} }) {
267 27 _remove_child($val);
268 }
269 111 $knot->{_data} = { };
270 }
271 elsif ($knot->{_type_int} == TYPE_ARRAY) {
272 58 for my $val (@{ $knot->{_data} }) {
273 20 _remove_child($val);
274 }
275 58 $knot->{_data} = [ ];
276 }
277  
278 169 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 1 168 169
279 1 $knot->{_was_changed} = 1;
280 }
281 else {
282 168 _write_to_seg($knot);
283 }
284 }
285 sub DELETE {
286 8 ▶ my $knot = shift;
Subroutine DELETE: called CC=5 SCAR=16.1
287 8 my $key = shift;
288  
289 croak "Cannot delete from a non-hash tied variable"
290 8 ▶ unless $knot->{_type_int} == TYPE_HASH;
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_type_int"} == 0 1 7 8
291  
292 7 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 1 6 7
293  
294 6 ▶ $knot->{_data} = $knot->_decode($knot->seg) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 5 1 6
295 6 my $val = delete $knot->{_data}->{$key};
296  
297 6 _remove_child($val);
298  
299 6 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 1 5 6
300 1 $knot->{_was_changed} = 1;
301 }
302 else {
303 5 _write_to_seg($knot);
304 }
305  
306 5 return $val;
307 }
308 sub EXISTS {
309 16 ▶ my $knot = shift;
Subroutine EXISTS: called CC=2 SCAR=6.9
310 16 my $key = shift;
311  
312 16 ▶ $knot->{_data} = $knot->_decode($knot->seg) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 14 2 16
313 16 return exists $knot->{_data}->{$key};
314 }
315 sub FIRSTKEY {
316 29 ▶ my $knot = shift;
Subroutine FIRSTKEY: called CC=2 SCAR=6.9
317 29 ▶ $knot->{_data} = $knot->_decode($knot->seg) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 28 1 29
318 29 $knot->{_hkey_list} = [ keys %{$knot->{_data}} ];
319 29 return $knot->NEXTKEY;
320 }
321 sub NEXTKEY {
322 76 ▶ my ($knot, $last_key_accessed) = @_;
Subroutine NEXTKEY: called CC=1 SCAR=0.0
323  
324 # We don't use ordered hashes, so we don't need to use
325 # the last key accessed parameter
326  
327 # Caveat emptor if hash was changed by another process
328  
329 76 return shift @{$knot->{_hkey_list}};
330 }
331 ▶sub EXTEND {
Subroutine EXTEND: called CC=1 SCAR=0.0
332 #XXX Noop
333 }
334 sub PUSH {
335 16 ▶ my $knot = shift;
Subroutine PUSH: called CC=5 SCAR=16.1
336  
337 croak "Cannot push to a non-array tied variable"
338 16 ▶ unless $knot->{_type_int} == TYPE_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_type_int"} == 1 1 15 16
339  
340 15 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 1 14 15
341  
342 14 ▶ $knot->{_data} = $knot->_decode($knot->seg, $knot->{_data}) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 3 11 14
343  
344 14 push @{$knot->{_data}}, @_;
345 14 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 11 3 14
346 11 $knot->{_was_changed} = 1;
347 }
348 else {
349 3 _write_to_seg($knot);
350 }
351 }
352 sub POP {
353 6 ▶ my $knot = shift;
Subroutine POP: called CC=5 SCAR=16.1
354  
355 croak "Cannot pop from a non-array tied variable"
356 6 ▶ unless $knot->{_type_int} == TYPE_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_type_int"} == 1 1 5 6
357  
358 5 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 1 4 5
359  
360 4 ▶ $knot->{_data} = $knot->_decode($knot->seg, $knot->{_data}) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 3 1 4
361  
362 4 my $val = pop @{$knot->{_data}};
363 4 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 1 3 4
364 1 $knot->{_was_changed} = 1;
365 }
366 else {
367 3 _write_to_seg($knot);
368 }
369 3 return $val;
370 }
371 sub SHIFT {
372 16 ▶ my $knot = shift;
Subroutine SHIFT: called CC=5 SCAR=16.1
373  
374 croak "Cannot shift from a non-array tied variable"
375 16 ▶ unless $knot->{_type_int} == TYPE_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_type_int"} == 1 1 15 16
376  
377 15 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 1 14 15
378  
379 14 ▶ $knot->{_data} = $knot->_decode($knot->seg, $knot->{_data}) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 3 11 14
380 14 my $val = shift @{$knot->{_data}};
381 14 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 11 3 14
382 11 $knot->{_was_changed} = 1;
383 }
384 else {
385 3 _write_to_seg($knot);
386 }
387 13 return $val;
388 }
389 sub UNSHIFT {
390 6 ▶ my $knot = shift;
Subroutine UNSHIFT: called CC=5 SCAR=16.1
391  
392 croak "Cannot unshift a non-array tied variable"
393 6 ▶ unless $knot->{_type_int} == TYPE_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_type_int"} == 1 1 5 6
394  
395 5 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 1 4 5
396  
397 4 ▶ $knot->{_data} = $knot->_decode($knot->seg, $knot->{_data}) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 3 1 4
398 4 my $val = unshift @{$knot->{_data}}, @_;
399 4 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 1 3 4
400 1 $knot->{_was_changed} = 1;
401 }
402 else {
403 3 _write_to_seg($knot);
404 }
405 3 return $val;
406 }
407 sub SPLICE {
408 6 ▶ my($knot, $off, $n, @av) = @_;
Subroutine SPLICE: called CC=5 SCAR=16.1
409  
410 croak "Cannot splice a non-array tied variable"
411 6 ▶ unless $knot->{_type_int} == TYPE_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_type_int"} == 1 1 5 6
412  
413 5 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 1 4 5
414  
415 4 ▶ $knot->{_data} = $knot->_decode($knot->seg, $knot->{_data}) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 3 1 4
416 4 my @val = splice @{$knot->{_data}}, $off, $n, @av;
417 4 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 1 3 4
418 1 $knot->{_was_changed} = 1;
419 }
420 else {
421 3 _write_to_seg($knot);
422 }
423 3 return @val;
424 }
425 sub FETCHSIZE {
426 43 ▶ my $knot = shift;
Subroutine FETCHSIZE: called CC=3 SCAR=11.0
427  
428 croak "Cannot fetchsize on a non-array tied variable"
429 43 ▶ unless $knot->{_type_int} == TYPE_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_type_int"} == 1 1 42 43
430  
431 42 ▶ $knot->{_data} = $knot->_decode($knot->seg) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 41 1 42
432 42 return scalar(@{$knot->{_data}});
433 }
434 sub STORESIZE {
435 6 ▶ my $knot = shift;
Subroutine STORESIZE: called CC=5 SCAR=16.1
436 6 my $n = shift;
437  
438 croak "Cannot storesize on a non-array tied variable"
439 6 ▶ unless $knot->{_type_int} == TYPE_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_type_int"} == 1 1 5 6
440  
441 5 ▶ return if ! _write_permitted($knot);
Branch: 1 branch
BranchTrueFalseTotal
unless _write_permitted($knot) 1 4 5
442  
443 4 ▶ $knot->{_data} = $knot->_decode($knot->seg) unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 3 1 4
444 4 $#{$knot->{_data}} = $n - 1;
445  
446 4 ▶ if ($knot->{_lock} & LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{'_lock'} & 2) { } 1 3 4
447 1 $knot->{_was_changed} = 1;
448 }
449 else {
450 3 _write_to_seg($knot);
451 }
452 3 return $n;
453 }
454  
455 # Public methods
456  
457 83 *shlock = \&lock;
458 83 *shunlock = \&unlock;
459  
460 # End user methods
461  
462 sub new {
463 7 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
464  
465 7 ▶ my $type = $opts{var} || 'HASH';
Condition100%2/2
$opts{'var'} || 'HASH'
l!l
43
MC/DC100%1/1
$opts{'var'} || 'HASH'
$opts{'var'}
Truth table100%2/2
A
A= $opts{'var'}
Aresult
11
00
466  
467 7 ▶ if ($type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 3 4 7
468 3 tie my %h, 'IPC::Shareable', \%opts;
469 3 return \%h;
470 }
471 4 ▶ if ($type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ARRAY") 2 2 4
472 2 tie my @a, 'IPC::Shareable', \%opts;
473 2 return \@a;
474 }
475 2 ▶ if ($type eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR") 2 0 2
476 2 tie my $s, 'IPC::Shareable', \%opts;
477 2 return \$s;
478 }
479 }
480 sub lock {
481 145 ▶ my $knot = shift;
Subroutine lock: called CC=18 SCAR=29.0
Pod documented
482  
483 # POD "lock($flags, $code)" documents both positional and code-only calling
484 # conventions. See =head2 lock section for the full parameter specification.
485 145 my ($flags, $code);
486  
487 145 ▶ if (scalar @_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 2) 9 136 145
488 9 ($flags, $code) = @_;
489 }
490  
491 145 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 97 48 145
492 97 ▶ if (ref $_[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 1 96 97
493 1 $code = shift;
494 }
495 else {
496 96 $flags = shift;
497 }
498 }
499  
500 145 ▶ if (defined $code && ref $code ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and ref $code ne "CODE") 1 144 145
Condition100%3/3
defined $code and ref $code ne "CODE"
!ll&&!rl&&r
13591
MC/DC100%2/2
defined $code and ref $code ne "CODE"
defined $code ref $code ne "CODE"
Truth table100%3/3
A and B
A= defined $codeB= ref $code ne "CODE"
ABresult
0X0
100
111
501 1 croak "\$code param to lock() must be a code reference";
502 }
503  
504 144 ▶ $flags = LOCK_EX if ! defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 49 95 144
505  
506 # Unlock was called
507  
508 144 ▶ return $knot->unlock if ($flags & LOCK_UN);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 8 0 144 144
509  
510 # Caller already has the same lock type
511  
512 144 ▶ if ($knot->{_lock} & $flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{"_lock"} & $flags) 3 141 144
513 3 ▶ if ($code && $flags == LOCK_EX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code and $flags == 2) 1 2 3
Condition66%2/3
$code and $flags == 2
!ll&&!rl&&r
021
MC/DC50%1/2
$code and $flags == 2
$code $flags == 2
Truth table66%2/3
A and B
A= $codeB= $flags == 2
ABresult
0X0
100
111
514 1 _execute_lock_coderef($knot, $code);
515 }
516 3 return 1;
517 }
518  
519 # If they have a different lock than they want, release it first
520  
521 141 ▶ $knot->unlock if ($knot->{_lock});
Branch: 1 branch
BranchTrueFalseTotal
if $knot->{"_lock"} 0 141 141
522  
523 141 my $sem = $knot->sem;
524 141 my $lock_success = $sem->op(@{ $semop_args{$flags} });
525  
526 141 ▶ if ($lock_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lock_success) 130 11 141
527 130 $knot->{_lock} = $flags;
528 130 $knot->{_data} = $knot->_decode($knot->seg);
529  
530 130 my $locked_ref = _lock_children($knot, $flags);
531  
532 130 ▶ if (! $locked_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $locked_ref) { } 2 128 130
533 2 my $rflags = $knot->{_lock} | LOCK_UN;
534 2 ▶ $rflags ^= LOCK_NB if $rflags & LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
if $rflags & 4 2 0 2
535 2 $knot->sem->op(@{ $semop_args{$rflags} });
536 2 $knot->{_lock} = 0;
537 2 $lock_success = 0;
538 }
539 else {
540 128 $knot->{_locked_children} = $locked_ref;
541 }
542 }
543  
544 141 ▶ if ($flags == LOCK_EX && $lock_success && $code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags == 2 and $lock_success and $code) 5 136 141
Condition83%5/6
$flags == 2 and $lock_success and $code
!ll&&!rl&&r
48885
$flags == 2 and $lock_success
!ll&&!rl&&r
48093
MC/DC66%2/3
$flags == 2 and $lock_success and $code
$flags == 2 $lock_success $code
Truth table75%3/4
A and B and C
A= $flags == 2B= $lock_successC= $code
ABCresult
0XX0
10X0
1100
1111
545 5 _execute_lock_coderef($knot, $code);
546 3 return 1;
547 }
548 136 return $lock_success;
549 }
550 sub unlock {
551 283 ▶ my $knot = shift;
Subroutine unlock: called CC=9 SCAR=22.0
Pod documented
552  
553 283 ▶ return 1 unless $knot->{_lock};
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->{"_lock"} 154 129 283
554  
555 129 ▶ if ($knot->{_was_changed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->{"_was_changed"}) 54 75 129
556 54 _write_to_seg($knot);
557 53 $knot->{_was_changed} = 0;
558 }
559  
560 128 ▶ for my $child (reverse @{ $knot->{_locked_children} // [] }) {
Condition50%1/2
$knot->{"_locked_children"} // []
l!l
1280
MC/DC0%0/1
$knot->{"_locked_children"} // []
$knot->{"_locked_children"}
Truth table50%1/2
A
A= $knot->{"_locked_children"}
Aresult
11
00
561 28 ▶ if ($child->{_was_changed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->{"_was_changed"}) 3 25 28
562 3 _write_to_seg($child);
563 3 $child->{_was_changed} = 0;
564 }
565  
566 28 my $child_flags = $child->{_lock} | LOCK_UN;
567  
568 28 ▶ $child_flags ^= LOCK_NB if $child_flags & LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
if $child_flags & 4 2 26 28
569 28 $child->sem->op(@{ $semop_args{$child_flags} });
570 28 $child->{_lock} = 0;
571 }
572  
573 128 $knot->{_locked_children} = [];
574  
575 128 my $sem = $knot->sem;
576 128 my $flags = $knot->{_lock} | LOCK_UN;
577  
578 128 ▶ $flags ^= LOCK_NB if ($flags & LOCK_NB);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 4 7 121 128
579  
580 128 ▶ if (! $sem->op(@{ $semop_args{$flags} })) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sem->op(@{$semop_args{$flags};})) 1 127 128
581 1 croak "Could not release semaphore lock: $!\n";
582 }
583  
584 127 $knot->{_lock} = 0;
585  
586 127 1;
587 }
588 sub singleton {
589  
590 # If called with IPC::Shareable::singleton() as opposed to
591 # IPC::Shareable->singleton(), the class isn't sent in. Check
592 # for this and fix it if necessary
593  
594 8 ▶ if (! defined $_[0] || $_[0] ne __PACKAGE__) {
Subroutine singleton: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0] or $_[0] ne __PACKAGE__) 3 5 8
Condition100%3/3
not defined $_[0] or $_[0] ne __PACKAGE__
l!l&&r!l&&!r
125
MC/DC100%2/2
not defined $_[0] or $_[0] ne __PACKAGE__
not defined $_[0] $_[0] ne __PACKAGE__
Truth table100%3/3
not(A) or B
A= not defined $_[0]B= $_[0] ne __PACKAGE__
ABresult
1X1
011
000
595 3 unshift @_, __PACKAGE__;
596 }
597  
598 8 my ($class, $glue, $warn) = @_;
599  
600 8 ▶ if (! defined $glue) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $glue) 2 6 8
601 2 croak "singleton() requires a GLUE parameter";
602 }
603  
604 6 ▶ $warn = 0 if ! defined $warn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $warn 4 2 6
605  
606 6 tie my $lock, 'IPC::Shareable', {
607 key => $glue,
608 create => 1,
609 exclusive => 1,
610 graceful => 1,
611 destroy => 1,
612 warn => $warn
613 };
614  
615 3 return $$;
616 }
617  
618 # Helper, maintenance and developer methods
619  
620 sub attributes {
621 13711 ▶ my ($knot, $attr) = @_;
Subroutine attributes: called CC=2 SCAR=6.9
Pod documented
622  
623 13711 ▶ if (defined $attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr) { } 13489 222 13711
624 13489 return $knot->{attributes}{$attr};
625 }
626 else {
627 222 return $knot->{attributes};
628 }
629 }
630 sub global_register {
631 122 ▶ return \%global_register;
Subroutine global_register: called CC=1 SCAR=0.0
Pod documented
632 }
633 sub process_register {
634 11 ▶ return \%process_register;
Subroutine process_register: called CC=1 SCAR=0.0
Pod documented
635 }
636 sub uuid {
637 567 ▶ my ($knot) = @_;
Subroutine uuid: called CC=2 SCAR=6.9
Pod documented
638  
639 567 ▶ if (! defined $knot->{_uuid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $knot->{"_uuid"}) 540 27 567
640 540 $knot->{_uuid} = md5_hex(rand());
641 }
642  
643 567 return $knot->{_uuid};
644 }
645  
646 sub seg {
647 6967 ▶ my ($knot) = @_;
Subroutine seg: called CC=2 SCAR=7.2
Pod documented
648 6967 ▶ return $knot->{_shm} if defined $knot->{_shm};
Branch: 1 branch
BranchTrueFalseTotal
if defined $knot->{"_shm"} 6967 0 6967
649 }
650 sub sem {
651 2550 ▶ my ($knot) = @_;
Subroutine sem: called CC=2 SCAR=7.2
Pod documented
652 2550 ▶ return $knot->{_sem} if defined $knot->{_sem};
Branch: 1 branch
BranchTrueFalseTotal
if defined $knot->{"_sem"} 2550 0 2550
653 }
654  
655 sub shm_segments {
656 45 ▶ shift if ref($_[0]) || (defined $_[0] && ! ref($_[0]) && UNIVERSAL::isa($_[0], __PACKAGE__));
Subroutine shm_segments: called CC=35 SCAR=43.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] or defined $_[0] and not ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__) 36 9 45
Condition77%7/9
ref $_[0] or defined $_[0] and not ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
l!l&&r!l&&!r
1395
defined $_[0] and not ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
!ll&&!rl&&r
9035
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
9035
MC/DC0%0/4
ref $_[0] or defined $_[0] and not ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
ref $_[0] defined $_[0] not ref $_[0] UNIVERSAL::isa($_[0], __PACKAGE__)
Truth table40%2/5
A or B and not(C) and D
A= ref $_[0]B= defined $_[0]C= not ref $_[0]D= UNIVERSAL::isa($_[0], __PACKAGE__)
ABCDresult
1XXX1
01111
00XX0
010X0
01100
657  
658 45 my ($filter_key) = @_;
659  
660 45 ▶ my $filter_int = _key_str_to_int($filter_key) if defined $filter_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter_key 2 43 45
661  
662 45 my %segments;
663  
664 45 ▶ open my $ipcs_fh, '-|', 'ipcs', '-m' or die "ipcs -m: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ipcs_fh, "-|", "ipcs", "-m" or die "ipcs -m: $!" 0 45 45
665 45 ▶ while (my $line = <$ipcs_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $ipcs_fh)) 420 45 465
666 420 my ($id, $raw_key);
667  
668 420 ▶ if ($line =~ /^\s*m\s+(\d+)\s+(\S+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s*m\s+(\d+)\s+(\S+)/) { } 0 420 420
elsif ($line =~ /^\s*(\d+)\s+(0x[0-9a-fA-F]+)\s+/) { } 0 420 420
elsif ($line =~ /^\s*(\S+)\s+(\d+)\s+\S+/) { } 240 180 420
669 # BSD/macOS format: m <shmid> <key> ...
670 0 ($id, $raw_key) = ($1, $2);
671 }
672 elsif ($line =~ /^\s*(\d+)\s+(0x[0-9a-fA-F]+)\s+/) {
673 # DragonFly BSD format: <shmid> <hex_key> ... (no 'm' type column)
674 0 ($id, $raw_key) = ($1, $2);
675 }
676 elsif ($line =~ /^\s*(\S+)\s+(\d+)\s+\S+/) {
677 # Linux format: <key> <shmid> ...
678 240 ($raw_key, $id) = ($1, $2);
679 }
680 else {
681 180 next;
682 }
683  
684 240 ▶ my $key_int = $raw_key =~ /^0x[0-9a-fA-F]+$/
Branch: 2 branches
BranchTrueFalseTotal
$raw_key =~ /^0x[0-9a-fA-F]+$/ ? : 240 0 240
$raw_key =~ /^\d+$/ ? : 0 0 0
685 ? hex($raw_key)
686 : $raw_key =~ /^\d+$/
687 ? int($raw_key)
688 : next;
689  
690 240 my $hex_key = sprintf('0x%08x', $key_int);
691  
692 240 ▶ next if $key_int == 0; # IPC_PRIVATE segments can't be found by key
Branch: 1 branch
BranchTrueFalseTotal
if $key_int == 0 0 240 240
693  
694 # Get segment size via IPC_STAT
695  
696 240 my $stat_buf = '';
697 240 ▶ shmctl($id, IPC_STAT, $stat_buf) or next;
Branch: 1 branch
BranchTrueFalseTotal
shmctl $id, IPC_STAT(), $stat_buf or next 0 240 240
698  
699 my ($segsz) = $^O eq 'linux'
700 ? ( $Config{longsize} == 8
701 ? unpack('x[48] Q', $stat_buf) # 64-bit Linux
702 : unpack('x[36] L', $stat_buf) ) # 32-bit Linux
703 : $^O eq 'freebsd' && $Config{longsize} == 8
704 ? unpack('x[32] Q', $stat_buf) # 64-bit FreeBSD (key_t=long=8, ipc_perm=32)
705 : $^O eq 'solaris'
706 ? ( $Config{longsize} == 8
707 ? unpack('x[32] Q', $stat_buf) # 64-bit Solaris (ipc_perm=28 + pad 4)
708 : unpack('x[44] L', $stat_buf) ) # 32-bit Solaris (ipc_perm=44)
709 : $^O eq 'openbsd' && $Config{longsize} == 8
710 ? unpack('x[32] L', $stat_buf) # 64-bit OpenBSD: segsz is int (4 bytes)
711 240 ▶ : $^O eq 'dragonfly' && $Config{longsize} == 8
Branch: 7 branches
BranchTrueFalseTotal
$^O eq 'linux' ? : 240 0 240
$IPC::Shareable::Config{'longsize'} == 8 ? : 240 0 240
$^O eq 'freebsd' && $IPC::Shareable::Config{'longsize'} == 8 ? : 0 0 0
$^O eq 'solaris' ? : 0 0 0
$IPC::Shareable::Config{'longsize'} == 8 ? : 0 0 0
$^O eq 'openbsd' && $IPC::Shareable::Config{'longsize'} == 8 ? : 0 0 0
$^O eq 'dragonfly' && $IPC::Shareable::Config{'longsize'} == 8 ? : 0 0 0
Condition0%0/9
$^O eq "freebsd" and $IPC::Shareable::Config{"longsize"} == 8
!ll&&!rl&&r
000
$^O eq "openbsd" and $IPC::Shareable::Config{"longsize"} == 8
!ll&&!rl&&r
000
$^O eq "dragonfly" and $IPC::Shareable::Config{"longsize"} == 8
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "freebsd" and $IPC::Shareable::Config{"longsize"} == 8
$^O eq "freebsd" $IPC::Shareable::Config{"longsize"} == 8
MC/DC0%0/2
$^O eq "openbsd" and $IPC::Shareable::Config{"longsize"} == 8
$^O eq "openbsd" $IPC::Shareable::Config{"longsize"} == 8
MC/DC0%0/2
$^O eq "dragonfly" and $IPC::Shareable::Config{"longsize"} == 8
$^O eq "dragonfly" $IPC::Shareable::Config{"longsize"} == 8
Truth table0%0/3
A and B
A= $^O eq "freebsd"B= $IPC::Shareable::Config{"longsize"} == 8
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $^O eq "openbsd"B= $IPC::Shareable::Config{"longsize"} == 8
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $^O eq "dragonfly"B= $IPC::Shareable::Config{"longsize"} == 8
ABresult
0X0
100
111
712 ? unpack('x[32] Q', $stat_buf) # 64-bit DragonFly (ipc_perm=28 + pad 4; segsz=size_t=8)
713 : unpack('x[24] Q', $stat_buf); # macOS
714  
715 240 ▶ next unless $segsz;
Branch: 1 branch
BranchTrueFalseTotal
unless $segsz 0 240 240
716  
717 # Probe the 14-byte tag first so we don't pull entire foreign
718 # segments (which may be gigabytes) into Perl just to discard them.
719  
720 240 my $head = '';
721 240 ▶ shmread($id, $head, 0, 14) or next;
Branch: 1 branch
BranchTrueFalseTotal
shmread $id, $head, 0, 14 or next 0 240 240
722 240 ▶ next unless $head eq 'IPC::Shareable';
Branch: 1 branch
BranchTrueFalseTotal
unless $head eq "IPC::Shareable" 2 238 240
723  
724 238 my $data = '';
725 238 ▶ shmread($id, $data, 0, $segsz) or next;
Branch: 1 branch
BranchTrueFalseTotal
shmread $id, $data, 0, $segsz or next 0 238 238
726  
727 # Strip trailing null bytes
728 238 $data =~ s/\x00+$//;
729  
730 238 my $json_part = substr($data, 14);
731 238 my @child_keys = ($json_part =~ /"child_key_hex":"([^"]+)"/g);
732  
733 $segments{$hex_key} = {
734 child_keys => \@child_keys,
735 content => $data,
736 id => $id,
737 local_process => (exists $process_register{$id} ? 1 : 0),
738 238 ▶ known => (exists $global_register{$id} ? 1 : 0),
Branch: 2 branches
BranchTrueFalseTotal
exists $process_register{$id} ? : 180 58 238
exists $global_register{$id} ? : 180 58 238
739 };
740 }
741 45 close $ipcs_fh;
742  
743 45 ▶ if (defined $filter_int) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter_int) 2 43 45
744 # Walk the segment tree starting from the root whose key matches
745 # $filter_int, collecting it and all its descendants. Use integer
746 # comparison so that hex formatting differences (zero-padding, case)
747 # between ipcs(1) output and child_key_hex values don't matter.
748  
749 2 my %int_to_hex = map { hex($_) => $_ } keys %segments;
750 2 my (%related, @queue);
751 2 push @queue, $filter_int;
752 2 ▶ while (my $k_int = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $k_int = shift @queue) 3 2 5
753 3 ▶ my $k_hex = $int_to_hex{$k_int} // next;
Condition100%2/2
$int_to_hex{$k_int} // (next)
l!l
21
MC/DC100%1/1
$int_to_hex{$k_int} // (next)
$int_to_hex{$k_int}
Truth table100%2/2
A
A= $int_to_hex{$k_int}
Aresult
11
00
754 2 ▶ next if $related{$k_hex}++;
Branch: 1 branch
BranchTrueFalseTotal
if $related{$k_hex}++ 0 2 2
755 2 push @queue, map { hex($_) } @{ $segments{$k_hex}{child_keys} };
756 }
757 2 %segments = map { $_ => $segments{$_} } keys %related;
758 }
759  
760 45 return \%segments;
761 }
762 sub unknown_segments {
763 4 ▶ shift if ref $_[0]; # Allow for object or class method call
Subroutine unknown_segments: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 1 3 4
764  
765 4 my $segs = shm_segments();
766  
767 4 return grep { ! $segs->{$_}{known} } keys %$segs;
768 }
769 sub seg_count {
770 161 ▶ my $count = 0;
Subroutine seg_count: called CC=6 SCAR=19.0
Pod documented
771  
772 161 ▶ open my $ipcs_fh, '-|', 'ipcs', '-m' or die "ipcs -m: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ipcs_fh, "-|", "ipcs", "-m" or die "ipcs -m: $!" 0 161 161
773 161 ▶ while (my $line = <$ipcs_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $ipcs_fh)) 867 161 1028
774 # BSD/macOS format: m <shmid> <key> ...
775 # DragonFly BSD: <shmid> <hex_key> ... (no type-letter column)
776 # Linux format: <key> <shmid> ...
777 867 ▶ if ($line =~ /^\s*m\s+\d+\s+\S+/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s*m\s+\d+\s+\S+/) { } 0 867 867
elsif ($line =~ /^\s*\d+\s+0x[0-9a-fA-F]+\s+/) { } 0 867 867
elsif ($line =~ /^\s*(?:0x[0-9a-fA-F]+|\d+)\s+\d+\s+\S+/) { } 223 644 867
778 0 $count++;
779 }
780 elsif ($line =~ /^\s*\d+\s+0x[0-9a-fA-F]+\s+/) {
781 0 $count++;
782 }
783 elsif ($line =~ /^\s*(?:0x[0-9a-fA-F]+|\d+)\s+\d+\s+\S+/) {
784 223 $count++;
785 }
786 }
787 161 close $ipcs_fh;
788  
789 161 return $count;
790 }
791 sub sem_count {
792 130 ▶ my $count = 0;
Subroutine sem_count: called CC=6 SCAR=19.0
Pod documented
793  
794 130 ▶ open my $ipcs_fh, '-|', 'ipcs', '-s' or die "ipcs -s: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ipcs_fh, "-|", "ipcs", "-s" or die "ipcs -s: $!" 0 130 130
795 130 ▶ while (my $line = <$ipcs_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $ipcs_fh)) 648 130 778
796 # BSD/macOS format: s <semid> <key> ...
797 # DragonFly BSD: <semid> <hex_key> ... (no type-letter column)
798 # Linux format: <key> <semid> ...
799 648 ▶ if ($line =~ /^\s*s\s+\d+\s+\S+/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s*s\s+\d+\s+\S+/) { } 0 648 648
elsif ($line =~ /^\s*\d+\s+0x[0-9a-fA-F]+\s+/) { } 0 648 648
elsif ($line =~ /^\s*(?:0x[0-9a-fA-F]+|\d+)\s+\d+\s+\S+/) { } 128 520 648
800 0 $count++;
801 }
802 elsif ($line =~ /^\s*\d+\s+0x[0-9a-fA-F]+\s+/) {
803 0 $count++;
804 }
805 elsif ($line =~ /^\s*(?:0x[0-9a-fA-F]+|\d+)\s+\d+\s+\S+/) {
806 128 $count++;
807 }
808 }
809 130 close $ipcs_fh;
810  
811 130 return $count;
812 }
813 sub seg_map {
814 6 ▶ croak "seg_map() must be called as an object method" unless ref $_[0];
Subroutine seg_map: called CC=30 SCAR=37.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 1 5 6
815 5 my $knot_filter = shift;
816  
817 5 my $segs = shm_segments();
818  
819 # Build hex_key -> OS segment ID from shm_segments() data
820 # (already parsed the ipcs output, no need to shell out again).
821  
822 5 my %id_by_hex;
823 5 $id_by_hex{ $_ } = $segs->{$_}{id} for keys %$segs;
824  
825 # Build hex_key -> knot from global_register (keyed by seg_id)
826  
827 5 my %knot_by_hex;
828 5 for my $id (keys %global_register) {
829 8 my $knot = $global_register{$id};
830 8 my $hex = $knot->{_key_hex};
831 8 ▶ $knot_by_hex{$hex} = $knot if defined $hex;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hex 8 0 8
832 }
833  
834 # Supplement child_keys from global_register for Storable segments.
835 # shm_segments() only extracts child_key_hex from JSON segment content;
836 # for Storable we walk each knot's _data looking for tied child references.
837  
838 5 my %extra_child_keys; # hex_key -> [ child_hex, ... ]
839 5 for my $hex (keys %knot_by_hex) {
840 8 my $knot = $knot_by_hex{$hex};
841 8 my $data = $knot->{_data};
842 8 ▶ my $rtype = Scalar::Util::reftype($data) // '';
Condition50%1/2
builtin::reftype($data) // ""
l!l
80
MC/DC0%0/1
builtin::reftype($data) // ""
builtin::reftype($data)
Truth table50%1/2
A
A= builtin::reftype($data)
Aresult
11
00
843  
844 8 ▶ my @vals = $rtype eq 'HASH' ? values %$data
Branch: 2 branches
BranchTrueFalseTotal
$rtype eq 'HASH' ? : 3 5 8
$rtype eq 'ARRAY' ? : 0 5 5
845 : $rtype eq 'ARRAY' ? @$data
846 : ();
847  
848 8 for my $v (@vals) {
849 3 ▶ next unless ref($v);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 2 1 3
850 1 ▶ my $vtype = Scalar::Util::reftype($v) // '';
Condition50%1/2
builtin::reftype($v) // ""
l!l
10
MC/DC0%0/1
builtin::reftype($v) // ""
builtin::reftype($v)
Truth table50%1/2
A
A= builtin::reftype($v)
Aresult
11
00
851 1 my $child_knot;
852 1 ▶ if ($vtype eq 'HASH') { $child_knot = tied(%$v) }
Branch: 3 branches
BranchTrueFalseTotal
if ($vtype eq 'HASH') { } 1 0 1
elsif ($vtype eq 'ARRAY') { } 0 0 0
elsif ($vtype eq 'SCALAR') { } 0 0 0
853 0 elsif ($vtype eq 'ARRAY') { $child_knot = tied(@$v) }
854 0 elsif ($vtype eq 'SCALAR') { $child_knot = tied($$v) }
855 1 ▶ next unless $child_knot && $child_knot->{_key_hex};
Branch: 1 branch
BranchTrueFalseTotal
unless $child_knot and $child_knot->{"_key_hex"} 0 1 1
Condition33%1/3
$child_knot and $child_knot->{"_key_hex"}
!ll&&!rl&&r
001
MC/DC0%0/2
$child_knot and $child_knot->{"_key_hex"}
$child_knot $child_knot->{"_key_hex"}
Truth table33%1/3
A and B
A= $child_knotB= $child_knot->{"_key_hex"}
ABresult
0X0
100
111
856 1 push @{ $extra_child_keys{$hex} }, $child_knot->{_key_hex};
857 }
858 }
859  
860 # If called as an object method, restrict output to just that knot's tree
861 # by BFS through both child_keys (JSON) and extra_child_keys (Storable).
862  
863 5 ▶ if ($knot_filter && $knot_filter->{_key_hex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot_filter and $knot_filter->{"_key_hex"}) 5 0 5
Condition33%1/3
$knot_filter and $knot_filter->{"_key_hex"}
!ll&&!rl&&r
005
MC/DC0%0/2
$knot_filter and $knot_filter->{"_key_hex"}
$knot_filter $knot_filter->{"_key_hex"}
Truth table33%1/3
A and B
A= $knot_filterB= $knot_filter->{"_key_hex"}
ABresult
0X0
100
111
864 5 my $root_hex = $knot_filter->{_key_hex};
865 5 my (%in_tree, @queue);
866 5 push @queue, $root_hex;
867 5 ▶ while (my $h = shift @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = shift @queue) 6 5 11
868 6 ▶ next if $in_tree{$h}++;
Branch: 1 branch
BranchTrueFalseTotal
if $in_tree{$h}++ 0 6 6
869 6 ▶ push @queue, @{ $segs->{$h}{child_keys} // [] };
Condition50%1/2
$segs->{$h}{"child_keys"} // []
l!l
60
MC/DC0%0/1
$segs->{$h}{"child_keys"} // []
$segs->{$h}{"child_keys"}
Truth table50%1/2
A
A= $segs->{$h}{"child_keys"}
Aresult
11
00
870 6 ▶ push @queue, @{ $extra_child_keys{$h} // [] };
Condition100%2/2
$extra_child_keys{$h} // []
l!l
15
MC/DC100%1/1
$extra_child_keys{$h} // []
$extra_child_keys{$h}
Truth table100%2/2
A
A= $extra_child_keys{$h}
Aresult
11
00
871 }
872 5 %$segs = map { $_ => $segs->{$_} } grep { $in_tree{$_} } keys %$segs;
873 }
874  
875 # Identify root segments (not a child of any other segment)
876  
877 5 my %is_child;
878 5 for my $hex (keys %$segs) {
879 6 $is_child{$_}++ for @{ $segs->{$hex}{child_keys} };
880 }
881 5 for my $hex (keys %extra_child_keys) {
882 1 ▶ next unless exists $segs->{$hex};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $segs->{$hex} 0 1 1
883 1 $is_child{$_}++ for @{ $extra_child_keys{$hex} };
884 }
885 5 my @roots = sort grep { ! $is_child{$_} } keys %$segs;
886  
887 5 my @lines;
888 5 push @lines, 'IPC::Shareable Segment Map';
889 5 push @lines, '=' x 26;
890  
891 5 ▶ if (! @roots) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@roots) 0 5 5
892 0 push @lines, '';
893 0 push @lines, ' (no IPC::Shareable segments found)';
894 0 return join("\n", @lines) . "\n";
895 }
896  
897 5 my $render;
898 $render = sub {
899 6 ▶ my ($hex, $depth) = @_;
Subroutine __ANON__: called CC=17 SCAR=30.1
900 6 my $indent = ' ' x $depth;
901 6 ▶ my $seg = $segs->{$hex} // {};
Condition50%1/2
$segs->{$hex} // {}
l!l
60
MC/DC0%0/1
$segs->{$hex} // {}
$segs->{$hex}
Truth table50%1/2
A
A= $segs->{$hex}
Aresult
11
00
902  
903 6 my @tags;
904 6 ▶ push @tags, $seg->{known} ? 'known' : 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
$seg->{'known'} ? : 6 0 6
905 6 ▶ push @tags, 'owner' if $seg->{local_process};
Branch: 1 branch
BranchTrueFalseTotal
if $seg->{"local_process"} 6 0 6
906 6 my $tag_str = '[' . join(', ', @tags) . ']';
907  
908 6 ▶ my $seg_id = $id_by_hex{$hex} // '?';
Condition50%1/2
$id_by_hex{$hex} // "?"
l!l
60
MC/DC0%0/1
$id_by_hex{$hex} // "?"
$id_by_hex{$hex}
Truth table50%1/2
A
A= $id_by_hex{$hex}
Aresult
11
00
909  
910 # Read semaphore slot values and ID; for segments not in
911 # global_register attach with nsems=0 (avoids EINVAL on existing sets)
912  
913 6 my ($sem_str, $content_str);
914 my $sem = $knot_by_hex{$hex}
915 6 ▶ ? $knot_by_hex{$hex}->sem
Branch: 1 branch
BranchTrueFalseTotal
$knot_by_hex{$hex} ? : 6 0 6
916 : IPC::Semaphore->new(hex($hex), 0, 0);
917  
918 6 ▶ if (defined $sem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sem) { } 6 0 6
919 6 ▶ my $sem_id = $sem->id // '?';
Condition50%1/2
$sem->id // "?"
l!l
60
MC/DC0%0/1
$sem->id // "?"
$sem->id
Truth table50%1/2
A
A= $sem->id
Aresult
11
00
920 6 ▶ my $marker = $sem->getval(SEM_MARKER) // '?';
Condition50%1/2
$sem->getval(0) // "?"
l!l
60
MC/DC0%0/1
$sem->getval(0) // "?"
$sem->getval(0)
Truth table50%1/2
A
A= $sem->getval(0)
Aresult
11
00
921 6 ▶ my $readers = $sem->getval(SEM_READERS) // '?';
Condition50%1/2
$sem->getval(1) // "?"
l!l
60
MC/DC0%0/1
$sem->getval(1) // "?"
$sem->getval(1)
Truth table50%1/2
A
A= $sem->getval(1)
Aresult
11
00
922 6 ▶ my $writers = $sem->getval(SEM_WRITERS) // '?';
Condition50%1/2
$sem->getval(2) // "?"
l!l
60
MC/DC0%0/1
$sem->getval(2) // "?"
$sem->getval(2)
Truth table50%1/2
A
A= $sem->getval(2)
Aresult
11
00
923 6 ▶ my $protected = $sem->getval(SEM_PROTECTED) // '?';
Condition50%1/2
$sem->getval(3) // "?"
l!l
60
MC/DC0%0/1
$sem->getval(3) // "?"
$sem->getval(3)
Truth table50%1/2
A
A= $sem->getval(3)
Aresult
11
00
924 # Continuation indent: one tab (8 spaces) from the left margin
925 6 my $cont = ' ' x 8;
926 6 $sem_str = join("\n",
927 "sem_id: $sem_id",
928 "${cont}1: SEM_MARKER=$marker",
929 "${cont}2: READERS=$readers",
930 "${cont}3: WRITERS=$writers",
931 "${cont}4: PROTECTED=$protected",
932 );
933 }
934 else {
935 0 $sem_str = '(not accessible)';
936 }
937  
938 $content_str = $knot_by_hex{$hex}
939 6 ▶ ? _shm_data_summary($knot_by_hex{$hex})
Branch: 1 branch
BranchTrueFalseTotal
$knot_by_hex{$hex} ? : 6 0 6
940 : '(not accessible - segment not tied in this process)';
941  
942 # Merge child keys from shm_segments() and from global_register walk
943  
944 6 my %seen_child;
945 1 my @child_keys = grep { ! $seen_child{$_}++ } (
946 6 ▶ @{ $seg->{child_keys} // [] },
Condition50%1/2
$seg->{"child_keys"} // []
l!l
60
MC/DC0%0/1
$seg->{"child_keys"} // []
$seg->{"child_keys"}
Truth table50%1/2
A
A= $seg->{"child_keys"}
Aresult
11
00
947 6 ▶ @{ $extra_child_keys{$hex} // [] },
Condition100%2/2
$extra_child_keys{$hex} // []
l!l
15
MC/DC100%1/1
$extra_child_keys{$hex} // []
$extra_child_keys{$hex}
Truth table100%2/2
A
A= $extra_child_keys{$hex}
Aresult
11
00
948 );
949 6 ▶ my $children = @child_keys ? join(', ', @child_keys) : '(none)';
Branch: 1 branch
BranchTrueFalseTotal
@child_keys ? : 1 5 6
950  
951 6 push @lines, '';
952 6 push @lines, "${indent}${tag_str} key: ${hex} seg_id: ${seg_id}";
953 6 push @lines, "${indent} Semaphores: ${sem_str}";
954 6 push @lines, "${indent} Children: ${children}";
955 6 push @lines, "${indent} Content: ${content_str}";
956  
957 6 $render->($_, $depth + 1) for @child_keys;
958 5 };
959  
960 5 $render->($_, 0) for @roots;
961  
962 5 push @lines, '';
963 5 return join("\n", @lines) . "\n";
964 }
965 sub sysv_info {
966 8 ▶ shift; # Discard invocant (object ref or class name)
Subroutine sysv_info: called CC=23 SCAR=41.8
Pod documented
967 8 my %opts = @_;
968 8 ▶ my $proc_dir = delete $opts{_proc_dir} // '/proc/sys/kernel';
Condition100%2/2
delete $opts{"_proc_dir"} // "/proc/sys/kernel"
l!l
17
MC/DC100%1/1
delete $opts{"_proc_dir"} // "/proc/sys/kernel"
delete $opts{"_proc_dir"}
Truth table100%2/2
A
A= delete $opts{"_proc_dir"}
Aresult
11
00
969 8 my $sysctl_out = delete $opts{_sysctl_out};
970  
971 8 my %info;
972  
973 8 ▶ if ($^O eq 'darwin') {
Branch: 4 branches
BranchTrueFalseTotal
if ($^O eq 'darwin') { } 0 8 8
elsif ($^O eq 'freebsd') { } 1 7 8
elsif ($^O eq 'openbsd') { } 1 6 7
elsif ($^O eq 'linux') { } 5 1 6
974 0 ▶ my $out = defined $sysctl_out ? $sysctl_out : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $sysctl_out ? : 0 0 0
975 0 ▶ open my $fh, '-|', 'sysctl', 'kern.sysv' or die "sysctl: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "sysctl", "kern.sysv" or die "sysctl: $!" 0 0 0
976 0 local $/;
977 0 my $s = <$fh>;
978 0 close $fh;
979 0 $s;
980 };
981 0 for my $line (split /\n/, $out) {
982 0 ▶ if ($line =~ /^kern\.sysv\.(\w+):\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^kern\.sysv\.(\w+):\s*(\S+)/) 0 0 0
983 0 $info{$1} = $2;
984 }
985 }
986 }
987 elsif ($^O eq 'freebsd') {
988 1 ▶ my $out = defined $sysctl_out ? $sysctl_out : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $sysctl_out ? : 1 0 1
989 0 ▶ open my $fh, '-|', 'sysctl', 'kern.ipc' or die "sysctl: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "sysctl", "kern.ipc" or die "sysctl: $!" 0 0 0
990 0 local $/;
991 0 my $s = <$fh>;
992 0 close $fh;
993 0 $s;
994 };
995 1 for my $line (split /\n/, $out) {
996 10 ▶ if ($line =~ /^kern\.ipc\.((?:shm|sem)\w+):\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^kern\.ipc\.((?:shm|sem)\w+):\s*(\S+)/) 9 1 10
997 9 $info{$1} = $2;
998 }
999 }
1000 }
1001 elsif ($^O eq 'openbsd') {
1002 1 ▶ my $out = defined $sysctl_out ? $sysctl_out : do {
Branch: 1 branch
BranchTrueFalseTotal
defined $sysctl_out ? : 1 0 1
1003 0 ▶ open my $fh, '-|', 'sysctl', 'kern.seminfo', 'kern.shminfo'
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", "sysctl", "kern.seminfo", "kern.shminfo" or die "sysctl: $!" 0 0 0
1004 or die "sysctl: $!";
1005 0 local $/;
1006 0 my $s = <$fh>;
1007 0 close $fh;
1008 0 $s;
1009 };
1010 1 for my $line (split /\n/, $out) {
1011 7 ▶ if ($line =~ /^kern\.(?:sem|shm)info\.(\w+)\s*=\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^kern\.(?:sem|shm)info\.(\w+)\s*=\s*(\S+)/) 7 0 7
1012 7 $info{$1} = $2;
1013 }
1014 }
1015 }
1016 elsif ($^O eq 'linux') {
1017 5 for my $key (qw(shmmax shmmin shmmni shmall)) {
1018 20 my $file = "$proc_dir/$key";
1019 20 ▶ if (open my $fh, '<', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $file) 16 4 20
1020 16 chomp(my $val = <$fh>);
1021 16 $info{$key} = $val;
1022 }
1023 }
1024 # /proc/sys/kernel/sem is a single line of 4 ints:
1025 # semmsl semmns semopm semmni
1026 5 ▶ if (open my $fh, '<', "$proc_dir/sem") {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", "$proc_dir/sem") 5 0 5
1027 5 chomp(my $line = <$fh>);
1028 5 close $fh;
1029 5 my @vals = split /\s+/, $line;
1030 5 ▶ if (@vals >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals >= 4) 5 0 5
1031 5 @info{qw(semmsl semmns semopm semmni)} = @vals[0..3];
1032 }
1033 }
1034 }
1035  
1036 8 ▶ return %info ? \%info : undef;
Branch: 1 branch
BranchTrueFalseTotal
%info ? : 7 1 8
1037 }
1038  
1039 sub clean_up {
1040 9 ▶ my $class = shift;
Subroutine clean_up: called CC=4 SCAR=14.2
Pod documented
1041  
1042 9 for my $id (keys %process_register) {
1043 8 my $s = $process_register{$id};
1044 8 ▶ next unless $s->attributes('owner') == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->attributes("owner") == $$ 0 8 8
1045 8 ▶ next if $s->attributes('protected');
Branch: 1 branch
BranchTrueFalseTotal
if $s->attributes("protected") 0 8 8
1046 8 remove($s);
1047 }
1048 }
1049 sub clean_up_all {
1050 94 ▶ my $class = shift;
Subroutine clean_up_all: called CC=3 SCAR=11.0
Pod documented
1051  
1052 94 my $global_register = __PACKAGE__->global_register;
1053  
1054 94 for my $id (keys %$global_register) {
1055 216 my $s = $global_register->{$id};
1056 216 ▶ next if $s->attributes('protected');
Branch: 1 branch
BranchTrueFalseTotal
if $s->attributes("protected") 3 213 216
1057 213 remove($s);
1058 }
1059 }
1060 sub clean_up_protected {
1061 9 ▶ my ($knot, $protect_key);
Subroutine clean_up_protected: called CC=8 SCAR=20.9
Pod documented
1062  
1063 9 ▶ if (scalar @_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 2) 5 4 9
1064 5 ($knot, $protect_key) = @_;
1065 }
1066 9 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 3 6 9
1067 3 ($protect_key) = @_;
1068 }
1069  
1070 9 ▶ if (! defined $protect_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $protect_key) 1 8 9
1071 1 croak "clean_up_protected() requires a \$protect_key param";
1072 }
1073  
1074 8 ▶ if ($protect_key !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($protect_key =~ /^\d+$/) 1 7 8
1075 1 croak
1076 "clean_up_protected() \$protect_key must be an integer. You sent $protect_key";
1077 }
1078  
1079 7 my $global_register = __PACKAGE__->global_register;
1080  
1081 7 for my $id (keys %$global_register) {
1082 8 my $s = $global_register->{$id};
1083 8 my $stored_key = $s->attributes('protected');
1084  
1085 8 ▶ if ($stored_key && $stored_key == $protect_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stored_key and $stored_key == $protect_key) 8 0 8
Condition33%1/3
$stored_key and $stored_key == $protect_key
!ll&&!rl&&r
008
MC/DC0%0/2
$stored_key and $stored_key == $protect_key
$stored_key $stored_key == $protect_key
Truth table33%1/3
A and B
A= $stored_keyB= $stored_key == $protect_key
ABresult
0X0
100
111
1086 8 remove($s);
1087 }
1088 }
1089 }
1090 sub remove {
1091 359 ▶ my ($knot, $key) = @_;
Subroutine remove: called CC=11 SCAR=24.2
Pod documented
1092  
1093 # If a key is passed, remove that specific segment by key rather than
1094 # via an existing tied object
1095  
1096 359 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 10 349 359
1097 10 $key = $knot->_shm_key($key);
1098 10 my $id = shmget($key, 0, 0);
1099  
1100 10 ▶ if (! defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 1 9 10
1101 1 warn "remove(): shmget failed for key $key: $!";
1102 1 return;
1103 }
1104  
1105 9 ▶ if (! shmctl($id, IPC_RMID, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not shmctl($id, IPC_RMID(), 0)) { } 0 9 9
1106 0 warn "Couldn't remove shm segment $id: $!";
1107 }
1108 else {
1109 9 delete $process_register{$id};
1110 9 delete $global_register{$id};
1111 }
1112  
1113 # Remove the associated semaphore set (same key, attach-only with nsems=0)
1114  
1115 9 my $sem = IPC::Semaphore->new($key, 0, 0);
1116 9 ▶ if (defined $sem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sem) 1 8 9
1117 1 ▶ $sem->remove or warn "Couldn't remove semaphore set for key $key: $!";
Branch: 1 branch
BranchTrueFalseTotal
$sem->remove or warn "Couldn't remove semaphore set for key ${key}: $!" 0 1 1
1118 }
1119  
1120 9 return;
1121 }
1122  
1123 # Standard object based removal
1124  
1125 349 my $seg = $knot->seg;
1126 349 my $id = $seg->id;
1127  
1128 349 my $seg_removed = 0;
1129  
1130 349 ▶ if (! $seg->remove) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $seg->remove) { } 0 349 349
1131 0 warn "Couldn't remove shm segment $id: $!";
1132 }
1133 else {
1134 349 $seg_removed = 1;
1135 }
1136  
1137 # Semaphore cleanup
1138  
1139 349 my $sem = $knot->sem;
1140  
1141 349 my $sem_removed = 0;
1142 349 my $sem_remove_status = $sem->remove;
1143  
1144 349 ▶ if ($sem_remove_status != 1 && $sem_remove_status ne '0 but true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sem_remove_status != 1 and $sem_remove_status ne '0 but true') { } 1 348 349
Condition66%2/3
$sem_remove_status != 1 and $sem_remove_status ne "0 but true"
!ll&&!rl&&r
03481
MC/DC50%1/2
$sem_remove_status != 1 and $sem_remove_status ne "0 but true"
$sem_remove_status != 1 $sem_remove_status ne "0 but true"
Truth table66%2/3
A and B
A= $sem_remove_status != 1B= $sem_remove_status ne "0 but true"
ABresult
0X0
100
111
1145 1 warn "Couldn't remove semaphore set $id: $!";
1146 }
1147 else {
1148 348 $sem_removed = 1;
1149 }
1150  
1151 # If the segment or semaphore couldn't be cleaned up, we need to
1152 # keep state
1153  
1154 349 ▶ if ($seg_removed && $sem_removed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg_removed and $sem_removed) 348 1 349
Condition66%2/3
$seg_removed and $sem_removed
!ll&&!rl&&r
01348
MC/DC50%1/2
$seg_removed and $sem_removed
$seg_removed $sem_removed
Truth table66%2/3
A and B
A= $seg_removedB= $sem_removed
ABresult
0X0
100
111
1155 348 delete $process_register{$id};
1156 348 delete $global_register{$id};
1157 }
1158 }
1159  
1160 # Unit testing
1161  
1162 sub testing_set {
1163 81 ▶ my ($class, $dist_name) = @_;
Subroutine testing_set: called CC=3 SCAR=11.0
Pod documented
1164 81 ▶ croak "testing_set() requires a distribution name string"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dist_name and length $dist_name 2 79 81
Condition100%3/3
defined $dist_name and length $dist_name
!ll&&!rl&&r
1179
MC/DC100%2/2
defined $dist_name and length $dist_name
defined $dist_name length $dist_name
Truth table100%3/3
A and B
A= defined $dist_nameB= length $dist_name
ABresult
0X0
100
111
1165 unless defined $dist_name && length $dist_name;
1166 79 $_testing_dist = $dist_name;
1167 }
1168 sub clean_up_testing {
1169 6 ▶ shift if @_ > 1 && ! ref $_[0] && defined $_[0] && UNIVERSAL::isa($_[0], __PACKAGE__);
Subroutine clean_up_testing: called CC=18 SCAR=32.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 and not ref $_[0] and defined $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__) 1 5 6
Condition66%6/9
@_ > 1 and not ref $_[0] and defined $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
!ll&&!rl&&r
501
@_ > 1 and not ref $_[0] and defined $_[0]
!ll&&!rl&&r
501
@_ > 1 and not ref $_[0]
!ll&&!rl&&r
501
MC/DC25%1/4
@_ > 1 and not ref $_[0] and defined $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
@_ > 1 not ref $_[0] defined $_[0] UNIVERSAL::isa($_[0], __PACKAGE__)
Truth table40%2/5
A and not(B) and C and D
A= @_ > 1B= not ref $_[0]C= defined $_[0]D= UNIVERSAL::isa($_[0], __PACKAGE__)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1170  
1171 6 my ($dist_name) = @_;
1172  
1173 6 ▶ croak "clean_up_testing() requires a distribution name string"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dist_name and length $dist_name 0 6 6
Condition33%1/3
defined $dist_name and length $dist_name
!ll&&!rl&&r
006
MC/DC0%0/2
defined $dist_name and length $dist_name
defined $dist_name length $dist_name
Truth table33%1/3
A and B
A= defined $dist_nameB= length $dist_name
ABresult
0X0
100
111
1174 unless defined $dist_name && length $dist_name;
1175  
1176 6 my $target = _testing_semaphore_key_hash($dist_name);
1177 6 my $removed = 0;
1178  
1179 # Scan ipcs -m for segment IDs and keys directly. We cannot use
1180 # shm_segments() here because it filters by the 'IPC::Shareable' 14-byte
1181 # tag, which is only written during STORE operations — empty tied segments
1182 # have no tag and would be invisible. The authoritative identifier for a
1183 # testing-tagged segment is the SEM_TESTING value on its semaphore set,
1184 # not the segment content.
1185  
1186 6 ▶ open my $ipcs_fh, '-|', 'ipcs', '-m' or die "ipcs -m: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $ipcs_fh, "-|", "ipcs", "-m" or die "ipcs -m: $!" 0 6 6
1187 6 ▶ while (my $line = <$ipcs_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $ipcs_fh)) 35 6 41
1188 35 my ($id, $raw_key);
1189  
1190 35 ▶ if ($line =~ /^\s*m\s+(\d+)\s+(\S+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s*m\s+(\d+)\s+(\S+)/) { } 0 35 35
elsif ($line =~ /^\s*(\d+)\s+(0x[0-9a-fA-F]+)\s+/) { } 0 35 35
elsif ($line =~ /^\s*(\S+)\s+(\d+)\s+\S+/) { } 11 24 35
1191 # BSD/macOS: m <shmid> <key> ...
1192 0 ($id, $raw_key) = ($1, $2);
1193 }
1194 elsif ($line =~ /^\s*(\d+)\s+(0x[0-9a-fA-F]+)\s+/) {
1195 # DragonFly BSD: <shmid> <hex_key> ... (no type-letter column)
1196 0 ($id, $raw_key) = ($1, $2);
1197 }
1198 elsif ($line =~ /^\s*(\S+)\s+(\d+)\s+\S+/) {
1199 # Linux: <key> <shmid> ...
1200 11 ($raw_key, $id) = ($1, $2);
1201 }
1202 else {
1203 24 next;
1204 }
1205  
1206 11 ▶ my $key_int = $raw_key =~ /^0x[0-9a-fA-F]+$/
Branch: 2 branches
BranchTrueFalseTotal
$raw_key =~ /^0x[0-9a-fA-F]+$/ ? : 11 0 11
$raw_key =~ /^-?\d+$/ ? : 0 0 0
1207 ? hex($raw_key)
1208 : $raw_key =~ /^-?\d+$/
1209 ? int($raw_key)
1210 : next;
1211  
1212 # IPC_PRIVATE segments cannot be re-attached across processes
1213 11 ▶ next if $key_int == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $key_int == 0 0 11 11
1214  
1215 11 my $sem = IPC::Semaphore->new($key_int, 0, 0);
1216 11 ▶ next unless defined $sem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sem 0 11 11
1217  
1218 11 ▶ next unless _testing_semaphore_value($sem) == $target;
Branch: 1 branch
BranchTrueFalseTotal
unless _testing_semaphore_value($sem) == $target 6 5 11
1219  
1220 5 ▶ if (shmctl($id, IPC_RMID, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (shmctl $id, IPC_RMID(), 0) { } 5 0 5
1221 5 $sem->remove;
1222 5 delete $process_register{$id};
1223 5 delete $global_register{$id};
1224 5 $removed++;
1225 }
1226 else {
1227 0 warn "clean_up_testing(): could not remove shm segment $id: $!";
1228 }
1229 }
1230 6 close $ipcs_fh;
1231  
1232 6 return $removed;
1233 }
1234  
1235 # Private methods
1236  
1237 # Encoding/Decoding
1238  
1239 sub _encode {
1240 932 ▶ my ($knot, $seg, $data) = @_;
Subroutine _encode: called CC=2 SCAR=6.9
1241  
1242 932 my $serializer = $knot->attributes('serializer');
1243  
1244 932 ▶ if ($serializer eq 'storable') {
Branch: 1 branch
BranchTrueFalseTotal
if ($serializer eq "storable") 447 485 932
1245 447 return _freeze($seg, $data);
1246 }
1247  
1248 485 return _encode_json($seg, $data);
1249 }
1250 sub _decode {
1251 2859 ▶ my ($knot, $seg) = @_;
Subroutine _decode: called CC=5 SCAR=16.1
1252  
1253 2859 my $serializer = $knot->attributes('serializer');
1254  
1255 2859 ▶ my $data = $serializer eq 'storable'
Branch: 1 branch
BranchTrueFalseTotal
$serializer eq 'storable' ? : 1221 1638 2859
1256 ? _thaw($seg)
1257 : _decode_json($seg, $knot);
1258  
1259 2853 ▶ return $data if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 2306 547 2853
1260  
1261 # Empty/never-written segment — return appropriate empty default so that
1262 # aggregate tie methods (FETCHSIZE, PUSH, CLEAR, etc.) can deref safely.
1263  
1264 547 ▶ return [] if $knot->{_type_int} == TYPE_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
if $knot->{"_type_int"} == 1 91 456 547
1265 456 ▶ return {} if $knot->{_type_int} == TYPE_HASH;
Branch: 1 branch
BranchTrueFalseTotal
if $knot->{"_type_int"} == 0 352 104 456
1266 104 return undef;
1267 }
1268 sub _encode_json {
1269 485 ▶ my $seg = shift;
Subroutine _encode_json: called CC=2 SCAR=6.9
1270 485 my $data = shift;
1271  
1272 485 my $json = encode_json _encode_json_prepare($data);
1273  
1274 485 substr $json, 0, 0, 'IPC::Shareable';
1275  
1276 485 ▶ if (length($json) > $seg->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $json > $seg->size) 1 484 485
1277 1 croak "Length of shared data exceeds shared segment size";
1278 }
1279  
1280 484 $seg->shmwrite($json);
1281 }
1282 sub _encode_json_prepare {
1283 486 ▶ my ($data) = @_;
Subroutine _encode_json_prepare: called CC=21 SCAR=30.6
1284  
1285 486 ▶ my $type = Scalar::Util::reftype($data) or return $data;
Branch: 1 branch
BranchTrueFalseTotal
my $type = builtin::reftype($data) or return $data 0 486 486
1286  
1287 # Replace direct IPC::Shareable child segments with __ics__ markers.
1288 # All nested refs are tied children — no recursion needed; each child
1289 # segment encodes its own children independently. We have to do this because
1290 # JSON can't store blessed objects
1291  
1292 486 ▶ if ($type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 318 168 486
1293 {
1294 318 my $has_child = 0;
1295 318 for my $val (values %$data) {
1296 1219 ▶ if (ref($val) && _is_child($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and _is_child($val)) 70 1149 1219
Condition66%2/3
ref $val and _is_child($val)
!ll&&!rl&&r
1149070
MC/DC50%1/2
ref $val and _is_child($val)
ref $val _is_child($val)
Truth table66%2/3
A and B
A= ref $valB= _is_child($val)
ABresult
0X0
100
111
1297 70 $has_child = 1;
1298 70 last;
1299 }
1300 }
1301 318 ▶ return $data if ! $has_child;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_child 248 70 318
1302 }
1303  
1304 70 my %result;
1305 70 for my $key (keys %$data) {
1306 151 my $val = $data->{$key};
1307 151 ▶ my $inner = ref($val) && _is_child($val);
Condition66%2/3
ref $val && _is_child($val)
!ll&&!rl&&r
77074
MC/DC50%1/2
ref $val && _is_child($val)
ref $val _is_child($val)
Truth table66%2/3
A && B
A= ref $valB= _is_child($val)
ABresult
0X0
100
111
1308 $result{$key} = $inner
1309 151 ▶ ? { '__ics__' => { type => $inner->{_type}, child_key => $inner->{_key}, child_key_hex => sprintf('0x%08x', $inner->{_key}) } }
Branch: 1 branch
BranchTrueFalseTotal
$inner ? : 74 77 151
1310 : $val;
1311 }
1312 70 return \%result;
1313 }
1314  
1315 168 ▶ if ($type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ARRAY") 152 16 168
1316 {
1317 152 my $has_child = 0;
1318 152 for my $val (@$data) {
1319 348 ▶ if (ref($val) && _is_child($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and _is_child($val)) 26 322 348
Condition66%2/3
ref $val and _is_child($val)
!ll&&!rl&&r
322026
MC/DC50%1/2
ref $val and _is_child($val)
ref $val _is_child($val)
Truth table66%2/3
A and B
A= ref $valB= _is_child($val)
ABresult
0X0
100
111
1320 26 $has_child = 1;
1321 26 last;
1322 }
1323 }
1324 152 ▶ return $data if ! $has_child;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_child 126 26 152
1325 }
1326  
1327 return [
1328 map {
1329 26 ▶ my $inner = ref($_) && _is_child($_);
Condition66%2/3
ref $_ && _is_child($_)
!ll&&!rl&&r
7043
MC/DC50%1/2
ref $_ && _is_child($_)
ref $_ _is_child($_)
Truth table66%2/3
A && B
A= ref $_B= _is_child($_)
ABresult
0X0
100
111
1330 $inner
1331 50 ▶ ? { '__ics__' => { type => $inner->{_type}, child_key => $inner->{_key}, child_key_hex => sprintf('0x%08x', $inner->{_key}) } }
Branch: 1 branch
BranchTrueFalseTotal
$inner ? : 43 7 50
1332 : $_
1333 } @$data
1334 ];
1335 }
1336  
1337 16 ▶ if ($type eq 'SCALAR' || $type eq 'REF') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR" or $type eq "REF") 15 1 16
Condition100%3/3
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
961
MC/DC100%2/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR" $type eq "REF"
Truth table100%3/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
ABresult
1X1
011
000
1338 15 my $val = $$data;
1339 15 ▶ my $inner = ref($val) && _is_child($val);
Condition66%2/3
ref $val && _is_child($val)
!ll&&!rl&&r
906
MC/DC50%1/2
ref $val && _is_child($val)
ref $val _is_child($val)
Truth table66%2/3
A && B
A= ref $valB= _is_child($val)
ABresult
0X0
100
111
1340 return $inner
1341 15 ▶ ? { '__ics__' => { type => $inner->{_type}, child_key => $inner->{_key}, child_key_hex => sprintf('0x%08x', $inner->{_key}) } }
Branch: 1 branch
BranchTrueFalseTotal
$inner ? : 6 9 15
1342 : { '__sv__' => $val };
1343 }
1344  
1345 1 return $data;
1346 }
1347 sub _decode_json {
1348 1639 ▶ my ($seg, $knot) = @_;
Subroutine _decode_json: called CC=13 SCAR=25.7
1349  
1350 1639 my $json = $seg->data;
1351  
1352 1639 ▶ return if ! $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 343 1296 1639
1353  
1354 # The return of shmread() is the actual size of the defined size of the
1355 # shared memory segment. Even if the return equates to an empty string
1356 # (which it will if it contains no data), there will always be a length().
1357 # Therefore, we must see if we've tagged this data as a valid structure,
1358 # or else decode will fail
1359  
1360 1296 my $tag = substr $json, 0, 14, '';
1361  
1362 1296 ▶ if ($tag eq 'IPC::Shareable') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'IPC::Shareable') { } 1295 1 1296
1363 1295 my $data = decode_json $json;
1364  
1365 1291 ▶ if (! defined($data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 1 1290 1291
1366 1 croak "Munged shared memory segment (size exceeded?)";
1367 }
1368  
1369 1290 ▶ _decode_json_restore($data, $knot) if defined $knot && index($json, '"__ics__"') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $knot and index($json, "\"__ics__\"") != -1 420 870 1290
Condition66%2/3
defined $knot and index($json, "\"__ics__\"") != -1
!ll&&!rl&&r
0870420
MC/DC50%1/2
defined $knot and index($json, "\"__ics__\"") != -1
defined $knot index($json, "\"__ics__\"") != -1
Truth table66%2/3
A and B
A= defined $knotB= index($json, "\"__ics__\"") != -1
ABresult
0X0
100
111
1370  
1371 # Unwrap scalar-tie values encoded as { '__sv__' => val } or { '__ics__' => {...} }
1372  
1373 1290 ▶ if (defined $knot && $knot->{_type_int} == TYPE_SCALAR && ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $knot and $knot->{"_type_int"} == 2 and ref $data eq "HASH") 85 1205 1290
Condition83%5/6
defined $knot and $knot->{"_type_int"} == 2 and ref $data eq "HASH"
!ll&&!rl&&r
11881785
defined $knot and $knot->{"_type_int"} == 2
!ll&&!rl&&r
01188102
MC/DC66%2/3
defined $knot and $knot->{"_type_int"} == 2 and ref $data eq "HASH"
defined $knot $knot->{"_type_int"} == 2 ref $data eq "HASH"
Truth table75%3/4
A and B and C
A= defined $knotB= $knot->{"_type_int"} == 2C= ref $data eq "HASH"
ABCresult
0XX0
10X0
1100
1111
1374 85 ▶ if (exists $data->{'__ics__'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"__ics__"}) 44 41 85
1375 44 my $prev = $knot->{_data};
1376 44 ▶ my $prev_val = (defined $prev && ref($prev)) ? $$prev : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $prev && ref $prev ? : 43 1 44
Condition66%2/3
defined $prev and ref $prev
!ll&&!rl&&r
1043
MC/DC50%1/2
defined $prev and ref $prev
defined $prev ref $prev
Truth table66%2/3
A and B
A= defined $prevB= ref $prev
ABresult
0X0
100
111
1377 44 my $resolved = _decode_json_resolve($data->{'__ics__'}, $prev_val, $knot);
1378 44 return \$resolved;
1379 }
1380 41 ▶ if (exists $data->{'__sv__'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"__sv__"}) 24 17 41
1381 24 my $val = $data->{'__sv__'};
1382 24 return \$val;
1383 }
1384 }
1385  
1386 1222 return $data;
1387 }
1388 else {
1389 1 return;
1390 }
1391 }
1392 sub _decode_json_restore {
1393 420 ▶ my ($data, $knot) = @_;
Subroutine _decode_json_restore: called CC=14 SCAR=26.5
1394  
1395 420 ▶ my $type = Scalar::Util::reftype($data) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $type = builtin::reftype($data) or return 0 420 420
1396  
1397 # Reuse existing tied child refs from previous decode where possible.
1398 # This avoids a shmget+semget system call pair for each child on every
1399 # decode cycle — only the first attach per segment incurs that cost.
1400  
1401 420 my $prev = $knot->{_data};
1402  
1403 420 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 313 107 420
elsif ($type eq 'ARRAY') { } 107 0 107
1404 313 my $prev_is_hash = ref($prev) eq 'HASH';
1405 313 for my $key (keys %$data) {
1406 671 ▶ next unless ref($data->{$key}) eq 'HASH' && exists $data->{$key}{'__ics__'};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{$key} eq "HASH" and exists $data->{$key}{"__ics__"} 277 394 671
Condition100%3/3
ref $data->{$key} eq "HASH" and exists $data->{$key}{"__ics__"}
!ll&&!rl&&r
23344394
MC/DC100%2/2
ref $data->{$key} eq "HASH" and exists $data->{$key}{"__ics__"}
ref $data->{$key} eq "HASH" exists $data->{$key}{"__ics__"}
Truth table100%3/3
A and B
A= ref $data->{$key} eq "HASH"B= exists $data->{$key}{"__ics__"}
ABresult
0X0
100
111
1407 $data->{$key} = _decode_json_resolve(
1408 $data->{$key}{'__ics__'},
1409 394 ▶ $prev_is_hash ? $prev->{$key} : undef,
Branch: 1 branch
BranchTrueFalseTotal
$prev_is_hash ? : 367 27 394
1410 $knot,
1411 );
1412 }
1413 }
1414 elsif ($type eq 'ARRAY') {
1415 107 my $prev_is_array = ref($prev) eq 'ARRAY';
1416 107 ▶ my $prev_max = $prev_is_array ? $#$prev : -1;
Branch: 1 branch
BranchTrueFalseTotal
$prev_is_array ? : 92 15 107
1417 107 for my $i (0 .. $#$data) {
1418 231 ▶ next unless ref($data->[$i]) eq 'HASH' && exists $data->[$i]{'__ics__'};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->[$i] eq "HASH" and exists $data->[$i]{"__ics__"} 44 187 231
Condition66%2/3
ref $data->[$i] eq "HASH" and exists $data->[$i]{"__ics__"}
!ll&&!rl&&r
440187
MC/DC50%1/2
ref $data->[$i] eq "HASH" and exists $data->[$i]{"__ics__"}
ref $data->[$i] eq "HASH" exists $data->[$i]{"__ics__"}
Truth table66%2/3
A and B
A= ref $data->[$i] eq "HASH"B= exists $data->[$i]{"__ics__"}
ABresult
0X0
100
111
1419 $data->[$i] = _decode_json_resolve(
1420 187 ▶ $data->[$i]{'__ics__'},
Branch: 1 branch
BranchTrueFalseTotal
$prev_is_array && $i <= $prev_max ? : 165 22 187
Condition100%3/3
$prev_is_array and $i <= $prev_max
!ll&&!rl&&r
202165
MC/DC100%2/2
$prev_is_array and $i <= $prev_max
$prev_is_array $i <= $prev_max
Truth table100%3/3
A and B
A= $prev_is_arrayB= $i <= $prev_max
ABresult
0X0
100
111
1421 $prev_is_array && $i <= $prev_max ? $prev->[$i] : undef,
1422 $knot,
1423 );
1424 }
1425 }
1426 }
1427 sub _decode_json_resolve {
1428 625 ▶ my ($info, $existing, $knot) = @_;
Subroutine _decode_json_resolve: called CC=5 SCAR=16.1
1429  
1430 625 ▶ if (defined $existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $existing) 560 65 625
1431 560 ▶ my $inner = ref($existing) && _is_child($existing);
Condition66%2/3
ref $existing && _is_child($existing)
!ll&&!rl&&r
30557
MC/DC50%1/2
ref $existing && _is_child($existing)
ref $existing _is_child($existing)
Truth table66%2/3
A && B
A= ref $existingB= _is_child($existing)
ABresult
0X0
100
111
1432 560 ▶ return $existing if $inner && $inner->{_key} == $info->{child_key};
Branch: 1 branch
BranchTrueFalseTotal
if $inner and $inner->{"_key"} == $info->{"child_key"} 549 11 560
Condition100%3/3
$inner and $inner->{"_key"} == $info->{"child_key"}
!ll&&!rl&&r
38549
MC/DC100%2/2
$inner and $inner->{"_key"} == $info->{"child_key"}
$inner $inner->{"_key"} == $info->{"child_key"}
Truth table100%3/3
A and B
A= $innerB= $inner->{"_key"} == $info->{"child_key"}
ABresult
0X0
100
111
1433 }
1434  
1435 76 return _decode_json_reattach($info, $knot);
1436 }
1437 sub _decode_json_reattach {
1438 76 ▶ my ($info, $knot) = @_;
Subroutine _decode_json_reattach: called CC=4 SCAR=13.9
1439  
1440 my %opts = (
1441 76 %{ $knot->attributes },
1442 key => $info->{child_key},
1443 76 exclusive => 0,
1444 create => 0,
1445 magic => 1,
1446 );
1447  
1448 76 ▶ if ($info->{type} eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($info->{'type'} eq 'HASH') { } 34 42 76
elsif ($info->{'type'} eq 'ARRAY') { } 41 1 42
elsif ($info->{'type'} eq 'SCALAR') { } 1 0 1
1449 34 my %h;
1450 34 tie %h, 'IPC::Shareable', \%opts;
1451 34 return \%h;
1452 }
1453 elsif ($info->{type} eq 'ARRAY') {
1454 41 my @a;
1455 41 tie @a, 'IPC::Shareable', \%opts;
1456 41 return \@a;
1457 }
1458 elsif ($info->{type} eq 'SCALAR') {
1459 1 my $s;
1460 1 tie $s, 'IPC::Shareable', \%opts;
1461 1 return \$s;
1462 }
1463 }
1464 sub _freeze {
1465 447 ▶ my ($seg, $water) = @_;
Subroutine _freeze: called CC=3 SCAR=11.0
1466  
1467 447 my $ice = freeze $water;
1468 447 ▶ croak "Could not serialize data for shared memory"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ice 0 447 447
1469 unless defined $ice;
1470 447 substr $ice, 0, 0, 'IPC::Shareable';
1471  
1472 447 ▶ if (length($ice) > $seg->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ice > $seg->size) 1 446 447
1473 1 croak "Length of shared data exceeds shared segment size";
1474 }
1475  
1476 446 $seg->shmwrite($ice);
1477 }
1478 sub _thaw {
1479 1487 ▶ my ($seg) = @_;
Subroutine _thaw: called CC=4 SCAR=13.9
1480  
1481 1487 my $ice = $seg->shmread;
1482  
1483 1487 ▶ return if ! $ice;
Branch: 1 branch
BranchTrueFalseTotal
unless $ice 7 1480 1487
1484  
1485 1480 my $tag = substr $ice, 0, 14, '';
1486  
1487 1480 ▶ if ($tag eq 'IPC::Shareable') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'IPC::Shareable') { } 1034 446 1480
1488 1034 my $water = thaw $ice;
1489 1034 ▶ if (! defined($water)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $water) 1 1033 1034
1490 1 croak "Munged shared memory segment (size exceeded?)";
1491 }
1492 1033 return $water;
1493 }
1494 else {
1495 446 return;
1496 }
1497 }
1498  
1499 # Data management
1500  
1501 sub _tie {
1502 540 ▶ my ($type, $class, $key_str, $opts);
Subroutine _tie: called CC=31 SCAR=34.5
1503  
1504 540 ▶ if (scalar @_ == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 4) { } 178 362 540
1505 # Legacy API allowed a string scalar key
1506 178 ($type, $class, $key_str, $opts) = @_;
1507 178 $opts->{key} = $key_str;
1508 }
1509 else {
1510 362 ($type, $class, $opts) = @_;
1511 }
1512  
1513 540 $opts = _parse_args($opts);
1514  
1515 540 my $knot = bless { attributes => $opts }, $class;
1516  
1517 540 $knot->uuid;
1518  
1519 540 my $key = $knot->_shm_key;
1520 540 my $flags = $knot->_shm_flags;
1521 540 my $shm_size = $knot->attributes('size');
1522  
1523 540 ▶ if ($knot->attributes('limit') && $shm_size > SHMMAX_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->attributes("limit") and $shm_size > 1073741824) 2 538 540
Condition100%3/3
$knot->attributes("limit") and $shm_size > 1073741824
!ll&&!rl&&r
15372
MC/DC100%2/2
$knot->attributes("limit") and $shm_size > 1073741824
$knot->attributes("limit") $shm_size > 1073741824
Truth table100%3/3
A and B
A= $knot->attributes("limit")B= $shm_size > 1073741824
ABresult
0X0
100
111
1524 2 croak
1525 "Shared memory segment size '$shm_size' is larger than max size of " .
1526 SHMMAX_BYTES;
1527 }
1528  
1529 538 my $seg;
1530  
1531 538 ▶ if ($knot->attributes('graceful')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->attributes('graceful')) { } 8 530 538
1532 8 my $exclusive = eval {
1533 8 $seg = IPC::Shareable::SharedMem->new(
1534 key => $key,
1535 size => $shm_size,
1536 flags => $flags,
1537 mode => $knot->attributes('mode'),
1538 type => $type,
1539 );
1540 4 1;
1541 };
1542  
1543 8 ▶ if (! defined $exclusive) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $exclusive) 4 4 8
1544 4 ▶ if ($knot->attributes('warn')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->attributes("warn")) 1 3 4
1545 1 my $key = lc(sprintf("0x%X", $knot->_shm_key));
1546  
1547 1 warn "Process ID $$ exited due to exclusive shared memory collision at segment/semaphore key '$key'\n";
1548 }
1549 4 exit(0);
1550 }
1551 }
1552 else {
1553 530 $seg = IPC::Shareable::SharedMem->new(
1554 key => $key,
1555 size => $shm_size,
1556 flags => $flags,
1557 mode => $knot->attributes('mode'),
1558 type => $type,
1559 );
1560 }
1561  
1562 532 ▶ if (! defined $seg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seg) 7 525 532
1563 7 ▶ if ($!{ENOMEM}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"ENOMEM"}) 2 5 7
1564 2 croak "\nERROR: Could not create shared memory segment: $!\n\n" .
1565 "Are you using too large a segment size, or spawning too many segments?";
1566 }
1567  
1568 5 ▶ if ($!{ENOSPC}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"ENOSPC"}) 0 5 5
1569 0 croak "\nERROR: Could not create shared memory segment: $!\n\n" .
1570 "Are you spawning too many segments (in a loop perhaps)?";
1571 }
1572  
1573 5 ▶ if (! $knot->attributes('create')) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $knot->attributes('create')) { } 3 2 5
elsif ($knot->attributes('create') and $knot->attributes('exclusive')) { } 1 1 2
1574 3 confess "ERROR: Could not acquire shared memory segment... 'create' ".
1575 "option is not set, and the segment hasn't been created " .
1576 "yet:\n\n $!";
1577 }
1578 ▶ elsif ($knot->attributes('create') && $knot->attributes('exclusive')) {
Condition66%2/3
$knot->attributes("create") and $knot->attributes("exclusive")
!ll&&!rl&&r
011
MC/DC50%1/2
$knot->attributes("create") and $knot->attributes("exclusive")
$knot->attributes("create") $knot->attributes("exclusive")
Truth table66%2/3
A and B
A= $knot->attributes("create")B= $knot->attributes("exclusive")
ABresult
0X0
100
111
1579 1 croak "ERROR: Could not create shared memory segment. 'create' " .
1580 "and 'exclusive' are set. Does the segment already exist? " .
1581 "\n\n$!";
1582 }
1583 else {
1584 1 croak "ERROR: Could not create shared memory segment.\n\n$!";
1585 }
1586 }
1587  
1588 # Try to attach to an existing semaphore set first using nsems=0, which
1589 # avoids EINVAL on macOS/BSD when the existing set has fewer slots than
1590 # the requested count. If the set does not exist yet, fall through to
1591 # create a new semaphore set: 5 slots when the 'testing' attribute is set
1592 # (adds SEM_TESTING at index 4), 4 slots otherwise.
1593  
1594 525 ▶ my $nsems = $knot->attributes('testing') ? 5 : 4;
Branch: 1 branch
BranchTrueFalseTotal
$knot->attributes('testing') ? : 518 7 525
1595 525 ▶ my $sem = IPC::Semaphore->new($key, 0, $seg->flags & 0777)
Condition100%3/3
"IPC::Semaphore"->new($key, 0, $seg->flags & 511) // "IPC::Semaphore"->new($key, $nsems, $seg->flags)
l!l&&r!l&&!r
1244001
MC/DC100%2/2
"IPC::Semaphore"->new($key, 0, $seg->flags & 511) // "IPC::Semaphore"->new($key, $nsems, $seg->flags)
"IPC::Semaphore"->new($key, 0, $seg->flags & 511) "IPC::Semaphore"->new($key, $nsems, $seg->flags)
Truth table100%3/3
A // B
A= "IPC::Semaphore"->new($key, 0, $seg->flags & 511)B= "IPC::Semaphore"->new($key, $nsems, $seg->flags)
ABresult
1X1
011
000
1596 // IPC::Semaphore->new($key, $nsems, $seg->flags);
1597  
1598 525 ▶ if (! defined $sem) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sem) 1 524 525
1599 1 croak "Could not create semaphore set: $!\n";
1600 }
1601  
1602 524 ▶ if (! $sem->op(@{ $semop_args{(LOCK_SH)} }) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sem->op(@{$semop_args{1};})) 1 523 524
1603 1 croak "Could not obtain semaphore set lock: $!\n";
1604 }
1605  
1606 523 ▶ %$knot = (
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'HASH' ? : 291 232 523
$type eq 'ARRAY' ? : 103 129 232
1607 %$knot,
1608 _hkey_list => undef,
1609 _key => $key,
1610 _key_hex => $seg->key_hex,
1611 _lock => 0,
1612 _shm => $seg,
1613 _sem => $sem,
1614 _type => $type,
1615 _type_int => $type eq 'HASH' ? TYPE_HASH : $type eq 'ARRAY' ? TYPE_ARRAY : TYPE_SCALAR,
1616 _was_changed => 0,
1617 );
1618  
1619 523 my $serializer = $knot->attributes('serializer');
1620  
1621 523 ▶ if ($serializer eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($serializer eq 'json') { } 261 262 523
1622 261 my $data;
1623 261 my $decoded_ok = eval { $data = $knot->_decode($seg); 1 };
1624  
1625 261 ▶ if (! $decoded_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $decoded_ok) { } 4 257 261
1626 # JSON decode threw; the segment may contain legacy Storable data.
1627 # Try Storable; if it succeeds, silently switch this session over
1628 # and warn the caller so they know to migrate.
1629  
1630 4 my $storable_data;
1631 4 my $thaw_ok = eval { $storable_data = _thaw($seg); 1 };
1632  
1633 4 ▶ if ($thaw_ok && defined $storable_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thaw_ok and defined $storable_data) { } 4 0 4
Condition33%1/3
$thaw_ok and defined $storable_data
!ll&&!rl&&r
004
MC/DC0%0/2
$thaw_ok and defined $storable_data
$thaw_ok defined $storable_data
Truth table33%1/3
A and B
A= $thaw_okB= defined $storable_data
ABresult
0X0
100
111
1634 4 carp sprintf(
1635 "IPC::Shareable: segment 0x%08x contains Storable-encoded data; "
1636 . "switching serializer to 'storable' for this session. "
1637 . "Re-create the segment to migrate it to JSON.",
1638 $key
1639 );
1640 4 $knot->{attributes}{serializer} = 'storable';
1641 4 $knot->{_data} = $storable_data;
1642 }
1643 else {
1644 0 die $@;
1645 }
1646 }
1647 else {
1648 257 $knot->{_data} = $data;
1649 }
1650 }
1651 else {
1652 262 $knot->{_data} = _thaw($seg);
1653 }
1654  
1655 # Register unconditionally so any process that attaches to an existing
1656 # segment (create=>0, re-attach, cross-process) is also tracked for
1657 # clean_up_all(). Previously only new segments were registered here,
1658 # requiring the Dumper hack in global_register() to catch the rest.
1659  
1660 523 ▶ if (! exists $global_register{$knot->seg->id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $global_register{$knot->seg->id}) 422 101 523
1661 422 $global_register{$knot->seg->id} = $knot;
1662 }
1663  
1664 523 ▶ if ($sem->getval(SEM_MARKER) != SHM_EXISTS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sem->getval(0) != 1) { } 399 124 523
1665  
1666 399 ▶ $process_register{$knot->seg->id} ||= $knot;
Condition33%1/3
$process_register{$knot->seg->id} ||= $knot
l!l&&r!l&&!r
03990
MC/DC0%0/2
$process_register{$knot->seg->id} ||= $knot
$process_register{$knot->seg->id} $knot
Truth table33%1/3
A ||= B
A= $process_register{$knot->seg->id}B= $knot
ABresult
1X1
011
000
1667  
1668 399 $sem->setval(SEM_PROTECTED, $knot->attributes('protected'));
1669  
1670 399 ▶ if ($knot->attributes('testing')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->attributes("testing")) 392 7 399
1671 392 $sem->setval(SEM_TESTING, _testing_semaphore_key_hash($knot->attributes('testing')));
1672 }
1673  
1674 399 ▶ if (! $sem->setval(SEM_MARKER, SHM_EXISTS)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sem->setval(0, 1)) 1 398 399
1675 1 croak "Couldn't set semaphore during object creation: $!";
1676 }
1677 }
1678 else {
1679 # Segment already existed — restore the protected and testing
1680 # attributes from the semaphore so that clean_up_all() / clean_up_testing()
1681 # in this process work correctly even when the caller did not explicitly
1682 # pass them on tie.
1683  
1684 124 my $stored_protected = $sem->getval(SEM_PROTECTED);
1685 124 ▶ $knot->{attributes}{protected} = $stored_protected
Branch: 1 branch
BranchTrueFalseTotal
if defined $stored_protected and $stored_protected != 0 3 121 124
Condition66%2/3
defined $stored_protected and $stored_protected != 0
!ll&&!rl&&r
01213
MC/DC50%1/2
defined $stored_protected and $stored_protected != 0
defined $stored_protected $stored_protected != 0
Truth table66%2/3
A and B
A= defined $stored_protectedB= $stored_protected != 0
ABresult
0X0
100
111
1686 if defined $stored_protected && $stored_protected != 0;
1687  
1688 124 my $stored_testing = _testing_semaphore_value($sem);
1689 124 ▶ if ($stored_testing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stored_testing) 124 0 124
1690 124 $knot->{attributes}{testing} = $stored_testing;
1691 }
1692 }
1693  
1694 522 $sem->op(@{ $semop_args{(LOCK_SH|LOCK_UN)} });
1695  
1696 522 return $knot;
1697 }
1698 sub _magic_tie {
1699 145 ▶ my ($parent, $val, $identifier) = @_;
Subroutine _magic_tie: called CC=10 SCAR=23.1
1700  
1701 145 my $key;
1702  
1703 145 ▶ if ($parent->{_key} == IPC_PRIVATE && $parent->attributes('serializer') ne 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{'_key'} == IPC_PRIVATE() and $parent->attributes('serializer') ne 'json') { } 30 115 145
Condition100%3/3
$parent->{"_key"} == IPC_PRIVATE() and $parent->attributes("serializer") ne "json"
!ll&&!rl&&r
952030
MC/DC100%2/2
$parent->{"_key"} == IPC_PRIVATE() and $parent->attributes("serializer") ne "json"
$parent->{"_key"} == IPC_PRIVATE() $parent->attributes("serializer") ne "json"
Truth table100%3/3
A and B
A= $parent->{"_key"} == IPC_PRIVATE()B= $parent->attributes("serializer") ne "json"
ABresult
0X0
100
111
1704 30 $key = IPC_PRIVATE;
1705 }
1706 else {
1707 115 $key = _shm_key_rand();
1708 }
1709  
1710 # The individual options in the hash override any pre-set options that are
1711 # being inherited from the parent
1712  
1713 my %opts = (
1714 144 %{ $parent->attributes },
1715 key => $key,
1716 exclusive => 1,
1717 create => 1,
1718 magic => 1,
1719 );
1720  
1721 # XXX I wish I didn't have to take a copy of data here and copy it back in
1722 # XXX Also, have to peek inside potential objects to see their implementation
1723  
1724 144 my $child;
1725 144 ▶ my $type = Scalar::Util::reftype($val) || '';
Condition50%1/2
builtin::reftype($val) || ''
l!l
1440
MC/DC0%0/1
builtin::reftype($val) || ''
builtin::reftype($val)
Truth table50%1/2
A
A= builtin::reftype($val)
Aresult
11
00
1726  
1727 144 ▶ if ($type eq "HASH") {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 103 41 144
elsif ($type eq 'ARRAY') { } 38 3 41
elsif ($type eq 'SCALAR') { } 2 1 3
1728 103 my %copy = %$val;
1729 103 $child = tie %$val, 'IPC::Shareable', $key, { %opts };
1730 103 ▶ croak "Could not create inner tie" if ! $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 103 103
1731  
1732 103 %$val = %copy;
1733 }
1734 elsif ($type eq "ARRAY") {
1735 38 my @copy = @$val;
1736 38 $child = tie @$val, 'IPC::Shareable', $key, { %opts };
1737 38 ▶ croak "Could not create inner tie" if ! $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 38 38
1738  
1739 38 @$val = @copy;
1740 }
1741 elsif ($type eq "SCALAR") {
1742 2 my $copy = $$val;
1743 2 $child = tie $$val, 'IPC::Shareable', $key, { %opts };
1744 2 ▶ croak "Could not create inner tie" if ! $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $child 0 2 2
1745  
1746 2 $$val = $copy;
1747 }
1748 else {
1749 1 croak "Variables of type $type not implemented";
1750 }
1751  
1752 143 return $child;
1753 }
1754 sub _need_tie {
1755 146 ▶ my ($knot, $val, $identifier) = @_;
Subroutine _need_tie: called CC=6 SCAR=18.0
1756  
1757 146 my $type = Scalar::Util::reftype($val);
1758 146 ▶ return 0 if ! $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 146 146
1759  
1760 146 my $need_tie;
1761  
1762 146 ▶ if ($type eq "HASH") {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 104 42 146
elsif ($type eq 'ARRAY') { } 38 4 42
elsif ($type eq 'SCALAR') { } 4 0 4
1763 104 $need_tie = !(tied %$val);
1764 }
1765 elsif ($type eq "ARRAY") {
1766 38 $need_tie = !(tied @$val);
1767 }
1768 elsif ($type eq "SCALAR") {
1769 4 $need_tie = !(tied $$val);
1770 }
1771  
1772 146 ▶ return $need_tie ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$need_tie ? : 144 2 146
1773 }
1774 sub _remove_child {
1775 705 ▶ my ($val) = @_;
Subroutine _remove_child: called CC=3 SCAR=11.0
1776 705 ▶ if (ref($val) && (my $child = _is_child($val))) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and my $child = _is_child($val)) 27 678 705
Condition66%2/3
ref $val and my $child = _is_child($val)
!ll&&!rl&&r
678027
MC/DC50%1/2
ref $val and my $child = _is_child($val)
ref $val my $child = _is_child($val)
Truth table66%2/3
A and B
A= ref $valB= my $child = _is_child($val)
ABresult
0X0
100
111
1777 27 $child->remove;
1778 }
1779 }
1780 sub _is_child {
1781 1368 ▶ return $_have_xs
Subroutine _is_child: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$IPC::Shareable::_have_xs ? : 1354 14 1368
1782 ? _is_child_xs($_[0])
1783 : _is_child_pp($_[0]);
1784 }
1785 sub _is_child_pp {
1786 14 ▶ my $data = shift or return;
Subroutine _is_child_pp: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
my $data = shift() or return 1 13 14
1787  
1788 13 my $type = Scalar::Util::reftype( $data );
1789 13 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 2 11 13
1790  
1791 11 my $obj;
1792  
1793 11 ▶ if ($type eq "HASH") {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 6 5 11
elsif ($type eq 'ARRAY') { } 2 3 5
elsif ($type eq 'SCALAR') { } 2 1 3
1794 6 $obj = tied %$data;
1795 }
1796 elsif ($type eq "ARRAY") {
1797 2 $obj = tied @$data;
1798 }
1799 elsif ($type eq "SCALAR") {
1800 2 $obj = tied $$data;
1801 }
1802  
1803 11 ▶ if (ref $obj eq 'IPC::Shareable') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "IPC::Shareable") 7 4 11
1804 7 return $obj;
1805 }
1806  
1807 4 return;
1808 }
1809 sub _write_to_seg {
1810 942 ▶ my ($knot) = @_;
Subroutine _write_to_seg: called CC=2 SCAR=6.9
1811 942 my $seg_id = $knot->seg->id;
1812 942 ▶ if (! defined $knot->_encode($knot->seg, $knot->{_data})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $knot->_encode($knot->seg, $knot->{"_data"})) 10 930 940
1813 10 croak "Could not write to shared memory segment $seg_id: $!";
1814 }
1815 }
1816  
1817 # Segment/semaphore operations
1818  
1819 sub _execute_lock_coderef {
1820 6 ▶ my ($knot, $code) = @_;
Subroutine _execute_lock_coderef: called CC=2 SCAR=6.9
1821 6 my $ok = eval { $code->(); 1 };
1822 6 my $err = $@;
1823 6 $knot->unlock;
1824 6 ▶ die $err if ! $ok;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok 2 4 6
1825 }
1826 sub _key_str_to_int {
1827 # Convert any key format (hex string, decimal integer string, or arbitrary
1828 # text) to a 32-bit integer using the same algorithm as _shm_key(), but
1829 # without the %used_ids side effect. Safe to call any number of times.
1830 5 ▶ my ($key_str) = @_;
Subroutine _key_str_to_int: called CC=4 SCAR=13.9
1831  
1832 5 ▶ return hex($key_str) if $key_str =~ /^0x[0-9a-fA-F]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $key_str =~ /^0x[0-9a-fA-F]+$/i 2 3 5
1833 3 ▶ return $key_str + 0 if $key_str =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key_str =~ /^\d+$/ 1 2 3
1834  
1835 2 my $int = crc32($key_str);
1836 2 ▶ $int -= MAX_KEY_INT_SIZE if $int > MAX_KEY_INT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $int > 2147483648 1 1 2
1837 2 return $int;
1838 }
1839 sub _lock_children {
1840 130 ▶ my ($root_knot, $flags) = @_;
Subroutine _lock_children: called CC=14 SCAR=26.5
1841  
1842 130 my @locked;
1843 130 my %seen = ($root_knot->seg->id => 1);
1844 130 my @stack = ([$root_knot, 0]);
1845  
1846 130 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 188 128 316
1847 188 my $frame = $stack[-1];
1848 188 my ($knot, $idx) = @$frame;
1849  
1850 188 my $data = $knot->{_data};
1851 188 ▶ my $rtype = Scalar::Util::reftype($data) // '';
Condition100%2/2
builtin::reftype($data) // ""
l!l
1844
MC/DC100%1/1
builtin::reftype($data) // ""
builtin::reftype($data)
Truth table100%2/2
A
A= builtin::reftype($data)
Aresult
11
00
1852  
1853 188 ▶ my @vals = $rtype eq 'HASH' ? values %$data
Branch: 2 branches
BranchTrueFalseTotal
$rtype eq 'HASH' ? : 134 54 188
$rtype eq 'ARRAY' ? : 24 30 54
1854 : $rtype eq 'ARRAY' ? @$data
1855 : ();
1856  
1857 188 my $found = 0;
1858 188 ▶ for (my $i = $idx; $i < @vals; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @vals) 276 156 432
1859 276 my $val = $vals[$i];
1860 276 ▶ next unless ref($val);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 244 32 276
1861 32 my $child = _is_child($val);
1862 32 ▶ next unless $child && $child->seg;
Branch: 1 branch
BranchTrueFalseTotal
unless $child and $child->seg 0 32 32
Condition33%1/3
$child and $child->seg
!ll&&!rl&&r
0032
MC/DC0%0/2
$child and $child->seg
$child $child->seg
Truth table33%1/3
A and B
A= $childB= $child->seg
ABresult
0X0
100
111
1863  
1864 32 my $id = $child->seg->id;
1865 32 ▶ next if $seen{$id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$id}++ 0 32 32
1866  
1867 32 ▶ if (! $child->sem->op(@{ $semop_args{$flags} })) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($child->sem->op(@{$semop_args{$flags};})) 2 30 32
1868 2 for my $locked (reverse @locked) {
1869 2 my $rflags = $locked->{_lock} | LOCK_UN;
1870 2 ▶ $rflags ^= LOCK_NB if $rflags & LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
if $rflags & 4 2 0 2
1871 2 $locked->sem->op(@{ $semop_args{$rflags} });
1872 2 $locked->{_lock} = 0;
1873 }
1874 2 return;
1875 }
1876  
1877 30 $child->{_data} = $child->_decode($child->seg);
1878 30 $child->{_lock} = $flags;
1879 30 push @locked, $child;
1880  
1881 30 $frame->[1] = $i + 1;
1882 30 push @stack, [$child, 0];
1883 30 $found = 1;
1884 30 last;
1885 }
1886 186 ▶ pop @stack unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 156 30 186
1887 }
1888  
1889 128 return \@locked;
1890 }
1891 sub _shm_data_summary {
1892 6 ▶ my ($knot) = @_;
Subroutine _shm_data_summary: called CC=25 SCAR=50.6
1893  
1894 6 my $data = $knot->{_data};
1895 6 ▶ my $rtype = Scalar::Util::reftype($data) // '';
Condition50%1/2
builtin::reftype($data) // ""
l!l
60
MC/DC0%0/1
builtin::reftype($data) // ""
builtin::reftype($data)
Truth table50%1/2
A
A= builtin::reftype($data)
Aresult
11
00
1896  
1897 6 ▶ if ($rtype eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtype eq "SCALAR") 3 3 6
1898 3 my $v = $$data;
1899 3 ▶ return defined $v ? qq("$v") : '(undef)';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 3 0 3
1900 }
1901  
1902 3 ▶ if ($rtype eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtype eq "HASH") 3 0 3
1903 3 my @parts;
1904 3 for my $k (sort keys %$data) {
1905 3 my $v = $data->{$k};
1906 3 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 1 2 3
1907 1 ▶ my $vt = Scalar::Util::reftype($v) // '';
Condition50%1/2
builtin::reftype($v) // ""
l!l
10
MC/DC0%0/1
builtin::reftype($v) // ""
builtin::reftype($v)
Truth table50%1/2
A
A= builtin::reftype($v)
Aresult
11
00
1908 1 ▶ my $child = $vt eq 'HASH' ? tied(%$v)
Branch: 3 branches
BranchTrueFalseTotal
$vt eq 'HASH' ? : 1 0 1
$vt eq 'ARRAY' ? : 0 0 0
$vt eq 'SCALAR' ? : 0 0 0
1909 : $vt eq 'ARRAY' ? tied(@$v)
1910 : $vt eq 'SCALAR' ? tied($$v)
1911 : undef;
1912 push @parts, $child && $child->{_key_hex}
1913 1 ▶ ? qq($k => <child: $child->{_key_hex}>)
Branch: 1 branch
BranchTrueFalseTotal
$child && $child->{'_key_hex'} ? : 1 0 1
Condition33%1/3
$child and $child->{"_key_hex"}
!ll&&!rl&&r
001
MC/DC0%0/2
$child and $child->{"_key_hex"}
$child $child->{"_key_hex"}
Truth table33%1/3
A and B
A= $childB= $child->{"_key_hex"}
ABresult
0X0
100
111
1914 : "$k => <ref>";
1915 }
1916 else {
1917 2 ▶ push @parts, defined $v ? qq($k => "$v") : "$k => (undef)";
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 2 0 2
1918 }
1919 }
1920 3 ▶ return @parts ? '{ ' . join(', ', @parts) . ' }' : '{}';
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 3 0 3
1921 }
1922  
1923 0 ▶ if ($rtype eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtype eq "ARRAY") 0 0 0
1924 0 my @parts;
1925 0 for my $v (@$data) {
1926 0 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 0 0 0
1927 0 ▶ my $vt = Scalar::Util::reftype($v) // '';
Condition0%0/2
builtin::reftype($v) // ""
l!l
00
MC/DC0%0/1
builtin::reftype($v) // ""
builtin::reftype($v)
Truth table0%0/2
A
A= builtin::reftype($v)
Aresult
11
00
1928 0 ▶ my $child = $vt eq 'HASH' ? tied(%$v)
Branch: 3 branches
BranchTrueFalseTotal
$vt eq 'HASH' ? : 0 0 0
$vt eq 'ARRAY' ? : 0 0 0
$vt eq 'SCALAR' ? : 0 0 0
1929 : $vt eq 'ARRAY' ? tied(@$v)
1930 : $vt eq 'SCALAR' ? tied($$v)
1931 : undef;
1932 push @parts, $child && $child->{_key_hex}
1933 0 ▶ ? "<child: $child->{_key_hex}>"
Branch: 1 branch
BranchTrueFalseTotal
$child && $child->{'_key_hex'} ? : 0 0 0
Condition0%0/3
$child and $child->{"_key_hex"}
!ll&&!rl&&r
000
MC/DC0%0/2
$child and $child->{"_key_hex"}
$child $child->{"_key_hex"}
Truth table0%0/3
A and B
A= $childB= $child->{"_key_hex"}
ABresult
0X0
100
111
1934 : '<ref>';
1935 }
1936 else {
1937 0 ▶ push @parts, defined $v ? qq("$v") : '(undef)';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
1938 }
1939 }
1940 0 return '[' . join(', ', @parts) . ']';
1941 }
1942  
1943 0 return '(unknown type)';
1944 }
1945 sub _shm_flags {
1946 # Parses the anonymous hash passed to constructors; returns a list
1947 # of args suitable for passing to shmget
1948  
1949 540 ▶ my ($knot) = @_;
Subroutine _shm_flags: called CC=3 SCAR=11.0
1950  
1951 540 my $flags = 0;
1952  
1953 540 ▶ $flags |= IPC_CREAT if $knot->attributes('create');
Branch: 1 branch
BranchTrueFalseTotal
if $knot->attributes("create") 401 139 540
1954 540 ▶ $flags |= IPC_EXCL if $knot->attributes('exclusive');
Branch: 1 branch
BranchTrueFalseTotal
if $knot->attributes("exclusive") 168 372 540
1955  
1956 540 return $flags;
1957 }
1958 sub _shm_key {
1959 # Generates a 32-bit CRC on the key string. The $key_str parameter is used
1960 # for testing only, for purposes of testing various key strings
1961  
1962 567 ▶ my ($knot, $key_str) = @_;
Subroutine _shm_key: called CC=8 SCAR=20.8
1963  
1964 567 ▶ $key_str //= ($knot->attributes('key') || '');
Condition80%4/5
$key_str //= $knot->attributes("key") || ""
l!l&&r!l&&!r
265410
$knot->attributes('key') || ''
l!l
45784
MC/DC0%0/2
$key_str //= $knot->attributes("key") || ""
$key_str $knot->attributes('key')
Truth table66%2/3
A //= B
A= $key_strB= $knot->attributes('key')
ABresult
1X1
011
000
1965  
1966 567 my $key;
1967  
1968 567 ▶ if ($key_str eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if ($key_str eq '') { } 84 483 567
elsif ($key_str =~ /^0x[0-9a-fA-F]+$/i) { } 28 455 483
elsif ($key_str =~ /^\d+$/) { } 213 242 455
1969 84 $key = IPC_PRIVATE;
1970 }
1971 elsif ($key_str =~ /^0x[0-9a-fA-F]+$/i) {
1972 # User specified an explicit hex string key (eg. '0xDEADBEEF'); use the
1973
1974 # bit pattern as-is so the segment key seen by ipcs(1) matches exactly.
1975 28 $key = hex($key_str);
1976 28 $used_ids{$key}++;
1977 28 return $key;
1978 }
1979 elsif ($key_str =~ /^\d+$/) {
1980 # User specified an explicit decimal integer key; use it as-is.
1981 213 $key = $key_str;
1982 213 $used_ids{$key}++;
1983 213 return $key;
1984 }
1985 else {
1986 # String key: compute a 32-bit CRC and apply overflow correction so the
1987 # result fits in a signed 32-bit key_t.
1988 242 $key = crc32($key_str);
1989 }
1990  
1991 326 $used_ids{$key}++;
1992  
1993 326 ▶ if ($key >= MAX_KEY_INT_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key >= 2147483648) 141 185 326
1994 141 $key = $key - MAX_KEY_INT_SIZE;
1995  
1996 141 ▶ if ($key == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key == 0) 1 140 141
1997 1 croak "We've calculated a key which equals 0. This is a fatal error";
1998 }
1999 }
2000  
2001 325 return $key;
2002 }
2003 sub _shm_key_rand {
2004 115 ▶ my $key;
Subroutine _shm_key_rand: called CC=7 SCAR=19.5
2005  
2006 # Unfortunately, the only way I know how to check if a segment exists is
2007 # to actually create it. We must do that here, then remove it just to
2008 # ensure the slot is available
2009  
2010 115 my $verified_exclusive = 0;
2011  
2012 115 my $check_count = 0;
2013  
2014 115 ▶ while (! $verified_exclusive && $check_count < EXCLUSIVE_CHECK_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $verified_exclusive and $check_count < 10) 124 115 239
Condition100%3/3
not $verified_exclusive and $check_count < 10
!ll&&!rl&&r
1141124
MC/DC100%2/2
not $verified_exclusive and $check_count < 10
not $verified_exclusive $check_count < 10
Truth table100%3/3
not(A) and B
A= not $verified_exclusiveB= $check_count < 10
ABresult
0X0
100
111
2015 124 $check_count++;
2016  
2017 124 $key = _shm_key_rand_int();
2018  
2019 124 ▶ next if $used_ids{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $used_ids{$key} 10 114 124
2020  
2021 114 my $flags;
2022 114 $flags |= IPC_CREAT;
2023 114 $flags |= IPC_EXCL;
2024  
2025 114 my $seg;
2026  
2027 114 my $shm_slot_available = eval {
2028 114 $seg = IPC::Shareable::SharedMem->new(
2029 key => $key,
2030 size => 1,
2031 flags => $flags,
2032 );
2033 114 1;
2034 };
2035  
2036 114 ▶ if ($shm_slot_available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shm_slot_available) 114 0 114
2037 114 $verified_exclusive = 1;
2038 114 ▶ $seg->remove if $seg;
Branch: 1 branch
BranchTrueFalseTotal
if $seg 114 0 114
2039 }
2040 }
2041  
2042 115 ▶ if (! $verified_exclusive) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($verified_exclusive) 1 114 115
2043 1 croak
2044 "_shm_key_rand() can't get an available key after $check_count tries";
2045 }
2046  
2047 114 $used_ids{$key}++;
2048  
2049 114 return $key;
2050 }
2051 sub _shm_key_rand_int {
2052 113 ▶ return int(rand(1_000_000));
Subroutine _shm_key_rand_int: called CC=1 SCAR=0.0
2053 }
2054 sub _read_check {
2055 962 ▶ my ($knot) = @_;
Subroutine _read_check: called CC=5 SCAR=16.1
2056  
2057 # Advisory only: never blocks the read, only warns. Called from FETCH
2058 # when this knot is unlocked (a locked FETCH uses _data cache and never
2059 # touches shmem). Race window exists between this getval() and the
2060 # subsequent _decode() — a writer could acquire in between — but this
2061 # still catches the common case where a reader forgot to lock.
2062  
2063 962 ▶ return unless $knot->attributes('enforced_read_locking');
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->attributes("enforced_read_locking") 13 949 962
2064 949 ▶ return unless $knot->attributes('violated_read_lock_warn');
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->attributes("violated_read_lock_warn") 3 946 949
2065  
2066 # getval() can return undef if the semaphore set has been removed (eg.
2067 # after clean_up_all). The check is advisory only, so silently skip when
2068 # the semaphore is no longer reachable.
2069  
2070 946 my $writers = $knot->sem->getval(SEM_WRITERS);
2071 946 ▶ return unless defined $writers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $writers 0 946 946
2072  
2073 946 ▶ if ($writers > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writers > 0) 4 942 946
2074 4 my $uuid = $knot->uuid;
2075 4 my $seg_id = $knot->seg->id;
2076 4 warn "Object with UUID $uuid attempted read from segment ID "
2077 . "$seg_id which is exclusively locked (enforced read locking "
2078 . "enabled); returned data may be stale or partially-written. "
2079 . "Acquire LOCK_SH before reading to guarantee a coherent snapshot";
2080 }
2081  
2082 946 return;
2083 }
2084 sub _write_permitted {
2085 962 ▶ my ($knot) = @_;
Subroutine _write_permitted: called CC=7 SCAR=19.5
2086  
2087 962 ▶ return 1 unless $knot->attributes('enforced_write_locking');
Branch: 1 branch
BranchTrueFalseTotal
unless $knot->attributes("enforced_write_locking") 30 932 962
2088  
2089 # If this knot itself holds LOCK_EX it is the owner of the lock and is
2090 # permitted to write.
2091  
2092 932 ▶ return 1 if $knot->{_lock} & LOCK_EX;
Branch: 1 branch
BranchTrueFalseTotal
if $knot->{"_lock"} & 2 58 874 932
2093  
2094 874 my $sem = $knot->sem;
2095  
2096 # Semaphore index 2 is the write-lock counter; it is 1 when any other knot
2097 # holds LOCK_EX (set via SEM_UNDO so it auto-releases on process exit).
2098  
2099 # Block if any process holds LOCK_EX
2100  
2101 874 ▶ if ($sem->getval(SEM_WRITERS) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sem->getval(2) > 0) 12 862 874
2102 12 ▶ if ($knot->attributes('violated_write_lock_warn')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->attributes("violated_write_lock_warn")) 11 1 12
2103 11 my $uuid = $knot->uuid;
2104 11 my $seg_id = $knot->seg->id;
2105 11 warn "Object with UUID $uuid attempted write to segment ID "
2106 . "$seg_id which is exclusively locked (enforced write "
2107 . "locking enabled). Your write was not accepted. Lock with "
2108 . "LOCK_EX to ensure successful writes when a segment is "
2109 . "already locked";
2110 }
2111  
2112 12 return 0;
2113 }
2114  
2115 # Block if any process holds LOCK_SH (active readers present)
2116  
2117 862 ▶ if ($sem->getval(SEM_READERS) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sem->getval(1) > 0) 3 859 862
2118 3 ▶ if ($knot->attributes('violated_write_lock_warn')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($knot->attributes("violated_write_lock_warn")) 3 0 3
2119 3 my $uuid = $knot->uuid;
2120 3 my $seg_id = $knot->seg->id;
2121 3 warn "Object with UUID $uuid attempted write to segment ID "
2122 . "$seg_id which has active readers (enforced write locking "
2123 . "enabled)";
2124 }
2125  
2126 3 return 0;
2127 }
2128  
2129 859 return 1;
2130 }
2131  
2132 # Unit testing support
2133  
2134 sub _testing_semaphore_key_hash {
2135 398 ▶ my ($dist_name) = @_;
Subroutine _testing_semaphore_key_hash: called CC=2 SCAR=7.1
2136 # SysV SEMVMX caps semaphore values at 32767 on most platforms (incl.
2137 # macOS, BSD); mask the CRC32 to 15 bits so setval() never silently fails.
2138 # 0 is reserved to mean "not a testing segment", so we shift any zero
2139 # collision off slot 0.
2140 398 my $h = String::CRC32::crc32($dist_name) & 0x7FFF;
2141 398 ▶ return $h || 1;
Condition50%1/2
$h || 1
l!l
3980
MC/DC0%0/1
$h || 1
$h
Truth table50%1/2
A
A= $h
Aresult
11
00
2142 }
2143 sub _testing_semaphore_value {
2144 135 ▶ my ($sem) = @_;
Subroutine _testing_semaphore_value: called CC=4 SCAR=14.9
2145 135 ▶ my $stat = $sem->stat or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $stat = $sem->stat or return 0 0 135 135
2146 135 ▶ return 0 if $stat->nsems < 5;
Branch: 1 branch
BranchTrueFalseTotal
if $stat->nsems < 5 0 135 135
2147 135 ▶ return $sem->getval(SEM_TESTING) // 0;
Condition50%1/2
$sem->getval(4) // 0
l!l
1350
MC/DC0%0/1
$sem->getval(4) // 0
$sem->getval(4)
Truth table50%1/2
A
A= $sem->getval(4)
Aresult
11
00
2148 }
2149  
2150 # Misc
2151  
2152 sub _parse_args {
2153 540 ▶ my ($opts) = @_;
Subroutine _parse_args: called CC=10 SCAR=23.0
2154  
2155 540 ▶ $opts = defined $opts ? $opts : { %default_options };
Branch: 1 branch
BranchTrueFalseTotal
defined $opts ? : 538 2 540
2156  
2157 # Note caller's explicit intent BEFORE defaults are merged in. A caller
2158 # who passes testing => 0 wants to opt out of auto-tagging; we must not
2159 # treat that as "absent" after defaulting.
2160 540 my $testing_explicit = exists $opts->{testing};
2161  
2162 540 for my $k (keys %default_options) {
2163 8640 ▶ if (not defined $opts->{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts->{$k}) { } 3770 4870 8640
elsif ($opts->{$k} eq 'no') { } 2 4868 4870
2164 3770 $opts->{$k} = $default_options{$k};
2165 }
2166 elsif ($opts->{$k} eq 'no') {
2167 2 ▶ if ($^W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^W) 1 1 2
2168 1 require Carp;
2169 1 Carp::carp("Use of `no' in IPC::Shareable args is obsolete");
2170 }
2171  
2172 2 $opts->{$k} = 0;
2173 }
2174 }
2175 540 ▶ $opts->{owner} = ($opts->{owner} or $$);
Condition66%2/3
$opts->{'owner'} || $$
l!l&&r!l&&!r
2203200
MC/DC0%0/2
$opts->{'owner'} || $$
$opts->{'owner'} $$
Truth table66%2/3
A || B
A= $opts->{'owner'}B= $$
ABresult
1X1
011
000
2176 540 ▶ $opts->{magic} = ($opts->{magic} or 0);
Condition100%2/2
$opts->{'magic'} || 0
l!l
219321
MC/DC100%1/1
$opts->{'magic'} || 0
$opts->{'magic'}
Truth table100%2/2
A
A= $opts->{'magic'}
Aresult
11
00
2177  
2178 # Inherit the process-level testing tag set by testing_set(), unless the
2179 # caller explicitly passed a testing value (including testing => 0).
2180 540 ▶ if ($_testing_dist && ! $testing_explicit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_testing_dist and not $testing_explicit) 308 232 540
Condition100%3/3
$_testing_dist and not $testing_explicit
!ll&&!rl&&r
3229308
MC/DC100%2/2
$_testing_dist and not $testing_explicit
$_testing_dist not $testing_explicit
Truth table100%3/3
A and not(B)
A= $_testing_distB= not $testing_explicit
ABresult
0X0
100
111
2181 308 $opts->{testing} = $_testing_dist;
2182 }
2183  
2184 540 return $opts;
2185 }
2186 sub _end {
2187 134 ▶ for my $s (values %process_register) {
Subroutine _end: called CC=5 SCAR=16.1
2188 153 eval { unlock($s) };
2189 153 ▶ next if $s->attributes('protected');
Branch: 1 branch
BranchTrueFalseTotal
if $s->attributes("protected") 0 153 153
2190 153 ▶ next if ! $s->attributes('destroy');
Branch: 1 branch
BranchTrueFalseTotal
unless $s->attributes("destroy") 17 136 153
2191 136 ▶ next if $s->attributes('owner') != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $s->attributes("owner") != $$ 49 87 136
2192 87 eval { remove($s) };
2193 }
2194 }
2195  
2196 END {
2197 83 ▶ _end();
Subroutine END: called CC=1 SCAR=0.0
2198 }
2199  
2200 ▶sub _placeholder {}
Subroutine _placeholder: not called CC=1 SCAR=0.0
2201  
2202 83 1;
2203  
2204 __END__
↑ Summary blib/lib/IPC/Shareable/SharedMem.pm »