[Greylist-users] [PATCH] Daemonize relaydelay.pl & add logging

Blair Zajac blair at orcaware.com
Sun Jul 13 18:54:26 PDT 2003


Hi Evan,

I'm guessing you'll want to clean this up, but here's some code to
daemonize relaydelay.pl and to send all output to a specified filename.

It may be time to use Getopt::Long to add a --daemonize and --logfile
command line options.

Best,
Blair

-- 
Blair Zajac <blair at orcaware.com>
Plots of your system's performance - http://www.orcaware.com/orca/


@@ -930,10 +930,34 @@
 BEGIN:
 {
   if (scalar(@ARGV) < 2) {
-    print "Usage: perl $0 <name_of_filter> <path_to_sendmail.cf>\n";
+    print "Usage: perl $0 <name_of_filter> <path_to_sendmail.cf> [log_file]\n";
     exit;
   }

+  if (1) {
+    my $expr = 'use POSIX qw(setsid)';
+    local $SIG{__DIE__}  = 'DEFAULT';
+    local $SIG{__WARN__} = \&die_when_called;
+    eval $expr;
+    if ($@) {
+      die "$0: cannot get setsid since eval '$expr' failed: $@\n";
+    }
+    chdir('/')               or die "$0: cannot chdir `/': $!\n";
+    open(STDIN, '/dev/null') or die "$0: cannot read `/dev/null': $!\n";
+    defined(my $pid = fork)  or die "$0: cannot fork: $!\n";
+    exit if $pid;
+    POSIX::setsid()          or die "$0: cannot start a new session: $!\n";
+  }
+
+  if (@ARGV > 2 and $ARGV[2]) {
+    open STDOUT, ">>$ARGV[2]" or
+      die "$0: cannot open `$ARGV[2]' for writing: $!\n";
+    open STDERR, ">&STDOUT" or
+      die "$0: cannot dup STDOUT: $!\n";
+    select((select(STDOUT), $| = 1)[0]);
+    select((select(STDERR), $| = 1)[0]);
+  }
+
   my $conn = Sendmail::Milter::auto_getconn($ARGV[0], $ARGV[1]);

   print "Found connection info for '$ARGV[0]': $conn\n";
@@ -994,5 +1018,3 @@
   print "Closing DB connection.\n";
   db_disconnect();
 }
-
-


More information about the Greylist-users mailing list