🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Why do daemons fork five times?

#469
From: Peter Whittaker, P.W.
Date: Fri, 15 Feb 1991 22:15
48 lines
2111 bytes
Several of the ISODE daemons (including tsbridge, the one I'm currently
playing with) perform an odd "try to fork five times" dance before shutting
down all ttys and getting on with real work.  One colleague suggested it had
something to do with latency in certain OS's, but could not elaborate.

Why don't the daemons just shut the ttys direclty, when they get to that
point?  Why the elaborate if not fork then sleep and try again
                          if fork and child exit
                          if fork and parent close ttys?
I've included the code in question for your perusal.

One possible bug herein:  the calls to dup() are not error checked.
This has resulted in ttys not being dupped onto /dev/null properly
with the result that user ttyps sometimes get spurious error messages.

advThanksance,

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

Code follows:

	if (!(debug = isatty (2))) {
		for (i = 0; i < 5; i++) {
			switch (fork ()) {
			    case NOTOK:
				sleep (5);
				continue;
NOTE: if parent, go ahead and shut the ttys (see below)
			    case OK:
				break;
      if child, _exit(0)........
			    default:
				_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);
Technical Details
Message-ID: <91Feb15.173220est.57761@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: <91Feb15.173220est.57761@ugw.utcs.utoronto.ca>
Date: 15 Feb 91 22:15:00 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Distribution: inet
Organization: The Internet
Lines: 48