| 1 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # Net::Daemon - Base class for implementing TCP/IP daemons | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Copyright (C) 1998, Jochen Wiedmann | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # Am Eisteich 9 | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # 72555 Metzingen | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # Germany | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # Phone: +49 7123 14887 | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # Email: joe@ispsoft.de | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # You may distribute this package under the terms of either the GNU | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # General Public License or the Artistic License, as specified in the | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # Perl README file. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||||||||||
| 19 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | package Net::Daemon; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 56 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 56 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 56 | ▶ | use Config; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 56 | ▶ | use Getopt::Long (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 56 | ▶ | use Symbol (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 56 | ▶ | use IO::Socket (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 56 | ▶ | use Net::Daemon::Log (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 56 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 56 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 56 | our $VERSION = '0.51'; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 56 | our @ISA = qw(Net::Daemon::Log); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 56 | our $RegExpLock = 1; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # Dummy share() in case we're >= 5.10. If we are, require/import of | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # threads::shared will replace it appropriately. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # But ONLY if we are built with threads and forks has not already been loaded. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 56 | ▶ | my $use_ithreads = ( $^V ge v5.10.0 && $Config{'useithreads'} && !$INC{'forks.pm'} ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$^V ge v5.10.0 and $Net::Daemon::Config{"useithreads"} and not $INC{"forks.pm"}
$^V ge v5.10.0 and $Net::Daemon::Config{"useithreads"}
MC/DC0%0/3
$^V ge v5.10.0 and $Net::Daemon::Config{"useithreads"} and not $INC{"forks.pm"}
$^V ge v5.10.0
$Net::Daemon::Config{"useithreads"}
not $INC{"forks.pm"}
Truth table0%0/4
A and B and not(C)
A= $^V ge v5.10.0B= $Net::Daemon::Config{"useithreads"}C= not $INC{"forks.pm"}
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 56 | ▶ | if ($use_ithreads) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | eval { require threads; }; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | eval { require threads::shared; }; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | threads::shared::share( $RegExpLock ) if $forks::threads; # Assuming this isn't threads masquerading as forks. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 56 | our $exit; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # Name: Options (Class method) | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # Purpose: Returns a hash ref of command line options | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # Inputs: $class - This class | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # Result: Options array; any option is represented by a hash ref; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # used keys are 'template', a string suitable for describing | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # the option to Getopt::Long::GetOptions and 'description', | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # a string for the Usage message | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||||||||||||||||
| 65 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | sub Options ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 18 | ▶ | 'catchint' => { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Options: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 'template' => 'catchint!', | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 'description' => '--nocatchint ' . "Try to catch interrupts when calling system\n" . ' ' . 'functions like bind(), recv()), ...' | ||||||||||||||||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 'childs' => { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 'template' => 'childs=i', | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 'description' => '--childs <num> ' . 'Set number of preforked childs, implies mode=single.' | ||||||||||||||||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 'chroot' => { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 'template' => 'chroot=s', | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 'description' => '--chroot <dir> ' . 'Change rootdir to given after binding to port.' | ||||||||||||||||||||||||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 'configfile' => { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 'template' => 'configfile=s', | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 'description' => '--configfile <file> ' . 'Read options from config file <file>.' | ||||||||||||||||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 'debug' => { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 'template' => 'debug', | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 'description' => '--debug ' . 'Turn debugging mode on' | ||||||||||||||||||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 'facility' => { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 'template' => 'facility=s', | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 'description' => '--facility <facility> ' . 'Syslog facility; defaults to \'daemon\'' | ||||||||||||||||||||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 'group' => { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 'template' => 'group=s', | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 'description' => '--group <gid> ' . 'Change gid to given group after binding to port.' | ||||||||||||||||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 'help' => { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 'template' => 'help', | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 'description' => '--help ' . 'Print this help message' | ||||||||||||||||||||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 'listen' => { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 'template' => 'listen=i', | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 'description' => '--listen <num> ' . 'Size of the listen queue (backlog); defaults to 10' | ||||||||||||||||||||||||||||||||||||||||||
| 104 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 'localaddr' => { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 'template' => 'localaddr=s', | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 'description' => '--localaddr <ip> ' . 'IP number to bind to; defaults to INADDR_ANY' | ||||||||||||||||||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 'localpath' => { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 'template' => 'localpath=s', | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 'description' => '--localpath <path> ' . 'UNIX socket domain path to bind to' | ||||||||||||||||||||||||||||||||||||||||||
| 112 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 'localport' => { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 'template' => 'localport=s', | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 'description' => '--localport <port> ' . 'Port number to bind to' | ||||||||||||||||||||||||||||||||||||||||||
| 116 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 'logfile' => { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 'template' => 'logfile=s', | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 'description' => '--logfile <file> ' . 'Force logging to <file>' | ||||||||||||||||||||||||||||||||||||||||||
| 120 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 'loop-child' => { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 'template' => 'loop-child', | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 'description' => '--loop-child ' . 'Create a child process for loops' | ||||||||||||||||||||||||||||||||||||||||||
| 124 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 'loop-timeout' => { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 'template' => 'loop-timeout=f', | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 'description' => '--loop-timeout <secs> ' . 'Looping mode, <secs> seconds per loop' | ||||||||||||||||||||||||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 'mode' => { | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 'template' => 'mode=s', | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 'description' => '--mode <mode> ' . 'Operation mode (threads, fork or single)' | ||||||||||||||||||||||||||||||||||||||||||
| 132 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 'pidfile' => { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 'template' => 'pidfile=s', | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 'description' => '--pidfile <file> ' . 'Use <file> as PID file' | ||||||||||||||||||||||||||||||||||||||||||
| 136 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 'proto' => { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 'template' => 'proto=s', | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 'description' => '--proto <protocol> ' . 'transport layer protocol: tcp (default) or unix' | ||||||||||||||||||||||||||||||||||||||||||
| 140 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 'user' => { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 'template' => 'user=s', | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 'description' => '--user <user> ' . 'Change uid to given user after binding to port.' | ||||||||||||||||||||||||||||||||||||||||||
| 144 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 'version' => { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 'template' => 'version', | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 'description' => '--version ' . 'Print version number and exit' | ||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 153 | # | ||||||||||||||||||||||||||||||||||||||||||
| 154 | # Name: Version (Class method) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # Purpose: Returns version string | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # Inputs: $class - This class | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # Result: Version string; suitable for printed by "--version" | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # | ||||||||||||||||||||||||||||||||||||||||||
| 162 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | sub Version ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | "Net::Daemon server, Copyright (C) 1998, Jochen Wiedmann"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Version: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # Name: Usage (Class method) | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # Purpose: Prints usage message | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # Inputs: $class - This class | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # Result: Nothing; aborts with error status | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # | ||||||||||||||||||||||||||||||||||||||||||
| 178 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | sub Usage ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | my ($class) = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Usage: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my ($options) = $class->Options(); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | my (@options) = sort ( keys %$options ); | |||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | print STDERR "Usage: $0 <options>\n\nPossible options are:\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my ($key); | |||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | foreach $key ( sort ( keys %$options ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my ($o) = $options->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | print STDERR " ", $o->{'description'}, "\n" if $o->{'description'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | print STDERR "\n", $class->Version(), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | exit(1); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # Name: ReadConfigFile (Instance method) | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # | ||||||||||||||||||||||||||||||||||||||||||
| 199 | # Purpose: Reads the config file. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # Inputs: $self - Instance | ||||||||||||||||||||||||||||||||||||||||||
| 202 | # $file - config file name | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # $options - Hash of command line options; these are not | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # really for being processed by this method. We pass | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # it just in case. The new() method will process them | ||||||||||||||||||||||||||||||||||||||||||
| 206 | # at a later time. | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # $args - Array ref of other command line options. | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # | ||||||||||||||||||||||||||||||||||||||||||
| 209 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | sub ReadConfigFile { | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | ▶ | my ( $self, $file, $options, $args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ReadConfigFile: called CC=6 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | ▶ | if ( !-f $file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $self->Fatal("No such config file: $file"); | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | my $copts = do File::Spec->rel2abs($file); | |||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | $self->Fatal("Error while processing config file $file: $@"); | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 1 | ▶ | if ( !$copts || ref($copts) ne 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $copts or ref $copts ne "HASH"
MC/DC0%0/2
not $copts or ref $copts ne "HASH"
not $copts
ref $copts ne "HASH"
Truth table33%1/3
not(A) or B
A= not $coptsB= ref $copts ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $self->Fatal("Config file $file did not return a hash ref."); | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | # Override current configuration with config file options. | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | ▶ | while ( my ( $var, $val ) = each %$copts ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 3 | $self->{$var} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # Name: new (Class method) | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # Purpose: Constructor | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # | ||||||||||||||||||||||||||||||||||||||||||
| 236 | # Inputs: $class - This class | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # $attr - Hash ref of attributes | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # $args - Array ref of command line arguments | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # Result: Server object for success, error message otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # | ||||||||||||||||||||||||||||||||||||||||||
| 242 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | sub new ($$;$) { | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 17 | ▶ | my ( $class, $attr, $args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=24 SCAR=44.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 17 | ▶ | my ($self) = $attr ? \%$attr : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 17 | ▶ | bless( $self, ( ref($class) || $class ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 17 | ▶ | my $options = ( $self->{'options'} ||= {} ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"options"} ||= {}
MC/DC0%0/1
$self->{"options"} ||= {}
$self->{"options"}
Truth table50%1/2
A
A= $self->{"options"}
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 17 | ▶ | $self->{'args'} ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"args"} ||= []
MC/DC0%0/1
$self->{"args"} ||= []
$self->{"args"}
Truth table50%1/2
A
A= $self->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 17 | ▶ | if ($args) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 17 | my @optList = map { $_->{'template'} } values( %{ $class->Options() } ); | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 17 | local @ARGV = @$args; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 17 | ▶ | if ( !Getopt::Long::GetOptions( $options, @optList ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | $self->Usage(); | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 17 | @{ $self->{'args'} } = @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 17 | ▶ | if ( $options->{'help'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | $self->Usage(); | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 17 | ▶ | if ( $options->{'version'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | print STDERR $self->Version(), "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 17 | ▶ | my $file = $options->{'configfile'} || $self->{'configfile'}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$options->{'configfile'} || $self->{'configfile'}
MC/DC50%1/2
$options->{'configfile'} || $self->{'configfile'}
$options->{'configfile'}
$self->{'configfile'}
Truth table66%2/3
A || B
A= $options->{'configfile'}B= $self->{'configfile'}
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 17 | ▶ | if ($file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 1 | $self->ReadConfigFile( $file, $options, $args ); | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 17 | ▶ | while ( my ( $var, $val ) = each %$options ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 27 | $self->{$var} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 17 | ▶ | if ( $self->{'childs'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | $self->{'mode'} = 'single'; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | elsif ( !defined( $self->{'mode'} ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if ( eval { require threads } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | $self->{'mode'} = 'ithreads'; | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my $fork = 0; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | if ( $^O ne "MSWin32" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | my $pid = eval { fork() }; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if ( defined($pid) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | if ( !$pid ) { exit; } # Child | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | $fork = 1; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | wait; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | if ($fork) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | $self->{'mode'} = 'fork'; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | $self->{'mode'} = 'single'; | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 17 | ▶ | if ( $self->{'mode'} eq 'ithreads' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 56 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | require threads; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 56 | ▶ | use warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | elsif ( $self->{'mode'} eq 'fork' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | # Initialize forking mode ... | ||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | elsif ( $self->{'mode'} eq 'single' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | # Initialize single mode ... | ||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | $self->Fatal("Unknown operation mode: $self->{'mode'}"); | |||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 17 | ▶ | $self->{'catchint'} = 1 unless exists( $self->{'catchint'} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 17 | $self->Debug("Server starting in operation mode $self->{'mode'}"); | |||||||||||||||||||||||||||||||||||||||||
| 321 | 17 | ▶ | if ( $self->{'childs'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | $self->Debug("Preforking $self->{'childs'} child processes ..."); | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 17 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | sub Clone ($$) { | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 63 | ▶ | my ( $proto, $client ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Clone: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 63 | my $self = {%$proto}; | |||||||||||||||||||||||||||||||||||||||||
| 331 | 63 | $self->{'socket'} = $client; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 63 | $self->{'parent'} = $proto; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 63 | bless( $self, ref($proto) ); | |||||||||||||||||||||||||||||||||||||||||
| 334 | 63 | $self->post_clone(); | |||||||||||||||||||||||||||||||||||||||||
| 335 | 63 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | ▶ | sub post_clone ($) { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
post_clone: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | # Override in subclasses to initialize cloned (per-connection) instances. | ||||||||||||||||||||||||||||||||||||||||||
| 340 | # Called by Clone() after the new object is created. The parent server | ||||||||||||||||||||||||||||||||||||||||||
| 341 | # object is available as $self->{'parent'}. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 345 | # | ||||||||||||||||||||||||||||||||||||||||||
| 346 | # Name: Accept (Instance method) | ||||||||||||||||||||||||||||||||||||||||||
| 347 | # | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # Purpose: Called for authentication purposes | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # | ||||||||||||||||||||||||||||||||||||||||||
| 350 | # Inputs: $self - Server instance | ||||||||||||||||||||||||||||||||||||||||||
| 351 | # | ||||||||||||||||||||||||||||||||||||||||||
| 352 | # Result: TRUE, if the client has successfully authorized, FALSE | ||||||||||||||||||||||||||||||||||||||||||
| 353 | # otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 354 | # | ||||||||||||||||||||||||||||||||||||||||||
| 355 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | sub Accept ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Accept: called CC=16 SCAR=30.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 15 | my $socket = $self->{'socket'}; | |||||||||||||||||||||||||||||||||||||||||
| 360 | 15 | my $clients = $self->{'clients'}; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 15 | ▶ | my $from = $self->{'proto'} eq 'unix' ? "Unix socket" : sprintf( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | "%s, port %s", | ||||||||||||||||||||||||||||||||||||||||||
| 363 | $socket->peerhost(), $socket->peerport() | ||||||||||||||||||||||||||||||||||||||||||
| 364 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | # Host based authorization | ||||||||||||||||||||||||||||||||||||||||||
| 367 | 15 | ▶ | if ( $self->{'clients'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 1 | my ( $name, $aliases, $addrtype, $length, @addrs ); | |||||||||||||||||||||||||||||||||||||||||
| 369 | 1 | ▶ | if ( $self->{'proto'} eq 'unix' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 1 | ( $name, $aliases, $addrtype, $length, @addrs ) = ( | |||||||||||||||||||||||||||||||||||||||||
| 371 | 'localhost', '', Socket::AF_INET(), | ||||||||||||||||||||||||||||||||||||||||||
| 372 | length( Socket::INADDR_ANY ), | ||||||||||||||||||||||||||||||||||||||||||
| 373 | Socket::inet_aton('127.0.0.1') | ||||||||||||||||||||||||||||||||||||||||||
| 374 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ( $name, $aliases, $addrtype, $length, @addrs ) = | |||||||||||||||||||||||||||||||||||||||||
| 378 | gethostbyaddr( $socket->peeraddr(), Socket::AF_INET() ); | ||||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | my @patterns = | ||||||||||||||||||||||||||||||||||||||||||
| 381 | @addrs | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 1 | ▶ | ? map { Socket::inet_ntoa($_) } @addrs | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | : $socket->peerhost(); | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 1 | ▶ | push( @patterns, $name ) if ($name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | 1 | ▶ | push( @patterns, split( / /, $aliases ) ) if $aliases; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 1 | my $found; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 1 | OUTER: foreach my $client (@$clients) { | |||||||||||||||||||||||||||||||||||||||||
| 389 | 1 | ▶ | if ( !$client->{'mask'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | $found = $client; | |||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 1 | ▶ | my $masks = ref( $client->{'mask'} ) ? $client->{'mask'} : [ $client->{'mask'} ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | # | ||||||||||||||||||||||||||||||||||||||||||
| 396 | # Regular expressions aren't thread safe, as of | ||||||||||||||||||||||||||||||||||||||||||
| 397 | # 5.00502 :-( | ||||||||||||||||||||||||||||||||||||||||||
| 398 | # | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 1 | my $lock; | |||||||||||||||||||||||||||||||||||||||||
| 400 | $lock = lock($RegExpLock) | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 1 | ▶ | if ( $self->{'mode'} eq 'threads' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 1 | foreach my $mask (@$masks) { | |||||||||||||||||||||||||||||||||||||||||
| 403 | 1 | foreach my $alias (@patterns) { | |||||||||||||||||||||||||||||||||||||||||
| 404 | 1 | ▶ | if ( $alias =~ /$mask/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | 1 | $found = $client; | |||||||||||||||||||||||||||||||||||||||||
| 406 | 1 | last OUTER; | |||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 1 | ▶ | if ( !$found || !$found->{'accept'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$found and $found->{"accept"}
MC/DC0%0/2
$found and $found->{"accept"}
$found
$found->{"accept"}
Truth table33%1/3
A and B
A= $foundB= $found->{"accept"}
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | $self->Error("Access not permitted from $from"); | |||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 1 | $self->{'client'} = $found; | |||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | 15 | $self->Debug("Accepting client from $from"); | |||||||||||||||||||||||||||||||||||||||||
| 420 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 424 | # | ||||||||||||||||||||||||||||||||||||||||||
| 425 | # Name: Run (Instance method) | ||||||||||||||||||||||||||||||||||||||||||
| 426 | # | ||||||||||||||||||||||||||||||||||||||||||
| 427 | # Purpose: Does the real work | ||||||||||||||||||||||||||||||||||||||||||
| 428 | # | ||||||||||||||||||||||||||||||||||||||||||
| 429 | # Inputs: $self - Server instance | ||||||||||||||||||||||||||||||||||||||||||
| 430 | # | ||||||||||||||||||||||||||||||||||||||||||
| 431 | # Result: Nothing; returning will make the connection to be closed | ||||||||||||||||||||||||||||||||||||||||||
| 432 | # | ||||||||||||||||||||||||||||||||||||||||||
| 433 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | ▶ | sub Run ($) { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Run: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 439 | # | ||||||||||||||||||||||||||||||||||||||||||
| 440 | # Name: Done (Instance method) | ||||||||||||||||||||||||||||||||||||||||||
| 441 | # | ||||||||||||||||||||||||||||||||||||||||||
| 442 | # Purpose: Called by the server before doing an accept(); a TRUE | ||||||||||||||||||||||||||||||||||||||||||
| 443 | # value makes the server terminate. | ||||||||||||||||||||||||||||||||||||||||||
| 444 | # | ||||||||||||||||||||||||||||||||||||||||||
| 445 | # Inputs: $self - Server instance | ||||||||||||||||||||||||||||||||||||||||||
| 446 | # | ||||||||||||||||||||||||||||||||||||||||||
| 447 | # Result: TRUE or FALSE | ||||||||||||||||||||||||||||||||||||||||||
| 448 | # | ||||||||||||||||||||||||||||||||||||||||||
| 449 | # Bugs: Doesn't work in forking mode. | ||||||||||||||||||||||||||||||||||||||||||
| 450 | # | ||||||||||||||||||||||||||||||||||||||||||
| 451 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | sub Done ($;$) { | ||||||||||||||||||||||||||||||||||||||||||
| 454 | 62 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Done: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | 62 | ▶ | $self->{'done'} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 62 | $self->{'done'}; | |||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 460 | # | ||||||||||||||||||||||||||||||||||||||||||
| 461 | # Name: Loop (Instance method) | ||||||||||||||||||||||||||||||||||||||||||
| 462 | # | ||||||||||||||||||||||||||||||||||||||||||
| 463 | # Purpose: If $self->{'loop-timeout'} option is set, then this method | ||||||||||||||||||||||||||||||||||||||||||
| 464 | # will be called every "loop-timeout" seconds. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | # | ||||||||||||||||||||||||||||||||||||||||||
| 466 | # Inputs: $self - Server instance | ||||||||||||||||||||||||||||||||||||||||||
| 467 | # | ||||||||||||||||||||||||||||||||||||||||||
| 468 | # Result: Nothing; aborts in case of trouble. Note, that this is *not* | ||||||||||||||||||||||||||||||||||||||||||
| 469 | # trapped and forces the server to exit. | ||||||||||||||||||||||||||||||||||||||||||
| 470 | # | ||||||||||||||||||||||||||||||||||||||||||
| 471 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | ▶ | sub Loop { | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Loop: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 477 | # | ||||||||||||||||||||||||||||||||||||||||||
| 478 | # Name: ChildFunc (Instance method) | ||||||||||||||||||||||||||||||||||||||||||
| 479 | # | ||||||||||||||||||||||||||||||||||||||||||
| 480 | # Purpose: If possible, spawn a child process which calls a given | ||||||||||||||||||||||||||||||||||||||||||
| 481 | # method. In server mode single the method is called | ||||||||||||||||||||||||||||||||||||||||||
| 482 | # directly. | ||||||||||||||||||||||||||||||||||||||||||
| 483 | # | ||||||||||||||||||||||||||||||||||||||||||
| 484 | # Inputs: $self - Instance | ||||||||||||||||||||||||||||||||||||||||||
| 485 | # $method - Method name | ||||||||||||||||||||||||||||||||||||||||||
| 486 | # @args - Method arguments | ||||||||||||||||||||||||||||||||||||||||||
| 487 | # | ||||||||||||||||||||||||||||||||||||||||||
| 488 | # Returns: Nothing; aborts in case of problems. | ||||||||||||||||||||||||||||||||||||||||||
| 489 | # | ||||||||||||||||||||||||||||||||||||||||||
| 490 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | sub ChildFunc { | ||||||||||||||||||||||||||||||||||||||||||
| 493 | 62 | ▶ | my ( $self, $method, @args ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ChildFunc: called CC=6 SCAR=23.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 494 | 62 | ▶ | if ( $self->{'mode'} eq 'single' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | $self->$method(@args); | |||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | elsif ( $self->{'mode'} eq 'ithreads' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 498 | my $startfunc = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 499 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | 0 | my $method = shift; | |||||||||||||||||||||||||||||||||||||||||
| 501 | 0 | $self->$method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | my $thr = threads->new( $startfunc, $self, $method, @args ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | or die "Failed to create a new thread: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | $thr->detach(); | |||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 507 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 62 | my $pid = fork(); | |||||||||||||||||||||||||||||||||||||||||
| 509 | 62 | ▶ | die "Cannot fork: $!" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | 62 | ▶ | return if $pid; # Parent | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 511 | 15 | $self->$method(@args); # Child | |||||||||||||||||||||||||||||||||||||||||
| 512 | 15 | exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 517 | # | ||||||||||||||||||||||||||||||||||||||||||
| 518 | # Name: Bind (Instance method) | ||||||||||||||||||||||||||||||||||||||||||
| 519 | # | ||||||||||||||||||||||||||||||||||||||||||
| 520 | # Purpose: Binds to a port; if successfull, it never returns. Instead | ||||||||||||||||||||||||||||||||||||||||||
| 521 | # it accepts connections. For any connection a new thread is | ||||||||||||||||||||||||||||||||||||||||||
| 522 | # created and the Accept method is executed. | ||||||||||||||||||||||||||||||||||||||||||
| 523 | # | ||||||||||||||||||||||||||||||||||||||||||
| 524 | # Inputs: $self - Server instance | ||||||||||||||||||||||||||||||||||||||||||
| 525 | # | ||||||||||||||||||||||||||||||||||||||||||
| 526 | # Result: Error message in case of failure | ||||||||||||||||||||||||||||||||||||||||||
| 527 | # | ||||||||||||||||||||||||||||||||||||||||||
| 528 | ############################################################################ | ||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | sub HandleChild { | ||||||||||||||||||||||||||||||||||||||||||
| 531 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
HandleChild: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 532 | 15 | $self->Debug("New child starting ($self)."); | |||||||||||||||||||||||||||||||||||||||||
| 533 | 15 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 534 | 15 | ▶ | if ( !$self->Accept() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | 0 | $self->Error('Refusing client'); | |||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 538 | 15 | $self->Debug('Accepting client'); | |||||||||||||||||||||||||||||||||||||||||
| 539 | 15 | $self->Run(); | |||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||
| 541 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 542 | 15 | ▶ | $self->Error("Child died: $@") if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 543 | 15 | $self->Debug("Child terminating."); | |||||||||||||||||||||||||||||||||||||||||
| 544 | 15 | $self->Close(); | |||||||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | sub SigChildHandler { | ||||||||||||||||||||||||||||||||||||||||||
| 548 | 17 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SigChildHandler: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | 17 | my $ref = shift; | |||||||||||||||||||||||||||||||||||||||||
| 550 | 17 | ▶ | if ( $self->{'mode'} eq 'fork' || $self->{'childs'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"mode"} eq "fork" or $self->{"childs"}
MC/DC0%0/2
$self->{"mode"} eq "fork" or $self->{"childs"}
$self->{"mode"} eq "fork"
$self->{"childs"}
Truth table33%1/3
A or B
A= $self->{"mode"} eq "fork"B= $self->{"childs"}
| |||||||||||||||||||||||||||||||||||||||||||
| 551 | return sub { | ||||||||||||||||||||||||||||||||||||||||||
| 552 | 1 | ▶ | while ( ( my $pid = waitpid( -1, POSIX::WNOHANG() ) ) > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=17.3
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | ▶ | $$ref = $pid if $ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 17 | }; | |||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||
| 557 | 0 | return undef; # Don't care for childs. | |||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | sub Bind ($) { | ||||||||||||||||||||||||||||||||||||||||||
| 561 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Bind: called CC=58 SCAR=73.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | 15 | my $fh; | |||||||||||||||||||||||||||||||||||||||||
| 563 | my $child_pid; | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | 15 | my $reaper = $self->SigChildHandler( \$child_pid ); | |||||||||||||||||||||||||||||||||||||||||
| 566 | 15 | ▶ | $SIG{'CHLD'} = $reaper if $reaper; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | 15 | ▶ | if ( !$self->{'socket'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 569 | 0 | ▶ | $self->{'proto'} ||= ( $self->{'localpath'} ) ? 'unix' : 'tcp'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"proto"} ||= $self->{"localpath"} ? "unix" : "tcp"
MC/DC0%0/2
$self->{"proto"} ||= $self->{"localpath"} ? "unix" : "tcp"
$self->{"proto"}
$self->{"localpath"} ? "unix" : "tcp"
Truth table0%0/3
A ||= B
A= $self->{"proto"}B= $self->{"localpath"} ? "unix" : "tcp"
| |||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | 0 | ▶ | if ( $self->{'proto'} eq 'unix' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 572 | 0 | ▶ | my $path = $self->{'localpath'} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 573 | or $self->Fatal('Missing option: localpath'); | ||||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | unlink $path; | |||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | ▶ | $self->Fatal("Can't remove stale Unix socket ($path): $!") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 576 | if -e $path; | ||||||||||||||||||||||||||||||||||||||||||
| 577 | 0 | my $old_umask = umask 0; | |||||||||||||||||||||||||||||||||||||||||
| 578 | $self->{'socket'} = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||||||||||||||||||
| 579 | 'Local' => $path, | ||||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | ▶ | 'Listen' => $self->{'listen'} || 10 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->{'listen'} || 10
MC/DC0%0/1
$self->{'listen'} || 10
$self->{'listen'}
Truth table0%0/2
A
A= $self->{'listen'}
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | ) or $self->Fatal("Cannot create Unix socket $path: $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | umask $old_umask; | |||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 584 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 585 | $self->{'socket'} = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||||
| 586 | 'LocalAddr' => $self->{'localaddr'}, | ||||||||||||||||||||||||||||||||||||||||||
| 587 | 'LocalPort' => $self->{'localport'}, | ||||||||||||||||||||||||||||||||||||||||||
| 588 | 'Proto' => $self->{'proto'} || 'tcp', | ||||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | ▶ | 'Listen' => $self->{'listen'} || 10, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$self->{'proto'} || 'tcp'
$self->{'listen'} || 10
MC/DC0%0/1
$self->{'proto'} || 'tcp'
$self->{'proto'}
MC/DC0%0/1
$self->{'listen'} || 10
$self->{'listen'}
Truth table0%0/2
A
A= $self->{'proto'}
Truth table0%0/2
A
A= $self->{'listen'}
| |||||||||||||||||||||||||||||||||||||||||||
| 590 | 'Reuse' => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 591 | ) or $self->Fatal("Cannot create socket: $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||
| 594 | 15 | $self->Log( 'notice', "Server starting" ); | |||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | 15 | ▶ | if ( ( my $pidfile = ( $self->{'pidfile'} || '' ) ) ne 'none' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{'pidfile'} || ''
MC/DC0%0/1
$self->{'pidfile'} || ''
$self->{'pidfile'}
Truth table50%1/2
A
A= $self->{'pidfile'}
| |||||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | $self->Debug("Writing PID to $pidfile"); | |||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | my $fh = Symbol::gensym(); | |||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | ▶ | $self->Fatal("Cannot write to $pidfile: $!") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
open OUT, ">$pidfile" and print OUT "${$}\n" and close OUT
open OUT, ">$pidfile" and print OUT "${$}\n"
MC/DC0%0/3
open OUT, ">$pidfile" and print OUT "${$}\n" and close OUT
open OUT, ">$pidfile"
print OUT "${$}\n"
close OUT
Truth table0%0/4
A and B and C
A= open OUT, ">$pidfile"B= print OUT "${$}\n"C= close OUT
| |||||||||||||||||||||||||||||||||||||||||||
| 600 | unless ( open( OUT, ">$pidfile" ) | ||||||||||||||||||||||||||||||||||||||||||
| 601 | and ( print OUT "$$\n" ) | ||||||||||||||||||||||||||||||||||||||||||
| 602 | and close(OUT) ); | ||||||||||||||||||||||||||||||||||||||||||
| 603 | } | ||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | 15 | ▶ | if ( my $dir = $self->{'chroot'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | $self->Debug("Changing root directory to $dir"); | |||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | ▶ | if ( !chroot($dir) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | $self->Fatal("Cannot change root directory to $dir: $!"); | |||||||||||||||||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||||||||||||||||
| 610 | } | ||||||||||||||||||||||||||||||||||||||||||
| 611 | 15 | ▶ | if ( my $group = $self->{'group'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 612 | 0 | $self->Debug("Changing GID to $group"); | |||||||||||||||||||||||||||||||||||||||||
| 613 | 0 | my $gid; | |||||||||||||||||||||||||||||||||||||||||
| 614 | 0 | ▶ | if ( $group !~ /^\d+$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | ▶ | if ( defined( my $gid = getgrnam($group) ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | $group = $gid; | |||||||||||||||||||||||||||||||||||||||||
| 617 | } | ||||||||||||||||||||||||||||||||||||||||||
| 618 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | $self->Fatal("Cannot determine gid of $group: $!"); | |||||||||||||||||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||||||||||||||||
| 622 | 0 | $( = ( $) = $group ); | |||||||||||||||||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||||||||||||||||
| 624 | 15 | ▶ | if ( my $user = $self->{'user'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 625 | 0 | $self->Debug("Changing UID to $user"); | |||||||||||||||||||||||||||||||||||||||||
| 626 | 0 | my $uid; | |||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | ▶ | if ( $user !~ /^\d+$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | ▶ | if ( defined( my $uid = getpwnam($user) ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 629 | 0 | $user = $uid; | |||||||||||||||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||||||||||||||||
| 631 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | $self->Fatal("Cannot determine uid of $user: $!"); | |||||||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||||||||||||||||
| 635 | 0 | $< = ( $> = $user ); | |||||||||||||||||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||
| 638 | 15 | ▶ | if ( $self->{'childs'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | my $pid; | |||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | 0 | my $childpids = $self->{'childpids'} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 642 | 0 | ▶ | for ( my $n = 0; $n < $self->{'childs'}; $n++ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 643 | 0 | $pid = fork(); | |||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | ▶ | die "Cannot fork: $!" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 645 | 0 | ▶ | if ( !$pid ) { #Child | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | $self->{'mode'} = 'single'; | |||||||||||||||||||||||||||||||||||||||||
| 647 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | # Parent | ||||||||||||||||||||||||||||||||||||||||||
| 651 | 0 | $childpids->{$pid} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||||||||||||||||
| 653 | 0 | ▶ | if ($pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||
| 655 | # Parent waits for childs in a loop, then exits ... | ||||||||||||||||||||||||||||||||||||||||||
| 656 | # We could also terminate the parent process, but | ||||||||||||||||||||||||||||||||||||||||||
| 657 | # if the parent is still running we can kill the | ||||||||||||||||||||||||||||||||||||||||||
| 658 | # whole group by killing the childs. | ||||||||||||||||||||||||||||||||||||||||||
| 659 | 0 | my $childpid; | |||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | $exit = 0; | |||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | ▶ | $SIG{'TERM'} = sub { die }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 662 | 0 | ▶ | $SIG{'INT'} = sub { die }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 663 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 664 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$childpid <= 0 or $Net::Daemon::exit or keys %$childpids == 0
$childpid <= 0 or $Net::Daemon::exit
MC/DC0%0/3
$childpid <= 0 or $Net::Daemon::exit or keys %$childpids == 0
$childpid <= 0
$Net::Daemon::exit
keys %$childpids == 0
Truth table0%0/4
A or B or C
A= $childpid <= 0B= $Net::Daemon::exitC= keys %$childpids == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 665 | 0 | $childpid = wait; | |||||||||||||||||||||||||||||||||||||||||
| 666 | 0 | delete $childpids->{$childpid}; | |||||||||||||||||||||||||||||||||||||||||
| 667 | 0 | $self->Debug("Child $childpid has exited"); | |||||||||||||||||||||||||||||||||||||||||
| 668 | } until ( $childpid <= 0 || $exit || keys(%$childpids) == 0 ); | ||||||||||||||||||||||||||||||||||||||||||
| 669 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 670 | 0 | my @pids = keys %{ $self->{'childpids'} }; | |||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | ▶ | if (@pids) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | $self->Debug( "kill TERM childs: " . join( ",", @pids ) ); | |||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | ▶ | kill 'TERM', @pids if @pids; # send a TERM to all childs | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||||||
| 675 | 0 | exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||
| 679 | 15 | ▶ | my $time = $self->{'loop-timeout'} ? ( time() + $self->{'loop-timeout'} ) : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||||||||||
| 681 | 15 | my $client; | |||||||||||||||||||||||||||||||||||||||||
| 682 | 15 | ▶ | while ( !$self->Done() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 683 | 62 | undef $child_pid; | |||||||||||||||||||||||||||||||||||||||||
| 684 | 62 | my $rin = ''; | |||||||||||||||||||||||||||||||||||||||||
| 685 | 62 | vec( $rin, $self->{'socket'}->fileno(), 1 ) = 1; | |||||||||||||||||||||||||||||||||||||||||
| 686 | 62 | my ( $rout, $t ); | |||||||||||||||||||||||||||||||||||||||||
| 687 | 62 | ▶ | if ($time) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 688 | 0 | my $tm = time(); | |||||||||||||||||||||||||||||||||||||||||
| 689 | 0 | $t = $time - $tm; | |||||||||||||||||||||||||||||||||||||||||
| 690 | 0 | ▶ | $t = 0 if $t < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 691 | 0 | $self->Debug("Loop time: time=$time now=$tm, t=$t"); | |||||||||||||||||||||||||||||||||||||||||
| 692 | } | ||||||||||||||||||||||||||||||||||||||||||
| 693 | 62 | my ($nfound) = select( $rout = $rin, undef, undef, $t ); | |||||||||||||||||||||||||||||||||||||||||
| 694 | 62 | ▶ | if ( $nfound < 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 695 | 0 | ▶ | if ( !$child_pid | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $child_pid and $! != 4 || !$self->{"catchint"}
$! != 4 or not $self->{"catchint"}
MC/DC0%0/3
not $child_pid and $! != 4 || !$self->{"catchint"}
not $child_pid
$! != 4
not $self->{"catchint"}
Truth table0%0/4
not(A) and B or not(C)
A= not $child_pidB= $! != 4C= not $self->{"catchint"}
| |||||||||||||||||||||||||||||||||||||||||||
| 696 | and ( $! != POSIX::EINTR() or !$self->{'catchint'} ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 697 | $self->Fatal( | ||||||||||||||||||||||||||||||||||||||||||
| 698 | "%s server failed to select(): %s", | ||||||||||||||||||||||||||||||||||||||||||
| 699 | 0 | ▶ | ref($self), $self->{'socket'}->error() || $! | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'socket'}->error || $!
MC/DC0%0/2
$self->{'socket'}->error || $!
$self->{'socket'}->error
$!
Truth table0%0/3
A || B
A= $self->{'socket'}->errorB= $!
| |||||||||||||||||||||||||||||||||||||||||||
| 700 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||||||||||||||||
| 703 | elsif ($nfound) { | ||||||||||||||||||||||||||||||||||||||||||
| 704 | 62 | my $client = $self->{'socket'}->accept(); | |||||||||||||||||||||||||||||||||||||||||
| 705 | 62 | ▶ | if ( !$client ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 706 | 0 | ▶ | if ( !$child_pid | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $child_pid and $! != 4 || !$self->{"catchint"}
$! != 4 or not $self->{"catchint"}
MC/DC0%0/3
not $child_pid and $! != 4 || !$self->{"catchint"}
not $child_pid
$! != 4
not $self->{"catchint"}
Truth table0%0/4
not(A) and B or not(C)
A= not $child_pidB= $! != 4C= not $self->{"catchint"}
| |||||||||||||||||||||||||||||||||||||||||||
| 707 | and ( $! != POSIX::EINTR() or !$self->{'catchint'} ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 708 | $self->Error( | ||||||||||||||||||||||||||||||||||||||||||
| 709 | "%s server failed to accept: %s", | ||||||||||||||||||||||||||||||||||||||||||
| 710 | 0 | ▶ | ref($self), $self->{'socket'}->error() || $! | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'socket'}->error || $!
MC/DC0%0/2
$self->{'socket'}->error || $!
$self->{'socket'}->error
$!
Truth table0%0/3
A || B
A= $self->{'socket'}->errorB= $!
| |||||||||||||||||||||||||||||||||||||||||||
| 711 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||||||||||||||||
| 714 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 715 | 62 | ▶ | if ( $self->{'debug'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 716 | my $from = | ||||||||||||||||||||||||||||||||||||||||||
| 717 | 3 | ▶ | $self->{'proto'} eq 'unix' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 718 | ? 'Unix socket' | ||||||||||||||||||||||||||||||||||||||||||
| 719 | : sprintf( | ||||||||||||||||||||||||||||||||||||||||||
| 720 | '%s, port %s', | ||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||
| 722 | # SE 19990917: display client data!! | ||||||||||||||||||||||||||||||||||||||||||
| 723 | $client->peerhost(), | ||||||||||||||||||||||||||||||||||||||||||
| 724 | $client->peerport() | ||||||||||||||||||||||||||||||||||||||||||
| 725 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 726 | 3 | $self->Debug("Connection from $from"); | |||||||||||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||||||||||
| 728 | 62 | my $sth = $self->Clone($client); | |||||||||||||||||||||||||||||||||||||||||
| 729 | 62 | $self->Debug("Child clone: $sth\n"); | |||||||||||||||||||||||||||||||||||||||||
| 730 | 62 | ▶ | $sth->ChildFunc('HandleChild') if $sth; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 731 | 47 | ▶ | if ( $self->{'mode'} eq 'fork' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 732 | 47 | $self->ServClose($client); | |||||||||||||||||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||||||||||||||||
| 736 | 47 | ▶ | if ($time) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 737 | 0 | my $t = time(); | |||||||||||||||||||||||||||||||||||||||||
| 738 | 0 | ▶ | if ( $t >= $time ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 739 | 0 | $time = $t; | |||||||||||||||||||||||||||||||||||||||||
| 740 | 0 | ▶ | if ( $self->{'loop-child'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 741 | 0 | $self->ChildFunc('Loop'); | |||||||||||||||||||||||||||||||||||||||||
| 742 | } | ||||||||||||||||||||||||||||||||||||||||||
| 743 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 744 | 0 | $self->Loop(); | |||||||||||||||||||||||||||||||||||||||||
| 745 | } | ||||||||||||||||||||||||||||||||||||||||||
| 746 | 0 | $time += $self->{'loop-timeout'}; | |||||||||||||||||||||||||||||||||||||||||
| 747 | } | ||||||||||||||||||||||||||||||||||||||||||
| 748 | } | ||||||||||||||||||||||||||||||||||||||||||
| 749 | } | ||||||||||||||||||||||||||||||||||||||||||
| 750 | 0 | $self->Log( 'notice', "%s server terminating", ref($self) ); | |||||||||||||||||||||||||||||||||||||||||
| 751 | } | ||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||
| 753 | sub Close { | ||||||||||||||||||||||||||||||||||||||||||
| 754 | 15 | ▶ | my $socket = shift->{'socket'}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Close: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 755 | 15 | ▶ | $socket->close() if $socket; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||
| 758 | sub ServClose { | ||||||||||||||||||||||||||||||||||||||||||
| 759 | 47 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ServClose: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 760 | 47 | my $socket = shift; | |||||||||||||||||||||||||||||||||||||||||
| 761 | 47 | ▶ | $socket->close() if $socket; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||
| 764 | 56 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||
| 766 | __END__ | ||||||||||||||||||||||||||||||||||||||||||