Analysis of STARTX start-up process

Source: Internet
Author: User
Tags auth case statement data structures
analysis of startx start-up process 1 Xinit

before I explain startx, I think we should get to know Xinit first. Because StartX is the start of X by calling Xinit. 1.1 function

When we install Ubuntu, Xinit is installed by default, which is located under/usr/bin. Xinit is a binary file, not a script. Its primary function is to start an X server and start an X-based application. 1.2 usage

The usage of Xinit is: Xinit [[Client] options] [--[Server] [display] options]. Where the client is used to specify an X-based application, the options behind the client are passed to the application's parameters, and the server is used to specify which X server to start, typically/usr/bin/x or/usr/bin/xorg, DISPL Ay is used to specify display number, typically 0, to represent the first display, and option is the parameter passed to the server.

If you do not specify a client, Xinit finds the. xinitrc file in the home (environment variable) directory, and if this file exists, Xinit directly calls the EXECVP function to execute the file. If the file does not exist, then the client and its options are: Xterm-geometry +1+1-n login-display:0.

If you do not specify server, Xinit finds the. xserverrc file in the home (environment variables) directory, and if this file exists, Xinit directly calls the EXECVP function to execute the file. If the file does not exist, then the server and its display are: x:0. If the x command does not exist in the system directory, then we need to create a link in the system directory called X to point to the real x Server command (Ubuntu is Xorg).

1.3 examples

Here are a few examples of xinit applications:

1) Xinit/usr/bin/xclock--/usr/bin/x: 0

This example starts the X server and will start Xclock. Note that when you specify a client or server, you need to use an absolute path, or xinit will be treated directly as a parameter because it is not possible to distinguish between a parameter passed to xterm or server or a specified client or server.

2 Create a new. xinitrc file at home and add the following lines:


Xsetroot-solid Gray &



Xclock-g 50X50-0+0-BW 0 &



Xterm-g 80x24+0+0 &



Xterm-g 80x24+0-0 &



Twm


When Xinit starts, it starts X server first, then starts a clock, two xterm, and finally starts the window manager twm.

Please note:

The last command does not run in the background, otherwise all commands run in the background xinit will return to exit, the same, except the last command must run in the background, otherwise the command will only be run after the command exits.

1.4 Analysis

See here, the eagle-eyed person may have seen that the Xinit function can be implemented entirely by script, such as to start X Server and a xterm, just as Xinit started by default, just add in a new script or rc.local:

x&

Export display=:0.0

Xterm

This implementation is completely correct, yet it does not fully implement the functionality that Xinit has, xinit all of which is that the X server exits when the last client (TWM window manager in the second example above) exits. Our script implementation does not exit the X server after we exit xterm.


2 startx script

people who have used Linux basically know that under Linux there is a command called STARTX, then it is how to achieve it. 2.1 function

when we want to start the graphical interface under the terminal, we will do it by typing startx, which can launch an X server and start a nice graphical interface (I'm in Gnome under Ubuntu). 2.2 usage

the usage of STARTX is the same as that of Xinit: StartX [[Client] Options ...] [--[Server] Options ...] 。 Why, then? This is because StartX is actually a script that starts X server by invoking the Xinit command, and the startx parameters are all passed to Xinit. Therefore, the meaning of these parameters is the same as that of the Xinit parameter. 2.3 Examples

Here are two simple examples of the STARTX command:

1) startx---depth 16

This example is primarily to start the X server with a 16-bit color.

2) startx---DPI 100

This example is primarily to start the X server at 100 dpi. 2.4 Analysis

Now let's analyze the StartX script. StartX script is located under/usr/bin, directly with vim to open we can see its specific implementation as follows:

#!/bin/bash # Note: This script is used by the Bash shell parsing the

# $Xorg: Startx.cpp,v 1.3 2000/08/17 19:54:29 cpqbld EXP $

#

# This is just a sample implementation of a slightly less primitive

# interface than Xinit. It looks for user. Xinitrc and. XSERVERRC

# files, then system xinitrc and XSERVERRC files, else lets Xinit choose

# its default. The system xinitrc should probably do things like check

# for. xresources files and merges them in, startup up a window manager,

# and pop a clock and serveral xterms.

#

# Site Administrators are strongly urged to write nicer versions.

#

# $XFree 86:xc/programs/xinit/startx.cpp,v 3.16tsi EXP $

# The following is mainly to assign values to some variables.

userclientrc= $HOME/.XINITRC

Sysclientrc=/etc/x11/xinit/xinitrc

userserverrc= $HOME/.XSERVERRC

Sysserverrc=/etc/x11/xinit/xserverrc

Defaultclient=xterm

defaultserver=/usr/bin/x

Defaultclientargs= ""

Defaultserverargs= ""

Clientargs= ""

Serverargs= ""

# The following statement mainly says: If the $HOME/.xinitrc file exists and is not a directory, then assign the Defaultclientargs to $HOME/.xinitrc, otherwise, if/ETC/X11/XINIT/XINITRC exists and is not a directory, the Defaultclientargs is assigned to/ETC/X11/XINIT/XINITRC.

If [f $USERCLIENTRC]; Then

defaultclientargs= $USERCLIENTRC

elif [f $SYSCLIENTRC]; Then

defaultclientargs= $SYSCLIENTRC

Fi

# The following statement mainly says: If the $HOME/.XSERVERRC file exists and is not a directory, then assign the Defaultclientargs to $HOME/.XSERVERRC, otherwise, if/etc/x11/xinit/ XSERVERRC exists and is not a directory, the Defaultclientargs is assigned to/ETC/X11/XINIT/XSERVERRC.

If [f $USERSERVERRC]; Then

defaultserverargs= $USERSERVERRC

elif [f $SYSSERVERRC]; Then

defaultserverargs= $SYSSERVERRC

Fi

# assigns the Whoseargs variable to the string "Client", which represents the currently resolved parameter for the specified client.

whoseargs= "Client"

# Enter the while loop when one of the startx arguments is not empty.

While [x '!= x]; Todo

Case "$" in

# ' required to prevent CPP from treating '/* ' as a C comment.

/''*|/./''*)

If ["$whoseargs" = "Client"]; Then

If [x "$clientargs" = x]; Then

client= "$" resolves the user-specified client program

Else

Clientargs= "$clientargs $" # resolves Client parameters

Fi

Else

If [x "$serverargs" = x]; Then

Server= "$" to parse out the user-specified X Server program

Else

Serverargs= "$serverargs $" to parse out the parameters of X Server

Fi

Fi

;;

--""-""-"to resolve the server

whoseargs= "Server"

;;

*)

If ["$whoseargs" = "Client"]; Then

Clientargs= "$clientargs $"

Else

# display must be the ' the ' argument

If [x "$serverargs = x] &&/

Expr "$: ': [0-9][0-9]*$ ' >/dev/null 2>&1; Then

Display= "$" to parse out display

Else

Serverargs= "$serverargs $"

Fi

Fi

;;

ESAC #case Statement End

Shift # Moves the argument list left one bit, parsing the next argument.

Done

# Process Client Arguments

If [x "$client" = x]; Then # If the client variable is empty, the user does not specify a client.

# If no client arguments either, use RC file instead

If [x "$clientargs" = x]; Then # If the user does not specify a client parameter, set the client to the default RC file previously set (for $HOME/.xinitrc, or/ETC/X11/XINIT/XINITRC)

Client= "$defaultclientargs"

Else

client= $defaultclient # If the user specifies a client parameter, the client is set to Xterm

Fi

Fi

# Process Server Arguments

If [x "$server" = x]; Then # if the server variable is empty, the user does not specify the server.

# If no server arguments or display either, use RC file instead

If [x "$serverargs" = x-a x "$display" = x]; Then # if Serverargs is empty and display is empty, set the server to the default RC file previously set (for $HOME/. XSERVERRC, or/ETC/X11/XINIT/XSERVERRC)

Server= "$defaultserverargs"

Else

server= $defaultserver # If the user specifies Serverargs or display, the server is set to/usr/bin/x

Fi

Fi

If [x "$XAUTHORITY" = x]; Then # if the environment variable xauthority is empty, set to $HOME/. Xauthority

xauthority= $HOME/. Xauthority

Export xauthority

Fi

removelist=

# set up default Xauth info for this machine

# Check for GNU hostname

If hostname--version >/dev/null 2>&1; Then # if the hostname command exists

If [-Z ' hostname--version 2>&1 | grep GNU ' "]; Then # if Hostname–version does not contain GNU, set the hostname variable to the string returned by the command hostname–f.

Hostname= ' Hostname-f '

Fi

Fi

If [-Z "$hostname"]; Then # if the hostname length is 0, the hostname variable is set to the string returned by the command hostname.

Hostname= ' hostname '

Fi

authdisplay=${display:-:0}

Mcookie= '/usr/bin/mcookie '

Dummy=0

# Create a file with auth information for the server. ': 0 ' is a dummy.

xserverauthfile= ' MKTEMP-P/tmp Serverauth. Xxxxxxxxxx '

Trap "Rm-f $xserverauthfile" HUP INT QUIT ILL trap KILL bus TERM

Xauth-q-F $xserverauthfile << EOF

Add: $dummy. $mcookie

Eof

Serverargs=${serverargs} "-auth" ${xserverauthfile}

# Now add the same credentials to the client authority file

# if ' $displayname ' already exists does not overwrite it as another

# Server man need it. Add them to the ' $xserverauthfile ' instead.

For DisplayName in $authdisplay $hostname $authdisplay; Todo

Authcookie= ' Xauth list "$displayname"/

| Sed-n "s/.* $displayname [: space:]*].*[[:space:]*]//p" ' 2>/dev/null;

if ["Z${authcookie}" = "Z"]; Then

Xauth-q << EOF

Add $displayname. $mcookie

Eof

removelist= "$displayname $removelist"

Else

dummy=$ (($dummy + 1));

Xauth-q-F $xserverauthfile << EOF

Add: $dummy. $authcookie

Eof

Fi

Done

echo "Client= $client, clientargs= $clientargs, server= $server, display= $display, serverargs= $serverargs"

# The following statement launches X server and Clients via Xinit.

Xinit $client $clientargs--$server $display $serverargs

If [x "$removelist"!= x]; Then

Xauth Remove $removelist

Fi

If [x "$xserverauthfile"!= x]; Then

Rm-f $xserverauthfile

Fi

If command-v deallocvt >/dev/null 2>&1; Then

DEALLOCVT # Frees up core memory and data structures for all unused virtual terminals

Fi

2.5 Summary

From the above analysis of the StartX script, we can know that: StartX will parse the user's arguments first, if the user specifies the parameter (that is, the parsing result is not empty), then StartX will start the xinit with that argument, or it will parse (rather than parse, execute) $ The RC file in the home directory, if the file does not exist, will resolve the system directory (/etc/x11/xinit/) of the RC file, if this file does not exist, then STARTX will be the default client (xterm) and server (/usr/b in/x) for the parameter to start the Xinit.


3 startx Default boot process

through the above analysis of the StartX script, we know the basic boot process of startx, but so far, we do not know how to launch the Gnome's beautiful desktop only in the terminal input startx, let's take a look at the startup process. Several start-up ways of 3.1 startx

From the analysis of the StartX script, we can know that there are three main ways to start StartX:

A), one is to specify the client and server that you want to start, for example: Startx/usr/bin/xclock-/usr/bin/x: 0 ;

b, one is to specify the server to start by specifying the number of client and. XSERVERRC to be started by creating a new. xinitrc file under $HOME (Note: These two files do not exist);

C, there is a direct input startx without specifying parameters, which is the way we start the GNOME desktop. Over here

We mainly introduce the last kind of startup method.

In C This startup method, we can know that the StartX script will first look at the system directory (/etc/x11/xinit/) under the RC file exists, if not exist will use the default Xterm and/usr/bin/x to start the xinit. Obviously, StartX does not start with xterm, but the GNOME desktop, so Gnome's startup is specified through System file/ETC/X11/XINIT/XINITRC.

The contents of the/etc/x11/xinit/xinitrc file are as follows:

#!/bin/bash # Note: This script is used by the Bash shell parsing the

# $Xorg: Xinitrc.cpp,v 1.3 2000/08/17 19:54:30 cpqbld EXP $

#/ETC/X11/XINIT/XINITRC

#

# global XINITRC file, used by all X sessions started by Xinit (STARTX)

# Invoke Global X session script

. /etc/x11/xsession # executes Xsession scripts in the current shell environment

Therefore, GNOME's launch should be in the xsession.

The launch of X Server is specified by the system file/ETC/X11/XINIT/XSERVERRC, which reads:

#!/bin/sh # Note: This script is used by the Bourne shell to parse the

# $Id: XSERVERRC 189 2005-06-11 00:04:27z Branden $

exec/usr/bin/x11/x-nolisten TCP

3.2 xsession

The following is the contents of the Xsession script:

#!/bin/sh # Note: This script is used by the Bourne shell to parse the

#

#/etc/x11/xsession

#

# Global Xsession file--used by display managers and Xinit (STARTX)

# $Id: xsession 967 2005-12-27 07:20:55z Dnusinow $

Set–e # Opens the Errexit option, which indicates that if the following command returns a status of not 0, the program exits.

Progname=xsession

# The following four is the information output function, you can no matter

Message () {

# Pretty-print messages of arbitrary length; Use Xmessage if it

# is available and $DISPLAY is set

Message= "$PROGNAME: $*"

echo "$MESSAGE" | Fold-s-W ${columns:-80} >&2

If [-N "$DISPLAY"] && which xmessage >/dev/null 2>&1; Then

echo "$MESSAGE" | Fold-s-W ${columns:-80} | Xmessage-center-file-

Fi

}

Message_nonl () {

# Pretty-print messages of arbitrary length (no trailing newline); Use

# Xmessage If it is available and $DISPLAY is set

Message= "$PROGNAME: $*"

Echo-n "$MESSAGE" | Fold-s-W ${columns:-80} >&2;

If [-N "$DISPLAY"] && which xmessage >/dev/null 2>&1; Then

Echo-n "$MESSAGE" | Fold-s-W ${columns:-80} | Xmessage-center-file-

Fi

}

ErrorMsg () {

# Exit script with Error

Message "$*"

Exit 1

}

Internal_errormsg () {

# exit script with error; Essentially a "This SHOULD NEVER happen" message

# One big call to message () for the sake of xmessage; If we had two then

# The user would have dismissed the error we want reported before seeing the

# request to the IT.

ErrorMsg "$*"/

"Please installed version of the"/"x11-common/" "/

"Package and the complete text of this error message to"/

&nb

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.