🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.protocols.iso.dev-environ
Article #471

Why do daemons fork five times?

#471
From: Peter Whittaker, P.W.
Date: Mon, 18 Feb 1991 20:03
63 lines
2956 bytes
One problem with posting to get answers is that if you make a booboo in
your post, people respond to that rather than to the intent of your post....

So, thanks to those who pointed out that my comments on the code snip below
were misplaced!  Regardless of whether it is the parent exiting or the child
exiting, could anyone tell me why the
     for (i = 0; i < 5; i++) {
is included, i.e. why try five times at five second intervals?  Especially
when the parent's behavior after the 5 attempts is identical with the child's
behavior after a succesful attempt?

Why don't the daemons just shut the ttys directly, when they get to that
point?  Why the elaborate if not fork then sleep and try again
                          if fork and child close ttys
                          if fork and parent exit   ?

One suggestion was that this is to wait the roughly 40 seconds required
to unregister a previously  active socket, but an active socket will not
prevent a fork (and this routine is called before there are any sockets
open).

Why not just do this:

         close(0); close(1); close(2);
         fd = open("/dev/tty", O_RDWR);
         ioctl(fd, TIOCNOTTY, (void *) 0);
         close(fd);
/* error checking removed for clarity */
This will close stdin, stderr, and stdout, then disassociate the process
from any tty whatsoever (via ioctl()).

The ioctl statement is in fact done (where TIOCNTTY is available) just after
the example of code below, but if it is available, why bother with all the
fork()ing and dup()ing? It seems more like overkill (and overkill that
doesn't work - since the dup()s aren't error checked) than anything else.

 if (!(debug = isatty (2))) {
  for (i = 0; i < 5; i++) {
   switch (fork ()) {
       case NOTOK:
    sleep (5);
    continue;
       case OK: /* child - proceed to closing all ttys */
    break;
       default: /* parent - _exit() - do not use any atexit() routines */
    _exit (0);
   }
   break;
  }

  (void) chdir ("/");

  if ((sd = open ("/dev/null", O_RDWR)) == NOTOK)
   adios ("/dev/null", "unable to read");
  if (sd != 0)
   (void) dup2 (sd, 0), (void) close (sd);
  (void) dup2 (0, 1);
  (void) dup2 (0, 2);

Peter Whittaker      [~~~~~~~~~~~~~~~~~~~~~~~~~~]   Open Systems Integration
pww@bnr.ca           [                          ]   Bell Northern Research
Ph: +1 613 765 2064  [                          ]   P.O. Box 3511, Station C
FAX:+1 613 763 3283  [__________________________]   Ottawa, Ontario, K1Y 4H7
Technical Details
Message-ID: <91Feb18.151607est.57826@ugw.utcs.utoronto.ca>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!BNR.CA!PWW

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!BNR.CA!PWW
From: PWW@BNR.CA (Peter  Whittaker, P.W.)
Newsgroups: comp.protocols.iso.dev-environ
Subject: Why do daemons fork five times?
Message-ID: <91Feb18.151607est.57826@ugw.utcs.utoronto.ca>
Date: 18 Feb 91 20:03:00 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Distribution: inet
Organization: The Internet
Lines: 63