tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Library support for two-phase daemonization

> I think this is needed as soon as you want to spawn a process, via
> posix_spawn() or fork()/exec(), and you want to be notified of the
> proper startup/initialization of the process.

Since "proper startup" is a program-specific notion (eg, some programs
need to load config files), this cannot be done without help from the
program being run.

If you're satisfied with exec() succeeding, the pattern I usually use
is to create an AF_LOCAL/SOCK_STREAM socketpair, set the child's end
close-on-exec, and exec.  If the exec fails, I then (in the child)
write errno to the socketpair and exit.  The parent then does a recv()
with MSG_WAITALL.  If it gets zero, the exec worked; if it gets
sizeof(int), it can report that errno.  If it gets anything else, my
code usually just coughs and dies; I've never actually seen it happen.
(I'm leaving out lots of details, such as closing the parent's end of
the socketpair by the child.)

I don't like signal-based approaches, especially for library routines,
both because they conflict with other uses for the signal(s) used and
because they make it somewhere between much harder and impossible to
pass the errno from a failed exec back to the parent.  (If the child is
still connected to a useful error-reporting channel, the child can
report it directly.  This is not always the case.)

/~\ The ASCII                             Mouse
\ / Ribbon Campaign
 X  Against HTML      
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

Home | Main Index | Thread Index | Old Index