Execution Process of the/sbin/INIT program in Linux

Source: Internet
Author: User

After init is started, it runs
Start various transactions to continue the boot process (check and monitor the file system, start the background program daemons, and so on) until all the user's operating environment settings are completed. This mainly involves four steps
Order: init, Getty (agetty), login, and Shell programs. The relationships between the four programs are shown in.

 

The main task of the INIT process is to execute the set life cycle according to the information set in the/etc/rc file.
Create a sub-process for each terminal device that is allowed to log on using fork () according to the information in the/etc/inittab file, and run it in each newly created sub-process
Agetty
(Getty) program. The INIT process calls wait () to wait for the completion of the sub-process. Every time one of its sub-processes stops and exits, it will know according to the PID Number returned by wait ().
The sub-process of the corresponding terminal is over. Therefore, a new sub-process is created for the corresponding terminal device and the agetty program is re-executed in the sub-process. In this way, each allowed Terminal Device
There is always a corresponding process waiting for processing.

In normal operation, init determines that agetty is working to allow users to log on and receive
Process. An isolated process is the process whose parent process has ended. All processes in Linux must belong to a single process tree, so the isolated process must be charged. When the system is disabled, init is responsible for killing
Stop all other processes, Uninstall all file systems, stop the processing of the processor, and configure it as needed.

 

The main task of the Getty program is to set the terminal type, attributes, speed, and line rules. It opens and initializes
Tty port, display prompt information, and wait for the user to enter the user name. This program can only be executed by Super Users. Generally, if a/etc/issue text file exists, Getty first displays
And then display the logon prompt information (for example, plinux login:), read the user-typed login name, and execute the login program.

To allow the INIT program to run Getty, the/etc/inittab file must contain the Getty (agetty) command. The following is an example of agetty in the/etc/inittab file.

 

List inittab files in the 3.1 poeigl-1.2

# Inittab for Linux, poeigl 1.2

# Format:

# Ttyline: termcap-entry: Getty-command

Tty1: con80x60:/bin/agetty 9600 tty1

Tty2: con80x60:/bin/agetty 9600 tty2

Tty3: con80x60:/bin/agetty 9600 tty3

Tty4: con80x60:/bin/agetty 9600 tty4

# Tty5: con80x60:/bin/agetty 9600 tty5

# Tty64: Dumb:/bin/agetty 9600 tty64

# Tty65: Dumb:/bin/agetty-m-t60 2400 tty65

 

Each terminal has its own Getty command. Which lists the logon item messages for the tty1-tty4
. The comment line starts. The column 1st is the name of the terminal device used, and the column 2nd is the type of the specified terminal. The terminal type is con80x60. The 3rd column is the command executed and its parameters.
Number. The tty64 and tty65 in the last two lines correspond to the terminal connected to the serial port.

For terminals directly connected to the host using the serial port and the end of the modem dial-up connection
Linux agetty program has other attributes. For example, the TTY setting information is automatically adjusted when the logon name is read, such as the parity bit, except character, Line End character, and top key character.
. You can check the Transmission baud rate from the link's Hayes compatible modem information.

The format of each parameter in/dev/inittab is related to the specific Getty program. Currently, Getty programs are commonly used in the following ways:

 

1. agetty (sometimes called Getty): it is easy to set and does not require configuration files. Applicable to terminals connected directly;

2. Getty (part of getty_ps): Applicable to terminals connected directly;

3. mgetty: it is best suited to modem connections or direct connections;

4. uugetty: used only to connect to the terminal through MODEM, which is part of the getty_ps software package;

5. mingetty: simple Getty. Applicable to console terminals or virtual terminals;

6. fbgetty: Applicable to the console or virtual terminal.

RedHat 9 The System Default Configuration contains the mingetty and agetty programs. The console or virtual terminal uses mingetty. For actual character terminals, agetty is generally used. Therefore, the following information is displayed in the/etc/inittab file of the RedHat 9 system.

 

List 3.2 Getty information in the/etc/inittab file of the RedHat 9 System

# Run Gettys in standard runlevels

1: 2345: respawn:/sbin/mingetty tty1

2: 2345: respawn:/sbin/mingetty tty2

3: 2345: respawn:/sbin/mingetty tty3

4: 2345: respawn:/sbin/mingetty tty4

5: 2345: respawn:/sbin/mingetty tty5

6: 2345: respawn:/sbin/mingetty tty6

 

The 1st column indicates the number after the TTY name, and 2345 indicates the running layer of the mingetty. Respawn indicates that if the mingetty is terminated, mingetty will be automatically executed again. /Sbin/mingetty is a command. Ttyn indicates/dev/ttyn (N indicates the number 1-5 ).

After logging on to Linux, you will find that the original Getty process on your terminal cannot be found (using the "TOP" or "PS-Ax" command. Because the Getty process executes the login program, it is replaced with the login process, and finally replaced with your login shell process.

 

When you are in the "login:
"After you type your username at the prompt, Getty will read the username and execute the login program, and pass the username information to it. Therefore, the Getty process is replaced with login
. The login process then asks you to enter the password. After the password check is passed, the program corresponding to the record in your username in the/etc/passwd file will be executed. Usually this program is
BASH Shell program. Therefore, the original Getty process is replaced with the bash process, and the corresponding three programs have the same process ID.

 

When you log out
All processes on the terminal will be terminated (killed), including logging on to the shell process bash. Therefore, for
Once the bash program is replaced and executed is terminated or exited, The INIT process creates a new Getty process for the corresponding terminal.

 

The login program is mainly used to require the login user to enter the password. Based on the user name entered by the user
Obtain the logon entry of the corresponding user in passwd, call getpass () to display the "Password:" prompt information, read the password entered by the user, and then use the encryption algorithm
And compare it with the pw_passwd field in the password file. If the password entered several times is invalid, the login program will exit execution with Error Code 1.
Indicates that the logon process failed. At this time, the wait () of the parent process (process init) will return the PID of the exited process. Therefore, a child process is created again based on the recorded information and
Execute the agetty program again for the terminal device and repeat the above process.

 

The login program can also be executed as a command in Shell during running. In this case, it can
Switch from one user to another at any time. If no parameter is provided during execution, login displays the prompt message for entering the user name. If the user is not a Super User (Root), and
A file named nologin exists in the/etc/directory. The information in the file will be displayed and the logon process will be terminated.

 

If special access restrictions are specified for the user in the/etc/usertty file, these restrictions must be met. If you are a super user, the used login tty device must be specified in the/etc/securetty file.

 

After all these conditions are met, login also requires the user to enter the password and check it. For example
If. hushlogin exists, login performs a "quiet" login process, that is, it does not check whether there is an email or display the last logon time and information in the motd file. Otherwise
If the/var/log/lastlog file exists, the last logon time is displayed.

 

If the password entered by the user is correct, login sets the current working directory (currend
Work
Directory) to the starting working directory of the user specified in the password file. Modify the access permission of the terminal device to user read/write and group write, and set the group ID of the process. Then use the obtained
Environment variable information, such as the start directory (home =), the shell program used (shell =), the user name (user = and LOGNAME =), and the system execution process.
The default path sequence (Path = ). Then, the text information in the/etc/motd file (Message-of-the-day) is displayed, and the user's email is checked and displayed.
. Finally, the login program changes to the user ID of the logged-on user and executes the shell program specified in this user item in the password file, such as Bash or CSH.

 

If no shell program is specified in this user item in the password file/etc/passwd,
The system uses the default/bin/sh program. If the user's start directory is not specified in the password file, the system uses the default root directory /. Execution of the login program
For more information about the options and special access restrictions, see the online manual page (MAN 8 login) in Linux ).

 

Shell is a complex command line interpreter. It is executed when a user logs on to the system for interactive operations.
Program. It is the place where users interact with computers. It obtains user input information and then executes the command. You can directly interact with the shell on the terminal, or use the shell
Input the script file to the shell interpreter. In Linux systems, common shells are:

 

Bourne again shell,/bin/bash

C shell,/bin/CSH (or tcsh)

BSD shell/bin/ash (or bsh)

During the login process, the system (LOGIN) will know which shell program should be executed for the user from the last field of the logon item corresponding to the user in the password file.

 

The shell program implements a language with a flow control structure, which is widely used. Currently
All Shell programs are directed to the IEEE POSIX
1003.2 compatibility is evolving. Therefore, although they have their own characteristics, their basic functions are becoming more and more similar. This book mainly introduces the working principle and implementation mechanism of bash.
The implementation mechanism of shell is similar.

 

When login starts executing Shell during logon, the first character of argv [0] is included.
Is '-', indicating that the shell is executed as a logon shell. In this case, the shell program executes some operations corresponding to the logon process based on the character. When you log on to the shell
The/etc/profile file and the. profile file (if any) read and run the command. If the ENV Environment variable is set when you enter the shell, or
If this variable is set in the. profile file of shell, shell will read and execute the command from the file named after the variable in the next step. Therefore, you should put the command to be executed every time you log on.
In the. profile file, place the command to be executed every time you run the shell in the file specified by the env variable. You can set the ENV Environment Variable by placing the following statements in your initial directory.
. Profile file.

 

ENV = $ home/. anyfilename; export env

 

When executing shell, in addition to some specified options, if the command line parameter is also specified, shell will regard the first parameter as a script file name and execute the command, other parameters are considered as shell location parameters (such as $1 and $2 ). Otherwise, the shell program will read the command from its standard input.

 

There are many options for executing Shell programs. For more information, see the description on the sh online manual page in Linux.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.