Apue (7)

Source: Internet
Author: User

 

9.2. Terminal logins
The system administrator creates a file, usually /etc/ttys, that has one line per terminal device. Each line specifies the name of the device and other parameters that are passed to the getty program. The init process reads the file /etc/ttys and, for every terminal device that allows a login, does a fork followed by an exec of the program getty.

All the processes shown in
Figure 9.1 have a real User ID of 0 and an alternative tive user ID of 0 (I. e., they all have superuser privileges).
InitProcess alsoExecS
GettyProgram with an empty environment.

It isGettyThat CILS
OpenFor the terminal device. The terminal is opened for reading and writing.

When we enter our user name,
Getty'S job is complete, and it then invokesLoginProgram, similar

 execle("/bin/login", "login", "-p", username, (char *)0, envp);

InitInvokes
GettyWith an empty environment;GettyCreates an environment
Login(EnvpArgument) with the name of the terminal (something like
Term = foo, Where the type of Terminal
FooIs taken fromGettytabFile ).

All the processes shown in
Figure 9.2 have superuser privileges, since the original
InitProcess has superuser privileges. The process ID of the bottom three processes in

Figure 9.2 is the same, since the process ID does not change into SS
Exec. Also, all the processes other than the originalInitProcess have a parent process ID of 1.

If we log in correctly,
LoginWill

  • Change to our home directory (Chdir)

  • Change the ownership of our terminal device (Chown) So we own it

  • Change the access permissions for our terminal device so we have permission to read from and write to it

  • Set our group IDs by calling
    SetgidAndInitgroups

  • Initialize the environment with all the information that
    LoginHas: Our Home Directory (Home), Shell (Shell), User name (UserAnd
    LOGNAME), And a default path (Path)

  • Change to our user ID (Setuid) And invoke our login shell, as in

     execl("/bin/sh", "-sh", (char *)0);

 

Our
Login Shell now reads its start-up files

These start-up files usually change some of the environment variables and add your additional variables to the environment.

 

The Linux Login procedure is very similar to the BSD procedure. The main difference between the BSD login procedure and the Linux Login procedure is in the way the terminal configuration is specified.

On Linux,/Etc/inittabContains the configuration information specifying the terminal devices for which
InitShocould startGettyProcess, similar to the way it is done on System V. Depending on the version
GettyIn use, the terminal characteristics are specified either on the command line (as
Agetty) Or in the file/Etc/gettydefs(As
Mgetty).

9.4. Process Groups
#include <unistd.h> pid_t getpgrp(void); pid_t getpgid(pid_t pid);
getpgid(0) == getpgrp()
Each process group can have a process group leader. The leader is identified by its process group ID being equal to its process ID.
 This is called the process group lifetimethe period of time that begins when the group is created and ends when the last remaining process leaves the group. The last remaining process in the process group can either terminate or enter some other process group.
#include <unistd.h> int setpgid(pid_t pid, pid_t pgid); A process joins an existing process group or creates a new process group by calling setpgid. (In the next section, we'll see that setsid also creates a new process group.)
If the two arguments are equal, the process specified by pid becomes a process group leader.
A process can set the process group ID of only itself or any of its children. Furthermore, it can't change the process group ID of one of its children after that child has called one of the exec functions.
9.5. Sessions

A session is a collection of one or more Process Groups.

The processes in a process group are usually placed there by a shell pipeline. For example, the arrangement shown in

Figure 9.6 cocould have been generated by shell commands of the form

 proc1 | proc2 & proc3 | proc4 | proc5 #include <unistd.h> pid_t setsid(void);
A process establishes a new session by callingSetsidFunction. The original process must not be a group leader. Generally, the former Fork sub-process is used to open the session. After the function, the process become the session leader, group leader and has no controlled terminal.
# Include <unistd. h> pid_t getsid (pid_t PID); return the process group ID of the session leader.
9.6. Controlling Terminal
A session can have a single controlling terminal. This is usually the terminal device (in the case of a terminal login) or pseudo-terminal device (in the case of a network login) on which we log in. The session leader that establishes the connection to the controlling terminal is called the controlling process. If a session has a controlling terminal, it has a single foreground process group, and all other process groups in the session are background process groups. Whenever we type the terminal's interrupt key (often DELETE or Control-C),terminal's quit key (often Control-backslash), this causes the interrupt signal be sent to all processes in the foreground process group. Systems derived from UNIX System V allocate the controlling terminal for a session when the session leader opens the first terminal device that is not already associated with a session. This assumes that the call to open by the session leader does not specify the O_NOCTTY flag (Section 3.3). The way a program guarantees that it is talking to the controlling terminal is to open the file /dev/tty. BSD-based systems allocate the controlling terminal for a session when the session leader calls ioctl with a request argument of TIOCSCTTY (the third argument is a null pointer). The session cannot already have a controlling terminal for this call to succeed. (Normally, this call to ioctl follows a call to setsid, which guarantees that the process is a session leader without a controlling terminal.)
9.7.
Tcgetpgrp, Tcsetpgrp, And TcgetsidFunctions
#include <unistd.h> pid_t tcgetpgrp(int filedes); int tcsetpgrp(int filedes, pid_t pgrpid); 9.8 --(?)
9.8. Job Control

The interrupt character (typically delete or control-C) generates
SIGINT.

The quit character (typically control-backslash) generates
Sigquit.

The suspend character (typically Control-Z) generates
Sigtstp.

When bgprocess need input, the terminal driver send sigttin to the bgprocess. Then it stops. Type FG wocould send the continue signal (Sigcont) To the process group. What happens if a background job outputs to the controlling terminal? This is an option that we can allow or disallow. Normally, we useStty(1) command to change this option. When the output to the terminal is aborted and the write is enabled, the terminal driver will issue sigttou to the job.
Let's take a look.

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.