-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastCGI.pm
213 lines (174 loc) · 5.83 KB
/
FastCGI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# FastCGI Runtime Engine of Foswiki - The Free and Open Source Wiki,
# http://foswiki.org/
#
# Copyright (C) 2008 Gilmar Santos Jr, jgasjr@gmail.com and Foswiki
# contributors. Foswiki contributors are listed in the AUTHORS file in the root
# of Foswiki distribution.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. For
# more details read LICENSE in the root of this distribution.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#------------------------------------------------------------------------------
# Parts of this package are based on Net::Server::Daemonize, which is
# Copyright (C) 2001-2007
#
# Jeremy Howard
# j+daemonize@howard.fm
#
# Paul Seamons
# paul@seamons.com
# http://seamons.com/
#
# For more details:
# http://search.cpan.org/perldoc?Net::Server::Daemonize
#------------------------------------------------------------------------------
# As per the GPL, removal of this notice is prohibited.
=begin TML
---+!! package Foswiki::Engine::FastCGI
Class that implements FastCGI execution mode.
Refer to Foswiki::Engine documentation for explanation about methos below.
=cut
package Foswiki::Engine::FastCGI;
use base 'Foswiki::Engine::CGI';
use strict;
use FCGI;
use POSIX qw(:signal_h);
require File::Spec;
use vars qw( $VERSION $RELEASE );
$VERSION = '$Rev$';
$RELEASE = '0.9.0';
our $hupRecieved = 0;
sub run {
my ( $this, $listen, $args ) = @_;
my $sock = 0;
if ($listen) {
$sock = FCGI::OpenSocket( $listen, 100 )
or die "Failed to create FastCGI socket: $!";
}
else {
sigaction( SIGHUP, POSIX::SigAction->new( sub { $hupRecieved++ } ) )
or warn "Could not install SIGHUP handler: $!";
}
$args ||= {};
my $r = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $sock,
&FCGI::FAIL_ACCEPT_ON_INTR );
my $manager;
if ($listen) {
$args->{manager} ||= 'Foswiki::Engine::FastCGI::ProcManager';
$args->{nproc} ||= 1;
$this->fork() if $args->{detach};
if ( $args->{manager} ) {
eval "use " . $args->{manager} . "; 1" or die $@;
$manager = $args->{manager}->new(
{
n_processes => $args->{nproc},
pid_fname => $args->{pidfile},
}
);
$this->daemonize() if $args->{detach};
$manager->pm_manage();
}
elsif ( $args->{detach} ) {
$this->daemonize();
}
}
my $localSiteCfg = File::Spec->catpath(
( File::Spec->splitpath( $INC{'Foswiki.pm'} ) )[ 0, 1 ],
'LocalSite.cfg' );
my $lastMTime = ( stat $localSiteCfg )[9];
while ( $r->Accept() >= 0 ) {
$manager && $manager->pm_pre_dispatch();
CGI::initialize_globals();
my $req = $this->prepare;
if ( UNIVERSAL::isa( $req, 'Foswiki::Request' ) ) {
my $res = Foswiki::UI::handleRequest($req);
$this->finalize( $res, $req );
}
my $mtime = ( stat $localSiteCfg )[9];
if ( $mtime > $lastMTime || $hupRecieved ) {
$r->LastCall();
if ($manager) {
kill SIGHUP, $manager->pm_parameter('MANAGER_PID');
}
else {
$hupRecieved++;
}
}
$manager && $manager->pm_post_dispatch();
}
reExec() if $hupRecieved;
FCGI::CloseSocket($sock) if $sock;
}
sub preparePath {
my $this = shift;
# In some hosted environments, users don't have access to "Alias", so they
# should use in .htaccess something like:
#
# SetHandler foswiki-fastcgi
# Action foswiki-fastcgi /foswiki/bin/foswiki.fcgi
# <Files "foswiki.fcgi">
# SetHandler fastcgi-script
# </Files>
#
# but then, the script is called by the webserver as
# /foswiki/bin/foswiki.fcgi/foswiki/bin/edit, for example. In other words:
# the seen PATH_INFO starts with ScriptUrlPath, so it must be removed. This
# way, SUPER::preparePath works fine.
$ENV{PATH_INFO} =~ s#^$Foswiki::cfg{ScriptUrlPath}/*#/#
if $ENV{PATH_INFO};
$this->SUPER::preparePath(@_);
}
sub write {
my ( $this, $buffer ) = @_;
syswrite STDOUT, $buffer;
}
sub reExec {
require Config;
$ENV{PERL5LIB} .= join $Config::Config{path_sep}, @INC;
$ENV{PATH} = $Foswiki::cfg{SafeEnvPath};
my $perl = $Config::Config{perlpath};
chdir $main::dir
or die
"Foswiki::Engine::FastCGI::reExec(): Could not restore directory: $!";
exec $perl, '-wT', $main::script, map { /^(.*)$/; $1 } @ARGV
or die "Foswiki::Engine::FastCGI::reExec(): Could not exec(): $!";
}
sub fork () {
### block signal for fork
my $sigset = POSIX::SigSet->new(SIGINT);
POSIX::sigprocmask( SIG_BLOCK, $sigset )
or die "Can't block SIGINT for fork: [$!]\n";
### fork off a child
my $pid = fork;
unless ( defined $pid ) {
die "Couldn't fork: [$!]\n";
}
### make SIGINT kill us as it did before
$SIG{INT} = 'DEFAULT';
### put back to normal
POSIX::sigprocmask( SIG_UNBLOCK, $sigset )
or die "Can't unblock SIGINT for fork: [$!]\n";
$pid && exit;
return $pid;
}
=begin TML
---++ StaticMethod detach()
Daemonize process. Currently not portable...
=cut
sub daemonize {
print "FastCGI daemon started (pid $$)\n";
umask(0);
chdir File::Spec->rootdir;
open STDIN, File::Spec->devnull or die $!;
open STDOUT, ">&STDIN" or die $!;
open STDERR, ">&STDIN" or die $!;
POSIX::setsid();
}
1;