| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | # See copyright, etc in below POD section. | ||||||||||||||||||||||||||||
| 3 | ###################################################################### | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | require 5.006_001; | |||||||||||||||||||||||||||
| 6 | 4 | ▶ | use lib './blib/lib'; # testing | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Cwd qw (getcwd chdir); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use vars qw ($Debug); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use IPC::Locker; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use IPC::PidStat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #====================================================================== | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | our @Opt_FgArgv; | |||||||||||||||||||||||||||
| 19 | 4 | our $Opt_Locklist; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #====================================================================== | ||||||||||||||||||||||||||||
| 22 | # main | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Beware, POSIX execve() et al. have an unspecified effect on the action | ||||||||||||||||||||||||||||
| 25 | # for SIGCHLD (alone). So this won't work. | ||||||||||||||||||||||||||||
| 26 | ## $SIG{CHLD} = 'IGNORE'; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | my %server_params = (lock=>[]); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 4 | $Debug = 0; | |||||||||||||||||||||||||||
| 31 | 4 | Getopt::Long::config ("require_order"); | |||||||||||||||||||||||||||
| 32 | 4 | ▶ | if (! GetOptions ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | "help" => \&usage, | ||||||||||||||||||||||||||||
| 34 | "debug" => \&debug, | ||||||||||||||||||||||||||||
| 35 | "version" => \&version, | ||||||||||||||||||||||||||||
| 36 | 2 | ▶ | "dhost=s" => sub {shift; $server_params{host} = shift;}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | "lock=s" => sub {shift; push @{$server_params{lock}}, split(':',shift);}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | "port=i" => sub {shift; $server_params{port} = shift;}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | "timeout=i" => sub {shift; $server_params{timeout} = shift;}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | "verbose!" => sub {shift; $server_params{verbose} = shift;}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | "locklist!" => \$Opt_Locklist, | ||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||
| 43 | "<>" => \¶meter, | ||||||||||||||||||||||||||||
| 44 | )) { | ||||||||||||||||||||||||||||
| 45 | 0 | die "%Error: Bad usage, see lockersh --help\n"; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 2 | push @Opt_FgArgv, @ARGV; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | if ($Opt_Locklist) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 1 | my $lock = new IPC::Locker (verbose=>0, | |||||||||||||||||||||||||||
| 51 | timeout=>0, | ||||||||||||||||||||||||||||
| 52 | autounlock=>1, | ||||||||||||||||||||||||||||
| 53 | destroy_unlock=>0, | ||||||||||||||||||||||||||||
| 54 | %server_params, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | 1 | locklist($lock); | |||||||||||||||||||||||||||
| 57 | 0 | exit(0); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | $#{$server_params{lock}}>=0 or die "%Error: --lock not specified; see --help\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | $Opt_FgArgv[0] or die "%Error: No command specified\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Fork once to start parent process | ||||||||||||||||||||||||||||
| 64 | 1 | my $foreground_pid = $$; # Unlike most forks, the job goes in the parent | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Do this while we still have STDERR. | ||||||||||||||||||||||||||||
| 67 | 1 | my $lock = new IPC::Locker (verbose=>0, | |||||||||||||||||||||||||||
| 68 | timeout=>0, | ||||||||||||||||||||||||||||
| 69 | autounlock=>1, | ||||||||||||||||||||||||||||
| 70 | destroy_unlock=>0, | ||||||||||||||||||||||||||||
| 71 | %server_params, | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | 1 | ▶ | $lock or die "%Error: Did not connect to lockerd,"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | $lock->lock; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (my $pid = fork()) { # Parent process, foreground job | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | print "\tForeground: @Opt_FgArgv\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | # The child forks again quickly. Sometimes, SIG_CHLD leaks to us and | ||||||||||||||||||||||||||||
| 79 | # wrecks the exec'd command, so wait for it now. | ||||||||||||||||||||||||||||
| 80 | 0 | my $rv = waitpid($pid, 0); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($rv != $pid) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 82 | 0 | die "%Error: waitpid() returned $rv: $!"; | |||||||||||||||||||||||||||
| 83 | } elsif ($?) { | ||||||||||||||||||||||||||||
| 84 | 0 | die "%Error: Child process died with status $?,"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | print "Exec in $$\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | exec ("/bin/sh", "-c", join(' ',@Opt_FgArgv)); | |||||||||||||||||||||||||||
| 89 | 0 | die "%Error: Exec failed: @Opt_FgArgv,"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | #else, rest is for child process. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Disassociate from controlling terminal | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | POSIX::setsid() or die "%Error: Can't start a new session: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Change working directory | ||||||||||||||||||||||||||||
| 97 | 0 | chdir "/"; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | open(STDIN, "+>/dev/null") or die "%Error: Can't re-open STDIN: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if (!$Debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | open(STDOUT, "+>&STDIN"); | |||||||||||||||||||||||||||
| 101 | 0 | open(STDERR, "+>&STDIN"); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | # Prevent possibility of acquiring a controlling terminal | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | exit(0) if fork(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Wait for child to complete. We can't waitpid, as we're not the parent | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | while (IPC::PidStat::local_pid_exists($foreground_pid)) { sleep 1; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | print "Parent $foreground_pid completed\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Unlock | ||||||||||||||||||||||||||||
| 111 | 0 | $lock->unlock; $lock=undef; | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | print "Child exiting\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub usage { | ||||||||||||||||||||||||||||
| 117 | 1 | ▶ | print "Version: $IPC::Locker::VERSION\n"; | ||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 118 | 1 | pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1); | |||||||||||||||||||||||||||
| 119 | 0 | exit (1); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub version { | ||||||||||||||||||||||||||||
| 123 | 1 | ▶ | print "Version: $IPC::Locker::VERSION\n"; | ||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 124 | 1 | exit (1); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub debug { | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | $Debug = 1; | ||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 129 | 0 | $IPC::Locker::Debug = 1; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub parameter { | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my $param = shift; | ||||||||||||||||||||||||||
|
Subroutine
parameter: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($Opt_FgArgv[0] || $param !~ /^-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$main::Opt_FgArgv[0] or not $param =~ /^-/
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 =~ /^-/
| |||||||||||||||||||||||||||||
| 135 | 0 | push @Opt_FgArgv, $param; | |||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | 0 | die "%Error: Unknown option: $param\n"; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | ####################################################################### | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub locklist { | ||||||||||||||||||||||||||||
| 144 | 1 | ▶ | my $lock = shift; | ||||||||||||||||||||||||||
|
Subroutine
locklist: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 145 | 1 | my %list = ($lock->lock_list); | |||||||||||||||||||||||||||
| 146 | 0 | foreach my $lockname (sort keys %list) { | |||||||||||||||||||||||||||
| 147 | 0 | printf ("%-20s %s\n",$lockname, $list{$lockname}); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | ####################################################################### | ||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||