[Da-tools-commits] ./da-tools/userdir-ldap-common r350: ud-fingerserv: implement daemonizing
Peter Palfrader
peter at palfrader.org
Wed May 14 22:45:13 UTC 2008
------------------------------------------------------------
revno: 350
committer: Peter Palfrader <peter at palfrader.org>
branch nick: test
timestamp: Tue 2007-12-25 19:37:37 +0100
message:
ud-fingerserv: implement daemonizing
ud-fingerserv now daemonizes into the background when not run in
inetd-mode (-i) or explicitly told to not detach using the -f flag.
Patch from Stephen Gran.
modified:
ud-fingerserv
------------------------------------------------------------
revno: 349.1.1
committer: Stephen Gran <steve at lobefin.net>
branch nick: userdir-ldap-common-local
timestamp: Tue 2007-12-25 15:09:51 +0000
message:
Make finger server daemonize when not in inetd mode.
Also add a foreground switch so that previous defalt behavior is preserved.
modified:
ud-fingerserv
------------------------------------------------------------
revno: 349.2.1
committer: Stephen Gran <steve at lobefin.net>
branch nick: userdir-ldap-common
timestamp: Tue 2007-12-25 15:11:05 +0000
message:
When not in inetd mode, detach from controlling terminal and daemonize
properly. This involves adding a -l (logfile) option to the command line
arguments, so that output will be captured somewhere.
Also add a -f (foreground) option, to preserve the previous default behavior.
modified:
ud-fingerserv
-------------- next part --------------
=== modified file 'ud-fingerserv'
--- a/ud-fingerserv 2004-11-18 18:10:57 +0000
+++ b/ud-fingerserv 2007-12-25 15:09:51 +0000
@@ -17,7 +17,7 @@
# Global settings...
my %config = &Util::ReadConfigFile;
my %opts;
-getopts("iqhv", \%opts);
+getopts("fiqhvl:", \%opts);
my $use_inetd = $config{use_inetd} || $opts{i};
$| = 1;
@@ -41,20 +41,39 @@
$SIG{CHLD} = \&Reaper;
&help if (defined($opts{h}));
-#my $logfh = STDOUT; #TODO
+
+my $logfh;
+unless ($opt{i} || $opt{f}) {
+ die "Need logfile unless running foreground\n" unless (defined($opt{l}));
+ open ($logfh, $opt{l}) or die "Can't open logfile: $!\n";
+} else {
+ $logfh = \*STDOUT;
+}
&log("Binding to LDAP server at $config{ldaphost}") if (defined($opts{v}));
my $ldap = Net::LDAP->new($config{ldaphost}) || die $1;
$ldap->bind;
if (!$use_inetd) {
+
+ unless ($opts{f}) {
+ use POSIX 'setsid';
+ chdir '/' or die "Can't chdir to /: $!";
+ open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
+ open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
+ defined(my $pid = fork) or die "Can't fork: $!";
+ exit if $pid;
+ setsid or die "Can't start a new session: $!";
+ open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
+ }
+
&log("Binding to port 79") if (defined($opts{v}));
my $server = IO::Socket::INET->new(Proto => 'tcp',
LocalPort => 'finger(79)',
Listen => SOMAXCONN,
Reuse => 1);
- die "Cannot listen on finger port" unless $server;
+ mydie "Cannot listen on finger port" unless $server;
&log("[Server listening for connections]");
my ($pid, $client, $hostinfo);
@@ -62,7 +81,7 @@
while ($client = $server->accept()) {
&log("Forking to handle client request") if (defined($opts{v}));
next if $pid = fork; # parent
- die "fork: $!" unless defined $pid;
+ mydie "fork: $!" unless defined $pid;
# child
$client->autoflush(1);
@@ -118,7 +137,7 @@
&log("Looking up $uid at $config{basedn}, uid=$uid");
$mesg = $ldap->search(base => $config{basedn}, filter => "uid=$uid");
- $mesg->code && die $mesg->error;
+ $mesg->code && mydie $mesg->error;
$entries = $mesg->as_struct;
if ($mesg->count == 0) {
@@ -168,11 +187,13 @@
}
sub help {
- print "fingerserv [-i | -q | -v | -h]\n";
+ print "fingerserv [-f | -l | -i | -q | -v | -h]\n";
+ print "-f = foreground; do not detach from tty\n";
print "-i = inetd mode; otherwise runs standalone\n";
print "-q = quiet mode; no output\n";
print "-v = verbose mode\n";
print "-h = this help message\n";
+ print "-l = log file. Necessary if not using -f or -i\n";
exit 0;
}
@@ -181,7 +202,13 @@
return if (defined($opts{q}));
my $time = localtime;
- print STDERR "$time $msg\n";
+ print $logfh "$time $msg\n";
+}
+
+sub mydie {
+ my $msg = shift;
+ log($msg);
+ exit 1;
}
sub readdata {
@@ -192,9 +219,9 @@
my $ret;
my $flags= fcntl($fh, F_GETFL, 0)
- or die "Can't get flags for socket: $!\n";
+ or mydie "Can't get flags for socket: $!\n";
fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
- or die "Can't make socket nonblocking: $!\n";
+ or mydie "Can't make socket nonblocking: $!\n";
while (($bytesread < 1024) && ($out !~ /\n/)) {
$ret = sysread($fh, $in, 1024);
More information about the Da-tools-commits
mailing list