|

楼主 |
发表于 2004-1-7 16:05:23
|
显示全部楼层
- # What to do at the "Three Finger Salute".
- ca::ctrlaltdel:/sbin/shutdown -t5 -r now
复制代码
不单微软知道所谓的“三指礼”(译注:ctrl-alt-del),在Linux中你也能决定它出现时该做什么。下面是init的manpage中关于这个行为的条目:
- ctrlaltdel
- The process will be executed when init receives the SIGINT
- signal. This means that someone on the system console has
- pressed the CTRL-ALT-DEL key combination. Typically one wants
- to execute some sort of shutdown either to get into
- single-user level or to reboot the machine.
复制代码
在Slackware的系统上,将会执行 /sbin/shutdown -t5 -r now 。
- SHUTDOWN(8) Linux System Administrator's Manual SHUTDOWN(8)
- NAME
- shutdown - bring the system down
- SYNOPSIS
- /sbin/shutdown [-t sec] [-arkhncfF] time [warning-message]
- DESCRIPTION
- shutdown brings the system down in a secure way. All
- logged-in users are notified that the system is going
- down, and login(1) is blocked. It is possible to shut the
- system down immediately or after a specified delay. All
- processes are first notified that the system is going down
- by the signal SIGTERM. This gives programs like vi(1) the
- time to save the file being edited, mail and news process-
- ing programs a chance to exit cleanly, etc. shutdown does
- its job by signalling the init process, asking it to
- change the runlevel. Runlevel 0 is used to halt the sys-
- tem, runlevel 6 is used to reboot the system, and runlevel
- 1 is used to put to system into a state where administra-
- tive tasks can be performed; this is the default if nei-
- ther the -h or -r flag is given to shutdown. To see which
- actions are taken on halt or reboot see the appropriate
- entries for these runlevels in the file /etc/inittab.
复制代码
参数 -r 的意思是重新启动(reboot),做法是给init进入运行级6的信号。延迟时间是从现在开始5秒。如果你有保持运行时间的癖好(uptime junkie),你可以让“三指礼”做完全不同的事情,并有效地迫使用户明确给出shutdown命令。(假如你有个NT类系统和Linux共存的服务器群,取消ctrl-alt-del的功能并不是一件坏事。)
当然第2部分(译注:指定运行级)就不必要了,init是对外界信号作出反应。
- # Runlevel 0 halts the system.
- l0:0:wait:/etc/rc.d/rc.0
复制代码
这里的行为又是wait,因此init又会等待脚本执行完毕。进入运行级0后,init将执行 /etc/rc.d/rc.0 (最后是零)。这个脚本所做的是让所有启动了的进程安全地停止。不是所有的程序都喜欢你直接拔出插头的。作为最后的行动,它将调用poweroff来关闭系统或通知用户可以拔插头了。
- # Runlevel 6 reboots the system.
- l6:6:wait:/etc/rc.d/rc.6
复制代码
这一行与上面那行非常像,事实上 /etc/rc.d/rc.0 是指向 /etc/rc.d/rc.6的符号链接,两个脚本其实是同一个。脚本的调用方式决定了最后一步会怎么做。在运行级6中,最后的命令将是reboot。
- # What to do when power fails.
- pf::powerfail:/sbin/genpowerfail start
- # If power is back, cancel the running shutdown.
- pg::powerokwait:/sbin/genpowerfail stop
复制代码
这两行的命令要合在一起说,因为它们有很多相关性。它们的行为是:
- powerwait
- The process will be executed when the power goes
- down. Init is usually informed about this by a pro-
- cess talking to a UPS connected to the computer.
- Init will wait for the process to finish before
- continuing.
- powerfail
- As for powerwait, except that init does not wait
- for the process's completion.
- powerokwait
- This process will be executed as soon as init is
- informormed that the power has been restored.
复制代码
这两行都是电力中断(或实际上是UPS电量耗尽)时该做的事情。/sbin/genpowerfail start由关闭系统开始,/sbin/genpowerfail stop则试图在电力恢复时中断关机的行为。你可能注意到用start参数时并不会等脚本执行完毕,假如等了,就不可能中断关机的过程。/sbin/genpowerfail是个使用shutdown命令的脚本,假如你有UPS你应该读一读这个脚本。
我们现在已经到了系统将要运行的关头。系统初始化的脚本已经执行了,缺省运行级的脚本也已经执行了- 单用户运行级是/etc/rc.d/rc.K,多用户运行级2、3、4、5执行的是/etc/rc.d/rc.M;所有需要的后台服务也在运行。在多用户运行级中,假如在/etc/rc.d/rc.M中启动了telnet和ssh服务,用户已经可以用这些方式登录系统了。假如你的系统是一个没有显示器和键盘的服务器,你让它这么待着就行了。
目前还不可能做到的是从控制台登录,有时能从控制台登录是会很方便的。init的下一个任务就是控制台登录。
- # These are the standard console login getties in multiuser mode:
- c1:1235:respawn:/sbin/agetty 38400 tty1 linux
- c2:1235:respawn:/sbin/agetty 38400 tty2 linux
- c3:1235:respawn:/sbin/agetty 38400 tty3 linux
- c4:1235:respawn:/sbin/agetty 38400 tty4 linux
- c5:1235:respawn:/sbin/agetty 38400 tty5 linux
- c6:12345:respawn:/sbin/agetty 38400 tty6 linux
复制代码
马上我们有了个新的行为,在inittab的manpage里:
- respawn
- The process will be restarted whenever it termi-
- nates (e.g. getty).
复制代码
因此,已经启动的进程/sbin/agetty若被终止了,会重新运行。很明显,为运行级1、2、3和5启动了tty1至tty6的虚拟控制台。这些都是没有X的运行级。例外的是tty6上运行的agetty也将在运行级4(有X的运行级)启动。
agetty的manpage让我们了解到agetty的实际行为:
- AGETTY(8) AGETTY(8)
- NAME
- agetty - alternative Linux getty
- SYNOPSIS
- agetty [-ihLmnw] [-f issue_file] [-l login_program] [-I
- init] [-t timeout] [-H login_host] port baud_rate,...
- [term]
- agetty [-ihLmnw] [-f issue_file] [-l login_program] [-I
- init] [-t timeout] [-H login_host] baud_rate,... port
- [term]
- DESCRIPTION
- agetty opens a tty port, prompts for a login name and
- invokes the /bin/login command. It is normally invoked by
- init(8).
复制代码
可见,agetty在一个tty的端口等待用户登录,然后将运行/bin/login。
- LOGIN(1) LOGIN(1)
- NAME
- login - begin session on the system
- SYNOPSIS
- login [-p] [username] [ENV=VAR ...]
- login [-p] [-h host] [-f username]
- login [-p] -r host
- DESCRIPTION
- login is used to establish a new session with the system.
- It is normally invoked automatically by responding to the
- login: prompt on the user's terminal. login may be spe-
- cial to the shell and may not be invoked as a sub-process.
- Typically, login is treated by the shell as exec login
- which causes the user to exit from the current shell.
- Attempting to execute login from any shell but the login
- shell will produce an error message.
复制代码
login在系统上启动了一个新的会话。让我们回头看看pstree的输出:
- bilbo@bilbo:~$ pstree
- init-+-4*[agetty]
- |-atd
- |-bash
- |-bash---startx---xinit-+-X
- | `-xinitrc-+-bbmail
- | `-blackbox-+-mozilla-bin---mozilla-bin---4+
- | `-rxvt---bash---pstree
复制代码
该死!不是应该有6个agetty吗?呃,对,曾经确实是有6个。但很显然,我从虚拟控制台登录了两次,有一次没有运行其他的程序,另一次运行了startx,变成了X会话。
当我logout的时候agetty会重新运行,还记得respawn吗?有趣的是,假如agetty启动得更早,会不会多个用户通过同一个虚拟控制台登录呢?
*nix总是提供了与系统联系的多种途径,我们已经知道了其中的2种:
- 通过网络,用telnet、ssh之类工具
- 通过直接连接到系统的键盘、显示器和鼠标
但是,*nix还有其他途径与系统联系:
- # Local serial lines:
- #s1:12345:respawn:/sbin/agetty -L ttyS0 9600 vt100
- #s2:12345:respawn:/sbin/agetty -L ttyS1 9600 vt100
- # Dialup lines:
- #d1:12345:respawn:/sbin/agetty -mt60 38400,19200,9600,2400,1200 ttyS0 vt100
- #d2:12345:respawn:/sbin/agetty -mt60 38400,19200,9600,2400,1200 ttyS1 vt100
复制代码
上面两段为串行线路启动agetty,用于串口连接的终端或者用modem拨号进入。
你可以看到,它们缺省情况是被注释掉的。但一旦你需要使用古老的VT类终端或拨号进入系统,你就要用到这些代码。
在最后,要由X来给我们提供一个图形界面的运行级:
- # Runlevel 4 used to be for an X window only system, until we discovered
- # that it throws init into a loop that keeps your load avg at least 1 all
- # the time. Thus, there is now one getty opened on tty6. Hopefully no one
- # will notice. ;^)
- # It might not be bad to have one text console anyway, in case something
- # happens to X.
- x1:4:wait:/etc/rc.d/rc.4
复制代码
注释首先解释了为运行级4在tty6留下一个getty的原因。行为是wait,执行/etc/rc.d/rc.4。这个脚本将依次搜索kdm(kde的登录管理器)、gdm(gnome的登录管理器),最后是xdm(缺省的X登录管理器)。它会运行它所找到的第一个。这些登录管理器可以和agetty类比,它们会等着用户登录,当用户退出后自己重新运行。
- bilbo@bilbo:~$ man kdm
- No manual entry for kdm
- bilbo@bilbo:~$
复制代码
显然,目前为止没有kdm的manpage。
- bilbo@bilbo:~$ man gdm
- No manual entry for gdm
- bilbo@bilbo:~$
复制代码
目前为止,gnome的用户也不关心gdm的manpage。(译注:Slackware 9.1中已经有了)
- XDM(1) XDM(1)
- NAME
- xdm - X Display Manager with support for XDMCP, host
- chooser
- SYNOPSIS
- xdm [ -config configuration_file ] [ -nodaemon ] [ -debug
- debug_level ] [ -error error_log_file ] [ -resources
- resource_file ] [ -server server_entry ] [ -session ses-
- sion_program ]
- DESCRIPTION
- Xdm manages a collection of X displays, which may be on
- the local host or remote servers. The design of xdm was
- guided by the needs of X terminals as well as The Open
- Group standard XDMCP, the X Display Manager Control Proto-
- col. Xdm provides services similar to those provided by
- init, getty and login on character terminals: prompting
- for login name and password, authenticating the user, and
- running a ``session.''
复制代码
最后我们终于找到了,这是xdm的manpage的片段。由于我总是在运行级3,我没法告诉你很多有关它的事。
我们已经看到,init - 所有进程之母 - 从内核那里接管了系统,然后init将处理/etc/inittab文件,根据inittab的输入,init将依次:
- 设置缺省的运行级
- 运行系统初始化脚本 /etc/rc.d/rc.S 并等待它结束
- 运行指定运行级的脚本并等待它结束
. - 运行级1是/etc/rc.d/rc.K
. - 运行级2、3、4、5是/etc/rc.d/rc.M
. - 运行级0(关机)是/etc/rc.d/rc.0
. - 运行级6(重新启动)是/etc/rc.d/rc.6
- 决定在特殊情况,例如ctrl-alt-del或停电时应采取的行动
- 为运行级1、2、3和5启动agetty(还有运行级4时启动6号终端,但有其特殊原因)
- 为串口连接启动终端,尽管这不是缺省的行为
- 为运行级4启动图形界面的登录管理器
结论
事实上这些就是init做的全部事情。剩下的“只不过是脚本”了。但是,仔细读它们也是很有趣的,否则你不会知道在哪里加载模块以及启动网络。 |
|