pidfile and become_daemon

Michael Steffens michael_steffens at
Fri Feb 15 08:57:02 GMT 2002

Hi Tim,

as 2.2.3 winbindd does not yet create a pid file preventing
it from being started multiple times, I started fiddling around
with that.  And things became less and less clear to me :-)

HEAD winbindd does have a create_pidfile call(), which is before
become_daemon() gets called.  Fine this way, because the caller
of winbindd can be notified about failure.

But the pid written to that file is meaningless, because
after that become_daemon() (lib/util.c) forks

        if (sys_fork()) {

  /* detach from the terminal */
#elif defined(TIOCNOTTY)
                int i = sys_open("/dev/tty", O_RDWR, 0);
                if (i != -1) {
                        ioctl(i, (int) TIOCNOTTY, (char *)0);      
#endif /* HAVE_SETSID */

making the daemon pid a different one! What is this fork good
for? Detaching is performed right afterwards...

Another question is about the pidfile size, rather cosmetical
may be.  But create_pidfile() always writes the entire buffer
of 20 bytes, regardless of content's size:

        memset(buf, 0, sizeof(buf));
        slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) sys_getpid());
        if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
                DEBUG(0,("ERROR: can't write to file %s: %s\n", 
                         pidFile, strerror(errno)));


        if (write(fd, buf, strlen(buf) + 1) != strlen(buf) + 1) {

make more sense?

And the last question may also belong to the cosmetical ones:
Why don't the Samba daemons clean up their pid files on termination?


More information about the samba-technical mailing list