Project

General

Profile

Bug #2712

Wait for grandchild to be ready when daemonizing

Added by pasdVn over 1 year ago. Updated over 1 year ago.

Status:
Fixed
Priority:
Normal
Assignee:
-
Category:
core
Target version:
Start date:
2016-01-31
Due date:
% Done:

100%

Estimated time:
Missing in 1.5.x:

Description

When lighttpd does the sys-v-init daemonizing (without the -D switch) the parent process will immeadeatly terminate after the first fork (see function daemonize in server.c).
This can cause problems, when right after the start of lighttpd the presence/correctness of the pid is checked or the pid of the daemon process is tried to grabbed by `pidof lighttpd` or similar. The second problem becomes a problem because of the double forking. If the timing "fits" you get only the pid of the child but not the grandchild (or both).

Following this guide (http://www.freedesktop.org/software/systemd/man/daemon.html) it is a more clean way to terminated the parent process only if the child is fully up and running (after every initialization is ok and the pid file is written).

I wrote a patch that fixes this behaviour. As suggested in the reference above it uses a pipe as communication channel between parent and grandchild.

Associated revisions

Revision 3127 (diff)
Added by stbuehler over 1 year ago

[core] wait for grandchild to be ready when daemonizing (fixes #2712, thx pasdVn)

From: Glenn Strauss <>

Revision f11089ed (diff)
Added by gstrauss over 1 year ago

[core] wait for grandchild to be ready when daemonizing (fixes #2712, thx pasdVn)

From: Glenn Strauss <>

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.4.x@3127 152afb58-edef-0310-8abb-c4023f1b3aa9

History

#1 Updated by gstrauss over 1 year ago

The child should

    close(pipefd[0]);

before returning from daemonize(), or else that fd leaks in the child.

#2 Updated by gstrauss over 1 year ago

Additionally, when there are multiple workers, the first worker (greatgrandchild) calls write(), and then the grandparent calls read() and close(). Once you fix the pipefd[0] leak in the grandchild (which I mentioned in the comment above), then subsequent workers might fail to write() with EPIPE after the grandparent calls close() on the read-end of the pipe (pipefd[0]).

Please consider moving the write() to a location prior to spawning of workers, but after the daemonized grandchild is up and running, and after the grandchild has written its pid to the pidfile. Such a location would be a reasonable place for the grandparent to exit while still ensuring that pidof would return the proper pid of the running grandchild.

#3 Updated by pasdVn over 1 year ago

Thanks for your feedback. I moved the grandchild's write() directly before the fork-loop where the workers are created. I think this is the best place under this circumstances.

#4 Updated by gstrauss over 1 year ago

Thanks for the update. This second patch looks better to me.

stbuehler: what do you think?

#5 Updated by stbuehler over 1 year ago

  • Target version set to 1.4.40

#6 Updated by gstrauss over 1 year ago

Prepared patch with credit to pasdVn: https://github.com/lighttpd/lighttpd1.4/pull/32

#7 Updated by stbuehler over 1 year ago

  • Status changed from Patch Pending to Fixed
  • % Done changed from 0 to 100

Applied in changeset r3127.

Also available in: Atom