[systemd-devel] possible bug guessing mainpid when service type=forking..

Lennart Poettering lennart at poettering.net
Mon Jun 20 12:36:39 PDT 2011


On Mon, 20.06.11 19:34, Marius Tolzmann (tolzmann at molgen.mpg.de) wrote:

> 
> We fixed this issue by patching ypbind-mt to run in foreground an let
> systemd do the forking - as intended 8)
> 
> But it still seems to be some kind of bug in setting the correct MAINPID
> in Type=forking service files.

This very much sounds as if the service does not wait in the top-level
process that the daemon process is properly forked and finished writing
the PID file. systemd tries to read the PID file immediately after the
top-level process exits which means that systemd will race against the
daemon there.

> Fixing the ypbind binary solved everything for us - so i don't know if
> anybody cares to fix this.. but it was hard to find what could have
> happened because the service just won't start even if started again and
> again manually and worked all the time.

So my guess is that ypbind-mt does something like this:

   fork() --> fork() --> write_pid_file()
   exit()     exit()     run_main_loop()
                         ...

but it should be doing:

   fork() ------------> fork() --> write_pid_file()
   wait_for_notify()    exit()     notify_grandparent()
   exit()                          run_main_loop()
                                   ... 

i.e. the main process should not exit before it hasn't ensure that the
PID file of the daemon process has been written. Otherwise any usage of
the PID file is necessarily racy.

> BTW a patch for ypbind-mt adding a -foreground commandline switch will
> be sent to the maintainers soon..

Nice, this is good to hear, Type=simple is definitely the ncier solution.

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list