| 1 | package | ||
| 2 | Apache::forks::Common; # hide from PAUSE | ||
| 3 | |||
| 4 | $VERSION = 0.03; | ||
| 5 | |||
| 6 | # Abstract class | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Carp (); | ||
| 11 | |||
| 12 | use constant BASE => ''; #to be set in concrete class definition | ||
| 13 | |||
| 14 | use constant MP2 => (exists $ENV{MOD_PERL_API_VERSION} && | ||
| 15 | $ENV{MOD_PERL_API_VERSION} == 2) ? 1 : 0; | ||
| 16 | my @Import; | ||
| 17 | our $DEBUG = 0; | ||
| 18 | my $BASE; | ||
| 19 | |||
| 20 | BEGIN { | ||
| 21 | if (MP2) { | ||
| 22 | require mod_perl2; | ||
| 23 | require Apache2::MPM; | ||
| 24 | require Apache2::Module; | ||
| 25 | require Apache2::ServerUtil; | ||
| 26 | } | ||
| 27 | else { | ||
| 28 | require mod_perl; | ||
| 29 | if (defined $mod_perl::VERSION && $mod_perl::VERSION > 1 && | ||
| 30 | $mod_perl::VERSION < 1.99) { | ||
| 31 | require Apache; | ||
| 32 | |||
| 33 | #not using Apache::fork support yet (may be non-portable and/or deprecated) | ||
| 34 | # require Apache::fork; | ||
| 35 | # Apache::fork::forkoption(1); | ||
| 36 | # no warnings 'redefine'; | ||
| 37 | # *threads::_fork = \&Apache::fork::fork; | ||
| 38 | } else { | ||
| 39 | die "Apache.pm is unavailable or unsupported version ($mod_perl::VERSION)"; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | ### functions ### | ||
| 45 | sub DEBUG { $DEBUG = shift() ? 1 : 0; } | ||
| 46 | |||
| 47 | sub debug { | ||
| 48 | print STDERR "$_[1]\n" if $DEBUG >= $_[0] || threads->debug >= $_[0]; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _load_forks { | ||
| 52 | my $self = shift; | ||
| 53 | $BASE = $self->BASE; | ||
| 54 | $forks::DEFER_INIT_BEGIN_REQUIRE = 1; | ||
| 55 | eval 'require '.$self->_forks(); | ||
| 56 | die "forks version 0.26 or later required--this is only version $forks::VERSION" | ||
| 57 | unless defined($forks::VERSION) && $forks::VERSION >= 0.26; | ||
| 58 | eval 'import '.$self->_forks(); #set environment and preload server process | ||
| 59 | eval 'require '.$self->_forks_shared(); | ||
| 60 | eval 'import '.$self->_forks_shared(); #set environment | ||
| 61 | |||
| 62 | { | ||
| 63 | no warnings 'redefine'; | ||
| 64 | my $old_server_pre_startup = \&threads::_server_pre_startup; | ||
| 65 | *threads::_server_pre_startup = sub { | ||
| 66 | unless ($self->DEBUG) { | ||
| 67 | ### close IO pipes to silence possible warnings to terminal ### | ||
| 68 | close(STDERR); | ||
| 69 | close(STDOUT); | ||
| 70 | close(STDIN); | ||
| 71 | } | ||
| 72 | $old_server_pre_startup->() | ||
| 73 | if ref($old_server_pre_startup) eq 'CODE'; | ||
| 74 | }; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub childinit { | ||
| 79 | threads->isthread; | ||
| 80 | my $timestamp = localtime(time); | ||
| 81 | debug(1, "[$timestamp] [notice] $$:".threads->tid | ||
| 82 | ." Apache::$BASE PerlChildInitHandler executed"); | ||
| 83 | |||
| 84 | 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | ### methods ### | ||
| 88 | sub _forks { return shift->BASE; } | ||
| 89 | |||
| 90 | sub _forks_shared { return shift->BASE.'::shared'; } | ||
| 91 | |||
| 92 | sub import { | ||
| 93 | my $self = shift; | ||
| 94 | $self->_forks->import(@_); | ||
| 95 | $self->_forks_shared->import(); #kludge: insures correct %INC for forks::shared | ||
| 96 | |||
| 97 | my $timestamp = localtime(time); | ||
| 98 | if (MP2) { | ||
| 99 | if (!@Import) { | ||
| 100 | Carp::carp("Apache MPM '".Apache2::MPM->show() | ||
| 101 | ."' is not supported: " | ||
| 102 | ."This package can't be used under threaded MPMs: " | ||
| 103 | ."Only 'Prefork' MPM is supported at this time\n") | ||
| 104 | and return if Apache2::MPM->is_threaded; | ||
| 105 | |||
| 106 | my $s = Apache2::ServerUtil->server; | ||
| 107 | $s->push_handlers(PerlChildInitHandler => \&childinit); | ||
| 108 | debug(1, "[$timestamp] [notice] $$:".threads->tid | ||
| 109 | ." Apache::".$self->_forks." PerlChildInitHandler enabled"); | ||
| 110 | } | ||
| 111 | } else { | ||
| 112 | Carp::carp("Apache.pm was not loaded\n") | ||
| 113 | and return unless $INC{'Apache.pm'}; | ||
| 114 | |||
| 115 | if (!@Import and Apache->can('push_handlers')) { | ||
| 116 | Apache->push_handlers(PerlChildInitHandler => \&childinit); | ||
| 117 | debug(1, "[$timestamp] [notice] $$:".threads->tid | ||
| 118 | ." Apache::".$self->_forks." PerlChildInitHandler enabled"); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | push @Import, [@_]; | ||
| 123 | } | ||
| 124 | |||
| 125 | package | ||
| 126 | Apache::forks::common::shared; # hide from PAUSE | ||
| 127 | |||
| 128 | sub import { | ||
| 129 | my $self = shift; | ||
| 130 | $self->_forks_shared->import(@_); | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; |