[systemd-devel] How to run forking daemon with systemd in correct way, without errors?
Gena Makhomed
gmm at csdoc.com
Thu Nov 23 09:32:13 UTC 2017
On 23.11.2017 7:45, Mantas Mikulėnas wrote:
>> This is bug in nginx code? And this bug should be fixed in nginx?
>>
>> But "daemon(); write_pidfile();" is common pattern
>> used by many services and even in library functions.
> It may be common, but not necessarily correct. The parent process should
> only exit *after* finishing all the preparations (i.e. something like
> "fork(); write_pidfile(); exit()"), since systemd uses the exit as a signal
> that the daemon is now "ready".
You are joking? Why you think that this pattern is not correct?
Is any systemd documentations exists, which requires from daemons
write pidfile of daemon process before exiting the parent process?
Does you see daemon() function from The GNU C Library (glibc)?
It exits from parent process immediately, without writing pid file.
Probably you should first fix function daemon() from glibc to meet
systemd requirements? And after this - rewrite all existing daemons?
Or may be it will be simpler to fix root cause of problem in systemd?
Common UNIX pattern is to perform a double fork when creating a daemon.
See Stevens' "Advanced Programming in the UNIX Environment" for details
https://www.amazon.com/Advanced-Programming-UNIX-Environment-3rd/dp/0321637739
Parent process can't easily get the PID of the grandchild process,
it is not possible in common case to write pidfile before exit parent.
# Fork a second child and exit immediately to prevent zombies. This
# causes the second child process to be orphaned, making the init
# process responsible for its cleanup. And, since the first child is
# a session leader without a controlling terminal, it's possible for
# it to acquire one by opening a terminal in the future (System V-
# based systems). This second fork guarantees that the child is no
# longer a session leader, preventing the daemon from ever acquiring
# a controlling terminal.
- https://stackoverflow.com/a/881434
--
Best regards,
Gena
More information about the systemd-devel
mailing list