daily pastebin goal
30%
SHARE
TWEET

0x000BLAh

a guest Oct 17th, 2009 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. use FCGI;
  3. use Socket;
  4. use FCGI::ProcManager;
  5. sub shutdown { FCGI::CloseSocket($socket); exit; }
  6. sub restart  { FCGI::CloseSocket($socket); &main; }
  7. use sigtrap 'handler', \&shutdown, 'normal-signals';
  8. use sigtrap 'handler', \&restart,  'HUP';
  9. require 'syscall.ph';
  10. use POSIX qw(setsid);
  11.  
  12. END()   { }
  13. BEGIN() { }
  14. {
  15.  no warnings;
  16. *CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
  17. };
  18. if ($@) {
  19.  exit unless $@ =~ /^fakeexit/;
  20. }
  21. &main;
  22.  
  23. sub daemonize() {
  24.  chdir '/' or die "Can't chdir to /: $!";
  25.  defined( my $pid = fork ) or die "Can't fork: $!";
  26.  exit if $pid;
  27.  setsid() or die "Can't start a new session: $!";
  28.  umask 0;
  29. }
  30.  
  31. sub main {
  32.  
  33.  $proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
  34.  $socket = FCGI::OpenSocket( "/nginx/fcgi/cgi.sock", 10 )
  35.  ; #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
  36.  $request =
  37.  FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
  38.  &FCGI::FAIL_ACCEPT_ON_INTR );
  39.  $proc_manager->pm_manage();
  40.  if ($request) { request_loop() }
  41.  FCGI::CloseSocket($socket);
  42. }
  43.  
  44. sub request_loop {
  45.  while ( $request->Accept() >= 0 ) {
  46.  $proc_manager->pm_pre_dispatch();
  47.  
  48.  #processing any STDIN input from WebServer (for CGI-POST actions)
  49.  $stdin_passthrough = '';
  50.  { no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };
  51.  if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) )
  52.  {
  53.  my $bytes_read = 0;
  54.  while ( $bytes_read < $req_len ) {
  55.  my $data = '';
  56.  my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );
  57.  last if ( $bytes == 0 || !defined($bytes) );
  58.  $stdin_passthrough .= $data;
  59.  $bytes_read += $bytes;
  60.  }
  61.  }
  62.  
  63.  #running the cgi app
  64.  if (
  65.  ( -x $req_params{SCRIPT_FILENAME} ) &&    #can I execute this?
  66.  ( -s $req_params{SCRIPT_FILENAME} ) &&    #Is this file empty?
  67.  ( -r $req_params{SCRIPT_FILENAME} )       #can I read this file?
  68.  )
  69.  {
  70.  pipe( CHILD_RD,   PARENT_WR );
  71.  pipe( PARENT_ERR, CHILD_ERR );
  72.  my $pid = open( CHILD_O, "-|" );
  73.  unless ( defined($pid) ) {
  74.  print("Content-type: text/plain\r\n\r\n");
  75. "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
  76.  next;
  77.  }
  78.  $oldfh = select(PARENT_ERR);
  79.  $|     = 1;
  80.  select(CHILD_O);
  81.  $| = 1;
  82.  select($oldfh);
  83.  if ( $pid > 0 ) {
  84.  close(CHILD_RD);
  85.  close(CHILD_ERR);
  86.  print PARENT_WR $stdin_passthrough;
  87.  close(PARENT_WR);
  88.  $rin = $rout = $ein = $eout = '';
  89.  vec( $rin, fileno(CHILD_O),    1 ) = 1;
  90.  vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
  91.  $ein    = $rin;
  92.  $nfound = 0;
  93.  
  94.  while ( $nfound =
  95.  select( $rout = $rin, undef, $ein = $eout, 10 ) )
  96.  {
  97.  die "$!" unless $nfound != -1;
  98.  $r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
  99.  $r2 = vec( $rout, fileno(CHILD_O),    1 ) == 1;
  100.  $e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
  101.  $e2 = vec( $eout, fileno(CHILD_O),    1 ) == 1;
  102.  
  103.  if ($r1) {
  104.  while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
  105.  print STDERR $errbytes;
  106.  }
  107.  if ($!) {
  108.  $err = $!;
  109.  die $!;
  110.  vec( $rin, fileno(PARENT_ERR), 1 ) = 0
  111.  unless ( $err == EINTR or $err == EAGAIN );
  112.  }
  113.  }
  114.  if ($r2) {
  115.  while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
  116.  print $s;
  117.  }
  118.  if ( !defined($bytes) ) {
  119.  $err = $!;
  120.  die $!;
  121.  vec( $rin, fileno(CHILD_O), 1 ) = 0
  122.  unless ( $err == EINTR or $err == EAGAIN );
  123.  }
  124.  }
  125.  last if ( $e1 || $e2 );
  126.  }
  127.  close CHILD_RD;
  128.  close PARENT_ERR;
  129.  waitpid( $pid, 0 );
  130.  } else {
  131.  foreach $key ( keys %req_params ) {
  132.  $ENV{$key} = $req_params{$key};
  133.  }
  134.  
  135.  # cd to the script's local directory
  136.  if ( $req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/] +$/ ) {
  137.  chdir $1;
  138.  }
  139.  close(PARENT_WR);
  140.  
  141.  #close(PARENT_ERR);
  142.  close(STDIN);
  143.  close(STDERR);
  144.  
  145.  #fcntl(CHILD_RD, F_DUPFD, 0);
  146.  syscall( &SYS_dup2, fileno(CHILD_RD),  0 );
  147.  syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );
  148.  
  149.  #open(STDIN, "<&CHILD_RD");
  150.  exec( $req_params{SCRIPT_FILENAME} );
  151.  die("exec failed");
  152.  }
  153.  } else {
  154.  print("Content-type: text/plain\r\n\r\n");
  155. "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
  156.  }
  157.  }
  158. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top