blib/lib/IPC/Shareable.pm

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