| 1 | package File::Flock::Subprocess; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 30 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 4 | 30 | @EXPORT = qw(lock unlock lock_rename forget_lock); | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # use Smart::Comments; | ||||||||||||||||||||||||||||
| 7 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 30 | require Exporter; | |||||||||||||||||||||||||||
| 10 | 30 | require POSIX; | |||||||||||||||||||||||||||
| 11 | 30 | ▶ | use Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 30 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 30 | ▶ | use Time::HiRes qw(sleep time); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 30 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 30 | ▶ | use File::Temp qw(tempdir); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 30 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 30 | ▶ | use Data::Structure::Util qw(unbless); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # shared | ||||||||||||||||||||||||||||
| 20 | 30 | my $dir; | |||||||||||||||||||||||||||
| 21 | my $socket; | ||||||||||||||||||||||||||||
| 22 | 30 | my $av0; | |||||||||||||||||||||||||||
| 23 | 30 | my $debug; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 30 | ▶ | BEGIN { $debug = 0; } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # proxy server | ||||||||||||||||||||||||||||
| 28 | 30 | my $connections; | |||||||||||||||||||||||||||
| 29 | 30 | my $parent_pid; | |||||||||||||||||||||||||||
| 30 | 30 | my $timer; | |||||||||||||||||||||||||||
| 31 | 30 | my $ioe_parent; | |||||||||||||||||||||||||||
| 32 | 30 | my $counter = '0001'; | |||||||||||||||||||||||||||
| 33 | 30 | my %locks; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # client side | ||||||||||||||||||||||||||||
| 36 | my $child; | ||||||||||||||||||||||||||||
| 37 | 30 | my %lock_pids; # filename -> pid | |||||||||||||||||||||||||||
| 38 | 30 | my %lock_proxies; # pid -> proxy | |||||||||||||||||||||||||||
| 39 | 30 | my %lock_count; # pid -> count | |||||||||||||||||||||||||||
| 40 | 30 | my $last_pid; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub new | ||||||||||||||||||||||||||||
| 43 | 156 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 156 | my ($pkg, $file, $shared, $nonblocking) = @_; | |||||||||||||||||||||||||||
| 45 | 156 | ▶ | &lock($file, $shared, $nonblocking) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 156 | return bless [$file], __PACKAGE__; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub DESTROY | ||||||||||||||||||||||||||||
| 50 | 136 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 136 | my ($this) = @_; | |||||||||||||||||||||||||||
| 52 | 136 | unlock($this->[0]); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub encode | ||||||||||||||||||||||||||||
| 56 | 2030 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 2030 | local($_); | |||||||||||||||||||||||||||
| 58 | 2030 | for $_ (@_) { | |||||||||||||||||||||||||||
| 59 | ### assert: defined $_ | ||||||||||||||||||||||||||||
| 60 | 2036 | s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 61 | 2036 | s/\n/\\n/g; | |||||||||||||||||||||||||||
| 62 | 2036 | s/\t/\\t/g; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub decode | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
decode: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 0 | local($_); | |||||||||||||||||||||||||||
| 69 | 0 | for $_ (@_) { | |||||||||||||||||||||||||||
| 70 | ### assert: defined $_ | ||||||||||||||||||||||||||||
| 71 | 0 | s/\\t/\t/g; | |||||||||||||||||||||||||||
| 72 | 0 | s/\\n/\n/g; | |||||||||||||||||||||||||||
| 73 | 0 | s/\\\\/\\/g; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub update_proxy_connections | ||||||||||||||||||||||||||||
| 78 | 2030 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
update_proxy_connections: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 30 | ▶ | use Carp qw(longmess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 2030 | ▶ | print STDERR longmess("last_pid undefined") unless defined $last_pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2030 | ▶ | return if $last_pid == $$; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | ### UPDATING PROXY CONNECTIONS: "$$ IS NOT $last_pid" | ||||||||||||||||||||||||||||
| 83 | 26 | $last_pid = $$; | |||||||||||||||||||||||||||
| 84 | 26 | for my $pid (keys %lock_proxies) { | |||||||||||||||||||||||||||
| 85 | 10 | ▶ | my $proxy = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | Peer => "$socket.$pid", | ||||||||||||||||||||||||||||
| 87 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||
| 88 | ) or carp "Could not open connection to lockserver $socket.$pid: $!"; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | ### CLOSING OLD $$ | ||||||||||||||||||||||||||||
| 91 | 10 | $lock_proxies{$pid}->close(); | |||||||||||||||||||||||||||
| 92 | 10 | $lock_proxies{$pid} = $proxy; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | ### DONE UPDATING: $$ | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub request | ||||||||||||||||||||||||||||
| 98 | 2070 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
request: called CC=6 SCAR=20.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 2070 | my ($request, $file) = @_; | |||||||||||||||||||||||||||
| 100 | 2070 | my $av0 = $0; | |||||||||||||||||||||||||||
| 101 | 2070 | local($0) = $av0; | |||||||||||||||||||||||||||
| 102 | 2070 | $0 = "$av0 - lock proxy request $request"; | |||||||||||||||||||||||||||
| 103 | 2070 | my $ts_before = time; | |||||||||||||||||||||||||||
| 104 | ### REQUEST: "$$ $request" | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2070 | ▶ | my $proxy = $lock_proxies{$lock_pids{$file}} or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2070 | ▶ | $proxy->print("$$ $request\n") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | or croak "print to lock proxy: $!"; | ||||||||||||||||||||||||||||
| 110 | 2070 | for(;;) { | |||||||||||||||||||||||||||
| 111 | 2070 | my $ok = $proxy->getline(); | |||||||||||||||||||||||||||
| 112 | 2070 | chomp($ok); | |||||||||||||||||||||||||||
| 113 | ### RESPONSE: $ok | ||||||||||||||||||||||||||||
| 114 | 2070 | ▶ | if ($ts_before) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 2070 | my $diff = time - $ts_before; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 2070 | ▶ | if ($ok =~ /^ERROR:(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 118 | 0 | my $error = $1; | |||||||||||||||||||||||||||
| 119 | 0 | decode($error); | |||||||||||||||||||||||||||
| 120 | ### ................. $error | ||||||||||||||||||||||||||||
| 121 | 0 | $error =~ s/\n.*//s; | |||||||||||||||||||||||||||
| 122 | ### ..... $error | ||||||||||||||||||||||||||||
| 123 | 0 | croak $error; | |||||||||||||||||||||||||||
| 124 | } elsif ($ok =~ /^RESULT=(\d+)/) { | ||||||||||||||||||||||||||||
| 125 | ### RESULT: $$.$1 | ||||||||||||||||||||||||||||
| 126 | 2070 | return $1; | |||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 0 | die "unexpected response from lock proxy: $ok"; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub lock | ||||||||||||||||||||||||||||
| 134 | 1446 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 1446 | my ($file, $shared, $nonblocking) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1446 | update_proxy_connections(); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1446 | ▶ | if (!$lock_pids{$file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 604 | $lock_pids{$file} = $$; | |||||||||||||||||||||||||||
| 141 | 604 | $lock_count{$$}++; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 1446 | ▶ | if (!$lock_proxies{$$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 40 | ▶ | $lock_proxies{$$} = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | Peer => $socket, | ||||||||||||||||||||||||||||
| 146 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||
| 147 | ) or carp "Could not open connection to lockserver $socket: $!"; | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 40 | request("LISTEN", $file); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 1446 | ▶ | $shared = $shared ? "1" : "0"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 1446 | ▶ | $nonblocking = $nonblocking ? "1" : "0"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 1446 | my $orig_file = $file; | |||||||||||||||||||||||||||
| 155 | 1446 | encode($file); | |||||||||||||||||||||||||||
| 156 | 1446 | my $r = request("LOCK $shared$nonblocking $file", $file); | |||||||||||||||||||||||||||
| 157 | 1446 | ▶ | $locks{$orig_file} = $$ if $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 1446 | return $r; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub unlock | ||||||||||||||||||||||||||||
| 162 | 578 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unlock: called CC=5 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 578 | my ($file) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 578 | ▶ | if (ref $file eq __PACKAGE__) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 20 | unbless $file; # avoid destructor later | |||||||||||||||||||||||||||
| 167 | 20 | $file = $file->[0]; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 578 | update_proxy_connections(); | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 578 | ▶ | if (ref $file eq 'File::Flock') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | bless $file, 'UNIVERSAL'; # avoid destructor later | |||||||||||||||||||||||||||
| 174 | 0 | $file = $$file; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 578 | ▶ | croak "File $file not locked" unless $lock_pids{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 578 | my $orig_file = $file; | |||||||||||||||||||||||||||
| 178 | 578 | encode($file); | |||||||||||||||||||||||||||
| 179 | 578 | my $r = request("UNLOCK $file", $file); | |||||||||||||||||||||||||||
| 180 | 578 | my $lock_pid = delete $lock_pids{$orig_file}; | |||||||||||||||||||||||||||
| 181 | 578 | ▶ | if ($lock_count{$lock_pid} <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | delete $lock_proxies{$lock_pid}; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 578 | delete $locks{$orig_file}; | |||||||||||||||||||||||||||
| 185 | 578 | return $r; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | sub lock_rename | ||||||||||||||||||||||||||||
| 189 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
lock_rename: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 190 | 6 | ▶ | croak "arguments to lock_rename" unless @_ == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 6 | my ($oldfile, $newfile) = @_; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 6 | ▶ | if (ref $oldfile eq 'File::Flock::Subprocess') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 6 | my $obj = $oldfile; | |||||||||||||||||||||||||||
| 195 | 6 | $oldfile = $obj->[0]; | |||||||||||||||||||||||||||
| 196 | 6 | $obj->[0] = $newfile; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 6 | update_proxy_connections(); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 6 | ▶ | carp "File $oldfile not locked" unless $lock_pids{$oldfile}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 6 | ▶ | carp "File $newfile already locked" if $lock_pids{$newfile}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 6 | my ($orig_oldfile, $orig_newfile) = ($oldfile, $newfile); | |||||||||||||||||||||||||||
| 204 | 6 | encode($oldfile, $newfile); | |||||||||||||||||||||||||||
| 205 | 6 | my $r = request("LOCK_RENAME $oldfile\t$newfile", $oldfile); | |||||||||||||||||||||||||||
| 206 | 6 | $lock_pids{$orig_newfile} = delete $lock_pids{$orig_oldfile}; | |||||||||||||||||||||||||||
| 207 | 6 | ▶ | $locks{$orig_newfile} = delete $locks{$orig_oldfile} if exists $locks{$orig_oldfile}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 6 | return $r; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | sub forget_locks | ||||||||||||||||||||||||||||
| 212 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
forget_locks: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 213 | 2 | %locks = (); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | sub final_cleanup | ||||||||||||||||||||||||||||
| 217 | 60 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
final_cleanup: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 218 | 60 | for (keys %locks) { | |||||||||||||||||||||||||||
| 219 | 50 | ▶ | unlock($_) if $locks{$_} == $$; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 60 | ▶ | $child->close() if defined $child; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 60 | undef $child; | |||||||||||||||||||||||||||
| 223 | 60 | undef %lock_proxies; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 30 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 227 | 30 | final_cleanup(); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | sub run_lockserver | ||||||||||||||||||||||||||||
| 231 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
run_lockserver: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 232 | 0 | my ($parent) = @_; | |||||||||||||||||||||||||||
| 233 | 0 | require IO::Event; | |||||||||||||||||||||||||||
| 234 | 0 | import IO::Event 'AnyEvent'; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | my $ioe_listener = IO::Event::Socket::UNIX->new( | |||||||||||||||||||||||||||
| 237 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||
| 238 | Local => $socket, | ||||||||||||||||||||||||||||
| 239 | Listen => 255, | ||||||||||||||||||||||||||||
| 240 | Handler => 'File::Flock::Subprocess::Master', | ||||||||||||||||||||||||||||
| 241 | Description => "listen($socket)", | ||||||||||||||||||||||||||||
| 242 | ); | ||||||||||||||||||||||||||||
| 243 | 0 | ▶ | carp "could not listen on unix socket: $!" unless $ioe_listener; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # we don't add a connection for the listener | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | $parent->print("ready\n"); | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | $ioe_parent = IO::Event->new($parent, __PACKAGE__, | |||||||||||||||||||||||||||
| 250 | { description => 'socketpair', read_only => 1}); | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 0 | $connections->add($ioe_parent); | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | $timer = IO::Event->timer( | ||||||||||||||||||||||||||||
| 256 | interval => 2, | ||||||||||||||||||||||||||||
| 257 | 0 | ▶ | cb => sub { $connections->display() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 258 | 0 | ); | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 0 | IO::Event::loop(); | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 0 | File::Flock::final_cleanup_flock(); | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | { | ||||||||||||||||||||||||||||
| 267 | package File::Flock::Subprocess::Master; | ||||||||||||||||||||||||||||
| 268 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 269 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # lock proxy master accepting connection to start new child | ||||||||||||||||||||||||||||
| 272 | sub ie_connection | ||||||||||||||||||||||||||||
| 273 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ie_connection: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 274 | 0 | my ($pkg, $ioe) = @_; | |||||||||||||||||||||||||||
| 275 | 0 | ▶ | my $client = $ioe->accept('File::Flock::Subprocess') or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | ### CONNECT IN MASTER: "$$ - @{[$ioe->ie_desc()]}" | ||||||||||||||||||||||||||||
| 277 | 0 | my $new_child; | |||||||||||||||||||||||||||
| 278 | 0 | for(;;) { | |||||||||||||||||||||||||||
| 279 | 0 | $new_child = fork(); | |||||||||||||||||||||||||||
| 280 | ### FORKED IN ACCEPT | ||||||||||||||||||||||||||||
| 281 | ### PID: $$ | ||||||||||||||||||||||||||||
| 282 | ### CHILD: $new_child | ||||||||||||||||||||||||||||
| 283 | 0 | ▶ | last if defined $new_child; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 0 | warn "Could not fork: $!"; | |||||||||||||||||||||||||||
| 285 | 0 | sleep(1); | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | 0 | ▶ | if ($new_child) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | # now is as good a time as any to clean up zombies | ||||||||||||||||||||||||||||
| 289 | 0 | my $kid; | |||||||||||||||||||||||||||
| 290 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 0 | $kid = waitpid(-1, &POSIX::WNOHANG); | |||||||||||||||||||||||||||
| 292 | ### CHILD PROXY ZOMBIE REAPED: $kid | ||||||||||||||||||||||||||||
| 293 | } while $kid > 0; | ||||||||||||||||||||||||||||
| 294 | 0 | $client->close(); | |||||||||||||||||||||||||||
| 295 | } else { | ||||||||||||||||||||||||||||
| 296 | 0 | $ioe->close(); | |||||||||||||||||||||||||||
| 297 | 0 | $connections->remove($ioe_parent); | |||||||||||||||||||||||||||
| 298 | 0 | $ioe_parent->close(); | |||||||||||||||||||||||||||
| 299 | 0 | undef $ioe_parent; | |||||||||||||||||||||||||||
| 300 | ### NEW CHILD PROXY SERVER $$ | ||||||||||||||||||||||||||||
| 301 | 0 | $av0 = "Locking proxy slave for $parent_pid using $socket"; | |||||||||||||||||||||||||||
| 302 | 0 | $connections->add($client, "connection($socket)"); | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | 0 | ▶ | sub ie_input { | ||||||||||||||||||||||||||
|
Subroutine
ie_input: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 306 | 0 | die; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | 0 | ▶ | sub ie_eof { | ||||||||||||||||||||||||||
|
Subroutine
ie_eof: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 309 | 0 | die; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # lock proxy children accepting replacement connections | ||||||||||||||||||||||||||||
| 314 | sub ie_connection | ||||||||||||||||||||||||||||
| 315 | 30 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ie_connection: not called CC=1 SCAR=4.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 316 | 0 | my ($pkg, $ioe) = @_; | |||||||||||||||||||||||||||
| 317 | 0 | my $replacement = $ioe->accept(); | |||||||||||||||||||||||||||
| 318 | 0 | $connections->add($replacement, "slave(@{[$ioe->ie_desc().$counter++]})"); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | # could be lock server master losing socketpair or lock server | ||||||||||||||||||||||||||||
| 322 | # proxy child losing a client | ||||||||||||||||||||||||||||
| 323 | sub ie_eof | ||||||||||||||||||||||||||||
| 324 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ie_eof: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 325 | ### EOF IN CHILD | ||||||||||||||||||||||||||||
| 326 | 0 | my ($handler, $ioe, $input_buffer_reference) = @_; | |||||||||||||||||||||||||||
| 327 | 0 | $ioe->close(); | |||||||||||||||||||||||||||
| 328 | 0 | ▶ | unless ($connections->remove($ioe)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | ### "PROXY SERVER $$ ALL DONE" | ||||||||||||||||||||||||||||
| 330 | 0 | IO::Event::unloop_all(); | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | sub ie_input | ||||||||||||||||||||||||||||
| 335 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ie_input: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 336 | ### INPUT IN CHILD | ||||||||||||||||||||||||||||
| 337 | 0 | my ($handler, $ioe, $input_buffer_reference) = @_; | |||||||||||||||||||||||||||
| 338 | 0 | $0 = "$av0: processing request"; | |||||||||||||||||||||||||||
| 339 | 0 | ▶ | while (my $request = $ioe->getline()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 0 | $0 = "$av0: handling $request"; | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | my $pid; | |||||||||||||||||||||||||||
| 343 | 0 | ▶ | $request =~ s/^(\d+) // | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | or die "bad request to lock proxy: $request"; | ||||||||||||||||||||||||||||
| 345 | 0 | $pid = $1; | |||||||||||||||||||||||||||
| 346 | 0 | $0 = "$av0: handling $request from $pid: $request"; | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 0 | my $r; | |||||||||||||||||||||||||||
| 349 | ### PROCESSING REQUEST FROM $pid : $request | ||||||||||||||||||||||||||||
| 350 | 0 | eval { | |||||||||||||||||||||||||||
| 351 | 0 | ▶ | if ($request =~ m{^LOCK (.)(.) (.*)\n}s) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 352 | 0 | my ($shared, $nonblocking, $file) = ($1, $2, $3); | |||||||||||||||||||||||||||
| 353 | 0 | decode($file); | |||||||||||||||||||||||||||
| 354 | 0 | $r = File::Flock::lock_flock($file, $shared, $nonblocking); | |||||||||||||||||||||||||||
| 355 | } elsif ($request =~ m{^UNLOCK (.*)\n}s) { | ||||||||||||||||||||||||||||
| 356 | 0 | my $file = $1; | |||||||||||||||||||||||||||
| 357 | 0 | decode($file); | |||||||||||||||||||||||||||
| 358 | 0 | $r = File::Flock::unlock_flock($file); | |||||||||||||||||||||||||||
| 359 | } elsif ($request =~ m{^LOCK_RENAME (.*?)\t(.*)\n}) { | ||||||||||||||||||||||||||||
| 360 | 0 | my ($oldfile, $newfile) = ($1, $2); | |||||||||||||||||||||||||||
| 361 | 0 | decode($oldfile, $newfile); | |||||||||||||||||||||||||||
| 362 | 0 | $r = File::Flock::lock_rename_flock($oldfile, $newfile); | |||||||||||||||||||||||||||
| 363 | } elsif ($request =~ m{^LISTEN\n}) { | ||||||||||||||||||||||||||||
| 364 | 0 | ▶ | IO::Event::Socket::UNIX->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||
| 366 | Local => "$socket.$pid", | ||||||||||||||||||||||||||||
| 367 | Listen => 255, | ||||||||||||||||||||||||||||
| 368 | Description => "slave($socket.$pid)", | ||||||||||||||||||||||||||||
| 369 | ) or die "Listen $socket.$pid: $!"; | ||||||||||||||||||||||||||||
| 370 | 0 | $r = 1; | |||||||||||||||||||||||||||
| 371 | } elsif ($request =~ m{^QUIT\n}) { | ||||||||||||||||||||||||||||
| 372 | 0 | $r = 1; | |||||||||||||||||||||||||||
| 373 | } else { | ||||||||||||||||||||||||||||
| 374 | 0 | die "Unknown remote lock request: $request"; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | }; | ||||||||||||||||||||||||||||
| 377 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | 0 | my $error = $@; | |||||||||||||||||||||||||||
| 379 | 0 | encode($error); | |||||||||||||||||||||||||||
| 380 | 0 | $ioe->print("ERROR:$error\n"); | |||||||||||||||||||||||||||
| 381 | } else { | ||||||||||||||||||||||||||||
| 382 | 0 | $r = 0 + $r; | |||||||||||||||||||||||||||
| 383 | 0 | $ioe->print("RESULT=$r\n"); | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | 0 | $0 = "$av0: idle"; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | { | ||||||||||||||||||||||||||||
| 390 | package File::Flock::Subprocess::Connections; | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 30 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 393 | 30 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 396 | 0 | return bless {}; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | 0 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 399 | 0 | my ($self, $ioe, $label) = @_; | |||||||||||||||||||||||||||
| 400 | 0 | ▶ | $ioe->ie_desc($label) if $label; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | 0 | ▶ | die "duplicate @{[$ioe->ie_desc()]}" if ++$self->{$ioe->ie_desc()} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 402 | 0 | ▶ | print STDERR "PROXY $$: " . join(' ', 'ADD', $ioe->ie_desc(), ':', sort keys %$self) . "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | sub remove | ||||||||||||||||||||||||||||
| 405 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 406 | 0 | my ($self, $ioe) = @_; | |||||||||||||||||||||||||||
| 407 | 0 | ▶ | die $ioe unless $self->{$ioe->ie_desc()}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | 0 | delete $self->{$ioe->ie_desc()};; | |||||||||||||||||||||||||||
| 409 | 0 | ▶ | print STDERR "PROXY $$: " . join(' ', 'REMOVE', $ioe->ie_desc(), ':', sort keys %$self) . "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 410 | 0 | return scalar(keys %$self); | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | 0 | ▶ | sub display { | ||||||||||||||||||||||||||
|
Subroutine
display: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 413 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 414 | 0 | ▶ | print STDERR "PROXY $$: " . join(' ', sort keys %$self) . "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | 30 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=8 SCAR=27.2
| |||||||||||||||||||||||||||||
| 420 | # Let File::Flock know we're live with Subprocess | ||||||||||||||||||||||||||||
| 421 | 30 | $File::Flock::Forking::SubprocessEnabled = 1; | |||||||||||||||||||||||||||
| 422 | 30 | require File::Flock; | |||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 30 | $dir = tempdir(CLEANUP => 0); | |||||||||||||||||||||||||||
| 425 | 30 | $socket = "$dir/lock"; | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 30 | my $parent = new IO::Handle; | |||||||||||||||||||||||||||
| 428 | 30 | $child = new IO::Handle; | |||||||||||||||||||||||||||
| 429 | 30 | ▶ | socketpair($parent, $child, AF_UNIX, SOCK_STREAM, PF_UNSPEC) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | or die "cannot create socketpair: $!"; | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 30 | $parent_pid = $$; | |||||||||||||||||||||||||||
| 433 | 30 | my $child_pid; | |||||||||||||||||||||||||||
| 434 | ### FORKING: $$ | ||||||||||||||||||||||||||||
| 435 | 30 | for(;;) { | |||||||||||||||||||||||||||
| 436 | 30 | $child_pid = fork(); | |||||||||||||||||||||||||||
| 437 | ### CHILD: $child_pid | ||||||||||||||||||||||||||||
| 438 | 30 | ▶ | last if defined $child_pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 439 | 0 | warn "Could not fork: $!"; | |||||||||||||||||||||||||||
| 440 | 0 | sleep(1); | |||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | 30 | ▶ | if ($child_pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 443 | 30 | $parent->close(); | |||||||||||||||||||||||||||
| 444 | 30 | my $ready = <$child>; | |||||||||||||||||||||||||||
| 445 | 30 | ▶ | die unless $ready && $ready eq "ready\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ready and $ready eq "ready\n"
MC/DC0%0/2
$ready and $ready eq "ready\n"
$ready
$ready eq "ready\n"
Truth table33%1/3
A and B
A= $readyB= $ready eq "ready\n"
| |||||||||||||||||||||||||||||
| 446 | 30 | $last_pid = $$; | |||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | # We need File::Flock->new() to work. This is a bit gross: | ||||||||||||||||||||||||||||
| 449 | 30 | ▶ | *File::Flock::new = \&File::Flock::Subprocess::new | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 450 | unless defined &File::Flock::new; | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 30 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 453 | 0 | ▶ | $SIG{ALRM} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 454 | 0 | ▶ | print STDERR "$$ Alive with " . ($child ? "child defined" : "child undefined") . "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | 0 | alarm(2); | |||||||||||||||||||||||||||
| 456 | 0 | }; | |||||||||||||||||||||||||||
| 457 | 0 | alarm(2); | |||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | } else { | ||||||||||||||||||||||||||||
| 460 | 0 | require IO::Event; | |||||||||||||||||||||||||||
| 461 | 0 | $av0 = "Locking proxy master for $parent_pid, using $socket"; | |||||||||||||||||||||||||||
| 462 | 0 | $0 = $av0; | |||||||||||||||||||||||||||
| 463 | 0 | $child->close(); | |||||||||||||||||||||||||||
| 464 | 0 | undef $child; | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 0 | $connections = File::Flock::Subprocess::Connections->new(); | |||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 0 | run_lockserver($parent); | |||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||
| 471 | 0 | die; | |||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 30 | 1; | |||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | __END__ | ||||||||||||||||||||||||||||