Daemon.pm

filter: Statementstmt 56.7% 169 / 298 Branchbran 36.5% 68 / 186 Conditioncond 16.6% 11 / 66 MC/DCmcdc 2.7% 1 / 37 Subroutinesub 78.1% 25 / 32 Podpod 0.0% 0 / 17 totaltotal 43.0% 274 / 636 CC 117 SCAR 64.8
CC
117
Cov
45%
Bind
1611.4
new
86.0
Accept
21.7
CRAP 649.3
↑ 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 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
BranchTrueFalseTotal
$^V ge v5.10.0 && $Net::Daemon::Config{'useithreads'} && !$INC{'forks.pm'} ? : 0 56 56
Condition33%2/6
$^V ge v5.10.0 and $Net::Daemon::Config{"useithreads"} and not $INC{"forks.pm"}
!ll&&!rl&&r
5600
$^V ge v5.10.0 and $Net::Daemon::Config{"useithreads"}
!ll&&!rl&&r
0560
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 56 ▶if ($use_ithreads) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_ithreads) 0 56 56
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 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
BranchTrueFalseTotal
if $o->{"description"} 0 0 0
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
BranchTrueFalseTotal
unless (-f $file) 0 1 1
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
BranchTrueFalseTotal
if ($@) 0 1 1
218 0 $self->Fatal("Error while processing config file $file: $@");
219 }
220 1 ▶ if ( !$copts || ref($copts) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $copts or ref $copts ne "HASH") 0 1 1
Condition33%1/3
not $copts or ref $copts ne "HASH"
l!l&&r!l&&!r
001
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"
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (() = each %$copts) 3 1 4
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
BranchTrueFalseTotal
$attr ? : 17 0 17
247 17 ▶ bless( $self, ( ref($class) || $class ) );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0170
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
248  
249 17 ▶ my $options = ( $self->{'options'} ||= {} );
Condition50%1/2
$self->{"options"} ||= {}
l!l
017
MC/DC0%0/1
$self->{"options"} ||= {}
$self->{"options"}
Truth table50%1/2
A
A= $self->{"options"}
Aresult
11
00
250 17 ▶ $self->{'args'} ||= [];
Condition50%1/2
$self->{"args"} ||= []
l!l
017
MC/DC0%0/1
$self->{"args"} ||= []
$self->{"args"}
Truth table50%1/2
A
A= $self->{"args"}
Aresult
11
00
251 17 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 17 0 17
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
BranchTrueFalseTotal
unless (&Getopt::Long::GetOptions($options, @optList)) 0 17 17
256 0 $self->Usage();
257 }
258 17 @{ $self->{'args'} } = @ARGV;
259  
260 17 ▶ if ( $options->{'help'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"help"}) 0 17 17
261 0 $self->Usage();
262 }
263 17 ▶ if ( $options->{'version'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"version"}) 0 17 17
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'}
l!l&&r!l&&!r
1016
MC/DC50%1/2
$options->{'configfile'} || $self->{'configfile'}
$options->{'configfile'} $self->{'configfile'}
Truth table66%2/3
A || B
A= $options->{'configfile'}B= $self->{'configfile'}
ABresult
1X1
011
000
270 17 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 1 16 17
271 1 $self->ReadConfigFile( $file, $options, $args );
272 }
273 17 ▶ while ( my ( $var, $val ) = each %$options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$options) 27 17 44
274 27 $self->{$var} = $val;
275 }
276  
277 17 ▶ if ( $self->{'childs'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'childs'}) { } 0 17 17
elsif (not defined $self->{'mode'}) { } 0 17 17
278 0 $self->{'mode'} = 'single';
279 }
280 elsif ( !defined( $self->{'mode'} ) ) {
281 0 ▶ if ( eval { require threads } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require threads } }) { } 0 0 0
282 0 $self->{'mode'} = 'ithreads';
283 }
284 else {
285 0 my $fork = 0;
286 0 ▶ if ( $^O ne "MSWin32" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 0 0 0
287 0 my $pid = eval { fork() };
288 0 ▶ if ( defined($pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) 0 0 0
289 0 ▶ if ( !$pid ) { exit; } # Child
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
290 0 $fork = 1;
291 0 wait;
292 }
293 }
294 0 ▶ if ($fork) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fork) { } 0 0 0
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
BranchTrueFalseTotal
if ($self->{'mode'} eq 'ithreads') { } 0 17 17
elsif ($self->{'mode'} eq 'fork') { } 15 2 17
elsif ($self->{'mode'} eq 'single') { } 2 0 2
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
BranchTrueFalseTotal
unless exists $self->{"catchint"} 17 0 17
320 17 $self->Debug("Server starting in operation mode $self->{'mode'}");
321 17 ▶ if ( $self->{'childs'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"childs"}) 0 17 17
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
BranchTrueFalseTotal
$self->{'proto'} eq 'unix' ? : 3 12 15
362 "%s, port %s",
363 $socket->peerhost(), $socket->peerport()
364 );
365  
366 # Host based authorization
367 15 ▶ if ( $self->{'clients'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"clients"}) 1 14 15
368 1 my ( $name, $aliases, $addrtype, $length, @addrs );
369 1 ▶ if ( $self->{'proto'} eq 'unix' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'proto'} eq 'unix') { } 1 0 1
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
BranchTrueFalseTotal
@addrs ? : 1 0 1
383 : $socket->peerhost();
384 1 ▶ push( @patterns, $name ) if ($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name 1 0 1
385 1 ▶ push( @patterns, split( / /, $aliases ) ) if $aliases;
Branch: 1 branch
BranchTrueFalseTotal
if $aliases 0 1 1
386  
387 1 my $found;
388 1 OUTER: foreach my $client (@$clients) {
389 1 ▶ if ( !$client->{'mask'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client->{"mask"}) 0 1 1
390 0 $found = $client;
391 0 last;
392 }
393 1 ▶ my $masks = ref( $client->{'mask'} ) ? $client->{'mask'} : [ $client->{'mask'} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $client->{'mask'} ? : 0 1 1
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
BranchTrueFalseTotal
if $self->{"mode"} eq "threads" 0 1 1
402 1 foreach my $mask (@$masks) {
403 1 foreach my $alias (@patterns) {
404 1 ▶ if ( $alias =~ /$mask/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias =~ /$mask/) 1 0 1
405 1 $found = $client;
406 1 last OUTER;
407 }
408 }
409 }
410 }
411  
412 1 ▶ if ( !$found || !$found->{'accept'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found and $found->{"accept"}) 0 1 1
Condition33%1/3
$found and $found->{"accept"}
!ll&&!rl&&r
001
MC/DC0%0/2
$found and $found->{"accept"}
$found $found->{"accept"}
Truth table33%1/3
A and B
A= $foundB= $found->{"accept"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if @_ 0 62 62
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
BranchTrueFalseTotal
if ($self->{'mode'} eq 'single') { } 0 62 62
elsif ($self->{'mode'} eq 'ithreads') { } 0 62 62
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
BranchTrueFalseTotal
my $thr = "threads"->new($startfunc, $self, $method, @args) or die "Failed to create a new thread: $!" 0 0 0
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
BranchTrueFalseTotal
unless defined $pid 0 62 62
510 62 ▶ return if $pid; # Parent
Branch: 1 branch
BranchTrueFalseTotal
if $pid 47 15 62
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
BranchTrueFalseTotal
if (not $self->Accept) { } 0 15 15
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
BranchTrueFalseTotal
if $@ 0 15 15
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
BranchTrueFalseTotal
if ($self->{"mode"} eq "fork" or $self->{"childs"}) 17 0 17
Condition33%1/3
$self->{"mode"} eq "fork" or $self->{"childs"}
l!l&&r!l&&!r
1700
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
551 return sub {
552 1 ▶ while ( ( my $pid = waitpid( -1, POSIX::WNOHANG() ) ) > 0 ) {
Subroutine __ANON__: called CC=3 SCAR=17.3
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pid = waitpid(-1, 1)) > 0) 0 1 1
553 0 ▶ $$ref = $pid if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 0 0 0
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
BranchTrueFalseTotal
if $reaper 15 0 15
567  
568 15 ▶ if ( !$self->{'socket'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"socket"}) 0 15 15
569 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
570  
571 0 ▶ if ( $self->{'proto'} eq 'unix' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'proto'} eq 'unix') { } 0 0 0
572 0 ▶ my $path = $self->{'localpath'}
Branch: 1 branch
BranchTrueFalseTotal
my $path = $self->{"localpath"} or $self->Fatal("Missing option: localpath") 0 0 0
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
BranchTrueFalseTotal
if -e $path 0 0 0
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
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
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
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
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
BranchTrueFalseTotal
if ((my $pidfile = $self->{"pidfile"} || "") ne "none") 0 15 15
Condition50%1/2
$self->{'pidfile'} || ''
l!l
150
MC/DC0%0/1
$self->{'pidfile'} || ''
$self->{'pidfile'}
Truth table50%1/2
A
A= $self->{'pidfile'}
Aresult
11
00
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
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
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
BranchTrueFalseTotal
if (my $dir = $self->{"chroot"}) 0 15 15
606 0 $self->Debug("Changing root directory to $dir");
607 0 ▶ if ( !chroot($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chroot $dir) 0 0 0
608 0 $self->Fatal("Cannot change root directory to $dir: $!");
609 }
610 }
611 15 ▶ if ( my $group = $self->{'group'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $self->{"group"}) 0 15 15
612 0 $self->Debug("Changing GID to $group");
613 0 my $gid;
614 0 ▶ if ( $group !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($group =~ /^\d+$/) 0 0 0
615 0 ▶ if ( defined( my $gid = getgrnam($group) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $gid = getgrnam $group)) { } 0 0 0
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
BranchTrueFalseTotal
if (my $user = $self->{"user"}) 0 15 15
625 0 $self->Debug("Changing UID to $user");
626 0 my $uid;
627 0 ▶ if ( $user !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user =~ /^\d+$/) 0 0 0
628 0 ▶ if ( defined( my $uid = getpwnam($user) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $uid = getpwnam $user)) { } 0 0 0
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
BranchTrueFalseTotal
if ($self->{"childs"}) 0 15 15
639 0 my $pid;
640  
641 0 my $childpids = $self->{'childpids'} = {};
642 0 ▶ for ( my $n = 0; $n < $self->{'childs'}; $n++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $self->{"childs"}) 0 0 0
643 0 $pid = fork();
644 0 ▶ die "Cannot fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
645 0 ▶ if ( !$pid ) { #Child
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
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
BranchTrueFalseTotal
if ($pid) 0 0 0
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
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
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
BranchTrueFalseTotal
if (@pids) 0 0 0
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
BranchTrueFalseTotal
if @pids 0 0 0
674 }
675 0 exit(0);
676 }
677 }
678  
679 15 ▶ my $time = $self->{'loop-timeout'} ? ( time() + $self->{'loop-timeout'} ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'loop-timeout'} ? : 0 15 15
680  
681 15 my $client;
682 15 ▶ while ( !$self->Done() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->Done) 62 0 62
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
BranchTrueFalseTotal
if ($time) 0 62 62
688 0 my $tm = time();
689 0 $t = $time - $tm;
690 0 ▶ $t = 0 if $t < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t < 0 0 0 0
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
BranchTrueFalseTotal
if ($nfound < 0) { } 0 62 62
elsif ($nfound) { } 62 0 62
695 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
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 || $!
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
700 );
701 }
702 }
703 elsif ($nfound) {
704 62 my $client = $self->{'socket'}->accept();
705 62 ▶ if ( !$client ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $client) { } 0 62 62
706 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
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 || $!
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
711 );
712 }
713 }
714 else {
715 62 ▶ if ( $self->{'debug'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 3 59 62
716 my $from =
717 3 ▶ $self->{'proto'} eq 'unix'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'proto'} eq 'unix' ? : 0 3 3
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
BranchTrueFalseTotal
if $sth 62 0 62
731 47 ▶ if ( $self->{'mode'} eq 'fork' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode"} eq "fork") 47 0 47
732 47 $self->ServClose($client);
733 }
734 }
735 }
736 47 ▶ if ($time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time) 0 47 47
737 0 my $t = time();
738 0 ▶ if ( $t >= $time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t >= $time) 0 0 0
739 0 $time = $t;
740 0 ▶ if ( $self->{'loop-child'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'loop-child'}) { } 0 0 0
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
BranchTrueFalseTotal
if $socket 15 0 15
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
BranchTrueFalseTotal
if $socket 47 0 47
762 }
763  
764 56 1;
765  
766 __END__
↑ Summary Daemon/Test.pm »