Skip to content
This repository
Browse code

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.


git-svn-id: http://svn.perl.org/qpsmtpd/trunk@938 958fd67b-6ff1-0310-b445-bb7760255be9
  • Loading branch information...
commit 714211e886a42337fdca06ff6f94f4bb78aeed03 1 parent 4d2ecbf
radu authored

Showing 3 changed files with 33 additions and 29 deletions. Show diff stats Hide diff stats

  1. +4 0 Changes
  2. +15 15 qpsmtpd-async
  3. +14 14 qpsmtpd-prefork
4 Changes
... ... @@ -1,3 +1,7 @@
  1 + async, prefork: detach and daemonize only after reading the configuration
  2 + and loading the plugins, to give the init scripts a chance to detect
  3 + failed startups due to broken configuration or plugins (Diego d'Ambra)
  4 +
1 5 plugins/tls: close the file descriptor for the SSL socket
2 6
3 7 Set the Return-Path header when queuing into maildir mailboxes.
30 qpsmtpd-async
@@ -248,21 +248,6 @@ sub run_as_server {
248 248 IO::Handle::blocking($CONFIG_SERVER, 0);
249 249 binmode($CONFIG_SERVER, ':raw');
250 250
251   - if ($DETACH) {
252   - open STDIN, '/dev/null' or die "/dev/null: $!";
253   - open STDOUT, '>/dev/null' or die "/dev/null: $!";
254   - open STDERR, '>&STDOUT' or die "open(stderr): $!";
255   - defined (my $pid = fork) or die "fork: $!";
256   - exit 0 if $pid;
257   - POSIX::setsid or die "setsid: $!";
258   - }
259   -
260   - if ($PID_FILE) {
261   - open PID, ">$PID_FILE" || die "$PID_FILE: $!";
262   - print PID $$,"\n";
263   - close PID;
264   - }
265   -
266 251 # Drop priviledges
267 252 my (undef, undef, $quid, $qgid) = getpwnam $USER or
268 253 die "unable to determine uid/gid for $USER\n";
@@ -285,6 +270,21 @@ sub run_as_server {
285 270 my $plugin_loader = Qpsmtpd::SMTP->new();
286 271 $plugin_loader->load_plugins;
287 272
  273 + if ($DETACH) {
  274 + open STDIN, '/dev/null' or die "/dev/null: $!";
  275 + open STDOUT, '>/dev/null' or die "/dev/null: $!";
  276 + open STDERR, '>&STDOUT' or die "open(stderr): $!";
  277 + defined (my $pid = fork) or die "fork: $!";
  278 + exit 0 if $pid;
  279 + POSIX::setsid or die "setsid: $!";
  280 + }
  281 +
  282 + if ($PID_FILE) {
  283 + open PID, ">$PID_FILE" || die "$PID_FILE: $!";
  284 + print PID $$,"\n";
  285 + close PID;
  286 + }
  287 +
288 288 $plugin_loader->log(LOGINFO, 'Running as user '.
289 289 (getpwuid($>) || $>) .
290 290 ', group '.
28 qpsmtpd-prefork
@@ -150,20 +150,6 @@ if ($pid_file) {
150 150 }
151 151 }
152 152
153   -if ($detach) {
154   - open STDIN, '/dev/null' or die "/dev/null: $!";
155   - open STDOUT, '>/dev/null' or die "/dev/null: $!";
156   - open STDERR, '>&STDOUT' or die "open(stderr): $!";
157   - defined (my $pid = fork) or die "fork: $!";
158   - exit 0 if $pid;
159   - POSIX::setsid or die "setsid: $!";
160   -}
161   -
162   -if ($pid_file) {
163   - print PID $$,"\n";
164   - close PID;
165   -}
166   -
167 153 run();
168 154
169 155 #start daemon
@@ -262,6 +248,20 @@ sub run {
262 248 # after each connection
263 249 $qpsmtpd = $qpsmtpd_base = qpsmtpd_instance();
264 250
  251 + if ($detach) {
  252 + open STDIN, '/dev/null' or die "/dev/null: $!";
  253 + open STDOUT, '>/dev/null' or die "/dev/null: $!";
  254 + open STDERR, '>&STDOUT' or die "open(stderr): $!";
  255 + defined (my $pid = fork) or die "fork: $!";
  256 + exit 0 if $pid;
  257 + POSIX::setsid or die "setsid: $!";
  258 + }
  259 +
  260 + if ($pid_file) {
  261 + print PID $$,"\n";
  262 + close PID;
  263 + }
  264 +
265 265 # child reaper
266 266 $SIG{CHLD} = \&reaper;
267 267 spawn_children();

0 comments on commit 714211e

Please sign in to comment.
Something went wrong with that request. Please try again.