| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | package File::CacheDir; | ||
| 4 | |||
| 5 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 5 | ▶ | use vars qw(@ISA @EXPORT_OK $VERSION %EXTANT_DIR); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 5 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use CGI qw(); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use File::Path qw(mkpath); | ||
| 10 | use IO::Dir; | ||
| 11 | |||
| 12 | use POSIX qw( setsid _exit ); | ||
| 13 | |||
| 14 | @ISA = ('Exporter'); | ||
| 15 | @EXPORT_OK = qw( cache_dir ); | ||
| 16 | $VERSION = "1.30"; | ||
| 17 | |||
| 18 | %EXTANT_DIR = (); | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $type = shift; | ||
| 22 | my $hash_ref = $_[0]; | ||
| 23 | my @PASSED_ARGS = (ref $hash_ref eq 'HASH') ? %{$_[0]} : @_; | ||
| 24 | my $cache_object; | ||
| 25 | my @DEFAULT_ARGS = ( | ||
| 26 | base_dir => "/tmp/cache_dir", | ||
| 27 | cache_stats => 1, | ||
| 28 | carry_forward => 1, | ||
| 29 | cleanup_fork => 1, | ||
| 30 | # percentage of time to attempt cleanup run | ||
| 31 | cleanup_frequency => 100, | ||
| 32 | cleanup_length => 3600, | ||
| 33 | cleanup_suffix => ".cleanup.lock", | ||
| 34 | content_typed => 0, | ||
| 35 | cookie_brick_over => 0, | ||
| 36 | cookie_name => "cache_dir", | ||
| 37 | cookie_path => '/', | ||
| 38 | filename => time . $$, | ||
| 39 | periods_to_keep => 2, | ||
| 40 | set_cookie => 0, | ||
| 41 | ttl => "1 day", | ||
| 42 | ); | ||
| 43 | my %ARGS = (@DEFAULT_ARGS, @PASSED_ARGS); | ||
| 44 | $cache_object = bless \%ARGS, $type; | ||
| 45 | |||
| 46 | # clean up a few blank things in the object | ||
| 47 | unless($cache_object->{set_cookie}) { | ||
| 48 | foreach(qw(set_cookie cookie_name cookie_path)) { | ||
| 49 | delete $cache_object->{$_}; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | foreach(qw(carry_forward)) { | ||
| 53 | delete $cache_object->{$_} unless($cache_object->{$_}); | ||
| 54 | } | ||
| 55 | |||
| 56 | return $cache_object; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub ttl_mkpath { | ||
| 60 | my $self = shift; | ||
| 61 | my $_ttl_dir = shift; | ||
| 62 | mkpath $_ttl_dir; | ||
| 63 | die "couldn't mkpath '$_ttl_dir': $!" unless($self->dash_d($_ttl_dir)); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub expired_check { | ||
| 67 | my $self = shift; | ||
| 68 | my $_sub_dir = shift; | ||
| 69 | my $diff = $self->{int_time} - $_sub_dir; | ||
| 70 | if($diff > $self->{periods_to_keep}) { | ||
| 71 | return 1; | ||
| 72 | } else { | ||
| 73 | return 0; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | ### want to be able to easily track -d checks, and want to have | ||
| 78 | ### all the cache_stats code in one place | ||
| 79 | sub dash_d { | ||
| 80 | my $self = shift; | ||
| 81 | my $_dir = shift; | ||
| 82 | my $return = 0; | ||
| 83 | if($self->{cache_stats} && $EXTANT_DIR{$_dir}) { | ||
| 84 | $return = 1; | ||
| 85 | } else { | ||
| 86 | if(-d $_dir) { | ||
| 87 | $return = 1; | ||
| 88 | $EXTANT_DIR{$_dir} = 1; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | return $return; | ||
| 92 | } | ||
| 93 | |||
| 94 | ### sub to wrap the cleanup code... this way we can control | ||
| 95 | ### when cleanup should occur, even if somebody overides | ||
| 96 | ### the cleanup logic | ||
| 97 | |||
| 98 | sub perhaps_cleanup { | ||
| 99 | my $self = shift; | ||
| 100 | my $_dir = shift; | ||
| 101 | |||
| 102 | ### let's do this each time a cleanup might happen | ||
| 103 | ### might be a little much, but shouldn't be anywhere | ||
| 104 | ### near as bad as the pre-cache days | ||
| 105 | foreach my $this_dir (keys %EXTANT_DIR) { | ||
| 106 | delete $EXTANT_DIR{$this_dir} unless(-d $EXTANT_DIR{$this_dir}); | ||
| 107 | } | ||
| 108 | |||
| 109 | ### might want to do cleanup only a portion of the time | ||
| 110 | return undef if( rand(100) >= $self->{cleanup_frequency} ); | ||
| 111 | |||
| 112 | ### Do quick lock file checking... (only one process should handle a cleanup) | ||
| 113 | my $file = "$_dir$self->{cleanup_suffix}"; | ||
| 114 | my @stat = stat $file; | ||
| 115 | |||
| 116 | if( @stat && time - $stat[9] < $self->{cleanup_length} ){ | ||
| 117 | return undef; | ||
| 118 | } | ||
| 119 | |||
| 120 | ## all checks passed... (we're still here) | ||
| 121 | ## make the lockfile, do the cleanup, and whatnot | ||
| 122 | |||
| 123 | ### realize this won't work over nfs, but better than nothing | ||
| 124 | open(FILE,">$file"); | ||
| 125 | flock(FILE, 6) or return undef; | ||
| 126 | |||
| 127 | if($self->{cleanup_fork}) { | ||
| 128 | return if strong_fork(); | ||
| 129 | } | ||
| 130 | $self->cleanup( $_dir ); | ||
| 131 | close(FILE); | ||
| 132 | unlink $file; | ||
| 133 | child_exit() if($self->{cleanup_fork}); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub cleanup { | ||
| 137 | my $self = shift; | ||
| 138 | my $root = shift; | ||
| 139 | return if($root =~ /$self->{cleanup_suffix}$/); | ||
| 140 | return unlink $root if | ||
| 141 | -l $root || !-d _; | ||
| 142 | if (my $dir = IO::Dir->new($root)) { | ||
| 143 | while (defined(my $node = $dir->read)) { | ||
| 144 | next if $node =~ /^\.\.?$/; | ||
| 145 | $node = $1 if $node =~ /(.+)/; | ||
| 146 | $self->cleanup("$root/$node"); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | return rmdir $root; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub handle_ttl { | ||
| 153 | my $self = shift; | ||
| 154 | |||
| 155 | if($self->{ttl} =~ /^\d+$/) { | ||
| 156 | # do nothing | ||
| 157 | } elsif($self->{ttl} =~ s/^(\d+)\s*(\D+)$/$1/) { | ||
| 158 | $self->{ttl} = $1 if defined $1; | ||
| 159 | my $units = (defined $2) ? $2 : ''; | ||
| 160 | if(($units =~ /^s/i) || (!$units)) { | ||
| 161 | $self->{ttl} = $self->{ttl}; | ||
| 162 | } elsif ($units =~ /^m/i) { | ||
| 163 | $self->{ttl} *= 60; | ||
| 164 | } elsif ($units =~ /^h/i) { | ||
| 165 | $self->{ttl} *= 3600; | ||
| 166 | } elsif ($units =~ /^d/i) { | ||
| 167 | $self->{ttl} *= 86400; | ||
| 168 | } elsif ($units =~ /^w/i) { | ||
| 169 | $self->{ttl} *= 604800; | ||
| 170 | } else { | ||
| 171 | die "invalid ttl '$self->{ttl}', bad units '$units'"; | ||
| 172 | } | ||
| 173 | } else { | ||
| 174 | die "invalid ttl '$self->{ttl}', not just number and couldn't find units"; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | sub sub_mkdir { | ||
| 179 | my $self = shift; | ||
| 180 | my $_dir = shift; | ||
| 181 | mkdir $_dir, 0755; | ||
| 182 | die "couldn't mkpath '$_dir': $!" unless($self->dash_d($_dir)); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub cache_dir { | ||
| 186 | my $self = $_[0]; | ||
| 187 | unless(UNIVERSAL::isa($self, __PACKAGE__)) { | ||
| 188 | $self = File::CacheDir->new(@_); | ||
| 189 | } | ||
| 190 | |||
| 191 | delete $self->{carried_forward}; | ||
| 192 | |||
| 193 | $self->handle_ttl; | ||
| 194 | |||
| 195 | $self->{base_dir} =~ s@/$@@; | ||
| 196 | my $ttl_dir = "$self->{base_dir}/$self->{ttl}/"; | ||
| 197 | |||
| 198 | unless($self->dash_d($ttl_dir)) { | ||
| 199 | $self->ttl_mkpath($ttl_dir); | ||
| 200 | } | ||
| 201 | |||
| 202 | $self->{int_time} = (int(time/$self->{ttl})); | ||
| 203 | $self->{full_dir} = "$ttl_dir$self->{int_time}/"; | ||
| 204 | $self->{last_int_time} = $self->{int_time} - 1; | ||
| 205 | $self->{last_dir} = "$ttl_dir$self->{last_int_time}/"; | ||
| 206 | |||
| 207 | if($self->{carry_forward}) { | ||
| 208 | $self->{last_int_time} = $self->{int_time} - 1; | ||
| 209 | $self->{last_int_dir} = "$ttl_dir$self->{last_int_time}/"; | ||
| 210 | $self->{carry_forward_filename} = "$self->{last_int_dir}$self->{filename}"; | ||
| 211 | if(-e $self->{carry_forward_filename}) { | ||
| 212 | unless($self->dash_d($self->{full_dir})) { | ||
| 213 | $self->sub_mkdir($self->{full_dir}); | ||
| 214 | die "couldn't mkpath '$self->{full_dir}': $!" unless($self->dash_d($self->{full_dir})); | ||
| 215 | } | ||
| 216 | |||
| 217 | $self->{full_path} = "$self->{full_dir}$self->{filename}"; | ||
| 218 | |||
| 219 | rename $self->{carry_forward_filename}, $self->{full_path}; | ||
| 220 | die "couldn't rename $self->{carry_forward_filename}, $self->{full_path}: $!" unless(-e $self->{full_path}); | ||
| 221 | |||
| 222 | $self->{carried_forward} = 1; | ||
| 223 | |||
| 224 | if($self->{set_cookie}) { | ||
| 225 | ($self->{cookie_value}) = $self->{full_path} =~ /^$self->{base_dir}(.+)/; | ||
| 226 | $self->set_cookie; | ||
| 227 | } | ||
| 228 | return $self->{full_path}; | ||
| 229 | |||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | if($self->dash_d($self->{full_dir})) { | ||
| 234 | $self->{full_path} = "$self->{full_dir}$self->{filename}"; | ||
| 235 | if($self->{set_cookie}) { | ||
| 236 | ($self->{cookie_value}) = $self->{full_path} =~ /^$self->{base_dir}(.+)/; | ||
| 237 | $self->set_cookie; | ||
| 238 | } | ||
| 239 | return $self->{full_path}; | ||
| 240 | } else { | ||
| 241 | if( rand(100) < $self->{cleanup_frequency}) { | ||
| 242 | opendir(DIR, $ttl_dir); | ||
| 243 | while (my $sub_dir = readdir(DIR)) { | ||
| 244 | next if($sub_dir =~ /^\.\.?$/); | ||
| 245 | next if($sub_dir =~ /$self->{cleanup_suffix}/); | ||
| 246 | $sub_dir = $1 if $sub_dir =~ /(.+)/; | ||
| 247 | if($self->expired_check($sub_dir)) { | ||
| 248 | $self->perhaps_cleanup("$ttl_dir$sub_dir"); | ||
| 249 | } | ||
| 250 | } | ||
| 251 | closedir(DIR); | ||
| 252 | } | ||
| 253 | $self->sub_mkdir($self->{full_dir}); | ||
| 254 | die "couldn't mkpath '$self->{full_dir}': $!" unless($self->dash_d($self->{full_dir})); | ||
| 255 | $self->{full_path} = "$self->{full_dir}$self->{filename}"; | ||
| 256 | if($self->{set_cookie}) { | ||
| 257 | ($self->{cookie_value}) = $self->{full_path} =~ /^$self->{base_dir}(.+)/; | ||
| 258 | $self->set_cookie; | ||
| 259 | } | ||
| 260 | return $self->{full_path}; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | sub set_cookie { | ||
| 265 | my $self = shift; | ||
| 266 | return unless($self->{set_cookie}); | ||
| 267 | my $old_cookie = CGI::cookie( -name => $self->{cookie_name} ); | ||
| 268 | if(!$self->{cookie_brick_over} && defined $old_cookie) { | ||
| 269 | $self->{cookie_value} = $old_cookie; | ||
| 270 | return $old_cookie; | ||
| 271 | } | ||
| 272 | $self->{cookie_value} =~ m@$self->{base_dir}(.+)@; | ||
| 273 | my $new_cookie = CGI::cookie | ||
| 274 | (-name => $self->{cookie_name}, | ||
| 275 | -value => $1, | ||
| 276 | -path => $self->{cookie_path}, | ||
| 277 | ); | ||
| 278 | if ($self->{content_typed}) { | ||
| 279 | print qq{<meta http-equiv="Set-Cookie" content="$new_cookie">\n}; | ||
| 280 | } else { | ||
| 281 | print "Set-Cookie: $new_cookie\n"; | ||
| 282 | } | ||
| 283 | return; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub strong_fork { | ||
| 287 | # Grab anonymous subroutine CODEREF or pointer to subroutine | ||
| 288 | # if a CODEREF is not passed - the routine will behave like fork. | ||
| 289 | my $routine=shift; | ||
| 290 | # STDOUT buffer must be clean before fork() because | ||
| 291 | # IO buffers are also replicated to the child process. | ||
| 292 | # We can't have the data already sent to STDOUT to be sent | ||
| 293 | # again by the child process! | ||
| 294 | $| = 1; | ||
| 295 | # Print nothing to STDOUT to force buffer to flush before fork() is called. | ||
| 296 | # This causes even a 'tie'd STDOUT (FastCGI or mod_perl) to actually flush. | ||
| 297 | print STDOUT ""; | ||
| 298 | # Create a pipe for the grandchild pid slide through | ||
| 299 | pipe (*RPID, *WPID); | ||
| 300 | # Fork is necessary to hide from apache's touch of death. | ||
| 301 | my $child = fork; | ||
| 302 | # Don't abort CGI if fork fails. | ||
| 303 | if (!defined $child) { | ||
| 304 | return undef; | ||
| 305 | } | ||
| 306 | # Parent should continue ASAP | ||
| 307 | if ($child) { | ||
| 308 | # I'm not going to put anything down this pipe. | ||
| 309 | close WPID; | ||
| 310 | my $grandchild_pid = ''; | ||
| 311 | # Waiting for Child to send the Grandchild pid | ||
| 312 | sysread(*RPID, $grandchild_pid, 10); | ||
| 313 | # It should be a number if the child fork()ed successfully. | ||
| 314 | $grandchild_pid += 0; | ||
| 315 | # Done with the pipe | ||
| 316 | close RPID; | ||
| 317 | if (!defined $grandchild_pid) { | ||
| 318 | return undef; | ||
| 319 | } | ||
| 320 | |||
| 321 | # Intermediate Child should hopefully terminate very soon | ||
| 322 | # (if it hasn't already). Clean off the zombie that it | ||
| 323 | # just became or wait until it dies. | ||
| 324 | waitpid($child, 0); | ||
| 325 | |||
| 326 | return $grandchild_pid; | ||
| 327 | } | ||
| 328 | # This is the Child process. | ||
| 329 | |||
| 330 | # I'm not going to look at anything in this pipe. | ||
| 331 | close RPID; | ||
| 332 | |||
| 333 | # Double fork makes cleaning zombies very easy. | ||
| 334 | # A defunct process always vanishes from existence | ||
| 335 | # when its parent is terminated. It is better than | ||
| 336 | # SIGCHLD handlers because, unfortunately, under | ||
| 337 | # mod_perl, the handler may get blown away by future | ||
| 338 | # requests that also use it. | ||
| 339 | my $grandchild_pid = fork; | ||
| 340 | # If second fork fails, pretend like I am the grandchild and | ||
| 341 | # make the parent process block until the subroutine completes. | ||
| 342 | if (!defined $grandchild_pid) { # Fork failed | ||
| 343 | print STDERR "Second process [$$] failed to fork for [$0]! [$!]\n"; | ||
| 344 | close WPID; | ||
| 345 | _exit(0); | ||
| 346 | } elsif ($grandchild_pid) { # Intermediate Child process | ||
| 347 | # Stuff the magic grandchild id down the pipe | ||
| 348 | # so grandpa knows who it is. Intermediate process | ||
| 349 | # does the stuffing so the grandchild process can | ||
| 350 | # get to work as soon as possible. | ||
| 351 | print WPID $grandchild_pid; | ||
| 352 | close WPID; | ||
| 353 | # This releases the waitpid() block above without | ||
| 354 | # calling any DESTROY nor END blocks while the detach | ||
| 355 | # variables are still within scope. It also slams all | ||
| 356 | # FD_CLOEXEC handles shut abruptly. | ||
| 357 | _exit(0); | ||
| 358 | } | ||
| 359 | # This is the Grandchild process running. | ||
| 360 | close WPID; | ||
| 361 | |||
| 362 | # Setsid is necessary to escape Apache touch of death | ||
| 363 | # when it is shutdown. We do not want a TERM signal to | ||
| 364 | # be sent to this strong_fork'ed child process just because | ||
| 365 | # the root web server gets shutdown. | ||
| 366 | setsid(); | ||
| 367 | |||
| 368 | # Untie is necessary to buttwag around those perl wrappers | ||
| 369 | # that do not implement a CLOSE method correctly (mod_perl | ||
| 370 | # and/or FastCGI) and in case STDOUT is tied and not a real | ||
| 371 | # handle. | ||
| 372 | untie *STDOUT if tied *STDOUT; | ||
| 373 | |||
| 374 | # Shutdown STDOUT handle to trick apache into releasing | ||
| 375 | # connection to client as soon as parent CGI finishes. | ||
| 376 | # (The STDERR should still be redirected to the error_log.) | ||
| 377 | close STDOUT; | ||
| 378 | |||
| 379 | # look to see if we want to keep this process going | ||
| 380 | # this makes us function more like the child process of a fork | ||
| 381 | # this allows for safe fork | ||
| 382 | if (! $routine || ! ref $routine) { | ||
| 383 | open STDOUT, '>/dev/null' || die "Can't write /dev/null [$!]"; | ||
| 384 | open STDIN, '</dev/null' || die "Can't read /dev/null [$!]"; | ||
| 385 | return 0; # this is what fork returns on a child | ||
| 386 | } | ||
| 387 | |||
| 388 | # Finally play the CODEREF that was passed. | ||
| 389 | $@ = ""; { | ||
| 390 | # Eval to catch the "die" calls from causing Apache::exit | ||
| 391 | # from being triggered leaving nasty dettached httpd | ||
| 392 | # processes under mod_perl | ||
| 393 | local $SIG{__DIE__} = 'DEFAULT'; | ||
| 394 | eval { | ||
| 395 | &$routine; | ||
| 396 | }; | ||
| 397 | # If an error occurred, log it and continue | ||
| 398 | if ($@) { | ||
| 399 | warn "strong_fork: $@"; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | # Program must terminate to avoid duplicate execution | ||
| 403 | # of code following this function in the caller program, | ||
| 404 | # but the DESTROY methods and END blocks should be run. | ||
| 405 | # Apache::exit should not be called since this is the | ||
| 406 | # strong_fork'd child process. | ||
| 407 | &child_exit(); | ||
| 408 | } | ||
| 409 | |||
| 410 | sub child_exit { | ||
| 411 | _exit(0); | ||
| 412 | } | ||
| 413 | |||
| 414 | 1; | ||
| 415 | |||
| 416 | __END__ | ||