Linux关机过程详解
上一篇 / 下一篇 2007-12-08 01:32:44 / 个人分类:软件开发
,xd1a%E)o^%X8p0在正常情况下halt或者reboot会调用shutdown,shutdown又会调用init来关闭电脑或者重新启动,所谓的正常情况是run level不等于0或者6的时候。51Testing软件测试网
N#^!Q{a$q;m.Q.Y
51Testing软件测试网5LE8\"A+G0J@1ox D
/sbin/poweroff相当于halt -p
"Rl$g:zE.e$AmI0当执行shutdown -h now时,调用init 0--》init.d/halt---->/sbin/halt
&Dd^yvQ*b0# the content of /etc/rc.d/init.d/halt
pJ4?wc r0# halt This file is executed by init when it goes into runlevel
# 0 (halt) or runlevel 6 (reboot). It kills all processes,
# unmounts file systems and then either halts or reboots.
#D)YrS*`0
poweroff将会使halt调用reboot(BMAGIC_POWEROFF)函数,定义于<sys/reboot.h>
w~:]?&i;N_6_O051Testing软件测试网-t i1j{k!c6U下面是man手册的节选内容:
A,V.K.xu"s8_kN0DEscrīptION of shutdown
-|z quhY4gz4iR0 shutdown brings the system down in a secure way. All logged-in users are notified that the system is going down, and login(1) is51Testing软件测试网G5umn7H}
blocked. It is possible to shut the system down immediately or after a specified delay. All processes are first notified that the51Testing软件测试网H^uDW|N-N]X
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 pro-
k'EyDY
V0 cessing programs a chance to exit cleanly, etc. shutdown does its job by signalling the init process, asking it to change the run-51Testing软件测试网0yG(h7bS"a
level. Runlevel 0 is used to halt the system, runlevel 6 is used to reboot the system, and runlevel 1 is used to put to system into a
Xu%X5~h0 state where administrative tasks can be performed; this is the default if neither the -h or -r flag is given to shutdown. To see
w
B%K6B } w EyIw0 which actions are taken on halt or reboot see the appropriate entries for these runlevels in the file /etc/inittab.
|tT@(~ V051Testing软件测试网h`%i'rz&N d
Init can only capture CTRL-ALT-DEL and start shutdown in console mode. If the system is running the X window System, the X server
TS4Ns4@*q6f$_9Q0 processes all key strokes. Some X11 environments make it possible to capture CTRL-ALT-DEL, but what exactly is done with that event51Testing软件测试网z2f5HtG
{+`6W%E
depends on that environment.
-G0RtjOn'eI&\051Testing软件测试网%o.]$\y{n
Shutdown wasn’t designed to be run setuid. /etc/shutdown.allow is not used to find out who is executing shutdown, it ONLY checks who51Testing软件测试网#c0y7]qH
is currently logged in on (one of the) console(s).
EcC0V6jJ0
y*m5v6NIL(d0DEscrīptION of halt and reboot
)M @4d [OU{0 Halt notes that the system is being brought down in the file /var/log/wtmp, and then either tells the kernel to halt, reboot or51Testing软件测试网Q$_ OR6Il5~0m
poweroff the system.51Testing软件测试网@heo'O,o9`^;K
51Testing软件测试网
P~ZCb"w
If halt or reboot is called when the system is not in runlevel 0 or 6, in other words when it’s running normally, shutdown will be
*l#p)L%q$ev:o:^)gi0 invoked instead (with the -h or -r flag). For more info see the shutdown(8) manpage.
1V5k\!Uq_051Testing软件测试网;TT/m
T4sj;@
Under older sysvinit releases , reboot and halt should never be called directly. From release 2.74 on halt and reboot invoke shut-51Testing软件测试网,H j*K5x:a9AE
down(8) if the system is not in runlevel 0 or 6. This means that if halt or reboot cannot find out the current runlevel (for example,51Testing软件测试网7_
x&?E2s:e
when /var/run/utmp hasn’t been initialized correctly) shutdown will be called, which might not be what you want. Use the -f flag if51Testing软件测试网*dtw1F&b,zb6N;sp
you want to do a hard halt or reboot.
'By[g0Y(G%v0============================================================
V.W-C2SE%f\0
Linux关机命令详解
在linux下一些常用的关机/重启命令有shutdown、halt、reboot、及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的。51Testing软件测试网!KS*Z D0l`1o^+P5\.r
x*\M W3py2mV"w0 1.shutdown
s/| k}[,t051Testing软件测试网guo1G7o!ab/SR#Pshutdown命令安全地将系统关机。 有些用户会使用直接断掉电源的方式来关闭linux,这是十分危险的。因为linux与windows不同,其后台运行着许多进程,所以强制关机可能会导 致进程的数据丢失﹐使系统处于不稳定的状态﹐甚至在有的系统中会损坏硬件设备。51Testing软件测试网R1LkMU Gr
w{+Z U-U3Q6~ Z0而在系统关机前使用shutdown命令﹐系统管理员会通知所有登录的用户系统将要关闭。并且login指令会被冻结﹐即新的用户不能再登录。直接 关机或者延迟一定的时间才关机都是可能的﹐还可能重启。这是由所有进程〔process〕都会收到系统所送达的信号〔signal〕决定的。这让像vi之 类的程序有时间储存目前正在编辑的文档﹐而像处理邮件〔mail〕和新闻〔news〕的程序则可以正常地离开等等。51Testing软件测试网6B)GfZ(a8r
51Testing软件测试网)S}7~ G2{_ gshutdown执行它的工作是送信号〔signal〕给init程序﹐要求它改变runlevel。Runlevel 0被用来停机〔halt〕﹐runlevel 6是用来重新激活〔reboot〕系统﹐而runlevel 1则是被用来让系统进入管理工作可以进行的状态﹔这是预设的﹐假定没有-h也没有-r参数给shutdown。要想了解在停机〔halt〕或者重新开机 〔reboot〕过程中做了哪些动作﹐你可以在这个文件/etc/inittab里看到这些runlevels相关的资料。51Testing软件测试网*Mw5M8o5yc X0G
|a;Si:B'}UV3M$Nc0 shutdown 参数说明:51Testing软件测试网5}F'h{v*h'O?
[-t] 在改变到其它runlevel之前﹐告诉init多久以后关机。51Testing软件测试网K
Wu%jx&]
[-r] 重启计算器。
6v"l)B2Q
v1R%P1h8}'M0[-k] 并不真正关机﹐只是送警告信号给每位登录者〔login〕。
aa'GoR!Z(|6N/xCtR6Q0[-h] 关机后关闭电源〔halt〕。
7i8X Af&_%_;e"D;v0[-n] 不用init﹐而是自己来关机。不鼓励使用这个选项﹐而且该选项所产生的后果往往不总是你所预期得到的。
EV`7u6Y0\7JC0[-c] cancel current process取消目前正在执行的关机程序。所以这个选项当然没有时间参数﹐但是可以输入一个用来解释的讯息﹐而这信息将会送到每位使用者。51Testing软件测试网!~+Q"@%Z
N:C
[-f] 在重启计算器〔reboot〕时忽略fsck。
7?fm/b2ws5? i0[-F] 在重启计算器〔reboot〕时强迫fsck。51Testing软件测试网4t(t4^5yd k,tmb
[-time] 设定关机〔shutdown〕前的时间。
C(vQ0e2Z/^02.halt----最简单的关机命令51Testing软件测试网?2Wz7`;{4M d2l*v*u
其实halt就是调用shutdown -h。halt执行时﹐杀死应用进程﹐执行sync系统调用﹐文件系统写操作完成后就会停止内核。
oS*u/ND
N0参数说明:51Testing软件测试网n7s,l$_e9I0` n__s
[-n] 防止sync系统调用﹐它用在用fsck修补根分区之后﹐以阻止内核用老版本的超级块〔superblock〕覆盖修补过的超级块。
@'jPF`@{!H0[-w] 并不是真正的重启或关机﹐只是写wtmp〔/var/log/wtmp〕纪录。
#Qc(L}9IG6s\3Mva0[-d] 不写wtmp纪录〔已包含在选项[-n]中〕。51Testing软件测试网*m
e'ic,F,~(~iTh
[-f] 没有调用shutdown而强制关机或重启。
sfMZ8ilX`4K0[-i] 关机〔或重启〕前﹐关掉所有的网络接口。
%x$E+q$uc)C0[-p] 该选项为缺省选项。就是关机时调用poweroff。51Testing软件测试网B#S,l:i z.L
TH1p)B9J4E~3g[m03.reboot
"j/d-u(O[t0reboot的工作过程差不多跟halt一样﹐不过它是引发主机重启﹐而halt是关机。它的参数与halt相差不多。51Testing软件测试网9T!A LV;L9D6u
YJ1A2\k@A5K04.init
K])`l%Jpd4G0init是所有进程的祖先﹐它的进程号始终为1﹐所以发送TERM信号给init会终止所有的用户进程﹑守护进程等。shutdown 就是使用这种机制。init定义了7个运行级别(runlevel),init 0为关机﹐init 1为重启。关于init可以长篇大论﹐这里就不再叙述。另外还有telinit命令可以改变init的运行级别﹐比如﹐telinit -iS可使系统进入单用户模式﹐并且得不到使用shutdown时的信息和等待时间。51Testing软件测试网!|_1U7i k9K.t2d3H0J
===========================================================
cf4kKs&h S0o[0
Linux 关机重启流程分析 |
范晓炬(xiaoju_f@263.net), 联想(北京)有限公司软件设计中心嵌入式研发处开发工程师, 联想(北京)有限公司软件设计中心 I'E#z5r/y\l o0Yb'X#Z5{0MiA02003 年 8 月 11 日 Z x2s z*\P t3MwP0linux下的关机和重启流程对于一般的桌面应用和网络服务器来说并不重要,但是在用户自己定义的嵌入式系统内核中就有一定的研究意义,通过了解Linux 关机重启的流程,我们对它可以修改和自定义,甚至以此为基础开发出全新的功能来。51Testing软件测试网)ja@&uE*]!WyJZ t.VnY2d0r?051Testing软件测试网| x~ p)i*QE 在linux下的关机和重启可能由两种行为引发,一是通过用户编程,一是系统自己产生的消息。用户和系统进行交互的方式也有两个,一个是系统调用:sys_reboot,另一个就是apm或则acpi的设备文件,通过对其操作也可以使系统关机或者重启。51Testing软件测试网B:S7`&\OnZ 51Testing软件测试网eRz z n6UP,g
51Testing软件测试网\d(D)J b/V(j 51Testing软件测试网#d8tT4j^p5~` 2.通过系统调用sys_reboot的重启51Testing软件测试网3d;\j6^R"Q,Q0V&l'e QN9u}6x0这个系统调用定义了一系列的MAGIC_NUMBER,在调用的开始部分首先检查MAGIC_NUMBER是否正确,只有正确才继续向下运行。在重启的时候转向分支51Testing软件测试网A/z9}0O\OU
P8L Qc.dF6ka9Mv0首先使用notifier_call_chain向其它部分发出重启的消息,然后调用machine_restart函数完成重启。51Testing软件测试网(e/O Y'hJS 51Testing软件测试网0XJt#N;uh @pmachine_restart 函数的开始部分有一段SMP相关的代码,主要完成多CPU时由一个CPU完成重启操作,其它CPU处于等待状态。之后系统根据一个变量 reboot_thru_bios的内容判断重启方式,通过阅读reboot_setup我们可以得知,这个参数的内容是在系统启动时指定的,决定了是否 利用bios,事实上是系统复位后的入口(FFFF:0000)地址的程序进行重启。在不通过bios进行重启的情况下,系统首先设定了重启标志,然后向 端口0xfe写入数字0x64,这种重启的具体原理我还不大清楚,似乎是模拟了一次reset键的按下,希望大家和我讨论。在通过bios重启的情况下, 系统同样先设定了重启模式,然后切换到了实模式,通过一条ljmp $0xffff,$0x0完成了重启。 ]B$[5N)\ `,DC7p2}051Testing软件测试网7fU+{&|K
51Testing软件测试网ZC&JA Bm| 51Testing软件测试网 Dw6b%WM(inAo 3.通过系统调用sys_reboot进行关机51Testing软件测试网fT:Wkw &{+\ oM#s {0在系统调用的处理分支上,我们可以看到,首先同样是检查MAGIC_NUMBER,然后在51Testing软件测试网jY pP7oX
#dA5Z[\!P0的执行流程里面,又是使用 notifier_call_chain发出了关闭计算机电源的消息,紧接着执行了machine_power_off函数。我们在 machine_power_off函数中可以看到,如果pm_power_off这个函数指针不为空,那么系统就会通过调用这个函数进行关机。在apm 已经加载的情况下(SMP除外),实际上pm_power_off函数实际上指向了apm.c中的apm_power_off,在这个函数里系统通过 apm_info结构里的值,使用切换到实模式关机,或者使用apm_bios_call_simple函数调用保护模式下的apm接口关机两种方法。 _"m-MucDp051Testing软件测试网|9Q-EuP
SF)a;|,t'e.F @)B0 $TGK,t~ lp0 ^|yd.iDO0g04.apm驱动本身的关机过程 %Jv$B\1A!b#a#V051Testing软件测试网nb6` Fcn.p;WWEuCapm使用其注册的设备的ioctl接口完成apm的操作,在apm.c的do_ioctl函数中可以看见处理的分支。这里只有suspend和standby的代码,所以我们不能通过ioctl这种方法使用apm关机。51Testing软件测试网2o1pMA+GS;?*z!X 51Testing软件测试网0O.Kq:yag(s8u _当 用户按下POWER开关的时候,如果有apm模块,那么关机流程是由apm来处理的。apm驱动在初始化的时候启动了一个apm内核线程: apm_mainloop,系统会在这里检测到POWEROFF按键消息并且将其命名为APM_SYS_SUSPEND,以区别apm -s设置的APM_USER_SUSPEND模式。紧接着进入了apm_event_handler函数,又从apm_event_handler函数进 入了check_events函数,处理函数对应的case分支上。系统同样使用了suspend函数进行关机,不过由于其它参数的原因,suspend 最后调用的是关机的流程。51Testing软件测试网OiS9r8a#K p{JYr~]i0
6Mq o Mv]:|0 :iq$V/?T`bAW6c0 QQ!Y1Z9M$R05.解决问题实例 (r0Dcm8b:n051Testing软件测试网 @ rsYvCuJL@1)按POWER键时某些主板死机51Testing软件测试网5Cr]q6l 51Testing软件测试网{W+^:rvm5nD(K经 查只有某些特定的驱动装载之后才会出现这样的情况,并且当使用关机系统调用sys_reboot的时候没有这样的问题。分析apm的处理流程,怀疑是在关 机前驱动程序没有正确处理apm发出的询问消息造成的。由于部分驱动程序没有源代码,决定hack掉apm.c的关机部分,让两种方式的关机走同样的流 程。于是把apm.c的check_events函数中对APM_SYS_SUSPEND部分改写为如下代码: ?;[\jzd-P9K0
U{C2CvfYB0 Mnj \-O6O0定义了一个用户态应用程序poweroff_helper_path,当POWEROFF键按下的时候系统运行这个kernel_helper程序。我们再写一个通过sys_reboot系统调用关机的程序,放在指定的位置下。死机的问题就解决了。51Testing软件测试网3q ~H$fkG2N[u:x $cZN:n2Lf|02)快速返回实模式重启51Testing软件测试网$UFbb PH 51Testing软件测试网pcODK4H3m7n*T主要可以参考了process.c中的返回实模式的代码,比如我把real_mode_switch换成如下代码: ?W{Ie0
|