Skip Menu |

This queue is for tickets about the Proc-Daemon CPAN distribution.

Report information
The Basics
Id: 64680
Status: resolved
Priority: 0/
Queue: Proc-Daemon

People
Owner: Nobody in particular
Requestors: RWSTAUNER [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.05
Fixed in: (no value)



Subject: PGID does not exist after calling Proc::Daemon::Init() (POSIX::setsid())
Using perl 5.8.3 on Linux (specifically Fedora Core 2 (5+ years old))... After doing Proc::Daemon::Init(); the current process group id is invalid (there is no process by that ID). This is because the POSIX::setsid() is called after the first fork and before the second. The daemon I had written was attempting to send a signal to its process group (an example I found in perlipc), and it took me a while to diagnose why it wasn't working... Specifically I had one process that forked and maintained a child (which looped around a system call. The main process was set up to intercept TERM and then pass it on to its children (using its process group): kill(TERM => -$$); But the children never got the signal, because they all belonged to a process group that was surprisingly different than my main process. I would end up with a ps listing like so: PID PGID 102 101 main 103 101 second 104 101 third main originally had a PID of 100... then Proc::Daemon::Init forked (PID 101), POSIX::setsid() (PGID 101), then forked again (PID 102). So I couldn't use "-$$" to signal the obvious process group. Trying to maintain my PIDs (particularly because I was doing a system()) seemed more complicated than necessary. When I realized what was happening I called POSIX::setsid() after calling Proc::Daemon::Init() and once again everything worked as expected. So I don't know if you have an argument one way or another about the way that it currently works, but at the very least I was confused and fooled around (with the wrong code) for an hour. So if there's a good reason for leaving the PGID at an unexistent process, then I'd suggest that you at least document it. Otherwise I appreciate the module (especially its simplicity) and look forward to replacing some more old code I have with a simple Proc::Daemon::Init() call. Thanks!
From: deti [...] cpan.org
Hi RWSTAUNER! Thanks for reporting! Show quoted text
> After doing Proc::Daemon::Init(); the current process > group id is invalid (there is no process by that ID).
Confirmed. Can you test the attached Daemon.pm in the .zip by replacing it with the original? Does it solve your problem? Show quoted text
> Otherwise I appreciate the module (especially its simplicity) > and look forward to replacing some more old code I have with a simple > Proc::Daemon::Init() call. > Thanks!
Thanks!
Subject: Daemon.zip
Download Daemon.zip
application/zip 5.1k

Message body not shown because it is not plain text.

I'm not sure this is a real bug. One of the main goals of running as a daemon process is to completely detaching from the parent process, including any session ID it may have. This way, signals to the session ID does not go to the daemon process. I.e. By definition, a daemon runs with its own session ID. It seems the case reported here is the "daemon" is not truly a daemon.
From: deti [...] cpan.org
Show quoted text
> I'm not sure this is a real bug. One of the main goals of running > as a daemon process is to completely detaching from the parent > process, including any session ID it may have. This way, signals > to the session ID does not go to the daemon process. > > I.e. By definition, a daemon runs with its own session ID. > > It seems the case reported here is the "daemon" is not truly > a daemon.
I wasn't sure too. Unfortunately I can't find an example out in the web showing and explaining a double-fork with setsid. Only double-fork without setsid or single fork with setsid. At the moment (with our double-fork) we detach from the parent and then we exit the detached child leaving the grandchild running without session leader. I came to the conclusion that this can't be right too. So in my previously attached file the grandchild (real daemon) also becomes a session leader. This way it is detached from the possibly running grandparent and the exited parent. Comments and/or examples how to do it better are strongly appreciated!
From: RWSTAUNER [...] cpan.org
Yes, that solves the problem and seems to make sense to me. Does that seem appropriate to you? There's no way for me to get the PGID in the way it works in v0.05, so I had to call setsid() myself. Just seemed like an extra step. Thanks! On Wed Jan 12 17:14:11 2011, DetlefPilzecker@web.de wrote: Show quoted text
> Hi RWSTAUNER! > > Thanks for reporting! > >
> > After doing Proc::Daemon::Init(); the current process > > group id is invalid (there is no process by that ID).
> > Confirmed. > > Can you test the attached Daemon.pm in the .zip by replacing it with > the original? Does it solve your problem? > >
> > Otherwise I appreciate the module (especially its simplicity) > > and look forward to replacing some more old code I have with a simple > > Proc::Daemon::Init() call. > > Thanks!
> > Thanks!
On Thu Jan 13 17:05:14 2011, RWSTAUNER wrote:
Show quoted text
> Yes, that solves the problem and seems to make sense to me.
> Does that seem appropriate to you?

First it did, now it doesn't any more.
After many researches I will add the following comment into the source code:

Show quoted text
# ...
# This second fork is not absolutely necessary, it is more a precaution.
# 1. Prevent possibility of reacquiring a controlling terminal.
# Without this fork the daemon would remain a session-leader. In
# this case there is a potential possibility that the process could
# reacquire a controlling terminal. E.g. if it opens a terminal device,
# without using the O_NOCTTY flag. In Perl this is normally the case
# when you use <open>, instead of <sysopen> with the O_NOCTTY flag
# set.
# ...

And in the documentation I will add: Show quoted text
NOTE: Because of the second fork the daemon will not be a session-leader and
therefore Signals will not be send to other members of his process group. If
you need the functionality of a session-leader you may want to call
POSIX::setsid() manually on your daemon.
I think this is the right way to do it. 
Thanks again for your contribution!
From: RWSTAUNER [...] cpan.org
Sounds ok to me. As long as the current functionality is clear (in comments and/or pod) I understand what's happening and how I should use the module. Thanks for investigating/considering this!
Uploaded version 0.06 on PAUSE now. It should be available in some hours at CPAN.