Package: libnet-daemon-perl
Version: 0.43-1
Severity: wishlist
Tags: patch

I hereby sends back patches we made to libnet-daemon-perl for our uses;

The first patch enables optional umask to be used when creating sockets,
instead of beeing hardcoded to 0.

The second patch enables Net::Daemon to behave like an daemon and
daemonize, and also use the pid-file as a pid file.

/Carl Fürstenberg <c...@excito.com>
#! /bin/sh /usr/share/dpatch/dpatch-run
## 04_optional-umask.dpatch by Carl Fürstenberg <azat...@gmail.com>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Adds option to allow umask to be given instead of be forced to 0

@DPATCH@
diff -urNad libnet-daemon-perl-0.43~/lib/Net/Daemon.pm libnet-daemon-perl-0.43/lib/Net/Daemon.pm
--- libnet-daemon-perl-0.43~/lib/Net/Daemon.pm	2008-12-12 21:07:45.000000000 +0100
+++ libnet-daemon-perl-0.43/lib/Net/Daemon.pm	2008-12-13 01:11:50.000000000 +0100
@@ -114,6 +114,9 @@
       'proto' => { 'template' => 'proto=s',
 		   'description' => '--proto <protocol>        '
 		   . 'transport layer protocol: tcp (default) or unix' },
+      'sockumask' => { 'template' => 'sockumask=s',
+		       'description' => '--sockumask <umask>      '
+		           . 'umask to be used when creating an UNIX socket' },
       'user' => { 'template' => 'user=s',
 		  'description' => '--user <user>           '
 		      . 'Change uid to given user after binding to port.' },
@@ -538,7 +541,7 @@
 	    unlink $path;
 	    $self->Fatal("Can't remove stale Unix socket ($path): $!")
 		if -e $path;
-	    my $old_umask = umask 0;
+	    my $old_umask = umask ( $self->{'sockumask'} || 0 );
 	    $self->{'socket'} =
 		IO::Socket::UNIX->new('Local' => $path,
 				      'Listen' => $self->{'listen'} || 10)
#! /bin/sh /usr/share/dpatch/dpatch-run
## 02_daemonize.dpatch by Carl Fürstenberg <azat...@gmail.com>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Make Net::Daemon to be an daemon\!

@DPATCH@
diff -urNad libnet-daemon-perl-0.43~/lib/Net/Daemon.pm libnet-daemon-perl-0.43/lib/Net/Daemon.pm
--- libnet-daemon-perl-0.43~/lib/Net/Daemon.pm	2008-12-13 02:02:55.000000000 +0100
+++ libnet-daemon-perl-0.43/lib/Net/Daemon.pm	2008-12-13 02:04:47.000000000 +0100
@@ -75,6 +75,9 @@
       'configfile' => { 'template' => 'configfile=s',
 			'description' =>  '--configfile <file>     '
 			    . 'Read options from config file <file>.' },
+      'daemonize' => { 'template' => 'daemonize',
+		   'description' =>  '--daemonize                 '
+		       . 'Deamonize on startup'},
       'debug' => { 'template' => 'debug',
 		   'description' =>  '--debug                 '
 		       . 'Turn debugging mode on'},
@@ -556,14 +559,42 @@
     }
     $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));
-    }
+	# Daemoize if specified and we havn't defined STDERR for logging
+	if( $self->{'daemonize'} and $self->{'logfile'} ne 'STDERR' ) {
+		use Fcntl qw(:DEFAULT :flock);
+		use POSIX;
+
+		chdir '/'					or $self->Fatal("Can't chdir to /: $!");
+		umask 0;
+		open STDIN, '/dev/null'		or $self->Fatal("Can't read /dev/null: $!");
+		open STDOUT, '>/dev/null'	or $self->Fatal("Can't write to /dev/null: $!");
+		defined(my $pid = fork)		or $self->Fatal("Can't fork: $!");
+		exit if $pid;
+		POSIX::setsid()				or $self->Fatal("Can't start a new session: $!");
+		open STDERR, '>&STDOUT'		or $self->Fatal("Can't dup stdout: $!");
+
+		# Check if we are allready running in an other process
+
+		if ( defined($self->{'pidfile'}) and (my $pidfile = ($self->{'pidfile'})) ne 'none') {
+			local *FH;
+			sysopen(FH, $pidfile, O_RDWR|O_CREAT)
+			|| $self->Fatal( "Cannot open pid file [$pidfile]: $!" );
+			flock(FH, LOCK_EX | LOCK_NB)
+			|| $self->Fatal( "Pidfile $pidfile already locked" );
+			my ($pid) = <FH> =~ /^(\d+)/;
+
+			if ($pid && $pid != $$ && kill(0, $pid)) {
+				$self->Debug("Allready running: $pid");
+				exit 0;
+			}
+
+			$self->Debug("Writing PID [$$] to pidfile $pidfile");
+			sysseek  FH, 0, 0;
+			truncate FH, 0;
+			syswrite FH, "$$\n", length("$$\n");
+			close(FH) || $self->Fatal( "Cannot write to pidfile [$pidfile]: $!" );
+		}
+	}
 
     if (my $dir = $self->{'chroot'}) {
 	$self->Debug("Changing root directory to $dir");

Reply via email to