https://issues.apache.org/SpamAssassin/show_bug.cgi?id=6682

             Bug #: 6682
           Summary: Using daemonize option writes wrong PID (pid of parent
                    process instead of child / daemon).
           Product: Spamassassin
           Version: 3.3.1
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: normal
          Priority: P2
         Component: spamc/spamd
        AssignedTo: [email protected]
        ReportedBy: [email protected]
    Classification: Unclassified


Starting spamd with --daemonize option writes wrong PID into pid-file.
Child PID is overwritten by parent PID.  Seems to be a race condition.

Small testscript:

test.pl

#!/usr/bin/perl

system("/usr/sbin/spamd --username=popuser --daemonize
--helper-home-dir=/var/qmail --max-children 5
--pidfile=/var/run/spamd/spamd_full.pid --socketpath=/tmp/spamd_full.sock");

exit(0);

Sample result (in my environment):

1. Using ps I can see one process "/usr/sbin/spamd" with PID 3840  
2. Second process /usr/sbin/spamd with PID 3878 is started by daemonize()
3. In PID file first I can see the PID 3878 (the correct one!)
4. Shortly after, the PID 3840 is written into PID file (the wrong one)
5. Parent Process with PID 3840 is killed.

=>  daemonized process with PID 3878 is running but PID file contains pid 3840


Potential fix (works in my environment):

In spamd change the order "write PID file, then kill parent" to "kill parent,
then write PID file:

Old:

# Make the pidfile ...
if (defined $opt{'pidfile'}) {
  if (open PIDF, ">$opt{'pidfile'}") {
    print PIDF "$$\n";
    close PIDF;
  }
  else {
    warn "spamd: cannot write to PID file: $!\n";
  }
}

# now allow waiting processes to connect, if they're watching the log.
# The test suite does this!
info("spamd: server pid: $$\n");
kill("USR1",$originalparent) if ($opt{'daemonize'});


New:

# now allow waiting processes to connect, if they're watching the log.
# The test suite does this!
info("spamd: server pid: $$\n");
kill("USR1",$originalparent) if ($opt{'daemonize'});

# Make the pidfile ...
if (defined $opt{'pidfile'}) {
  if (open PIDF, ">$opt{'pidfile'}") {
    print PIDF "$$\n";
    close PIDF;
  }
  else {
    warn "spamd: cannot write to PID file: $!\n";
  }
}

I am running on a virtual server which is not the fastest => maybe it is
working correctly on "fast" machines.  

Maybe one can verify if my proposed fix is ok.

Thank you.

-- 
Configure bugmail: 
https://issues.apache.org/SpamAssassin/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

Reply via email to