diff --git a/qpsmtpd-forkserver b/qpsmtpd-forkserver index 5db808a..d50b283 100755 --- a/qpsmtpd-forkserver +++ b/qpsmtpd-forkserver @@ -24,6 +24,7 @@ my @LOCALADDR; # ip address(es) to bind to my $USER = 'smtpd'; # user to suid to my $MAXCONNIP = 5; # max simultaneous connections from one IP my $PID_FILE = ''; +my $DETACH; # daemonize on startup sub usage { print <<"EOT"; @@ -36,6 +37,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 + -d, --detach : detach from controlling terminal (daemonize) EOT exit 0; } @@ -47,6 +49,7 @@ GetOptions('h|help' => \&usage, 'p|port=i' => \$PORT, 'u|user=s' => \$USER, 'pid-file=s' => \$PID_FILE, + 'd|detach' => \$DETACH, ) || &usage; # detaint the commandline @@ -119,8 +122,6 @@ if ($PID_FILE) { open PID, ">$PID_FILE" or die "open pid_file: $!\n"; } - print PID $$,"\n"; - close PID; } # Load plugins here @@ -151,6 +152,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;