blib/lib/IPC/Transit/Internal.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 163 CC 9 SCAR 20.8
CC
9
Cov
0%
_get_queue_id
20.0
_lock_config_file
12.0
_initialize_queue
6.0
CRAP 8.0
1 package IPC::Transit::Internal;
2  
3 use strict;
4 use IPC::SysV;
5 use IPC::Msg;
6 use POSIX;
7  
8  
9 use vars qw(
10 $config
11 );
12  
13 {
14 my $queue_cache = {};
15 sub
16 _initialize_queue {
17 my %args = @_;
18 my $qid = _get_queue_id(%args);
19 if(not $queue_cache->{$qid}) {
20 $queue_cache->{$qid} = IPC::Msg->new($qid, _get_flags('create_ipc'))
21 or die "failed to _initialize_queue: failed to create queue_id $qid: $!\n";
22 }
23 return $queue_cache->{$qid};
24 }
25 }
26  
27 sub
28 _get_transit_config_file {
29 my $cf = $IPC::Transit::config_file || 'transit.conf';
30 my $dir = $IPC::Transit::config_dir || '/tmp/';
31 return "$dir/$cf";
32 }
33  
34 sub
35 _lock_config_file {
36 my $lock_file = _get_transit_config_file() . '.lock';
37 my ($have_lock, $fh);
38 for (1..10) {
39 if(sysopen($fh, $lock_file, _get_flags('exclusive_lock'))) {
40 $have_lock = 1;
41 last;
42 }
43 sleep 1;
44 }
45 if(not $have_lock) {
46 _unlock_config_file();
47 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 my $lock_file = _get_transit_config_file() . '.lock';
56 unlink $lock_file or die "_unlock_config_file: failed to unlink $lock_file: $!";
57 }
58  
59 sub
60 _load_transit_config {
61 my $cf = _get_transit_config_file();
62 if(not -r $cf) {
63 my $previous_umask = umask 0000;
64 open my $fh, '>', $cf or die "failed to open '$cf' for writing: $!\n";
65 print $fh '' or die "failed to write to '$cf': $!\n";
66 close $fh or die "failed to close '$cf': $!";
67 umask $previous_umask;
68 }
69 my $queues = {};
70 open my $fh, '<', $cf or die "failed to open '$cf' for writing: $!\n";
71 while(my $line = <$fh>) {
72 chomp $line;
73 my ($qname, $qid, @others) = split ':', $line;
74 $queues->{$qname} = { qid => $qid, @others };
75 }
76 close $fh or die "failed to close '$cf': $!";
77 $config->{queues} = $queues;
78 return $config;
79 }
80  
81 sub
82 _write_transit_config {
83 my $cf = _get_transit_config_file();
84 my $previous_umask = umask 0000;
85 _lock_config_file();
86 eval {
87 open my $fh, '>', $cf or die "failed to open '$cf' for writing: $!\n";
88 while (my($qname, $rec) = each %{$config->{queues}}) {
89 print $fh "$qname:$rec->{qid}\n" or die "failed to write to '$cf': $!\n";
90 }
91 close $fh or die "failed to close '$cf': $!";
92 };
93 _unlock_config_file();
94 umask $previous_umask;
95 die "_write_transit_config failed: $@\n" if $@;
96 return $config;
97 }
98  
99  
100 sub
101 _get_queue_id {
102 my %args = @_;
103 my $qname = $args{qname};
104 return $config->{queues}->{$qname}->{qid}
105 if $config->{queues} and $config->{queues}->{$qname};
106  
107 _lock_config_file();
108 eval {
109 _load_transit_config();
110 if($config->{queues} and $config->{queues}->{$qname}) {
111 _unlock_config_file();
112 return $config->{queues}->{$qname}->{qid};
113 }
114 my $next_number;
115 if(scalar keys %{$config->{queues}}) {
116 { my @current_numbers = sort {$a <=> $b} values %{$config->{queues}};
117 my $highest_number = pop @current_numbers;
118 $next_number = $highest_number++;
119 }
120 } else {
121 #$config->{queues}->{$qname} = { qid => 1 };
122 $next_number = 1;
123 }
124 $config->{queues}->{$qname} = { qid => $next_number };
125 _unlock_config_file();
126 _write_transit_config();
127 };
128  
129 _unlock_config_file();
130 return $config->{queues}->{$qname}->{qid};
131 }
132  
133 #gnarly looking UNIX goop hidden below
134 {
135 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 my $name = shift;
156 return $flags->{$name};
157 }
158 }
159 1;