SysV
SysV-style init is invoked as pid 1 by the kernel. It:
- Spawns a set of processes specified in its configuration file to bring a system to a given runlevel.
- Reaps zombie child processes (processes that have either exited or killed by a signal, triggering a
SIGCHLD
signal to be sent to the parent). - Supervises
respawn
andondemand
processes. - Responds to signals.
- Processes commands received via
/run/initctl
(/dev/initctl
in later versions).
Runlevels
The runlevels standardised by SysV are:
0
: shutdown1
(orS
ors
): single user mode6
: reboot
Linux distributions typically extend these:
Runlevel | Debian-based | RHEL-based |
---|---|---|
0 | Halt | Halt |
1 | Single user mode | Single user mode |
2 | Multi user with GUI | Multi user without NFS |
3 | Unused; behaves as 2 | Full multi user mode |
4 | Unused; behaves as 2 | Unused |
5 | Unused; behaves as 2 | Unused |
6 | Reboot | Reboot |
init can be signalled to change the runlevel using telinit
, conventionally a link to the init
binary.
On-demand procedures
On-demand procedures are sets of processes (up to three; named a
, b
, and c
) that can be launched explicitly via telinit
invocations, though not during the boot sequence or from within single-user mode. Subsequent invocations restart the specified processes.
They're defined by using the characters (abc
) in place of a runlevel number.
Configuration
/etc/inittab
is the configuration file read by init
. It contains a series of directives in the form:
id:runlevels:action:process
In the above:
id
is a 1-4 character unique identity for the line.runlevels
lists the runlevels to which the action applies. If not specified, defaults to all runlevels (e.g.0123456789
).action
specifies what the configuration line does:- Assign a process to a runlevel:
respawn
causes the specifiedprocess
to be restarted on termination (e.g. for a TTY/PTY).wait
awaits completion ofprocess
before continuing.once
once upon entering the runlevel.
- Configure the boot sequence:
sysinit
defines processes required for early boot, regardless of runlevel.boot
adds a process to the boot process (aftersysinit
completion).bootwait
behaves asboot
, also awaiting completion ofprocess
before continuing.initdefault
specifies the default runlevel.
ondemand
specifies an on-demand procedure.- Responses to signals:
powerwait
powerfail
powerokwait
powerfailnow
ctrlaltdel
specifies a process to execute in response to init receivingSIGINT
.kbrequest
default runlevel includesS
- Assign a process to a runlevel:
Signal handling
init responds to signals as follows:
SIGHUP
reloads configuration from/etc/inittab
.SIGINT
starts the first activectrlaltdel
process.SIGWINCH
starts the first activekbrequest
process.SIGUSR1
has init close (if open) and reopen its control FIFO.SUGUSR2
has init close the control FIFO and not reopen it. It can later be reopened withSIGUSR1
.SIGPWR
causes init to read/var/run/powerstatus
(formerly/etc/powerstatus
) and take an action based on its content:O
(for OK): consider normal power status, that there's no need for a shutdown. Start all processes with apowerokwait
action field.L
(for low): consider the batterly level to be low and the power failing; an imminent need to shutdown. Start all processes with apowerfailnow
action field.- Anything else (conventionally
F
, for fail): consider the power to be failing, and a possible need for a shutdown in the near future. Start all processes with thepowerfail
andpowerwait
action fields.
Resources
Backlinks