| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | package FCGI::Daemon; | ||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.20111121'; | |||||||||||||||||||||||||||
| 4 | 2 | ▶ | use English '-no_match_vars'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use FCGI 0.71; # on Debian available as libfcgi-perl | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use FCGI::ProcManager 0.18; # on Debian available as libfcgi-procmanager-perl | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | #use warnings; | ||||||||||||||||||||||||||||
| 10 | #use diagnostics -verbose; | ||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Getopt::Std; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use autouse 'Pod::Usage'=>qw(pod2usage); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | FCGI::Daemon - Perl-aware Fast CGI daemon for use with nginx web server. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 VERSION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Version 0.20111121 | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =begin comment | ||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | my %o; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | __PACKAGE__->run() unless caller(); # modulino i.e. executable rather than module | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head2 help() | ||||||||||||||||||||||||||||
| 30 | print help screen extracted from POD | ||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub help { pod2usage(-verbose=>$ARG[0],-noperldoc=>1) and exit; } | ||||||||||||||||||||||||||
|
Subroutine
help: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head2 run() | ||||||||||||||||||||||||||||
| 35 | Modulino-style main routine | ||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | sub run { | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | getopts('hde:f:q:p:s:g:u:m:c:l:w:',\%o) or help(0); | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=58 SCAR=80.4
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | help(2) if $o{'h'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $o{sockfile}=$o{'s'}||'/var/run/fcgi-daemon.sock'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$o{'s'} || '/var/run/fcgi-daemon.sock'
MC/DC0%0/1
$o{'s'} || '/var/run/fcgi-daemon.sock'
$o{'s'}
Truth table0%0/2
A
A= $o{'s'}
| |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $o{pidfile}=$o{'p'}||'/var/run/fcgi-daemon.pid' if $o{'d'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$o{'p'} || '/var/run/fcgi-daemon.pid'
MC/DC0%0/1
$o{'p'} || '/var/run/fcgi-daemon.pid'
$o{'p'}
Truth table0%0/2
A
A= $o{'p'}
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $o{prefork}=defined $o{'w'} ? $o{'w'} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $o{queue}=defined $o{'q'} ? $o{'q'} : 96; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $o{rlimit_vmem}=($o{'m'}||512)*1024*1024; | ||||||||||||||||||||||||||
|
Condition0%0/2
$o{'m'} || 512
MC/DC0%0/1
$o{'m'} || 512
$o{'m'}
Truth table0%0/2
A
A= $o{'m'}
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $o{rlimit_cpu}=$o{'c'}||32; | ||||||||||||||||||||||||||
|
Condition0%0/2
$o{'c'} || 32
MC/DC0%0/1
$o{'c'} || 32
$o{'c'}
Truth table0%0/2
A
A= $o{'c'}
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $o{max_evals}=defined $o{'e'} ? $o{'e'} : 10240; #max evals before exit - paranoid to free memory if leaks | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $o{file_pattern}=$o{'f'}||qr{\.pl}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$o{'f'} || qr/\.pl/
MC/DC0%0/2
$o{'f'} || qr/\.pl/
$o{'f'}
qr/\.pl/
Truth table0%0/3
A || B
A= $o{'f'}B= qr/\.pl/
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $o{leak_threshold}=$o{'l'}||1.3; | ||||||||||||||||||||||||||
|
Condition0%0/2
$o{'l'} || 1.3
MC/DC0%0/1
$o{'l'} || 1.3
$o{'l'}
Truth table0%0/2
A
A= $o{'l'}
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if($REAL_USER_ID==$EFFECTIVE_USER_ID and $EFFECTIVE_USER_ID==0){ # if run as root | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$FCGI::Daemon::REAL_USER_ID == $FCGI::Daemon::EFFECTIVE_USER_ID and $FCGI::Daemon::EFFECTIVE_USER_ID == 0
MC/DC0%0/2
$FCGI::Daemon::REAL_USER_ID == $FCGI::Daemon::EFFECTIVE_USER_ID and $FCGI::Daemon::EFFECTIVE_USER_ID == 0
$FCGI::Daemon::REAL_USER_ID == $FCGI::Daemon::EFFECTIVE_USER_ID
$FCGI::Daemon::EFFECTIVE_USER_ID == 0
Truth table0%0/3
A and B
A= $FCGI::Daemon::REAL_USER_ID == $FCGI::Daemon::EFFECTIVE_USER_IDB= $FCGI::Daemon::EFFECTIVE_USER_ID == 0
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $o{gid}=$o{g}||'www-data'; $o{gid_num}=scalar getgrnam($o{gid}); | ||||||||||||||||||||||||||
|
Condition0%0/2
$o{'g'} || 'www-data'
MC/DC0%0/1
$o{'g'} || 'www-data'
$o{'g'}
Truth table0%0/2
A
A= $o{'g'}
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $o{uid}=$o{u}||'www-data'; $o{uid_num}=scalar getpwnam($o{uid}); | ||||||||||||||||||||||||||
|
Condition0%0/2
$o{'u'} || 'www-data'
MC/DC0%0/1
$o{'u'} || 'www-data'
$o{'u'}
Truth table0%0/2
A
A= $o{'u'}
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head2 dieif() | ||||||||||||||||||||||||||||
| 57 | exit handler | ||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | sub dieif { | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if($ARG[0]){ | ||||||||||||||||||||||||||
|
Subroutine
dieif: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | my $err=$ARG[1]; | |||||||||||||||||||||||||||
| 62 | 0 | unlink @o{'pidfile','sockfile'}; | |||||||||||||||||||||||||||
| 63 | 0 | print "Error - $err:\n",$ARG[0],"\n"; | |||||||||||||||||||||||||||
| 64 | 0 | exit 1; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | $SIG{INT}=$SIG{TERM}=sub{ # actually FCGI::ProcManager override our TERM handler so .sock and .pid files will be removed only by sysv script... :( | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $o{fcgi_pm}->pm_remove_pid_file() if $o{fcgi_pm}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | unlink @o{'sockfile','pidfile'}; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | $o{fcgi_pm}->pm_die() if $o{fcgi_pm}; #pm_die() does not return | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | exit 0; | |||||||||||||||||||||||||||
| 73 | 0 | }; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # daemonize | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if($o{'d'}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | chdir '/'; # this is good practice for unmounting | |||||||||||||||||||||||||||
| 78 | 0 | $PROGRAM_NAME='FCGI::Daemon'; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | defined(my $pid=fork) or die "Can't fork: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | exit if $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | eval {use POSIX qw(setsid); POSIX::setsid();} or die q{Can't start a new session: }.$OS_ERROR; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.7
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | open *STDIN,'<','/dev/null'; | |||||||||||||||||||||||||||
| 83 | 0 | open *STDOUT,'>>','/dev/null'; | |||||||||||||||||||||||||||
| 84 | 0 | open *STDERR,'>>','/dev/null'; | |||||||||||||||||||||||||||
| 85 | 0 | umask 022; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my %req_env; | |||||||||||||||||||||||||||
| 89 | $o{fcgi_pm}=FCGI::ProcManager->new({n_processes=>$o{prefork}, | ||||||||||||||||||||||||||||
| 90 | die_timeout=>28, | ||||||||||||||||||||||||||||
| 91 | pid_fname=>$o{pidfile} | ||||||||||||||||||||||||||||
| 92 | 0 | }); | |||||||||||||||||||||||||||
| 93 | 0 | print "Opening socket $o{sockfile}\n"; | |||||||||||||||||||||||||||
| 94 | my $rqst=FCGI::Request(\*STDIN,\*STDOUT,\*STDERR,\%req_env, | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | FCGI::OpenSocket($o{sockfile},$o{prefork}*$o{queue}), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | FCGI::FAIL_ACCEPT_ON_INTR()) | ||||||||||||||||||||||||||||
| 97 | or die "Error: Unable to create FCGI::Request..."; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if(defined $o{gid_num} and defined $o{uid_num}){ # if run as root | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $o{"gid_num"} and defined $o{"uid_num"}
MC/DC0%0/2
defined $o{"gid_num"} and defined $o{"uid_num"}
defined $o{"gid_num"}
defined $o{"uid_num"}
Truth table0%0/3
A and B
A= defined $o{"gid_num"}B= defined $o{"uid_num"}
| |||||||||||||||||||||||||||||
| 100 | chown $o{uid_num},$o{gid_num},$o{sockfile} # chown SOCKfile | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | or dieif($OS_ERROR,'Unable to chown SOCKfile'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | $o{fcgi_pm}->pm_manage(); # from now on we are worker process | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # drop privileges if run as root | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if(defined $o{gid_num} and defined $o{uid_num}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $o{"gid_num"} and defined $o{"uid_num"}
MC/DC0%0/2
defined $o{"gid_num"} and defined $o{"uid_num"}
defined $o{"gid_num"}
defined $o{"uid_num"}
Truth table0%0/3
A and B
A= defined $o{"gid_num"}B= defined $o{"uid_num"}
| |||||||||||||||||||||||||||||
| 108 | 0 | $REAL_GROUP_ID=$EFFECTIVE_GROUP_ID=getgrnam($o{gid}); | |||||||||||||||||||||||||||
| 109 | 0 | dieif($OS_ERROR,'Unable to change group_id to '.$o{gid}); | |||||||||||||||||||||||||||
| 110 | 0 | $REAL_USER_ID=$EFFECTIVE_USER_ID=getpwnam($o{uid}); | |||||||||||||||||||||||||||
| 111 | 0 | dieif($OS_ERROR,'Unable to change user_id to '.$o{uid}); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | ## set rlimit(s) | ||||||||||||||||||||||||||||
| 115 | 0 | require 'syscall.ph'; | |||||||||||||||||||||||||||
| 116 | 0 | require 'sys/resource.ph'; | |||||||||||||||||||||||||||
| 117 | 0 | my $rlim=pack 'L!L!',$o{rlimit_vmem},$o{rlimit_vmem}; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | dieif($OS_ERROR,"Unable to set RLIMIT_AS: ") if -1==syscall(SYS_setrlimit(),RLIMIT_AS(),$rlim); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | $rlim=pack 'L!L!',$o{rlimit_cpu},$o{rlimit_cpu}; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | dieif($OS_ERROR,"Unable to set RLIMIT_CPU: ") if -1==syscall(SYS_setrlimit(),RLIMIT_CPU(),$rlim); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | REQ_LOOP: # main loop | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | while($rqst->Accept()>=0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | $req_env{'PATH_INFO'}=$req_env{'SCRIPT_FILENAME'}; | |||||||||||||||||||||||||||
| 126 | 0 | $req_env{'SCRIPT_FILENAME'}=get_file_from_path($req_env{SCRIPT_FILENAME}); | |||||||||||||||||||||||||||
| 127 | 0 | $req_env{'PATH_INFO'}=~s/$req_env{'SCRIPT_FILENAME'}//; | |||||||||||||||||||||||||||
| 128 | 0 | $req_env{'SCRIPT_NAME'}=$req_env{'SCRIPT_FILENAME'}; | |||||||||||||||||||||||||||
| 129 | 0 | $req_env{'SCRIPT_NAME'}=~s/$req_env{'DOCUMENT_ROOT'}//; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # check if script (exacutable, readable, non-zero size) | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | unless(-x -s -r $req_env{'SCRIPT_FILENAME'}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | print "Content-type: text/plain\r\n\r\n"; | |||||||||||||||||||||||||||
| 134 | 0 | $_="Error: No such CGI app - $req_env{SCRIPT_FILENAME} may not exist or is not executable by this process.\n"; | |||||||||||||||||||||||||||
| 135 | 0 | print $_; | |||||||||||||||||||||||||||
| 136 | 0 | print {*STDERR} $_; | |||||||||||||||||||||||||||
| 137 | 0 | next; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | local @ENV{keys %req_env}=values %req_env; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | chdir $1 if $req_env{'SCRIPT_FILENAME'}=~m{^(.*)\/}; # cd to the script's local directory | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Fast Perl-CGI processing | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if($o{max_evals}>0 and $req_env{'SCRIPT_FILENAME'}=~m{$o{file_pattern}\z}){ # detect if perl script | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"max_evals"} > 0 and $req_env{"SCRIPT_FILENAME"} =~ /$o{"file_pattern"}\z/
MC/DC0%0/2
$o{"max_evals"} > 0 and $req_env{"SCRIPT_FILENAME"} =~ /$o{"file_pattern"}\z/
$o{"max_evals"} > 0
$req_env{"SCRIPT_FILENAME"} =~ /$o{"file_pattern"}\z/
Truth table0%0/3
A and B
A= $o{"max_evals"} > 0B= $req_env{"SCRIPT_FILENAME"} =~ /$o{"file_pattern"}\z/
| |||||||||||||||||||||||||||||
| 145 | 0 | my %allvars; | |||||||||||||||||||||||||||
| 146 | 0 | @allvars{keys %main::}=(); | |||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | local *CORE::GLOBAL::exit=sub { die 'notr3a11yeXit' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 149 | 0 | local $0=$req_env{SCRIPT_FILENAME}; #fixes FindBin (in English $0 means $PROGRAM_NAME) | |||||||||||||||||||||||||||
| 150 | 2 | ▶ | no strict; # default for Perl5 | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 151 | 0 | do $0; # do $0; could be enough for strict scripts | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | if($EVAL_ERROR){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $EVAL_ERROR=~s{\n+\z}{}; | |||||||||||||||||||||||||||
| 154 | 0 | ▶ | print {*STDERR} "$0\n$EVAL_ERROR\n\b" unless $EVAL_ERROR =~ m{^notr3a11yeXit}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | #untested experimental callback to execute on script exit | ||||||||||||||||||||||||||||
| 159 | #$_{$req_env{SCRIPT_FILENAME}}->{SIGTERM}() if defined $_{$req_env{SCRIPT_FILENAME}}->{SIGTERM}; | ||||||||||||||||||||||||||||
| 160 | #Perl scripts can cache persistent data in $_{$0}->{mydata} | ||||||||||||||||||||||||||||
| 161 | #However if you store too much data it may trigger termination by rlimit | ||||||||||||||||||||||||||||
| 162 | #After DO/EVAL $_{$0}->{'SIGTERM'} is being called so termination handler | ||||||||||||||||||||||||||||
| 163 | #can be used to close DB connections etc. | ||||||||||||||||||||||||||||
| 164 | #$_{$0}->{'SIGTERM'}=sub { print "I closed my handles"; }; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | foreach(keys %main::){ # cleanup garbage after do() | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | next if exists $allvars{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | next if m{::$}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | next if m{^_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | delete $main::{$_}; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if(open my $STAT,'<',"/proc/$$/status"){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | my %stat; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | while(my ($k,$v)=split /\:\s+/,<$STAT>){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | chop $v; | |||||||||||||||||||||||||||
| 177 | 0 | $stat{$k}=$v; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 0 | close $STAT; | |||||||||||||||||||||||||||
| 180 | # check if child takes too much resident memory and terminate if necessary | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if($stat{VmSize}/$stat{VmRSS}<$o{leak_threshold}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | print {*STDERR} 'fcgi-daemon :: terminating child - memory leak? ' | |||||||||||||||||||||||||||
| 183 | 0 | ."VmSize:$stat{VmSize}; VmRSS:$stat{VmRSS}; Ratio:".$stat{VmSize}/$stat{VmRSS}; | |||||||||||||||||||||||||||
| 184 | 0 | exit; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 0 | ▶ | exit unless --$o{max_evals}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | next REQ_LOOP; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Normal CGI processing | ||||||||||||||||||||||||||||
| 192 | 0 | $o{fcgi_pm}->pm_pre_dispatch(); | |||||||||||||||||||||||||||
| 193 | 0 | local $OUTPUT_AUTOFLUSH=1; # select below is equivalent of: my $oldfh=select($CERR); $|=1; select($oldfh); | |||||||||||||||||||||||||||
| 194 | 0 | pipe my($PERR),my($CERR); select((select($CERR),$OUTPUT_AUTOFLUSH=1)[0]); #prepare child-to-parent pipe and swith off buffering | |||||||||||||||||||||||||||
| 195 | 0 | pipe my($CIN),my($PIN); select((select($PIN),$OUTPUT_AUTOFLUSH=1)[0]); #prepare parent-to-child pipe and swith off buffering | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | my $pid=open my($COUT),"-|"; ## fork and pipe to us | |||||||||||||||||||||||||||
| 198 | 0 | ▶ | unless(defined $pid){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | print "Content-type: text/plain\r\n\r\n" | |||||||||||||||||||||||||||
| 200 | ."Error: CGI app returned no output - Executing $req_env{SCRIPT_FILENAME} failed !\n"; | ||||||||||||||||||||||||||||
| 201 | 0 | next; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 0 | $rqst->Detach(); # needed to restore original STDIN,STDOUT,STDERR | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | ▶ | unless($pid){ #### Child #### | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | close $PIN; # <--perhaps not really necessary | |||||||||||||||||||||||||||
| 207 | 0 | ▶ | open *STDIN,'<&=',$CIN or die 'unable to reopen STDIN'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | open *STDERR,'>&=',$CERR or die 'unable to reopen STDERR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | exec $req_env{'SCRIPT_FILENAME'} or die "exec failed"; # running the cgi app (exec does not return so child terminates here) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | }else{ #### Parent #### | ||||||||||||||||||||||||||||
| 211 | 0 | close $CIN; # <--perhaps not really necessary | |||||||||||||||||||||||||||
| 212 | 0 | close $CERR; # *must* close child's file handle to avoid deadlock | |||||||||||||||||||||||||||
| 213 | 0 | $rqst->Attach(); #reattach FCGI's STDIN,STDOUT,STDERR | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | ## send STDIN to child | ||||||||||||||||||||||||||||
| 216 | 0 | my $buffer; | |||||||||||||||||||||||||||
| 217 | #print {$PIN} $buffer while (read *STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); ## longer version below may be safer for very long input. | ||||||||||||||||||||||||||||
| 218 | 0 | ▶ | if($req_env{'REQUEST_METHOD'} eq 'POST' and $req_env{'CONTENT_LENGTH'}!=0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$req_env{"REQUEST_METHOD"} eq "POST" and $req_env{"CONTENT_LENGTH"} != 0
MC/DC0%0/2
$req_env{"REQUEST_METHOD"} eq "POST" and $req_env{"CONTENT_LENGTH"} != 0
$req_env{"REQUEST_METHOD"} eq "POST"
$req_env{"CONTENT_LENGTH"} != 0
Truth table0%0/3
A and B
A= $req_env{"REQUEST_METHOD"} eq "POST"B= $req_env{"CONTENT_LENGTH"} != 0
| |||||||||||||||||||||||||||||
| 219 | 0 | my $bytes=0; | |||||||||||||||||||||||||||
| 220 | 0 | ▶ | while ($bytes<$req_env{'CONTENT_LENGTH'}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | 0 | $bytes+=read *STDIN,$buffer,($req_env{'CONTENT_LENGTH'}-$bytes); | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | last if ($bytes==0 or !defined $bytes); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$bytes == 0 or not defined $bytes
MC/DC0%0/2
$bytes == 0 or not defined $bytes
$bytes == 0
not defined $bytes
Truth table0%0/3
A or not(B)
A= $bytes == 0B= not defined $bytes
| |||||||||||||||||||||||||||||
| 223 | 0 | print {$PIN} $buffer; | |||||||||||||||||||||||||||
| 224 | } } | ||||||||||||||||||||||||||||
| 225 | 0 | close $PIN; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 2 | ▶ | use IO::Select; # non-blocking read from child's redirected STDOUT and STDERR | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 228 | 0 | my $sel = IO::Select->new($COUT,$PERR); | |||||||||||||||||||||||||||
| 229 | 0 | ▶ | while(my @ready=$sel->can_read){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | for my $FH (@ready){ | |||||||||||||||||||||||||||
| 231 | 0 | ▶ | if(0==sysread $FH,$buffer,4096){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 0 | $sel->remove($FH); | |||||||||||||||||||||||||||
| 233 | 0 | close $FH; | |||||||||||||||||||||||||||
| 234 | }else{ | ||||||||||||||||||||||||||||
| 235 | 0 | ▶ | print {$FH==$COUT ? *STDOUT:*STDERR} $buffer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | } } } | ||||||||||||||||||||||||||||
| 237 | 0 | waitpid $pid,0; | |||||||||||||||||||||||||||
| 238 | 0 | $rqst->Finish(); | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | 0 | $o{fcgi_pm}->pm_post_dispatch(); | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # overriding process names | ||||||||||||||||||||||||||||
| 245 | sub FCGI::ProcManager::pm_change_process_name { | ||||||||||||||||||||||||||||
| 246 | 0 | ▶ | my ($self,$name)=@_; | ||||||||||||||||||||||||||
|
Subroutine
pm_change_process_name: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 247 | 0 | my %p=( 'perl-fcgi-pm' =>'FCGI::Daemon', | |||||||||||||||||||||||||||
| 248 | 'perl-fcgi' =>'FCGI::Daemon-worker', | ||||||||||||||||||||||||||||
| 249 | ); | ||||||||||||||||||||||||||||
| 250 | 0 | ▶ | $0=$p{$name} if $p{$name} ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head2 get_file_from_path() | ||||||||||||||||||||||||||||
| 254 | Find first file in path | ||||||||||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||||||||||
| 256 | sub get_file_from_path { | ||||||||||||||||||||||||||||
| 257 | 0 | ▶ | local $_=shift; | ||||||||||||||||||||||||||
|
Subroutine
get_file_from_path: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 258 | 0 | my $file=''; | |||||||||||||||||||||||||||
| 259 | 0 | for(split '/',$_){ | |||||||||||||||||||||||||||
| 260 | 0 | ▶ | next if $_ eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 0 | $file.='/'.$_; | |||||||||||||||||||||||||||
| 262 | 0 | ▶ | last if -f -s $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | 0 | $file; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 2 | 1; | |||||||||||||||||||||||||||
| 268 | __END__ | ||||||||||||||||||||||||||||