Daemon.pm

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