[BACK]Return to NOTES CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sbin / init

File: [cvs.NetBSD.org] / src / sbin / init / NOTES (download)

Revision 1.3, Tue Apr 18 11:40:26 2006 UTC (8 years, 5 months ago) by salo
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, wrstuden-fixsa-newbase, wrstuden-fixsa-base-1, wrstuden-fixsa-base, wrstuden-fixsa, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-7-base, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, netbsd-4-base, netbsd-4-0-RELEASE, netbsd-4-0-RC5, netbsd-4-0-RC4, netbsd-4-0-RC3, netbsd-4-0-RC2, netbsd-4-0-RC1, netbsd-4-0-1-RELEASE, netbsd-4-0, netbsd-4, mjf-devfs2-base, mjf-devfs2, mjf-devfs-base, mjf-devfs, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, cube-autoconf-base, cube-autoconf, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, abandoned-netbsd-4-base, abandoned-netbsd-4, HEAD
Changes since 1.2: +8 -3 lines

Add support for running multi-user in a chroot() environment.

How it works:

- after successful execution of /etc/rc, check the value of "init.root"
  sysctl node, if it's different than "/", chroot() into its value and run
  /etc/rc inside the chroot(),

- in single-user, return back to the original / file system.

Allows running with / file system on e.g., cgd(4), vnd(4) or ccd(4) volumes.

Idea first discussed with Matt Thomas, implemented by Jachym Holecek <freza
(at) liberouter.org> with some nitpicks by me.  Successfully used by me for
almost a year with / on a cgd(4) volume (for more information about the setup
check ftp://ftp.NetBSD.org/pub/NetBSD/misc/salo/init-chroot/ ).

$NetBSD: NOTES,v 1.3 2006/04/18 11:40:26 salo Exp $

POSIX and init:
--------------

POSIX.1 does not define 'init' but it mentions it in a few places.

B.2.2.2, p205 line 873:

	This is part of the extensive 'job control' glossary entry.
	This specific reference says that 'init' must by default provide
	protection from job control signals to jobs it starts --
	it sets SIGTSTP, SIGTTIN and SIGTTOU to SIG_IGN.

B.2.2.2, p206 line 889:

	Here is a reference to 'vhangup'.  It says, 'POSIX.1 does
	not specify how controlling terminal access is affected by
	a user logging out (that is, by a controlling process
	terminating).'  vhangup() is recognized as one way to handle
	the problem.  I'm not clear what happens in Reno; I have
	the impression that when the controlling process terminates,
	references to the controlling terminal are converted to
	references to a 'dead' vnode.  I don't know whether vhangup()
	is required.

B.2.2.2, p206 line 921:

	Orphaned process groups bear indirectly on this issue.  A
	session leader's process group is considered to be orphaned;
	that is, it's immune to job control signals from the terminal.

B.2.2.2, p233 line 2055:

	'Historically, the implementation-dependent process that
	inherits children whose parents have terminated without
	waiting on them is called "init" and has a process ID of 1.'

	It goes on to note that it used to be the case that 'init'
	was responsible for sending SIGHUP to the foreground process
	group of a tty whose controlling process has exited, using
	vhangup().  It is now the responsibility of the kernel to
	do this when the controlling process calls _exit().  The
	kernel is also responsible for sending SIGCONT to stopped
	process groups that become orphaned.  This is like old BSD
	but entire process groups are signaled instead of individual
	processes.

	In general it appears that the kernel now automatically
	takes care of orphans, relieving 'init' of any responsibility.
	Specifics are listed on the _exit() page (p50).

On setsid():
-----------

It appears that neither getty nor login call setsid(), so init must
do this -- seems reasonable.  B.4.3.2 p 248 implies that this is the
way that 'init' should work; it says that setsid() should be called
after forking.

Process group leaders cannot call setsid() -- another reason to
fork!  Of course setsid() causes the current process to become a
process group leader, so we can only call setsid() once.  Note that
the controlling terminal acquires the session leader's process
group when opened.

Controlling terminals:
---------------------

B.7.1.1.3 p276: 'POSIX.1 does not specify a mechanism by which to
allocate a controlling terminal.  This is normally done by a system
utility (such as 'getty') and is considered ... outside the scope
of POSIX.1.'  It goes on to say that historically the first open()
of a tty in a session sets the controlling terminal.  P130 has the
full details; nothing particularly surprising.

The glossary p12 describes a 'controlling process' as the first
process in a session that acquires a controlling terminal.  Access
to the terminal from the session is revoked if the controlling
process exits (see p50, in the discussion of process termination).

Design notes:
------------

your generic finite state machine
we are fascist about which signals we elect to receive,
	even signals purportedly generated by hardware
handle fatal errors gracefully if possible (we reboot if we goof!!)
	if we get a segmentation fault etc., print a message on the console
	and spin for a while before rebooting
	(this at least decreases the amount of paper consumed :-)
apply hysteresis to rapidly exiting gettys
check wait status of children we reap
	don't wait for stopped children
don't use SIGCHILD, it's too expensive
	but it may close windows and avoid races, sigh
look for EINTR in case we need to change state
init is responsible for utmp and wtmp maintenance (ick)
	maybe now we can consider replacements?  maintain them in parallel
	init only removes utmp and closes out wtmp entries...

necessary states and state transitions (gleaned from the man page):
	1: single user shell (with password checking?); on exit, go to 2
	2: run rc script, on exit 0 check if init.root sysctl != "/", if it
           differs then fork + chroot into the value of init.root and run
           /etc/rc inside the chroot: on exit 0, go to 3; on exit N (error),
           go to 1 (applies also to /etc/rc when init.root == "/")
	3: read ttys file: on completion, go to 4.  If we did chroot in
	   state 2, we chroot after forking each getty to the same dir
	   (init.root is not re-read)
	4: multi-user operation: on SIGTERM, go to 7; on SIGHUP, go to 5;
		on SIGTSTP, go to 6
	5: clean up mode (re-read ttys file, killing off controlling processes
		on lines that are now 'off', starting them on lines newly 'on')
		on completion, go to 4
	6: boring mode (no new sessions); signals as in 4
	7: death: send SIGHUP to all controlling processes, reap for 30 seconds,
		then go to 1 (warn if not all processes died, i.e. wait blocks)
Given the -s flag, we start at state 1; otherwise state 2