| 1 | package IPC::Transit::Internal; | ||||||||||||||||||||||||||||
| 2 | 16 | $IPC::Transit::Internal::VERSION = '1.162230'; | |||||||||||||||||||||||||||
| 3 | 16 | ▶ | use strict;use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use IPC::SysV; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use IPC::Msg; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use POSIX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 16 | use vars qw( | |||||||||||||||||||||||||||
| 10 | $config | ||||||||||||||||||||||||||||
| 11 | 16 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | { | ||||||||||||||||||||||||||||
| 14 | 16 | my $queue_cache = {}; | |||||||||||||||||||||||||||
| 15 | sub _initialize_queue { | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
_initialize_queue: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 17 | 0 | my $qid = _get_queue_id(%args); | |||||||||||||||||||||||||||
| 18 | 0 | ▶ | if(not $queue_cache->{$qid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | $queue_cache->{$qid} = IPC::Msg->new($qid, _get_flags('create_ipc')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | or die "failed to _initialize_queue: failed to create queue_id $qid: $!\n"; | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 0 | return $queue_cache->{$qid}; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub _remove { | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
_remove: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 27 | 0 | my $qname = $args{qname}; | |||||||||||||||||||||||||||
| 28 | 0 | my $qid = _get_queue_id(%args); | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | $queue_cache->{$qid}->remove if $queue_cache->{$qid}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | unlink _get_transit_config_dir() . "/$qname"; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub _stat { | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
_stat: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 35 | 0 | my $qid = _get_queue_id(%args); | |||||||||||||||||||||||||||
| 36 | 0 | _initialize_queue(%args); | |||||||||||||||||||||||||||
| 37 | 0 | my @heads = qw(uid gid cuid cgid mode qnum qbytes lspid lrpid stime rtime ctime); | |||||||||||||||||||||||||||
| 38 | 0 | my $ret = {}; | |||||||||||||||||||||||||||
| 39 | 0 | my @items = @{$queue_cache->{$qid}->stat}; | |||||||||||||||||||||||||||
| 40 | 0 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 41 | 0 | $ret->{shift @heads} = $item; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | $ret->{qname} = $args{qname}; | |||||||||||||||||||||||||||
| 44 | 0 | $ret->{qid} = $qid; | |||||||||||||||||||||||||||
| 45 | 0 | return $ret; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 16 | sub _drop_all_queues { | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | foreach my $qname (keys %{$config->{queues}}) { | ||||||||||||||||||||||||||
|
Subroutine
_drop_all_queues: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 51 | 0 | _remove(qname => $qname); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | sub _stats { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my $ret = []; | ||||||||||||||||||||||||||
|
Subroutine
_stats: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 56 | 0 | _gather_queue_info(); | |||||||||||||||||||||||||||
| 57 | 0 | foreach my $queue_name (keys %{$config->{queues}}) { | |||||||||||||||||||||||||||
| 58 | 0 | push @$ret, IPC::Transit::stat(qname => $queue_name); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | return $ret; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub _get_transit_config_dir { | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $dir = $IPC::Transit::config_dir || '/tmp/ipc_transit/'; | ||||||||||||||||||||||||||
|
Subroutine
_get_transit_config_dir: not called CC=2 SCAR=17.9
Condition0%0/2
$IPC::Transit::config_dir || '/tmp/ipc_transit/'
MC/DC0%0/1
$IPC::Transit::config_dir || '/tmp/ipc_transit/'
$IPC::Transit::config_dir
Truth table0%0/2
A
A= $IPC::Transit::config_dir
| |||||||||||||||||||||||||||||
| 65 | 0 | return $dir; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub _lock_config_dir { | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $lock_file = _get_transit_config_dir() . '/.lock'; | ||||||||||||||||||||||||||
|
Subroutine
_lock_config_dir: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 70 | 0 | my ($have_lock, $fh); | |||||||||||||||||||||||||||
| 71 | 0 | for (1..2) { | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | if(sysopen($fh, $lock_file, _get_flags('exclusive_lock'))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $have_lock = 1; | |||||||||||||||||||||||||||
| 74 | 0 | last; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if(not $have_lock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | _unlock_config_dir(); | |||||||||||||||||||||||||||
| 80 | 0 | sysopen($fh, $lock_file, _get_flags('exclusive_lock')); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | #we have the advisory lock for sure now | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub _unlock_config_dir { | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $lock_file = _get_transit_config_dir() . '/.lock'; | ||||||||||||||||||||||||||
|
Subroutine
_unlock_config_dir: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | unlink $lock_file or die "_unlock_config_dir: failed to unlink $lock_file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub _gather_queue_info { | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | _mk_queue_dir(); | ||||||||||||||||||||||||||
|
Subroutine
_gather_queue_info: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | $config->{queues} = {} unless $config->{queues}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | foreach my $filename (glob _get_transit_config_dir() . '/*') { | |||||||||||||||||||||||||||
| 95 | 0 | my $info = {}; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | open my $fh, '<', $filename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | or die "IPC::Transit::Internal::_gather_queue_info: failed to open $filename for reading: $!"; | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | while(my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 100 | 0 | my ($key, $value) = split '=', $line; | |||||||||||||||||||||||||||
| 101 | 0 | $info->{$key} = $value; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | die 'required key "qid" not found' unless $info->{qid}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | die 'required key "qname" not found' unless $info->{qname}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | $config->{queues}->{$info->{qname}} = $info; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub _queue_exists { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my $qname = shift; | ||||||||||||||||||||||||||
|
Subroutine
_queue_exists: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 111 | 0 | _mk_queue_dir(); | |||||||||||||||||||||||||||
| 112 | 0 | return $config->{queues}->{$qname}; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub _get_queue_id { | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_queue_id: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 117 | 0 | _mk_queue_dir(); | |||||||||||||||||||||||||||
| 118 | 0 | my $qname = $args{qname}; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | #return it if we have it | ||||||||||||||||||||||||||||
| 121 | return $config->{queues}->{$qname}->{qid} | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if $config->{queues} and $config->{queues}->{$qname}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
MC/DC0%0/2
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
$IPC::Transit::Internal::config->{"queues"}
$IPC::Transit::Internal::config->{"queues"}{$qname}
Truth table0%0/3
A and B
A= $IPC::Transit::Internal::config->{"queues"}B= $IPC::Transit::Internal::config->{"queues"}{$qname}
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | #we don't have it; let's load it and try again | ||||||||||||||||||||||||||||
| 125 | 0 | _gather_queue_info(); | |||||||||||||||||||||||||||
| 126 | return $config->{queues}->{$qname}->{qid} | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if $config->{queues} and $config->{queues}->{$qname}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
MC/DC0%0/2
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
$IPC::Transit::Internal::config->{"queues"}
$IPC::Transit::Internal::config->{"queues"}{$qname}
Truth table0%0/3
A and B
A= $IPC::Transit::Internal::config->{"queues"}B= $IPC::Transit::Internal::config->{"queues"}{$qname}
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | #we still don't have it; get a lock, load it, try again, ane make | ||||||||||||||||||||||||||||
| 130 | #it if necessary | ||||||||||||||||||||||||||||
| 131 | 0 | _lock_config_dir(); | |||||||||||||||||||||||||||
| 132 | 0 | eval { | |||||||||||||||||||||||||||
| 133 | #now re-load the config | ||||||||||||||||||||||||||||
| 134 | 0 | _gather_queue_info(); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | #if we now have it, unlock and return it | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if($config->{queues} and $config->{queues}->{$qname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
MC/DC0%0/2
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
$IPC::Transit::Internal::config->{"queues"}
$IPC::Transit::Internal::config->{"queues"}{$qname}
Truth table0%0/3
A and B
A= $IPC::Transit::Internal::config->{"queues"}B= $IPC::Transit::Internal::config->{"queues"}{$qname}
| |||||||||||||||||||||||||||||
| 138 | 0 | _unlock_config_dir(); | |||||||||||||||||||||||||||
| 139 | 0 | return $config->{queues}->{$qname}->{qid}; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | #otherwise, we need to make one | ||||||||||||||||||||||||||||
| 143 | 0 | { my $file = _get_transit_config_dir() . "/$qname"; | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | open my $fh, '>', $file or die "IPC::Transit::Internal::_get_queue_id: failed to open $file for writing: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | my $new_qid = IPC::SysV::ftok($file, 1); | |||||||||||||||||||||||||||
| 146 | 0 | print $fh "qid=$new_qid\n"; | |||||||||||||||||||||||||||
| 147 | 0 | print $fh "qname=$qname\n"; | |||||||||||||||||||||||||||
| 148 | 0 | close $fh; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 0 | _unlock_config_dir(); | |||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | _unlock_config_dir(); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 0 | _gather_queue_info(); | |||||||||||||||||||||||||||
| 156 | 0 | return $config->{queues}->{$qname}->{qid}; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | sub _mk_queue_dir { | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | mkdir _get_transit_config_dir(), 0777 | ||||||||||||||||||||||||||
|
Subroutine
_mk_queue_dir: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | unless -d _get_transit_config_dir(); | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | #gnarly looking UNIX goop hidden below | ||||||||||||||||||||||||||||
| 165 | { | ||||||||||||||||||||||||||||
| 166 | 16 | my $flags = { | |||||||||||||||||||||||||||
| 167 | create_ipc => IPC::SysV::S_IRUSR() | | ||||||||||||||||||||||||||||
| 168 | IPC::SysV::S_IWUSR() | | ||||||||||||||||||||||||||||
| 169 | IPC::SysV::S_IRGRP() | | ||||||||||||||||||||||||||||
| 170 | IPC::SysV::S_IWGRP() | | ||||||||||||||||||||||||||||
| 171 | IPC::SysV::S_IROTH() | | ||||||||||||||||||||||||||||
| 172 | IPC::SysV::S_IWOTH() | | ||||||||||||||||||||||||||||
| 173 | IPC::SysV::IPC_CREAT(), | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | nowait => IPC::SysV::IPC_NOWAIT(), | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | exclusive_lock => POSIX::O_RDWR() | | ||||||||||||||||||||||||||||
| 178 | POSIX::O_CREAT() | | ||||||||||||||||||||||||||||
| 179 | POSIX::O_EXCL(), | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | nonblock => POSIX::O_NONBLOCK(), | ||||||||||||||||||||||||||||
| 182 | }; | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | sub | ||||||||||||||||||||||||||||
| 185 | _get_flags { | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | my $name = shift; | ||||||||||||||||||||||||||
|
Subroutine
_get_flags: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 187 | 0 | return $flags->{$name}; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 16 | 1; | |||||||||||||||||||||||||||