In the previous chapters we learned that there is a correlation between processes:
- Each process has a parent process;
- When a child process exits, the parent process can perceive and get the exit status of the child process.
In this chapter we will look at:
- More details of the process group;
- The content of sessions;
- The relationship between the login shell and the process that we started from the login shell.
?
One terminal login (Terminal Logins) BSD Terminal Logins
? The BSD terminal login program has not changed in the last 35 years.
- The system adminstrator creates a file/etc/ttys, each of which has a single line in the file, a row containing the login terminal name, and other parameters that are passed to the Getty function.
- One of the parameters is the transmission baud rate (baud) of the terminal.
- When the system boot is complete, the kernel creates the Init process with a process ID of 1. The INIT process is responsible for booting the system up.
- The init process reads the file/etc/ttys and fork a process for each logon device, and then executes the Exec run Getty program.
? The above process is as follows:
The real user ID of the process created by the Init process Fork is 0,effective User ID 0, and they all have superuser privileges.
The function of the program Getty: To invoke the open functions for the terminal equipment, once the device is opened, the file descriptor 0,1,2 is set to the device. Then Getty output Some prompts, waiting for us to enter the user name. When we enter the username, Getty's work is done, and then the login function is executed by calling the EXEC function, as follows.
Execle ("/bin/login", "Login", "-P", username, (char *) 0, ENVP);
After adding the login program, the process is as follows:
The process in which the fork comes out has superuser privileges because they fork out from the init process and the INIT process has superuser privileges.
The process IDs for the following processes are the same because the EXEC function does not change the ID of the process, and the ID of their parent process is 1.
Now the login program goes to login program execution, the login program will do the following things:
- According to the user name we entered, call the function Getpwnam get the password corresponding to the user name;
- Call function Getpass Print prompt Password:, wait to read the password we entered;
- Encrypt the password we entered, compare the encrypted password with the password obtained from the system password file, and if the password is different, the login program calls the Exit function to exit and returns to exit status 1. When the init process gets a 1 process termination state, the fork is executed again to retry the login.
If we log in correctly, the login program will continue to do the following things:
- Current working directory switch to our home directory (CHDIR);
- Change the ownership of our terminal equipment for ourselves all (chown);
- Change the authority of our terminal equipment, so that we can read from the terminal device and input;
- Set our group ID (setgid and initgroups);
- Initialize our environment variables;
- Change our User ID (setuid) to activate our login shell, such as execl ("/bin/sh", "-sh", (char *) 0);
? The process is as follows:
After our shell has started, it will read the boot file (. Profile or. bash_profile or. bash_login or. Profile, different naming of the system boot files). The role of these startup files is to increase the environment variables of the system, set some global variables, links and so on.
?
2 Network Login (Logins)
The difference between a physical login and a network logon is whether the connection to the host to the terminal is point-to-point.
In the case of network logons, a login is an available service, just like other services, such as FTP or SMTP.
The Network Logon service feature is not sure how many login requests will come. So the kernel is not waiting for every possible login, but instead is waiting for a network connection logon request through the network interface driver (interface drivers).
In order to handle both physical and network logins, a software driver called a virtual terminal (pseudo terminal) is used to map the behavior requests after the network login to the real terminal.
BSD Network Logins
The process inetd waits for most of the network connections to be processed.
Below we will learn about the process of network login.
- When the system starts, the init process creates a shell execution script/etc/rc, and one of the daemon processes is inetd. Once the script terminates, the parent process of the inetd process becomes the init process;
- INETD's responsibility is to wait for TCP/IP connection requests, and once a new connection request arrives, inetd executes the fork and exec to execute the corresponding handler;
- The TELNETD program initiates a telnet server, waits for the user to log on remotely, the user links the server through the TCP protocol, and logs in with a valid user password.
The process for starting the TELNETD program is as follows:
?
The action after the telnetd process starts is:
- Open a virtual terminal (pseudo teminal) and call fork to create two processes;
- The parent process processes connection requests from the network;
- The child process executes the EXEC function calling the login program;
- Parent and child processes are linked through a virtual terminal;
- If the child process is logged on correctly, the subsequent procedures and physical logins are the same.
The process is as follows:
?
? 3 Process Group (Groups)
Each process belongs to a process group.
A process group is a collection of processes that are often associated with the same job and receive signals from the same terminal.
Each process group has a unique process group ID.
The function GETPGRP returns the process group ID of the calling process.
function declaration:
? #include <unistd.h>
pid_t getpgrp (void);
? ? ? ? Returns:process group ID of calling process
?
pid_t getpgid (pid_t pid);
? ? ? ? Returns:process group ID If OK, 1 on Error
Function call Getpgid (0); and function call GETPGRP (); function, return the process group ID of the calling process.
Each process group has a head process with the same process ID as the process group ID.
The life cycle of a process group: starts with a process that creates a group, up to the process in which the process terminates within one group or becomes a different group.
A process can call a function Setpgid join to another process group or create a process group.
function declaration:
? #include <unistd.h>
int Setpgid (pid_t pid, pid_t pgid);
The process group ID for the process that sets the process ID for the PID is pgid.
If the PID and Pgid are the same as the process ID of a process, then the process PID becomes the head process of a process group.
If the PID is 0, the process to be set is the current process.
?
4 Sessions
A session is a collection of one or several process groups.
As shown in the following example:
A process creates a new session by calling the function Setsid.
function declaration:
? #include <unistd.h>
pid_t setsid (void);
? ? ? ? Returns:process group ID If OK, 1 on Error
If the calling process is not a group header process, three things happen:
- This process becomes the session leader of the new session created;
- The process becomes the head process of a new process group, and the new process group ID is the process number of the calling process;
- The process does not correlate the terminal.
The function GetSID returns the process group ID of a session leader process.
function declaration:
#include <unistd.h>
pid_t getsid (pid_t pid);
? ? ? ? Returns:session leader ' s process group ID if OK,-1 On Error
If the PID is 0, the function GetSID returns the process group number of the session leader process where the calling process resides.
?
?
Resources:
"Advanced programming in the UNIX envinronment 3rd"
?
UNIX Advanced Environment Programming (12) Process Association (relationships)-terminal login process, process Group, session