script/pidwatch

filter: Statementstmt 70.0% 77 / 110 Branchbran 46.1% 24 / 52 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 80.0% 12 / 15 Podpod n/a totaltotal 57.7% 116 / 201 SCAR 12.9
CC
3
Cov
59%
parameter
12.0
__ANON__
2.0
debug
2.0
CRAP 3.6
« script/lockersh ↑ Summary script/uriexec »
1 #!/usr/bin/perl -w
2 # See copyright, etc in below POD section.
3 ######################################################################
4  
5 11 require 5.006_001;
6 11 ▶use lib './blib/lib'; # testing
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Cwd qw (getcwd chdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use vars qw ($Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use IPC::PidStat;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use IPC::Locker;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #======================================================================
17  
18 11 our @Orig_Argv = @ARGV;
19 11 our @Opt_FgArgv;
20 11 our $Opt_Host = IPC::Locker::hostfqdn();
21 11 our $Opt_Pid;
22 11 our $Opt_Signal = 'HUP';
23 11 our $Opt_Killer;
24 11 our $Opt_Foreground;
25  
26 #======================================================================
27 # main
28  
29 # Beware, POSIX execve() et al. have an unspecified effect on the action
30 # for SIGCHLD (alone). So this won't work.
31 ## $SIG{CHLD} = 'IGNORE';
32  
33 11 my $opt_cd;
34 11 my %server_params = ();
35  
36 11 $Debug = 0;
37 11 Getopt::Long::config ("require_order");
38 11 ▶if (! GetOptions (
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions("help", \&main::usage, "debug", \&main::debug, "version", \&main::version, "port=i", sub { shift(); $server_params{"port"} = shift(); } , "host=s", \$main::Opt_Host, "pid=i", \$main::Opt_Pid, "cd=s", \$opt_cd, "foreground=i", \$main::Opt_Foreground, "signal=s", \$main::Opt_Signal, "killer=s", \$main::Opt_Killer, "<>", \&main::parameter)) 0 9 9
39 "help" => \&usage,
40 "debug" => \&debug,
41 "version" => \&version,
42 9 ▶ "port=i" => sub {shift; $server_params{port} = shift;},
Subroutine __ANON__: called CC=1 SCAR=0.0
43 #
44 "host=s" => \$Opt_Host,
45 "pid=i" => \$Opt_Pid,
46 "cd=s" => \$opt_cd,
47 "foreground=i"=> \$Opt_Foreground,
48 "signal=s" => \$Opt_Signal,
49 "killer=s" => \$Opt_Killer,
50 "<>" => \&parameter,
51 )) {
52 0 die "%Error: Bad usage, try 'pidwatch --help'\n";
53 }
54 9 push @Opt_FgArgv, @ARGV;
55  
56 9 ▶$Opt_Host or die "%Error: --host not specified\n";
Branch: 1 branch
BranchTrueFalseTotal
$main::Opt_Host or die "%Error: --host not specified\n" 0 9 9
57 9 ▶$Opt_Pid or die "%Error: --pid not specified\n";
Branch: 1 branch
BranchTrueFalseTotal
$main::Opt_Pid or die "%Error: --pid not specified\n" 0 9 9
58 9 ▶if ($Opt_Foreground) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt_Foreground) { } 3 6 9
59 3 ▶ !$Opt_FgArgv[0] or die "%Error: --foreground option does not take additional command arguments\n";
Branch: 1 branch
BranchTrueFalseTotal
$main::Opt_FgArgv[0] and die "%Error: --foreground option does not take additional command arguments\n" 0 3 3
60 } else {
61 6 ▶ $Opt_FgArgv[0] or die "%Error: No command specified\n";
Branch: 1 branch
BranchTrueFalseTotal
$main::Opt_FgArgv[0] or die "%Error: No command specified\n" 0 6 6
62 }
63  
64 9 ▶if ($opt_cd && -d $opt_cd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_cd and -d $opt_cd) 0 9 9
Condition33%1/3
$opt_cd and -d $opt_cd
!ll&&!rl&&r
900
MC/DC0%0/2
$opt_cd and -d $opt_cd
$opt_cd -d $opt_cd
Truth table33%1/3
A and B
A= $opt_cdB= -d $opt_cd
ABresult
0X0
100
111
65 0 $ENV{PWD} = $opt_cd;
66 0 chdir ($opt_cd);
67 }
68  
69 # Debugging
70 9 my $dbgfh = \*STDERR;
71 #$dbgfh = IO::File->new("$ENV{HOME}/pidwatch_$$","w") if $Debug;
72 9 ▶print $dbgfh "\tArgs: @Orig_Argv\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $main::Debug 0 9 9
73  
74 # Fork once to start parent process
75 9 ▶my $foreground_pid = $Opt_Foreground || $$; # Unlike most forks, the job goes in the parent
Condition66%2/3
$main::Opt_Foreground || $$
l!l&&r!l&&!r
360
MC/DC0%0/2
$main::Opt_Foreground || $$
$main::Opt_Foreground $$
Truth table66%2/3
A || B
A= $main::Opt_ForegroundB= $$
ABresult
1X1
011
000
76  
77 9 ▶if (my $pid = fork()) { # Parent process, foreground job
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) 3 6 9
78 3 ▶ print $dbgfh "\tForeground: @Opt_FgArgv\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $main::Debug 0 3 3
79 # The child forks again quickly. Sometimes, SIG_CHLD leaks to us and
80 # wrecks the exec'd command, so wait for it now.
81 3 my $rv = waitpid($pid, 0);
82 3 ▶ if ($rv != $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv != $pid) { } 0 3 3
elsif ($?) { } 0 3 3
83 0 die "%Error: waitpid() returned $rv: $!";
84 } elsif ($?) {
85 0 die "%Error: Child process died with status $?,";
86 }
87  
88 3 ▶ if ($Opt_Foreground) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt_Foreground) { } 1 2 3
89 1 exit(0); # Not relevant
90 } else {
91 2 my $shArg = join(' ', map{"$_"} @Opt_FgArgv);
92 2 ▶ print $dbgfh "\t/bin/sh -c $shArg\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $main::Debug 0 2 2
93 2 exec ("/bin/sh", "-c", $shArg);
94 0 die "%Error: Exec failed: @Opt_FgArgv,";
95 }
96 }
97 #else, rest is for child process.
98  
99 # Do this while we still have STDERR.
100 6 ▶my $exister = new IPC::PidStat (%server_params)
Branch: 1 branch
BranchTrueFalseTotal
my $exister = "IPC::PidStat"->new(%server_params) or die "%Error: Did not connect to pidstatd," 0 6 6
101 or die "%Error: Did not connect to pidstatd,";
102  
103 # Disassociate from controlling terminal
104 6 ▶POSIX::setsid() or die "%Error: Can't start a new session: $!";
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or die "%Error: Can't start a new session: $!" 0 6 6
105  
106 # Change working directory
107 6 chdir "/";
108 6 ▶open(STDIN, "+>/dev/null") or die "%Error: Can't re-open STDIN: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "+>/dev/null" or die "%Error: Can't re-open STDIN: $!" 0 6 6
109 6 ▶if (!$Debug) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($main::Debug) 6 0 6
110 6 open(STDOUT, "+>&STDIN");
111 6 open(STDERR, "+>&STDIN");
112 }
113 # Prevent possibility of acquiring a controling terminal
114 6 ▶exit(0) if fork();
Branch: 1 branch
BranchTrueFalseTotal
if fork 3 3 6
115  
116 3 while (1) {
117 3 ▶ if (IPC::PidStat::local_pid_doesnt_exist($foreground_pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (IPC::PidStat::local_pid_doesnt_exist($foreground_pid)) 0 3 3
118 0 ▶ print $dbgfh " Local pid $foreground_pid died\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $main::Debug 0 0 0
119 0 exit(0);
120 }
121 3 $exister->pid_request(host=>$Opt_Host, pid=>$Opt_Pid,
122 return_exist=>0,
123 return_doesnt=>1,
124 return_unknown=>0,);
125 0 my @recved;
126 0 eval {
127 0 ▶ local $SIG{ALRM} = sub { die "Timeout\n"; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 alarm(1);
129 0 @recved = $exister->recv_stat();
130 0 alarm(0);
131 };
132 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
133 0 alarm(0);
134 # Recovery of PidStat protocol must be handled in that class.
135 }
136 0 ▶ if ($recved[0]
Branch: 1 branch
BranchTrueFalseTotal
if ($recved[0] and $recved[0] == $main::Opt_Pid and $recved[1] == 0) 0 0 0
Condition0%0/6
$recved[0] and $recved[0] == $main::Opt_Pid and $recved[1] == 0
!ll&&!rl&&r
000
$recved[0] and $recved[0] == $main::Opt_Pid
!ll&&!rl&&r
000
MC/DC0%0/3
$recved[0] and $recved[0] == $main::Opt_Pid and $recved[1] == 0
$recved[0] $recved[0] == $main::Opt_Pid $recved[1] == 0
Truth table0%0/4
A and B and C
A= $recved[0]B= $recved[0] == $main::Opt_PidC= $recved[1] == 0
ABCresult
0XX0
10X0
1100
1111
137 && $recved[0]==$Opt_Pid
138 && $recved[1]==0) { # It doesn't exist. Oh my.
139 0 ▶ print $dbgfh " Remote pid $Opt_Pid died\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $main::Debug 0 0 0
140 0 $Opt_Signal =~ s/^-?(SIG)?//;
141 0 ▶ if ($Opt_Killer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt_Killer) { } 0 0 0
142 0 exec ($Opt_Killer, "-".$Opt_Signal, $foreground_pid);
143 0 exit(0); # If exec fails...
144 } else {
145 0 kill $Opt_Signal, $foreground_pid;
146 }
147 0 exit(0);
148 }
149 0 sleep 1;
150 }
151  
152 #----------------------------------------------------------------------
153  
154 sub usage {
155 1 ▶ print "Version: $IPC::PidStat::VERSION\n";
Subroutine usage: called CC=1 SCAR=0.4
156 1 pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1);
157 0 exit (1);
158 }
159  
160 sub version {
161 1 ▶ print "Version: $IPC::Locker::VERSION\n";
Subroutine version: called CC=1 SCAR=0.0
162 1 exit (1);
163 }
164  
165 sub debug {
166 0 ▶ $Debug = 1;
Subroutine debug: not called CC=1 SCAR=6.9
167 0 $IPC::PidStat::Debug = 1;
168 }
169  
170 sub parameter {
171 0 ▶ my $param = shift;
Subroutine parameter: not called CC=3 SCAR=24.8
172 0 ▶ if ($Opt_FgArgv[0] || $param !~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::Opt_FgArgv[0] or not $param =~ /^-/) { } 0 0 0
Condition0%0/3
$main::Opt_FgArgv[0] or not $param =~ /^-/
l!l&&r!l&&!r
000
MC/DC0%0/2
$main::Opt_FgArgv[0] or not $param =~ /^-/
$main::Opt_FgArgv[0] not $param =~ /^-/
Truth table0%0/3
A or not(B)
A= $main::Opt_FgArgv[0]B= not $param =~ /^-/
ABresult
1X1
011
000
173 0 push @Opt_FgArgv, $param;
174 } else {
175 0 die "%Error: Unknown option: $param\n";
176 }
177 }
178  
179 #######################################################################
180 __END__
« script/lockersh ↑ Summary script/uriexec »