Merge daemonization support from 0.31 branch. Removed its -d commandline

switch since the debug switch is already using it.


git-svn-id: https://svn.perl.org/qpsmtpd/trunk@538 958fd67b-6ff1-0310-b445-bb7760255be9
This commit is contained in:
Devin Carraway 2005-07-31 08:42:43 +00:00
parent 8bb7cf67de
commit 4cdae6bf05

View File

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