[svn:qpsmtpd] r538 - trunk

[email protected] 31 Jul 2005 08:42:44 -0000
Newsgroups perl.cvs.qpsmtpd
Message-ID <[email protected]>
Author: aqua
Date: Sun Jul 31 01:42:43 2005
New Revision: 538

Modified:
   trunk/qpsmtpd-forkserver
Log:
Merge daemonization support from 0.31 branch.  Removed its -d commandline
switch since the debug switch is already using it.


Modified: trunk/qpsmtpd-forkserver
==============================================================================
--- trunk/qpsmtpd-forkserver	(original)
+++ trunk/qpsmtpd-forkserver	Sun Jul 31 01:42:43 2005
@@ -25,6 +25,7 @@ my @LOCALADDR;                       # i
 my $USER      = 'smtpd';             # user to suid to
 my $MAXCONNIP = 5;                   # max simultaneous connections from one IP
 my $PID_FILE   = '';                 # file to which server PID will be written
+my $DETACH;                          # daemonize on startup
 our $DEBUG = 0;
 
 sub usage {
@@ -38,6 +39,7 @@ usage: qpsmtpd-forkserver [ options ]
  -u, --user U              : run as a particular user (default 'smtpd')
  -m, --max-from-ip M       : limit connections from a single IP; default 5
      --pid-file P          : print main servers PID to file P
+     --detach              : detach from controlling terminal (daemonize)
 EOT
         exit 0;
 }
@@ -50,6 +52,7 @@ GetOptions('h|help' => \&usage,
            'u|user=s' => \$USER,
            'pid-file=s' => \$PID_FILE,
            'd|debug+' => \$DEBUG,
+           'detach' => \$DETACH,
 	  ) || &usage;
 
 # detaint the commandline
@@ -125,8 +128,6 @@ if ($PID_FILE) {
     open PID, ">$PID_FILE"
       or die "open pid_file: $!\n";
   }
-  print PID $$,"\n";
-  close PID;
 }
 
 # Load plugins here
@@ -157,6 +158,20 @@ $> = $quid;
 	', group '.
 	(getgrgid($)) || $)));
 
+if ($DETACH) {
+  open STDIN, '/dev/null' or die "/dev/null: $!";
+  open STDOUT, '>/dev/null' or die "/dev/null: $!";
+  open STDERR, '>&STDOUT' or die "open(stderr): $!";
+  defined (my $pid = fork) or die "fork: $!";
+  exit 0 if $pid;
+  POSIX::setsid or die "setsid: $!";
+}
+
+if ($PID_FILE) {
+  print PID $$,"\n";
+  close PID;
+}
+
 while (1) {
   REAPER();
   my $running = scalar keys %childstatus;