On Tue, Oct 19, 1999 at 09:13:15PM +0200, Niels M?ller wrote:
> Thayne Harbaugh <[EMAIL PROTECTED]> writes:
>
> > daemonization cannot be done be a shell script - it has to happen
> > in the executeable. It is quite easy to do, however:
>
> Ok, I guess I should drop that idea. Even if I'm not sure I see why it
> can't be done (at least on this machine, there's a setsid program that
> should be usable from a script).
>
> > cd /
> > fork/exec - parent exits
> > set sid
> > close all file descriptors (more of a concern on sysV)
> > fork/exec - parent exits
> > syslog all messages
> >
> > It's also a good idea to add some commandline args to tell it
> > to not fork/exec and become a daemon and also dump messages to
> > stdout/stderr instead of syslog.
preventing core dumps is also a good idea for security,
and creating a locked pid file to prevent multiple instances running,
and leaving stdin/out/err open to /dev/null for stray library calls
that want them to be open to something. also, the second fork/exec
is only usefdul on svr4 (to prevent gaining a controlling terminal).
on bsd it doesn't prevent it. you still need to use O_NOCTTY when
opening terminals.
> Another case is if the deamon is started directly by init (listed in
> inittab). In this case it's a bad idea to fork and let the parent
> exit. Perhaps
>
> if ( (getppid() != 1) && fork() ) exit(0);
>
> should do that trick.
there's shouldn't be any problem as long as init hasn't been told
to respawn it.
another case is daemons started by inetd. if stdin/out/err are open
to a socket, the daemon was invoked by inetd and is already in an
appropriate context for a daemon so the fork/exec/setsid... isn't
necessary and the closing of file descriptors would be very bad.
> > If you have any questions concerning the above - let me know.
>
> Thanks.
>
> > Hmmm. I guess I'm just as liable telling you how to do it as
> > writing the code - maybe I'll just write some code . . . I dunno
> > (stinking munition laws).
> >
>
> Somebody else just offered to write the code. So we'll see what
> happens.
i've got the code at home (but i'm at work now).
i can mail it to you later today if you want it.
it doesn't handle the inetd case but if it needs
to it can be added in a few seconds.
raf