| 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::Locker::Server; | ||||||||
|
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 | ▶ | "host=s" => sub {$server_params{host} = $_[1];}, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 34 | 0 | ▶ | "port=i" => sub {$server_params{port} = $_[1];}, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 35 | 0 | ▶ | "path=s" => sub {$server_params{port} = $_[1]; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 36 | 0 | $server_params{family}='UNIX';}, | |||||||||
| 37 | "fork!" => \$opt_fork, | ||||||||||
| 38 | )) { | ||||||||||
| 39 | 0 | die "%Error: Bad usage, try 'lockerd --help'\n"; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | # Must be after we've done --help | ||||||||||
| 43 | 0 | $0 = 'lockerd'; # So ps and pidof can find this daemon | |||||||||
| 44 | |||||||||||
| 45 | # Loop in case something kills us | ||||||||||
| 46 | 0 | $SIG{HUP} = \&sig_HUP; | |||||||||
| 47 | 0 | $SIG{CHLD} = \&sig_HUP; | |||||||||
| 48 | 0 | while (1) { | |||||||||
| 49 | 0 | ▶ | print "Starting server\n" if $Debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | ▶ | $Pid = fork if $opt_fork; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | ▶ | unless ($Pid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | IPC::Locker::Server->new(%server_params)->start_server (); | |||||||||
| 53 | 0 | exit(0); | |||||||||
| 54 | } | ||||||||||
| 55 | 0 | ▶ | waitpid($Pid,0) if $Pid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | ▶ | warn "%Warning: Server aborted, status=$?\n" if $Debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | sleep(1); | |||||||||
| 58 | 0 | ▶ | kill 9, $Pid if $Pid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | $Pid = 0; | |||||||||
| 60 | 0 | sleep(1); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | exit (0); | |||||||||
| 64 | |||||||||||
| 65 | sub sig_HUP { | ||||||||||
| 66 | 0 | ▶ | kill 9, $Pid if $Pid; | ||||||||
|
Subroutine
sig_HUP: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 67 | 0 | $Pid = 0; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | ###################################################################### | ||||||||||
| 71 | |||||||||||
| 72 | sub usage { | ||||||||||
| 73 | 1 | ▶ | pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1); | ||||||||
|
Subroutine
usage: called CC=1 SCAR=1.2
| |||||||||||
| 74 | 0 | exit(1); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | sub version { | ||||||||||
| 78 | 1 | ▶ | print "Version: $IPC::Locker::VERSION\n"; | ||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 1 | exit (1); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | sub debug { | ||||||||||
| 83 | 0 | ▶ | $Debug = 1; | ||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
| |||||||||||
| 84 | 0 | $IPC::Locker::Server::Debug = 1; | |||||||||
| 85 | 0 | $IPC::PidStat::Debug = 1; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | ###################################################################### | ||||||||||
| 89 | __END__ | ||||||||||