Init process on UNIX and Linux systems


Init is the parent of all processes, executed by the kernel during the booting of a system. Its principle role is to create processes from a script stored in the file /etc/inittab. It usually has entries which cause init to spawn gettys on each line that users can log in. It controls autonomous processes required by any particular system.

After reading this file, how the system should be set up in each runlevel is determined by init and also set default runlevel. Init starts all background process after setting default runlevel for the system.

Runlevels

Runlevel, a software configuration of the system which allows only a selected group of processes to exist. The processes produced by init for each of these runlevels are defined in the /etc/inittab file.

Init can be in one of these eight runlevels: 0-6 and S or s. The runlevel can be changed by having a privileged user run telinit, which sends appropriate signals to init, telling it which runlevel to change to.

Here,

  • 0,1,6-reserved runlevels.

  • S or s are same.

  • 7-9 are valid runlevels. though not really documented as "traditional" Unix variants don’t use them. runlevels S and s are in fact the same. Internally they are aliases for the same runlevel.

RunlevelsFunctions
0
To halt the system
1
To get the system down into single user mode
2

To get multiuser mode without networking
3
To get multiuser mode with networking
4
Not used
5
To get multiuser mode with networking and X windows
6
To reboot the system
S or s
Not used directly.

Booting

After invoking init as the last step of the kernel boot sequence, it sees if an entry of the type initdefault is present in the file /etc/inittab. The initdefault entry determines the initial runlevel of the system. If no such entry (or no /etc/inittab at all) is present there, a runlevel must be entered at the system console.

Changing Runlevels

After specifying all the processes, init waits for one of its descendant processes to die, a powerfail signal, or until it is signaled by telinit to change the system’s runlevel. It re-examines the /etc/inittab file, when one of the above three conditions occurs.

init still waits for one of the above three conditions to occur. For providing an instantaneous response, the telinit Q or q command can wake up init to re-examine the/etc/inittab file.

If init is not in single user mode and receives a powerfail signal (SIGPWR), it reads the file /etc/powerstatus. Then it starts a command based on the contents of this file −

TagDescription
F(AIL)
Power is failing, UPS is providing the power. Execute the powerwait and powerfail entries.
O(K)
Power has been restored, execute the powerokwait entries.
L(OW)
The power is failing and the UPS has a low battery. Execute the powerfailnow entries.

If /etc/powerstatus contains anything else then the letters F, O or L or doesn’t exist, init will behave as if it has read the letter F.

Usage of SIGPWR and /etc/powerstatus is discouraged. If Someone wanting to interact with init should use the /dev/initctl control channel - see the source code of the sysvinit package for more documentation about this.

When init is requested to change the runlevel, it sends the warning signal SIGTERM to all processes that are undefined in the new runlevel. Then It waits 5 seconds before forcibly terminating these processes via the SIGKILL signal.

Init assumes that all these processes and their descendants remain in the same process group which init originally created for them. It will not receive these signals, if any process changes its process group affiliation. Such processes need to be terminated separately.

TELINIT

/sbin/telinit is linked to /sbin/init which takes a one-character argument and signals init to perform the appropriate action. The following arguments serve as directives to telinit

TagDescription
0,1,2,3,4,5 or 6
tell init to switch to the specified run level.
a, b, c
tell init to process only those /etc/inittab file entries having runlevel a, b or c.
Q or q
tell init to re-examine the /etc/inittab file.
S or s
tell init to switch to single user mode.
U or u
tell init to re-execute itself (preserving the state). No re-examining of/etc/inittab file happens. Request would be silently ignored, if Run level is not one of Ss12345.

It can also tell init how long it should wait between sending processes the SIGTERM and SIGKILL signals. 5 seconds is the default, but this can be changed with the -t sec option.

Only by users with appropriate privileges can invoke telinit.

By looking at its process id, init binary checks if it is init or telinit; the real init’s process id is always 1. So, instead of calling telinit one can also just use init instead as a shortcut.

raja
Published on 11-Oct-2019 16:56:20
Advertisements