File: Daemon.pm

package info (click to toggle)
libnet-daemon-perl 0.36-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 176 kB
  • ctags: 38
  • sloc: perl: 731; makefile: 48
file content (1253 lines) | stat: -rw-r--r-- 36,927 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
# -*- perl -*-
#
#   $Id: Daemon.pm,v 1.3 1999/09/26 14:50:12 joe Exp $
#
#   Net::Daemon - Base class for implementing TCP/IP daemons
#
#   Copyright (C) 1998, Jochen Wiedmann
#                       Am Eisteich 9
#                       72555 Metzingen
#                       Germany
#
#                       Phone: +49 7123 14887
#                       Email: joe@ispsoft.de
#
#   All rights reserved.
#
#   You may distribute this package under the terms of either the GNU
#   General Public License or the Artistic License, as specified in the
#   Perl README file.
#
############################################################################

require 5.004;
use strict;

use Getopt::Long ();
use Symbol ();
use IO::Socket ();
use Config ();
use Net::Daemon::Log ();
use POSIX ();


package Net::Daemon;

$Net::Daemon::VERSION = '0.36';
@Net::Daemon::ISA = qw(Net::Daemon::Log);

#
#   Regexps aren't thread safe, as of 5.00502 :-( (See the test script
#   regexp-threads.)
#
$Net::Daemon::RegExpLock = 1;

use vars qw($exit);

############################################################################
#
#   Name:    Options (Class method)
#
#   Purpose: Returns a hash ref of command line options
#
#   Inputs:  $class - This class
#
#   Result:  Options array; any option is represented by a hash ref;
#            used keys are 'template', a string suitable for describing
#            the option to Getopt::Long::GetOptions and 'description',
#            a string for the Usage message
#
############################################################################

sub Options ($) {
    { 'catchint' => { 'template' => 'catchint!',
		      'description' => '--nocatchint            '
		          . "Try to catch interrupts when calling system\n"
		          . '                        '
		          . 'functions like bind(), recv()), ...'
		    },
      'childs' => { 'template' => 'childs=i',
		    'description' =>  '--childs <num>          '
			. 'Set number of preforked childs, implies mode=single.' },
      'chroot' => { 'template' => 'chroot=s',
		    'description' =>  '--chroot <dir>          '
			. 'Change rootdir to given after binding to port.' },
      'configfile' => { 'template' => 'configfile=s',
			'description' =>  '--configfile <file>     '
			    . 'Read options from config file <file>.' },
      'debug' => { 'template' => 'debug',
		   'description' =>  '--debug                 '
		       . 'Turn debugging mode on'},
      'facility' => { 'template' => 'facility=s',
		      'description' => '--facility <facility>   '
			  . 'Syslog facility; defaults to \'daemon\'' },
      'group' => { 'template' => 'group=s',
		   'description' => '--group <gid>           '
		       . 'Change gid to given group after binding to port.' },
      'help' => { 'template' => 'help',
		  'description' => '--help                  '
		      . 'Print this help message' },
      'localaddr' => { 'template' => 'localaddr=s',
		       'description' => '--localaddr <ip>        '
			   . 'IP number to bind to; defaults to INADDR_ANY' },
      'localpath' => { 'template' => 'localpath=s',
		       'description' => '--localpath <path>      '
		           . 'UNIX socket domain path to bind to' },
      'localport' => { 'template' => 'localport=s',
		       'description' => '--localport <port>      '
			   . 'Port number to bind to' },
      'logfile' => { 'template' => 'logfile=s',
		     'description' => '--logfile <file>        '
		         . 'Force logging to <file>' },
      'loop-child' => { 'template' => 'loop-child',
		       'description' => '--loop-child            '
			      . 'Create a child process for loops' },
      'loop-timeout' => { 'template' => 'loop-timeout=f',
		       'description' => '--loop-timeout <secs>   '
			      . 'Looping mode, <secs> seconds per loop' },
      'mode' => { 'template' => 'mode=s',
		  'description' => '--mode <mode>           '
		      . 'Operation mode (threads, fork or single)' },
      'pidfile' => { 'template' => 'pidfile=s',
		     'description' => '--pidfile <file>        '
			 . 'Use <file> as PID file' },
      'proto' => { 'template' => 'proto=s',
		   'description' => '--proto <protocol>        '
		   . 'transport layer protocol: tcp (default) or unix' },
      'user' => { 'template' => 'user=s',
		  'description' => '--user <user>           '
		      . 'Change uid to given user after binding to port.' },
      'version' => { 'template' => 'version',
		     'description' => '--version               '
			 . 'Print version number and exit' } }
}


############################################################################
#
#   Name:    Version (Class method)
#
#   Purpose: Returns version string
#
#   Inputs:  $class - This class
#
#   Result:  Version string; suitable for printed by "--version"
#
############################################################################

sub Version ($) {
    "Net::Daemon server, Copyright (C) 1998, Jochen Wiedmann";
}


############################################################################
#
#   Name:    Usage (Class method)
#
#   Purpose: Prints usage message
#
#   Inputs:  $class - This class
#
#   Result:  Nothing; aborts with error status
#
############################################################################

sub Usage ($) {
    my($class) = shift;
    my($options) = $class->Options();
    my(@options) = sort (keys %$options);

    print STDERR "Usage: $0 <options>\n\nPossible options are:\n\n";
    my($key);
    foreach $key (sort (keys %$options)) {
	my($o) = $options->{$key};
	print STDERR "  ", $o->{'description'}, "\n" if $o->{'description'};
    }
    print STDERR "\n", $class->Version(), "\n";
    exit(1);
}



############################################################################
#
#   Name:    ReadConfigFile (Instance method)
#
#   Purpose: Reads the config file.
#
#   Inputs:  $self - Instance
#            $file - config file name
#            $options - Hash of command line options; these are not
#                really for being processed by this method. We pass
#                it just in case. The new() method will process them
#                at a later time.
#            $args - Array ref of other command line options.
#
############################################################################

sub ReadConfigFile {
    my($self, $file, $options, $args) = @_;
    if (! -f $file) {
	$self->Fatal("No such config file: $file");
    }
    my $copts = do $file;
    if ($@) {
	$self->Fatal("Error while processing config file $file: $@");
    }
    if (!$copts || ref($copts) ne 'HASH') {
	$self->Fatal("Config file $file did not return a hash ref.");
    }
    # Override current configuration with config file options.
    while (my($var, $val) = each %$copts) {
	$self->{$var} = $val;
    }
}


############################################################################
#
#   Name:    new (Class method)
#
#   Purpose: Constructor
#
#   Inputs:  $class - This class
#            $attr - Hash ref of attributes
#            $args - Array ref of command line arguments
#
#   Result:  Server object for success, error message otherwise
#
############################################################################

sub new ($$;$) {
    my($class, $attr, $args) = @_;
    my($self) = $attr ? \%$attr : {};
    bless($self, (ref($class) || $class));

    my $options = ($self->{'options'} ||= {});
    $self->{'args'} ||= [];
    if ($args) {
	my @optList = map { $_->{'template'} } values(%{$class->Options()});

	local @ARGV = @$args;
	if (!Getopt::Long::GetOptions($options, @optList)) {
	    $self->Usage();
	}
	@{$self->{'args'}} = @ARGV;

	if ($options->{'help'}) {
	    $self->Usage();
	}
	if ($options->{'version'}) {
	    print STDERR $self->Version(), "\n";
	    exit 1;
	}
    }

    my $file = $options->{'configfile'}  ||  $self->{'configfile'};
    if ($file) {
	$self->ReadConfigFile($file, $options, $args);
    }
    while (my($var, $val) = each %$options) {
	$self->{$var} = $val;
    }

    if ($self->{'childs'}) {
	$self->{'mode'} = 'single';
    } elsif (!defined($self->{'mode'})) {
	if (eval { require Thread }) {
	    $self->{'mode'} = 'threads';
	} else {
	    my $fork = 0;
	    if ($^O ne "MSWin32") {
		my $pid = eval { fork() };
		if (defined($pid)) {
		    if (!$pid) { exit; } # Child
		    $fork = 1;
		    wait;
	        }
	    }
	    if ($fork) {
		$self->{'mode'} = 'fork';
	    } else {
		$self->{'mode'} = 'single';
	    }
	}
    }

    if ($self->{'mode'} eq 'threads') {
	require Thread;
    } elsif ($self->{'mode'} eq 'fork') {
	# Initialize forking mode ...
    } elsif ($self->{'mode'} eq 'single') {
	# Initialize single mode ...
    } else {
	$self->Fatal("Unknown operation mode: $self->{'mode'}");
    }
    $self->{'catchint'} = 1 unless exists($self->{'catchint'});
    $self->Debug("Server starting in operation mode $self->{'mode'}");
    if ($self->{'childs'}) {
	$self->Debug("Preforking $self->{'childs'} child processes ...");
    }

    $self;
}

sub Clone ($$) {
    my($proto, $client) = @_;
    my $self = { %$proto };
    $self->{'socket'} = $client;
    $self->{'parent'} = $proto;
    bless($self, ref($proto));
    $self;
}


############################################################################
#
#   Name:    Accept (Instance method)
#
#   Purpose: Called for authentication purposes
#
#   Inputs:  $self - Server instance
#
#   Result:  TRUE, if the client has successfully authorized, FALSE
#            otherwise.
#
############################################################################

sub Accept ($) {
    my $self = shift;
    my $socket = $self->{'socket'};
    my $clients = $self->{'clients'};
    my $from = $self->{'proto'} eq 'unix' ?
	"Unix socket" : sprintf("%s, port %s",
				$socket->peerhost(), $socket->peerport());

    # Host based authorization
    if ($self->{'clients'}) {
	my ($name, $aliases, $addrtype, $length, @addrs);
	if ($self->{'proto'} eq 'unix') {
	    ($name, $aliases, $addrtype, $length, @addrs) =
		('localhost', '', Socket::AF_INET(),
		 length(Socket::IN_ADDR_ANY()),
			Socket::inet_aton('127.0.0.1'));
	} else {
	    ($name, $aliases, $addrtype, $length, @addrs) =
		gethostbyaddr($socket->peeraddr(), Socket::AF_INET());
	}
	my @patterns = @addrs ?
	    map { Socket::inet_ntoa($_) } @addrs  :
	    $socket->peerhost();
	push(@patterns, $name)			if ($name);
	push(@patterns, split(/ /, $aliases))	if $aliases;

	my $found;
	OUTER: foreach my $client (@$clients) {
	    if (!$client->{'mask'}) {
		$found = $client;
		last;
	    }
	    my $masks = ref($client->{'mask'}) ?
		$client->{'mask'} : [ $client->{'mask'} ];

	    #
	    # Regular expressions aren't thread safe, as of
	    # 5.00502 :-(
	    #
	    my $lock;
	    $lock = lock($Net::Daemon::RegExpLock)
		if ($self->{'mode'} eq 'threads');
	    foreach my $mask (@$masks) {
		foreach my $alias (@patterns) {
		    if ($alias =~ /$mask/) {
			$found = $client;
			last OUTER;
		    }
		}
	    }
	}

	if (!$found  ||  !$found->{'accept'}) {
	    $self->Error("Access not permitted from $from");
	    return 0;
	}
	$self->{'client'} = $found;
    }

    $self->Debug("Accepting client from $from");
    1;
}


############################################################################
#
#   Name:    Run (Instance method)
#
#   Purpose: Does the real work
#
#   Inputs:  $self - Server instance
#
#   Result:  Nothing; returning will make the connection to be closed
#
############################################################################

sub Run ($) {
}


############################################################################
#
#   Name:    Done (Instance method)
#
#   Purpose: Called by the server before doing an accept(); a TRUE
#            value makes the server terminate.
#
#   Inputs:  $self - Server instance
#
#   Result:  TRUE or FALSE
#
#   Bugs:    Doesn't work in forking mode.
#
############################################################################

sub Done ($;$) {
    my $self = shift;
    $self->{'done'} = shift if @_;
    $self->{'done'}
}


############################################################################
#
#   Name:    Loop (Instance method)
#
#   Purpose: If $self->{'loop-timeout'} option is set, then this method
#            will be called every "loop-timeout" seconds.
#
#   Inputs:  $self - Server instance
#
#   Result:  Nothing; aborts in case of trouble. Note, that this is *not*
#            trapped and forces the server to exit.
#
############################################################################

sub Loop {
}


############################################################################
#
#   Name:    ChildFunc (Instance method)
#
#   Purpose: If possible, spawn a child process which calls a given
#	     method. In server mode single the method is called
#            directly.
#
#   Inputs:  $self - Instance
#            $method - Method name
#            @args - Method arguments
#
#   Returns: Nothing; aborts in case of problems.
#
############################################################################

sub ChildFunc {
    my($self, $method, @args) = @_;
    if ($self->{'mode'} eq 'single') {
	$self->$method(@args);
    } elsif ($self->{'mode'} eq 'threads') {
	my $startfunc = sub {
	    my $self = shift;
	    my $method = shift;
	    $self->$method(@_)
	};
	Thread->new($startfunc, $self, $method, @args)
	    or die "Failed to create a new thread: $!";
    } else {
	my $pid = fork();
	die "Cannot fork: $!" unless defined $pid;
	return if $pid;        # Parent
	$self->$method(@args); # Child
	exit(0);
    }
}


############################################################################
#
#   Name:    Bind (Instance method)
#
#   Purpose: Binds to a port; if successfull, it never returns. Instead
#            it accepts connections. For any connection a new thread is
#            created and the Accept method is executed.
#
#   Inputs:  $self - Server instance
#
#   Result:  Error message in case of failure
#
############################################################################

sub HandleChild {
    my $self = shift;
    $self->Debug("New child starting ($self).");
    eval {
	if (!$self->Accept()) {
	    $self->Error('Refusing client');
	} else {
	    $self->Debug('Accepting client');
	    $self->Run();
	}
    };
    $self->Error("Child died: $@") if $@;
    $self->Debug("Child terminating.");
    $self->Close();
};

sub SigChildHandler {
    my $self = shift; my $ref = shift;
    return 'IGNORE' if $self->{'mode'} eq 'fork' || $self->{'childs'};
    return undef; # Don't care for childs.
}

sub Bind ($) {
    my $self = shift;
    my $fh;
    my $child_pid;

    my $reaper = $self->SigChildHandler(\$child_pid);
    $SIG{'CHLD'} = $reaper if $reaper;

    if (!$self->{'socket'}) {
	$self->{'proto'} ||= ($self->{'localpath'}) ? 'unix' : 'tcp';

	if ($self->{'proto'} eq 'unix') {
	    my $path = $self->{'localpath'}
		or $self->Fatal('Missing option: localpath');
	    unlink $path;
	    $self->Fatal("Can't remove stale Unix socket ($path): $!")
		if -e $path;
	    my $old_umask = umask 0;
	    $self->{'socket'} =
		IO::Socket::UNIX->new('Local' => $path,
				      'Listen' => $self->{'listen'} || 10)
		      or $self->Fatal("Cannot create Unix socket $path: $!");
	    umask $old_umask;
	} else {
	    $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: $!");
	}
    }
    $self->Log('notice', "Server starting");

    if ((my $pidfile = ($self->{'pidfile'} || '')) ne 'none') {
	$self->Debug("Writing PID to $pidfile");
	my $fh = Symbol::gensym();
	$self->Fatal("Cannot write to $pidfile: $!")
	    unless (open (OUT, ">$pidfile")
		    and (print OUT "$$\n")
		    and close(OUT));
    }

    if (my $dir = $self->{'chroot'}) {
	$self->Debug("Changing root directory to $dir");
	if (!chroot($dir)) {
	    $self->Fatal("Cannot change root directory to $dir: $!");
	}
    }
    if (my $group = $self->{'group'}) {
	$self->Debug("Changing GID to $group");
	my $gid;
	if ($group !~ /^\d+$/) {
	    if (my $gid = getgrnam($group)) {
		$group = $gid;
	    } else {
		$self->Fatal("Cannot determine gid of $group: $!");
	    }
	}
	$( = ($) = $group);
    }
    if (my $user = $self->{'user'}) {
	$self->Debug("Changing UID to $user");
	my $uid;
	if ($user !~ /^\d+$/) {
	    if (my $uid = getpwnam($user)) {
		$user = $uid;
	    } else {
		$self->Fatal("Cannot determine uid of $user: $!");
	    }
	}
	$< = ($> = $user);
    }

    if ($self->{'childs'}) {
      my $pid;

      my $childpids = $self->{'childpids'} = {};
      for (my $n = 0; $n < $self->{'childs'}; $n++) {
	$pid = fork();
	die "Cannot fork: $!" unless defined $pid;
	if (!$pid) { #Child
	  $self->{'mode'} = 'single';
	  last;
	}
	# Parent
	$childpids->{$pid} = 1;
      }
      if ($pid) {
	# Parent waits for childs in a loop, then exits ...
	# We could also terminate the parent process, but
	# if the parent is still running we can kill the
	# whole group by killing the childs.
	my $childpid;
	$exit = 0;
	$SIG{'TERM'} = sub { die };
	$SIG{'INT'}  = sub { die };
	eval {
	  do {
	    $childpid = wait;
	    delete $childpids->{$childpid};
	    $self->Debug("Child $childpid has exited");
	  } until ($childpid <= 0 || $exit || keys(%$childpids) == 0);
	};
	my @pids = keys %{$self -> {'childpids'}};
	if (@pids) {
	  $self->Debug("kill TERM childs: " . join(",", @pids));
	  kill 'TERM', @pids if @pids ; # send a TERM to all childs
	}
	exit (0);
      }
    }

    my $time = $self->{'loop-timeout'} ?
	(time() + $self->{'loop-timeout'}) : 0;

    my $client;
    while (!$self->Done()) {
	undef $child_pid;
	my $rin = '';
	vec($rin,$self->{'socket'}->fileno(),1) = 1;
	my($rout, $t);
	if ($time) {
	    my $tm = time();
	    $t = $time - $tm;
	    $t = 0 if $t < 0;
	    $self->Debug("Loop time: time=$time now=$tm, t=$t");
	}
	my($nfound) = select($rout=$rin, undef, undef, $t);
	if ($nfound < 0) {
	    if (!$child_pid  and
		($! != POSIX::EINTR() or !$self->{'catchint'})) {
		$self->Fatal("%s server failed to select(): %s",
			     ref($self), $self->{'socket'}->error() || $!);
	    }
	} elsif ($nfound) {
	    my $client = $self->{'socket'}->accept();
	    if (!$client) {
		if (!$child_pid  and
		    ($! != POSIX::EINTR() or !$self->{'catchint'})) {
		    $self->Fatal("%s server failed to accept: %s",
				 ref($self), $self->{'socket'}->error() || $!);
		}
	    } else {
		if ($self->{'debug'}) {
		    my $from = $self->{'proto'} eq 'unix' ?
			'Unix socket' :
			sprintf('%s, port %s',
				# SE 19990917: display client data!!
				$client->peerhost(),
				$client->peerport());
		    $self->Debug("Connection from $from");
		}
		my $sth = $self->Clone($client);
		$self->Debug("Child clone: $sth\n");
		$sth->ChildFunc('HandleChild') if $sth;
	    }
	}
	if ($time) {
	    my $t = time();
	    if ($t >= $time) {
		$time = $t;
		if ($self->{'loop-child'}) {
		    $self->ChildFunc('Loop');
		} else {
		    $self->Loop();
		}
		$time += $self->{'loop-timeout'};
	    }
	}
    }
    $self->Log('notice', "%s server terminating", ref($self));
}

sub Close {
    my $socket = shift->{'socket'};
    $socket->close() if $socket;
}


1;

__END__

=head1 NAME

Net::Daemon - Perl extension for portable daemons


=head1 SYNOPSIS

  # Create a subclass of Net::Daemon
  require Net::Daemon;
  package MyDaemon;
  @MyDaemon::ISA = qw(Net::Daemon);

  sub Run ($) {
    # This function does the real work; it is invoked whenever a
    # new connection is made.
  }


=head1 WARNING

THIS IS ALPHA SOFTWARE. It is *only* 'Alpha' because the interface (API)
is not finalised. The Alpha status does not reflect code quality or
stability.


=head1 DESCRIPTION

Net::Daemon is an abstract base class for implementing portable server
applications in a very simple way. The module is designed for Perl 5.005
and threads, but can work with fork() and Perl 5.004.

The Net::Daemon class offers methods for the most common tasks a daemon
needs: Starting up, logging, accepting clients, authorization, restricting
its own environment for security and doing the true work. You only have to
override those methods that aren't appropriate for you, but typically
inheriting will safe you a lot of work anyways.


=head2 Constructors

  $server = Net::Daemon->new($attr, $options);

  $connection = $server->Clone($socket);

Two constructors are available: The B<new> method is called upon startup
and creates an object that will basically act as an anchor over the
complete program. It supports command line parsing via L<Getopt::Long (3)>.

Arguments of B<new> are I<$attr>, an hash ref of attributes (see below)
and I<$options> an array ref of options, typically command line arguments
(for example B<\@ARGV>) that will be passed to B<Getopt::Long::GetOptions>.

The second constructor is B<Clone>: It is called whenever a client
connects. It receives the main server object as input and returns a
new object. This new object will be passed to the methods that finally
do the true work of communicating with the client. Communication occurs
over the socket B<$socket>, B<Clone>'s argument.

Possible object attributes and the corresponding command line
arguments are:

=over 4

=item I<catchint> (B<--nocatchint>)

On some systems, in particular Solaris, the functions accept(),
read() and so on are not safe against interrupts by signals. For
example, if the user raises a USR1 signal (as typically used to
reread config files), then the function returns an error EINTR.
If the I<catchint> option is on (by default it is, use
B<--nocatchint> to turn this off), then the package will ignore
EINTR errors whereever possible.

=item I<chroot> (B<--chroot=dir>)

(UNIX only)  After doing a bind(), change root directory to the given
directory by doing a chroot(). This is usefull for security operations,
but it restricts programming a lot. For example, you typically have to
load external Perl extensions before doing a chroot(), or you need to
create hard links to Unix sockets. This is typically done in the config
file, see the --configfile option. See also the --group and --user
options.

If you don't know chroot(), think of an FTP server where you can see a
certain directory tree only after logging in.

=item I<clients>

An array ref with a list of clients. Clients are hash refs, the attributes
I<accept> (0 for denying access and 1 for permitting) and I<mask>, a Perl
regular expression for the clients IP number or its host name. See
L<"Access control"> below.

=item I<configfile> (B<--configfile=file>)

Net::Daemon supports the use of config files. These files are assumed
to contain a single hash ref that overrides the arguments of the new
method. However, command line arguments in turn take precedence over
the config file. See the L<"Config File"> section below for details
on the config file.

=item I<debug> (B<--debug>)

Turn debugging mode on. Mainly this asserts that logging messages of
level "debug" are created.

=item I<facility> (B<--facility=mode>)

(UNIX only) Facility to use for L<Sys::Syslog (3)>. The default is
B<daemon>.

=item I<group> (B<--group=gid>)

After doing a bind(), change the real and effective GID to the given.
This is usefull, if you want your server to bind to a privileged port
(<1024), but don't want the server to execute as root. See also
the --user option.

GID's can be passed as group names or numeric values.

=item I<localaddr> (B<--localaddr=ip>)

By default a daemon is listening to any IP number that a machine
has. This attribute allows to restrict the server to the given
IP number.

=item I<localpath> (B<--localpath=path>)

If you want to restrict your server to local services only, you'll
prefer using Unix sockets, if available. In that case you can use
this option for setting the path of the Unix socket being created.
This option implies B<--proto=unix>.

=item I<localport> (B<--localport=port>)

This attribute sets the port on which the daemon is listening. It
must be given somehow, as there's no default.

=item I<logfile> (B<--logfile=file>)

By default logging messages will be written to the syslog (Unix) or
to the event log (Windows NT). On other operating systems you need to
specify a log file. The special value "STDERR" forces logging to
stderr.

=item I<loop-child> (B<--loop-child>)

This option forces creation of a new child for loops. (See the
I<loop-timeout> option.) By default the loops are serialized.

=item I<loop-timeout> (B<--loop-timeout=secs>)

Some servers need to take an action from time to time. For example the
Net::Daemon::Spooler attempts to empty its spooling queue every 5
minutes. If this option is set to a positive value (zero being the
default), then the server will call its Loop method every "loop-timeout"
seconds.

Don't trust too much on the precision of the interval: It depends on
a number of factors, in particular the execution time of the Loop()
method. The loop is implemented by using the I<select> function. If
you need an exact interval, you should better try to use the alarm()
function and a signal handler. (And don't forget to look at the
I<catchint> option!)

It is recommended to use the I<loop-child> option in conjunction with
I<loop-timeout>.

=item I<mode> (B<--mode=modename>)

The Net::Daemon server can run in three different modes, depending on
the environment.

If you are running Perl 5.005 and did compile it for threads, then
the server will create a new thread for each connection. The thread
will execute the server's Run() method and then terminate. This mode
is the default, you can force it with "--mode=threads".

If threads are not available, but you have a working fork(), then the
server will behave similar by creating a new process for each connection.
This mode will be used automatically in the absence of threads or if
you use the "--mode=fork" option.

Finally there's a single-connection mode: If the server has accepted a
connection, he will enter the Run() method. No other connections are
accepted until the Run() method returns. This operation mode is useful
if you have neither threads nor fork(), for example on the Macintosh.
For debugging purposes you can force this mode with "--mode=single".

When running in mode single, you can still handle multiple clients at
a time by preforking multiple child processes. The number of childs
is configured with the option "--childs".

=item I<childs>

Use this parameter to let Net::Daemon run in prefork mode, which means
it forks the number of childs processes you give with this parameter,
and all child handle connections concurrently. The difference to
fork mode is, that the child processes continue to run after a
connection has terminated and are able to accept a new connection.
This is useful for caching inside the childs process (e.g.
DBI::ProxyServer connect_cached attribute)

=item I<options>

Array ref of Command line options that have been passed to the server object
via the B<new> method.

=item I<parent>

When creating an object with B<Clone> the original object becomes
the parent of the new object. Objects created with B<new> usually
don't have a parent, thus this attribute is not set.

=item I<pidfile> (B<--pidfile=file>)

(UNIX only) If this option is present, a PID file will be created at the
given location.

=item I<proto> (B<--proto=proto>)

The transport layer to use, by default I<tcp> or I<unix> for a Unix
socket. It is not yet possible to combine both.

=item I<socket>

The socket that is connected to the client; passed as B<$client> argument
to the B<Clone> method. If the server object was created with B<new>,
this attribute can be undef, as long as the B<Bind> method isn't called.
Sockets are assumed to be IO::Socket objects.

=item I<user> (B<--user=uid>)

After doing a bind(), change the real and effective UID to the given.
This is usefull, if you want your server to bind to a privileged port
(<1024), but don't want the server to execute as root. See also
the --group and the --chroot options.

UID's can be passed as group names or numeric values.

=item I<version> (B<--version>)

Supresses startup of the server; instead the version string will
be printed and the program exits immediately.

=back

Note that most of these attributes (facility, mode, localaddr, localport,
pidfile, version) are meaningfull only at startup. If you set them later,
they will be simply ignored. As almost all attributes have appropriate
defaults, you will typically use the B<localport> attribute only.


=head2 Command Line Parsing

  my $optionsAvailable = Net::Daemon->Options();

  print Net::Daemon->Version(), "\n";

  Net::Daemon->Usage();

The B<Options> method returns a hash ref of possible command line options.
The keys are option names, the values are again hash refs with the
following keys:

=over 4

=item template

An option template that can be passed to B<Getopt::Long::GetOptions>.

=item description

A description of this option, as used in B<Usage>

=back

The B<Usage> method prints a list of all possible options and returns.
It uses the B<Version> method for printing program name and version.


=head2 Config File

If the config file option is set in the command line options or in the
in the "new" args, then the method

  $server->ReadConfigFile($file, $options, $args)

is invoked. By default the config file is expected to contain Perl source
that returns a hash ref of options. These options override the "new"
args and will in turn be overwritten by the command line options, as
present in the $options hash ref.

A typical config file might look as follows, we use the DBI::ProxyServer
as an example:

    # Load external modules; this is not required unless you use
    # the chroot() option.
    #require DBD::mysql;
    #require DBD::CSV;

    {
	# 'chroot' => '/var/dbiproxy',
	'facility' => 'daemon',
	'pidfile' => '/var/dbiproxy/dbiproxy.pid',
	'user' => 'nobody',
	'group' => 'nobody',
	'localport' => '1003',
	'mode' => 'fork'

	# Access control
        'clients' => [
	    # Accept the local
	    {
		'mask' => '^192\.168\.1\.\d+$',
                'accept' => 1
            },
	    # Accept myhost.company.com
	    {
		'mask' => '^myhost\.company\.com$',
                'accept' => 1
            }
	    # Deny everything else
	    {
		'mask' => '.*',
		'accept' => 0
	    }
        ]
    }


=head2 Access control

The Net::Daemon package supports a host based access control scheme. By
default access is open for anyone. However, if you create an attribute
$self->{'clients'}, typically in the config file, then access control
is disabled by default. For any connection the client list is processed:
The clients attribute is an array ref to a list of hash refs. Any of the
hash refs may contain arbitrary attributes, including the following:

=over 8

=item mask

A Perl regular expression that has to match the clients IP number or
its host name. The list is processed from the left to the right, whenever
a 'mask' attribute matches, then the related hash ref is choosen as
client and processing the client list stops.

=item accept

This may be set to true or false (default when omitting the attribute),
the former means accepting the client.

=back


=head2 Event logging

  $server->Log($level, $format, @args);
  $server->Debug($format, @args);
  $server->Error($format, @args);
  $server->Fatal($format, @args);

The B<Log> method is an interface to L<Sys::Syslog (3)> or
L<Win32::EventLog (3)>. It's arguments are I<$level>, a syslog
level like B<debug>, B<notice> or B<err>, a format string in the
style of printf and the format strings arguments.

The B<Debug> and B<Error> methods are shorthands for calling
B<Log> with a level of debug and err, respectively. The B<Fatal>
method is like B<Error>, except it additionally throws the given
message as exception.

See L<Net::Daemon::Log(3)> for details.


=head2 Flow of control

  $server->Bind();
  # The following inside Bind():
  if ($connection->Accept()) {
      $connection->Run();
  } else {
      $connection->Log('err', 'Connection refused');
  }

The B<Bind> method is called by the application when the server should
start. Typically this can be done right after creating the server object
B<$server>. B<Bind> usually never returns, except in case of errors.

When a client connects, the server uses B<Clone> to derive a connection
object B<$connection> from the server object. A new thread or process
is created that uses the connection object to call your classes
B<Accept> method. This method is intended for host authorization and
should return either FALSE (refuse the client) or TRUE (accept the client).

If the client is accepted, the B<Run> method is called which does the
true work. The connection is closed when B<Run> returns and the corresponding
thread or process exits.


=head2 Error Handling

All methods are supposed to throw Perl exceptions in case of errors.


=head1 MULTITHREADING CONSIDERATIONS

All methods are working with lexically scoped data and handle data
only, the exception being the OpenLog method which is invoked before
threading starts. Thus you are safe as long as you don't share
handles between threads. I strongly recommend that your application
behaves similar.



=head1 EXAMPLE

As an example we'll write a simple calculator server. After connecting
to this server you may type expressions, one per line. The server
evaluates the expressions and prints the result. (Note this is an example,
in real life we'd never implement such a security hole. :-)

For the purpose of example we add a command line option I<--base> that
takes 'hex', 'oct' or 'dec' as values: The servers output will use the
given base.

  # -*- perl -*-
  #
  # Calculator server
  #
  require 5.004;
  use strict;

  require Net::Daemon;


  package Calculator;

  use vars qw($VERSION @ISA);
  $VERSION = '0.01';
  @ISA = qw(Net::Daemon); # to inherit from Net::Daemon

  sub Version ($) { 'Calculator Example Server, 0.01'; }

  # Add a command line option "--base"
  sub Options ($) {
      my($self) = @_;
      my($options) = $self->SUPER::Options();
      $options->{'base'} = { 'template' => 'base=s',
			     'description' => '--base                  '
				    . 'dec (default), hex or oct'
			      };
      $options;
  }

  # Treat command line option in the constructor
  sub new ($$;$) {
      my($class, $attr, $args) = @_;
      my($self) = $class->SUPER::new($attr, $args);
      if ($self->{'parent'}) {
	  # Called via Clone()
	  $self->{'base'} = $self->{'parent'}->{'base'};
      } else {
	  # Initial call
	  if ($self->{'options'}  &&  $self->{'options'}->{'base'}) {
	      $self->{'base'} = $self->{'options'}->{'base'}
          }
      }
      if (!$self->{'base'}) {
	  $self->{'base'} = 'dec';
      }
      $self;
  }

  sub Run ($) {
      my($self) = @_;
      my($line, $sock);
      $sock = $self->{'socket'};
      while (1) {
	  if (!defined($line = $sock->getline())) {
	      if ($sock->error()) {
		  $self->Error("Client connection error %s",
			       $sock->error());
	      }
	      $sock->close();
	      return;
	  }
	  $line =~ s/\s+$//; # Remove CRLF
	  my($result) = eval $line;
	  my($rc);
	  if ($self->{'base'} eq 'hex') {
	      $rc = printf $sock ("%x\n", $result);
	  } elsif ($self->{'base'} eq 'oct') {
	      $rc = printf $sock ("%o\n", $result);
	  } else {
	      $rc = printf $sock ("%d\n", $result);
	  }
	  if (!$rc) {
	      $self->Error("Client connection error %s",
			   $sock->error());
	      $sock->close();
	      return;
	  }
      }
  }

  package main;

  my $server = Calculator->new({'pidfile' => 'none',
				'localport' => 2000}, \@ARGV);
  $server->Bind();


=head1 KNOWN PROBLEMS

Most, or even any, known problems are related to the Sys::Syslog module
which is by default used for logging events under Unix. I'll quote some
examples:

=over

=item Usage: Sys::Syslog::_PATH_LOG at ...

This problem is treated in perl bug 20000712.003. A workaround is
changing line 277 of Syslog.pm to

  my $syslog = &_PATH_LOG() || croak "_PATH_LOG not found in syslog.ph";

=back


=head1 AUTHOR AND COPYRIGHT

  Net::Daemon is Copyright (C) 1998, Jochen Wiedmann
                                     Am Eisteich 9
                                     72555 Metzingen
                                     Germany

                                     Phone: +49 7123 14887
                                     Email: joe@ispsoft.de

  All rights reserved.

  You may distribute this package under the terms of either the GNU
  General Public License or the Artistic License, as specified in the
  Perl README file.


=head1 SEE ALSO

L<RPC::pServer(3)>, L<Netserver::Generic(3)>, L<Net::Daemon::Log(3)>,
L<Net::Daemon::Test(3)>

=cut