Skip to content

Commit

Permalink
Detach and daemonize only after reading the configuration and loading
Browse files Browse the repository at this point in the history
the plugins, to give the init scripts a chance to detect failed startups
due to broken configuration or plugins.


git-svn-id: http://svn.perl.org/qpsmtpd/trunk@938 958fd67b-6ff1-0310-b445-bb7760255be9
  • Loading branch information
radu committed Sep 4, 2008
1 parent 4d2ecbf commit 714211e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,3 +1,7 @@
async, prefork: detach and daemonize only after reading the configuration
and loading the plugins, to give the init scripts a chance to detect
failed startups due to broken configuration or plugins (Diego d'Ambra)

plugins/tls: close the file descriptor for the SSL socket plugins/tls: close the file descriptor for the SSL socket


Set the Return-Path header when queuing into maildir mailboxes. Set the Return-Path header when queuing into maildir mailboxes.
Expand Down
30 changes: 15 additions & 15 deletions qpsmtpd-async
Expand Up @@ -248,21 +248,6 @@ sub run_as_server {
IO::Handle::blocking($CONFIG_SERVER, 0); IO::Handle::blocking($CONFIG_SERVER, 0);
binmode($CONFIG_SERVER, ':raw'); binmode($CONFIG_SERVER, ':raw');


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) {
open PID, ">$PID_FILE" || die "$PID_FILE: $!";
print PID $$,"\n";
close PID;
}

# Drop priviledges # Drop priviledges
my (undef, undef, $quid, $qgid) = getpwnam $USER or my (undef, undef, $quid, $qgid) = getpwnam $USER or
die "unable to determine uid/gid for $USER\n"; die "unable to determine uid/gid for $USER\n";
Expand All @@ -285,6 +270,21 @@ sub run_as_server {
my $plugin_loader = Qpsmtpd::SMTP->new(); my $plugin_loader = Qpsmtpd::SMTP->new();
$plugin_loader->load_plugins; $plugin_loader->load_plugins;


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) {
open PID, ">$PID_FILE" || die "$PID_FILE: $!";
print PID $$,"\n";
close PID;
}

$plugin_loader->log(LOGINFO, 'Running as user '. $plugin_loader->log(LOGINFO, 'Running as user '.
(getpwuid($>) || $>) . (getpwuid($>) || $>) .
', group '. ', group '.
Expand Down
28 changes: 14 additions & 14 deletions qpsmtpd-prefork
Expand Up @@ -150,20 +150,6 @@ if ($pid_file) {
} }
} }


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;
}

run(); run();


#start daemon #start daemon
Expand Down Expand Up @@ -262,6 +248,20 @@ sub run {
# after each connection # after each connection
$qpsmtpd = $qpsmtpd_base = qpsmtpd_instance(); $qpsmtpd = $qpsmtpd_base = qpsmtpd_instance();


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;
}

# child reaper # child reaper
$SIG{CHLD} = \&reaper; $SIG{CHLD} = \&reaper;
spawn_children(); spawn_children();
Expand Down

0 comments on commit 714211e

Please sign in to comment.