#!/usr/bin/perl -w # # MT-Dispatch 1.1 # # Copyright (c) 2006-2007 Reed A. Cartwright, PhD # # Derived from script written by Brad Choate # # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # # INSTRUCTIONS: # # MT-Dispatch is a perl script that implements a fastcgi server for Movable # Type. Invoking it via spawn-fcgi from lighttpd is the easiest way to start # the server. See the associated FreeBSD rc.d script for details. # # MT-Dispatch requires four perl modules: FCGI, FCGI::ProcManager, CGI::Fast, # and Sub::Override. # # Environmental Variables: # PERL_FCGI_CHILDREN: specifies the number of worker threads (default 2) # PHP_FCGI_CHILDREN: same as above (used because of spawn-fcgi) # PERL_FCGI_MAX_REQUESTS: A worker will exit after serving this many requests, # cleanling up memory leaks (default 100). # PHP_FCGI_MAX_REQUESTS: same as above # PERL_FCGI_LOG: log location (default /var/log/mt-dispatch.log) # PHP_FCGI_LOG: same as above # # MovableType Environment: # MT_HOME: Location of MT install # (e.g. /usr/local/www/cgi-bin/mt) # MT_CONFIG: Location of MT config # (e.g. /usr/local/www/cgi-bin/mt/mt-config.cgi) # PERL5LIB: MT library directories and any other perl libraries # (e.g. /usr/local/www/cgi-bin/mt/lib:/usr/local/www/cgi-bin/mt/extlib) # BEGIN { #needed to work around a bug in FCGI::ProcManager *CORE::GLOBAL::getppid = sub { CORE::getppid }; } use strict; use FCGI::ProcManager; use FCGI; use Sub::Override; use CGI::Fast; # FCGI will replace the environment, so we will save our current one. my %env = %ENV; $CGI::Fast::Ext_Request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, 0, FCGI::FAIL_ACCEPT_ON_INTR()); my $handlers = { 'mt.fcgi' => { class => 'MT::App::CMS', name => 'AdminScript' }, 'mt-comments.fcgi' => { class => 'MT::App::Comments', name => 'CommentScript' }, 'mt-tb.fcgi' => { class => 'MT::App::Trackback', name => 'TrackbackScript' }, 'mt-search.fcgi' => { class => 'MT::App::Search', name => 'SearchScript' }, 'mt-atom.fcgi' => { class => 'MT::AtomServer', name => 'AtomScript' }, }; my $max_requests = $ENV{PERL_FCGI_MAX_REQUESTS} || $ENV{PHP_FCGI_MAX_REQUESTS} || 100; my $children = $ENV{PERL_FCGI_CHILDREN} || $ENV{PHP_FCGI_CHILDREN} || 2; my $logfile = $ENV{PERL_FCGI_LOG} || $ENV{PHP_FCGI_LOG} || '/var/log/mt-dispatch.log'; my $proc_manager = FCGI::ProcManager->new({ n_processes => $children, die_timeout => 10 }); open STDERR, ">>$logfile" or die $!; { #needed to work around a bug in FCGI::ProcManager my $or = Sub::Override->new('CORE::GLOBAL::getppid', sub { 0; } ); $proc_manager->pm_manage(); } # The loading of MT libraries is done at run time to reduce the memory footprint # of the manager process. eval { require MT::App::CMS; require MT::App::Comments; require MT::App::Trackback; require MT::App::Search; require MT::AtomServer; my $override = Sub::Override->new('MT::Util::start_background_task', sub {&start_background_task_fcgi;}); my $num_requests = 0; while (my $q = new CGI::Fast) { $proc_manager->pm_pre_dispatch(); for (keys %env) { $ENV{$_} = $env{$_} unless exists $ENV{$_}; } $num_requests += 1; my $cgi = $q->script_name; $cgi =~ s!.*/!!; my $pkg = $handlers->{$cgi}{class}; die "Invalid handler for $cgi" unless $pkg; my $app = $pkg->new(CGIObject => $q) or die $pkg->errstr; local $SIG{__WARN__} = sub { $app->trace($_[0]) }; $app->init_request(CGIObject => $q) unless $app->{init_request}; fixup_script_names($app); $app->run; my $mode = $app->mode || ''; if ("$pkg->$mode" eq 'MT::App::CMS->plugin_control') { exit(0); # allows server to recycle after changing plugin switches } exit(0) if($num_requests >= $max_requests); $proc_manager->pm_post_dispatch(); } }; if ($@) { print "Content-Type: text/html\n\n"; print "Got an error: $@"; } sub fixup_script_names { my ($app) = @_; $app->config($handlers->{$_}{name}, $_) foreach keys %$handlers; } # MT's background tasks are incompatible with FCGI. This is a modified version # of the subfunction from the MT codebase. sub start_background_task_fcgi { my ($func) = @_; if (!MT::Util::launch_background_tasks()) { $func->(); } else { $| = 1; # Flush open filehandles my $pid = fork(); if (!$pid) { # child $CGI::Fast::Ext_Request->Detach(); MT::Object->driver->init(); MT::Object->driver->configure(); $func->(); CORE::exit(0) if defined($pid) && !$pid; } else { MT::Object->driver->init(); MT::Object->driver->configure(); return 1; } } }