Fixed FastCGI win32 support
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / FastCGI.pm
index 2eb34cd..af96ca3 100644 (file)
@@ -17,9 +17,7 @@ This is the FastCGI engine.
 
 This class overloads some methods from C<Catalyst::Engine::CGI>.
 
-=over 4
-
-=item $self->run($c, $listen, { option => value, ... })
+=head2 $self->run($c, $listen, { option => value, ... })
  
 Starts the FastCGI server.  If C<$listen> is set, then it specifies a
 location to listen for FastCGI requests;
@@ -39,6 +37,8 @@ Options may also be specified;
   nproc           Specify a number of processes for
                   FCGI::ProcManager
   pidfile         Specify a filename for the pid file
+  manager         Specify a FCGI::ProcManager sub-class
+  detach          Detach from console
 
 =cut
 
@@ -57,7 +57,7 @@ sub run {
             umask($old_umask);
         }
     }
-    else {
+    elsif ( $^O ne 'MSWin32' ) {
         -S STDIN
           or die "STDIN is not a socket; specify a listen location";
     }
@@ -74,17 +74,29 @@ sub run {
     my $proc_manager;
 
     if ($listen) {
-        require FCGI::ProcManager;
-        $options->{nproc} ||= 1;
+        $options->{manager} ||= "FCGI::ProcManager";
+        $options->{nproc}   ||= 1;
 
-        $proc_manager =
-          FCGI::ProcManager->new( { n_processes => $options->{nproc} } );
+        $self->daemon_fork() if $options->{detach};
 
-        if ( $options->{pidfile} ) {
-            $proc_manager->pm_write_pid_file( $options->{pidfile} );
-        }
+        if ( $options->{manager} ) {
+            eval "use $options->{manager}; 1" or die $@;
+
+            $proc_manager = $options->{manager}->new(
+                {
+                    n_processes => $options->{nproc},
+                    pid_fname   => $options->{pidfile},
+                }
+            );
 
-        $proc_manager->pm_manage();
+            # detach *before* the ProcManager inits
+            $self->daemon_detach() if $options->{detach};
+
+            $proc_manager->pm_manage();
+        }
+        elsif ( $options->{detach} ) {
+            $self->daemon_detach();
+        }
     }
 
     while ( $request->Accept >= 0 ) {
@@ -94,7 +106,7 @@ sub run {
     }
 }
 
-=item $self->write($c, $buffer)
+=head2 $self->write($c, $buffer)
 
 =cut
 
@@ -111,11 +123,42 @@ sub write {
     *STDOUT->syswrite($buffer);
 }
 
+=head2 $self->daemon_fork()
+
+Performs the first part of daemon initialisation.  Specifically,
+forking.  STDERR, etc are still connected to a terminal.
+
+=cut
+
+sub daemon_fork {
+    require POSIX;
+    fork && exit;
+}
+
+=head2 $self->daemon_detach( )
+
+Performs the second part of daemon initialisation.  Specifically,
+disassociates from the terminal.
+
+However, this does B<not> change the current working directory to "/",
+as normal daemons do.  It also does not close all open file
+descriptors (except STDIN, STDOUT and STDERR, which are re-opened from
+F</dev/null>).
+
+=cut
+
+sub daemon_detach {
+    my $self = shift;
+    print "FastCGI daemon started (pid $$)\n";
+    open STDIN,  "+</dev/null" or die $!;
+    open STDOUT, ">&STDIN"     or die $!;
+    open STDERR, ">&STDIN"     or die $!;
+    POSIX::setsid();
+}
+
 1;
 __END__
 
-=back
-
 =head1 WEB SERVER CONFIGURATIONS
 
 =head2 Apache 1.x, 2.x