| 1 | package IPC::Transit::Internal; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use IPC::SysV; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use IPC::Msg; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use POSIX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | use vars qw( | |||||||||||||||||||||||||||
| 10 | $config | ||||||||||||||||||||||||||||
| 11 | 3 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | { | ||||||||||||||||||||||||||||
| 14 | 3 | my $queue_cache = {}; | |||||||||||||||||||||||||||
| 15 | sub | ||||||||||||||||||||||||||||
| 16 | _initialize_queue { | ||||||||||||||||||||||||||||
| 17 | 384 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
_initialize_queue: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 18 | 384 | my $qid = _get_queue_id(%args); | |||||||||||||||||||||||||||
| 19 | 381 | ▶ | if(not $queue_cache->{$qid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | $queue_cache->{$qid} = IPC::Msg->new($qid, _get_flags('create_ipc')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | or die "failed to _initialize_queue: failed to create queue_id $qid: $!\n"; | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 381 | return $queue_cache->{$qid}; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub | ||||||||||||||||||||||||||||
| 28 | 3 | _get_transit_config_file { | |||||||||||||||||||||||||||
| 29 | 15 | ▶ | my $cf = $IPC::Transit::config_file || 'transit.conf'; | ||||||||||||||||||||||||||
|
Subroutine
_get_transit_config_file: called CC=3 SCAR=11.7
Condition50%1/2
$IPC::Transit::config_file || 'transit.conf'
MC/DC0%0/1
$IPC::Transit::config_file || 'transit.conf'
$IPC::Transit::config_file
Truth table50%1/2
A
A= $IPC::Transit::config_file
| |||||||||||||||||||||||||||||
| 30 | 15 | ▶ | my $dir = $IPC::Transit::config_dir || '/tmp/'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$IPC::Transit::config_dir || '/tmp/'
MC/DC0%0/1
$IPC::Transit::config_dir || '/tmp/'
$IPC::Transit::config_dir
Truth table50%1/2
A
A= $IPC::Transit::config_dir
| |||||||||||||||||||||||||||||
| 31 | 15 | return "$dir/$cf"; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub | ||||||||||||||||||||||||||||
| 35 | _lock_config_file { | ||||||||||||||||||||||||||||
| 36 | 4 | ▶ | my $lock_file = _get_transit_config_file() . '.lock'; | ||||||||||||||||||||||||||
|
Subroutine
_lock_config_file: called CC=4 SCAR=15.5
| |||||||||||||||||||||||||||||
| 37 | 4 | my ($have_lock, $fh); | |||||||||||||||||||||||||||
| 38 | 4 | for (1..10) { | |||||||||||||||||||||||||||
| 39 | 4 | ▶ | if(sysopen($fh, $lock_file, _get_flags('exclusive_lock'))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 4 | $have_lock = 1; | |||||||||||||||||||||||||||
| 41 | 4 | last; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 4 | ▶ | if(not $have_lock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | _unlock_config_file(); | |||||||||||||||||||||||||||
| 47 | 0 | sysopen($fh, $lock_file, _get_flags('exclusive_lock')); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | #we have the advisory lock for sure now | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub | ||||||||||||||||||||||||||||
| 54 | _unlock_config_file { | ||||||||||||||||||||||||||||
| 55 | 7 | ▶ | my $lock_file = _get_transit_config_file() . '.lock'; | ||||||||||||||||||||||||||
|
Subroutine
_unlock_config_file: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | 7 | ▶ | unlink $lock_file or die "_unlock_config_file: failed to unlink $lock_file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub | ||||||||||||||||||||||||||||
| 60 | _load_transit_config { | ||||||||||||||||||||||||||||
| 61 | 3 | ▶ | my $cf = _get_transit_config_file(); | ||||||||||||||||||||||||||
|
Subroutine
_load_transit_config: called CC=8 SCAR=21.2
| |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | if(not -r $cf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | my $previous_umask = umask 0000; | |||||||||||||||||||||||||||
| 64 | 1 | ▶ | open my $fh, '>', $cf or die "failed to open '$cf' for writing: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | print $fh '' or die "failed to write to '$cf': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | close $fh or die "failed to close '$cf': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | umask $previous_umask; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 3 | my $queues = {}; | |||||||||||||||||||||||||||
| 70 | 3 | ▶ | open my $fh, '<', $cf or die "failed to open '$cf' for writing: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | while(my $line = <$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 2 | chomp $line; | |||||||||||||||||||||||||||
| 73 | 2 | my ($qname, $qid, @others) = split ':', $line; | |||||||||||||||||||||||||||
| 74 | 2 | $queues->{$qname} = { qid => $qid, @others }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 3 | ▶ | close $fh or die "failed to close '$cf': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 3 | $config->{queues} = $queues; | |||||||||||||||||||||||||||
| 78 | 3 | return $config; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | sub | ||||||||||||||||||||||||||||
| 82 | _write_transit_config { | ||||||||||||||||||||||||||||
| 83 | 1 | ▶ | my $cf = _get_transit_config_file(); | ||||||||||||||||||||||||||
|
Subroutine
_write_transit_config: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 84 | 1 | my $previous_umask = umask 0000; | |||||||||||||||||||||||||||
| 85 | 1 | _lock_config_file(); | |||||||||||||||||||||||||||
| 86 | 1 | eval { | |||||||||||||||||||||||||||
| 87 | 1 | ▶ | open my $fh, '>', $cf or die "failed to open '$cf' for writing: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | while (my($qname, $rec) = each %{$config->{queues}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | print $fh "$qname:$rec->{qid}\n" or die "failed to write to '$cf': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | close $fh or die "failed to close '$cf': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||
| 93 | 1 | _unlock_config_file(); | |||||||||||||||||||||||||||
| 94 | 1 | umask $previous_umask; | |||||||||||||||||||||||||||
| 95 | 1 | ▶ | die "_write_transit_config failed: $@\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | return $config; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub | ||||||||||||||||||||||||||||
| 101 | _get_queue_id { | ||||||||||||||||||||||||||||
| 102 | 384 | ▶ | my %args = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_queue_id: called CC=6 SCAR=19.1
| |||||||||||||||||||||||||||||
| 103 | 384 | my $qname = $args{qname}; | |||||||||||||||||||||||||||
| 104 | return $config->{queues}->{$qname}->{qid} | ||||||||||||||||||||||||||||
| 105 | 384 | ▶ | if $config->{queues} and $config->{queues}->{$qname}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
MC/DC50%1/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 table66%2/3
A and B
A= $IPC::Transit::Internal::config->{"queues"}B= $IPC::Transit::Internal::config->{"queues"}{$qname}
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | _lock_config_file(); | |||||||||||||||||||||||||||
| 108 | 3 | eval { | |||||||||||||||||||||||||||
| 109 | 3 | _load_transit_config(); | |||||||||||||||||||||||||||
| 110 | 3 | ▶ | if($config->{queues} and $config->{queues}->{$qname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$IPC::Transit::Internal::config->{"queues"} and $IPC::Transit::Internal::config->{"queues"}{$qname}
MC/DC50%1/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 table66%2/3
A and B
A= $IPC::Transit::Internal::config->{"queues"}B= $IPC::Transit::Internal::config->{"queues"}{$qname}
| |||||||||||||||||||||||||||||
| 111 | 2 | _unlock_config_file(); | |||||||||||||||||||||||||||
| 112 | 2 | return $config->{queues}->{$qname}->{qid}; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 1 | my $next_number; | |||||||||||||||||||||||||||
| 115 | 1 | ▶ | if(scalar keys %{$config->{queues}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | { my @current_numbers = sort {$a <=> $b} values %{$config->{queues}}; | |||||||||||||||||||||||||||
| 117 | 0 | my $highest_number = pop @current_numbers; | |||||||||||||||||||||||||||
| 118 | 0 | $next_number = $highest_number++; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||
| 121 | #$config->{queues}->{$qname} = { qid => 1 }; | ||||||||||||||||||||||||||||
| 122 | 1 | $next_number = 1; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 1 | $config->{queues}->{$qname} = { qid => $next_number }; | |||||||||||||||||||||||||||
| 125 | 1 | _unlock_config_file(); | |||||||||||||||||||||||||||
| 126 | 1 | _write_transit_config(); | |||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 3 | _unlock_config_file(); | |||||||||||||||||||||||||||
| 130 | 0 | return $config->{queues}->{$qname}->{qid}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | #gnarly looking UNIX goop hidden below | ||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 3 | my $flags = { | |||||||||||||||||||||||||||
| 136 | create_ipc => IPC::SysV::S_IRUSR() | | ||||||||||||||||||||||||||||
| 137 | IPC::SysV::S_IWUSR() | | ||||||||||||||||||||||||||||
| 138 | IPC::SysV::S_IRGRP() | | ||||||||||||||||||||||||||||
| 139 | IPC::SysV::S_IWGRP() | | ||||||||||||||||||||||||||||
| 140 | IPC::SysV::S_IROTH() | | ||||||||||||||||||||||||||||
| 141 | IPC::SysV::S_IWOTH() | | ||||||||||||||||||||||||||||
| 142 | IPC::SysV::IPC_CREAT(), | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | nowait => IPC::SysV::IPC_NOWAIT(), | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | exclusive_lock => POSIX::O_RDWR() | | ||||||||||||||||||||||||||||
| 147 | POSIX::O_CREAT() | | ||||||||||||||||||||||||||||
| 148 | POSIX::O_EXCL(), | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | nonblock => POSIX::O_NONBLOCK(), | ||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub | ||||||||||||||||||||||||||||
| 154 | _get_flags { | ||||||||||||||||||||||||||||
| 155 | 388 | ▶ | my $name = shift; | ||||||||||||||||||||||||||
|
Subroutine
_get_flags: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | 388 | return $flags->{$name}; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 3 | 1; | |||||||||||||||||||||||||||