| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | # See copyright, etc in below POD section. | ||||||||||||||||||||||||||||
| 3 | ###################################################################### | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | require 5.004; | |||||||||||||||||||||||||||
| 6 | 2 | ▶ | use lib './blib/lib'; # testing | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use IPC::PidStat::PidServer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | BEGIN { $ENV{PATH} = '/usr/ucb:/bin' } # Secure path | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | ###################################################################### | ||||||||||||||||||||||||||||
| 15 | # configuration | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | ###################################################################### | ||||||||||||||||||||||||||||
| 18 | # globals | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use vars qw($Pid); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ###################################################################### | ||||||||||||||||||||||||||||
| 23 | # main | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | my $Debug = 0; | |||||||||||||||||||||||||||
| 26 | 2 | my %server_params = (); | |||||||||||||||||||||||||||
| 27 | 2 | my $opt_fork = 1; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | if (!GetOptions ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | "help" => \&usage, | ||||||||||||||||||||||||||||
| 31 | "debug" => \&debug, | ||||||||||||||||||||||||||||
| 32 | "version" => \&version, | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | "port=i" => sub {$server_params{port} = $_[1];}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | "fork!" => \$opt_fork, | ||||||||||||||||||||||||||||
| 35 | )) { | ||||||||||||||||||||||||||||
| 36 | 0 | die "%Error: Bad usage, try 'pidstatd --help'\n"; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Must be after we've done --help | ||||||||||||||||||||||||||||
| 40 | 0 | $0 = 'pidstatd'; # So ps and pidof can find this daemon | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if (!$Debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($opt_fork) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | # Fork once to let parent die | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | exit if fork(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | # Disassociate from controlling terminal | ||||||||||||||||||||||||||||
| 47 | 0 | POSIX::setsid(); | |||||||||||||||||||||||||||
| 48 | # Prevent possibility of acquiring a controling terminal | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | exit if fork(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | # Change working directory | ||||||||||||||||||||||||||||
| 51 | 0 | chdir "/"; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | # Close open file descriptors | ||||||||||||||||||||||||||||
| 54 | 0 | my $openmax = POSIX::sysconf( &POSIX::_SC_OPEN_MAX ); | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | $openmax = (!defined($openmax) || $openmax < 0) ? 64 : $openmax; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $openmax or $openmax < 0
MC/DC0%0/2
not defined $openmax or $openmax < 0
not defined $openmax
$openmax < 0
Truth table0%0/3
not(A) or B
A= not defined $openmaxB= $openmax < 0
| |||||||||||||||||||||||||||||
| 56 | 0 | foreach my $i (0 .. $openmax) { POSIX::close($i); } | |||||||||||||||||||||||||||
| 57 | # Silence please (in case user didn't pipe when starting us) | ||||||||||||||||||||||||||||
| 58 | 0 | open(STDIN, "+>/dev/null"); | |||||||||||||||||||||||||||
| 59 | 0 | open(STDOUT, "+>&STDIN"); | |||||||||||||||||||||||||||
| 60 | 0 | open(STDERR, "+>&STDIN"); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Loop in case something kills us | ||||||||||||||||||||||||||||
| 64 | 0 | $SIG{HUP} = \&sig_HUP; | |||||||||||||||||||||||||||
| 65 | 0 | $SIG{CHLD} = \&sig_HUP; | |||||||||||||||||||||||||||
| 66 | 0 | while (1) { | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | print "Starting server\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | unless ($Pid = fork) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | IPC::PidStat::PidServer->new(%server_params)->start_server (); | |||||||||||||||||||||||||||
| 70 | 0 | exit(0); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | waitpid($Pid,0) if $Pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | warn "%Warning: Server aborted\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | sleep(1); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | kill 9, $Pid if $Pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $Pid = 0; | |||||||||||||||||||||||||||
| 77 | 0 | sleep(1); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | exit (0); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub sig_HUP { | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | kill 9, $Pid if $Pid; | ||||||||||||||||||||||||||
|
Subroutine
sig_HUP: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | $Pid = 0; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ###################################################################### | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub usage { | ||||||||||||||||||||||||||||
| 90 | 1 | ▶ | print "Version: $IPC::PidStat::VERSION\n"; | ||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 91 | 1 | pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1); | |||||||||||||||||||||||||||
| 92 | 0 | exit(1); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub version { | ||||||||||||||||||||||||||||
| 96 | 1 | ▶ | print "Version: $IPC::PidStat::VERSION\n"; | ||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 1 | exit (1); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub debug { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | $Debug = 1; | ||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 0 | $IPC::PidStat::PidServer::Debug = 1; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | ###################################################################### | ||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||