xref: /openbsd-src/sbin/init/NOTES (revision 7b91b0e0924a7b48e47836349650bb5814f82be0)
1*7b91b0e0Sderaadt$OpenBSD: NOTES,v 1.2 1996/06/23 14:30:49 deraadt Exp $
2df930be7Sderaadt$NetBSD: NOTES,v 1.2 1995/03/18 14:56:29 cgd Exp $
3df930be7Sderaadt
4df930be7SderaadtPOSIX and init:
5df930be7Sderaadt--------------
6df930be7Sderaadt
7df930be7SderaadtPOSIX.1 does not define 'init' but it mentions it in a few places.
8df930be7Sderaadt
9df930be7SderaadtB.2.2.2, p205 line 873:
10df930be7Sderaadt
11df930be7Sderaadt	This is part of the extensive 'job control' glossary entry.
12df930be7Sderaadt	This specific reference says that 'init' must by default provide
13df930be7Sderaadt	protection from job control signals to jobs it starts --
14df930be7Sderaadt	it sets SIGTSTP, SIGTTIN and SIGTTOU to SIG_IGN.
15df930be7Sderaadt
16df930be7SderaadtB.2.2.2, p206 line 889:
17df930be7Sderaadt
18df930be7Sderaadt	Here is a reference to 'vhangup'.  It says, 'POSIX.1 does
19df930be7Sderaadt	not specify how controlling terminal access is affected by
20df930be7Sderaadt	a user logging out (that is, by a controlling process
21df930be7Sderaadt	terminating).'  vhangup() is recognized as one way to handle
22df930be7Sderaadt	the problem.  I'm not clear what happens in Reno; I have
23df930be7Sderaadt	the impression that when the controlling process terminates,
24df930be7Sderaadt	references to the controlling terminal are converted to
25df930be7Sderaadt	references to a 'dead' vnode.  I don't know whether vhangup()
26df930be7Sderaadt	is required.
27df930be7Sderaadt
28df930be7SderaadtB.2.2.2, p206 line 921:
29df930be7Sderaadt
30df930be7Sderaadt	Orphaned process groups bear indirectly on this issue.  A
31df930be7Sderaadt	session leader's process group is considered to be orphaned;
32df930be7Sderaadt	that is, it's immune to job control signals from the terminal.
33df930be7Sderaadt
34df930be7SderaadtB.2.2.2, p233 line 2055:
35df930be7Sderaadt
36df930be7Sderaadt	'Historically, the implementation-dependent process that
37df930be7Sderaadt	inherits children whose parents have terminated without
38df930be7Sderaadt	waiting on them is called "init" and has a process ID of 1.'
39df930be7Sderaadt
40df930be7Sderaadt	It goes on to note that it used to be the case that 'init'
41df930be7Sderaadt	was responsible for sending SIGHUP to the foreground process
42df930be7Sderaadt	group of a tty whose controlling process has exited, using
43df930be7Sderaadt	vhangup().  It is now the responsibility of the kernel to
44df930be7Sderaadt	do this when the controlling process calls _exit().  The
45df930be7Sderaadt	kernel is also responsible for sending SIGCONT to stopped
46df930be7Sderaadt	process groups that become orphaned.  This is like old BSD
47df930be7Sderaadt	but entire process groups are signaled instead of individual
48df930be7Sderaadt	processes.
49df930be7Sderaadt
50df930be7Sderaadt	In general it appears that the kernel now automatically
51df930be7Sderaadt	takes care of orphans, relieving 'init' of any responsibility.
52df930be7Sderaadt	Specifics are listed on the _exit() page (p50).
53df930be7Sderaadt
54df930be7SderaadtOn setsid():
55df930be7Sderaadt-----------
56df930be7Sderaadt
57df930be7SderaadtIt appears that neither getty nor login call setsid(), so init must
58df930be7Sderaadtdo this -- seems reasonable.  B.4.3.2 p 248 implies that this is the
59df930be7Sderaadtway that 'init' should work; it says that setsid() should be called
60df930be7Sderaadtafter forking.
61df930be7Sderaadt
62df930be7SderaadtProcess group leaders cannot call setsid() -- another reason to
63df930be7Sderaadtfork!  Of course setsid() causes the current process to become a
64df930be7Sderaadtprocess group leader, so we can only call setsid() once.  Note that
65df930be7Sderaadtthe controlling terminal acquires the session leader's process
66df930be7Sderaadtgroup when opened.
67df930be7Sderaadt
68df930be7SderaadtControlling terminals:
69df930be7Sderaadt---------------------
70df930be7Sderaadt
71df930be7SderaadtB.7.1.1.3 p276: 'POSIX.1 does not specify a mechanism by which to
72df930be7Sderaadtallocate a controlling terminal.  This is normally done by a system
73df930be7Sderaadtutility (such as 'getty') and is considered ... outside the scope
74df930be7Sderaadtof POSIX.1.'  It goes on to say that historically the first open()
75df930be7Sderaadtof a tty in a session sets the controlling terminal.  P130 has the
76df930be7Sderaadtfull details; nothing particularly surprising.
77df930be7Sderaadt
78df930be7SderaadtThe glossary p12 describes a 'controlling process' as the first
79df930be7Sderaadtprocess in a session that acquires a controlling terminal.  Access
80df930be7Sderaadtto the terminal from the session is revoked if the controlling
81df930be7Sderaadtprocess exits (see p50, in the discussion of process termination).
82df930be7Sderaadt
83df930be7SderaadtDesign notes:
84df930be7Sderaadt------------
85df930be7Sderaadt
86df930be7Sderaadtyour generic finite state machine
87df930be7Sderaadtwe are fascist about which signals we elect to receive,
88df930be7Sderaadt	even signals purportedly generated by hardware
89df930be7Sderaadthandle fatal errors gracefully if possible (we reboot if we goof!!)
90df930be7Sderaadt	if we get a segmentation fault etc., print a message on the console
91df930be7Sderaadt	and spin for a while before rebooting
92df930be7Sderaadt	(this at least decreases the amount of paper consumed :-)
93df930be7Sderaadtapply hysteresis to rapidly exiting gettys
94df930be7Sderaadtcheck wait status of children we reap
95df930be7Sderaadt	don't wait for stopped children
96df930be7Sderaadtdon't use SIGCHILD, it's too expensive
97df930be7Sderaadt	but it may close windows and avoid races, sigh
98df930be7Sderaadtlook for EINTR in case we need to change state
99df930be7Sderaadtinit is responsible for utmp and wtmp maintenance (ick)
100df930be7Sderaadt	maybe now we can consider replacements?  maintain them in parallel
101df930be7Sderaadt	init only removes utmp and closes out wtmp entries...
102df930be7Sderaadt
103df930be7Sderaadtnecessary states and state transitions (gleaned from the man page):
104df930be7Sderaadt	1: single user shell (with password checking?); on exit, go to 2
105df930be7Sderaadt	2: rc script: on exit 0, go to 3; on exit N (error), go to 1
106df930be7Sderaadt	3: read ttys file: on completion, go to 4
107df930be7Sderaadt	4: multi-user operation: on SIGTERM, go to 7; on SIGHUP, go to 5;
108df930be7Sderaadt		on SIGTSTP, go to 6
109df930be7Sderaadt	5: clean up mode (re-read ttys file, killing off controlling processes
110df930be7Sderaadt		on lines that are now 'off', starting them on lines newly 'on')
111df930be7Sderaadt		on completion, go to 4
112df930be7Sderaadt	6: boring mode (no new sessions); signals as in 4
113df930be7Sderaadt	7: death: send SIGHUP to all controlling processes, reap for 30 seconds,
114df930be7Sderaadt		then go to 1 (warn if not all processes died, i.e. wait blocks)
115df930be7SderaadtGiven the -s flag, we start at state 1; otherwise state 2
116