blib/lib/IPC/Shareable.pm

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