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@1oxD

/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
p J4?wcr0# 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

k[Otc0

poweroff将会使halt调用reboot(BMAGIC_POWEROFF)函数,定义于<sys/reboot.h>

w ~:]?&i;N_6_O051Testing软件测试网-ti1j{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^u DW|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(h7b S"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 }wEyIw0       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软件测试网,Hj*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

-l2f8lIu G-eE0

Linux关机命令详解

51Testing软件测试网(X R6z|+f?P \

在linux下一些常用的关机/重启命令有shutdown、halt、reboot、及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的。51Testing软件测试网!KS*ZD0l`1o^+P5\.r

x*\M W3py2mV"w0   1.shutdown

s/|k}[,t051Testing软件测试网 guo1G7o!a b/SR#P

shutdown命令安全地将系统关机。 有些用户会使用直接断掉电源的方式来关闭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{_ g

shutdown执行它的工作是送信号〔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^5ydk,tmb
[-time] 设定关机〔shutdown〕前的时间。

o)^4dl Y#VJ0

C(vQ0e2Z/^02.halt----最简单的关机命令51Testing软件测试网?2Wz7`;{4Md2l*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\3Mv a0[-d] 不写wtmp纪录〔已包含在选项[-n]中〕。51Testing软件测试网*m e'ic,F,~(~iTh
[-f] 没有调用shutdown而强制关机或重启。
sfMZ8ilX`4K0[-i] 关机〔或重启〕前﹐关掉所有的网络接口。
%x$E+q$u c)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软件测试网!|_1U7ik9K.t2d3H0J

51Testing软件测试网x c)`H Ul-VcW

===========================================================
cf4kKs&h S0o[0

Linux 关机重启流程分析

51Testing软件测试网-m `:G*N&J T)H

范晓炬(xiaoju_f@263.net), 联想(北京)有限公司软件设计中心嵌入式研发处开发工程师, 联想(北京)有限公司软件设计中心

I'E#z5r/y\l o0

Y b'X#Z5{0MiA02003 年 8 月 11 日

Zx2sz*\P t3MwP0
linux下的关机和重启流程对于一般的桌面应用和网络服务器来说并不重要,但是在用户自己定义的嵌入式系统内核中就有一定的研究意义,通过了解Linux 关机重启的流程,我们对它可以修改和自定义,甚至以此为基础开发出全新的功能来。
51Testing软件测试网)ja@&uE*]!WyJZ t

1.概述

.VnY2d0r?051Testing软件测试网|x~ p)i*QE

在linux下的关机和重启可能由两种行为引发,一是通过用户编程,一是系统自己产生的消息。用户和系统进行交互的方式也有两个,一个是系统调用:sys_reboot,另一个就是apm或则acpi的设备文件,通过对其操作也可以使系统关机或者重启。51Testing软件测试网B:S7`&\OnZ

51Testing软件测试网eRz z n6UP,g
51Testing软件测试网W"Z"k;A7r2eE;wv*y

aL^^9F'a%IDo0
回页首
51Testing软件测试网WqosL!p
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

case LINUX_REBOOT_CMD_RESTART:
51Testing软件测试网;r-L:b,LP)fO

P8L Qc.dF6ka9Mv0首先使用notifier_call_chain向其它部分发出重启的消息,然后调用machine_restart函数完成重启。51Testing软件测试网(e/O Y'hJS

51Testing软件测试网0XJt#N;uh @p

machine_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

be(S0q&F2CW0

'X&v!]e1|X1F4ms-[;T0
回页首
51Testing软件测试网C'ewO p"c5b
51Testing软件测试网ZC&JA Bm|
51Testing软件测试网 Dw6b%WM(inAo

3.通过系统调用sys_reboot进行关机51Testing软件测试网fT:Wkw

&{+\oM#s {0在系统调用的处理分支上,我们可以看到,首先同样是检查MAGIC_NUMBER,然后在51Testing软件测试网jY pP7o X

case LINUX_REBOOT_CMD_POWER_OFF:
51Testing软件测试网:Y^~| D(B

#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
51Testing软件测试网$@aDv0abh5^
51Testing软件测试网.nYdtSoF0G g
回页首

S F)a;|,t'e.F @)B0
$TGK,t~ lp0

^|yd.iDO0g04.apm驱动本身的关机过程

%Jv$B\1A!b#a#V051Testing软件测试网nb6` Fcn.p;WWEuC

apm使用其注册的设备的ioctl接口完成apm的操作,在apm.c的do_ioctl函数中可以看见处理的分支。这里只有suspend和standby的代码,所以我们不能通过ioctl这种方法使用apm关机。51Testing软件测试网2o1pMA+G S;?*z!X

51Testing软件测试网0O.Kq:ya g(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

sED;z7RS0

Y}:@W[ ]O`U3^0
回页首

6Mq o Mv]:|0
:iq$V/?T`bAW6c0

Q Q!Y1Z9M$R05.解决问题实例

(r0Dcm8b:n051Testing软件测试网@rs YvCuJL@

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
ret = exec_usermodehelper(poweroff_helper_path, argv, envp);
U has!w.a bt0if (ret) {51Testing软件测试网&M)s:X n:S
printk(KERN_ERR51Testing软件测试网B8@{r4B
"apm.c: failed to exec %s , errno = %d\\n",51Testing软件测试网,e'fJ*T;h]0T
poweroff_helper_path, errno);51Testing软件测试网6j N n.J CGi
}51Testing软件测试网4pP;{ {Sx.i
break;

U{C2Cvf YB0

Mnj \-O6O0定义了一个用户态应用程序poweroff_helper_path,当POWEROFF键按下的时候系统运行这个kernel_helper程序。我们再写一个通过sys_reboot系统调用关机的程序,放在指定的位置下。死机的问题就解决了。51Testing软件测试网3q ~ H$f kG2N[ u:x

$cZN:n2L f|02)快速返回实模式重启51Testing软件测试网$UF bb P H

51Testing软件测试网pcODK4H3m7n*T

主要可以参考了process.c中的返回实模式的代码,比如我把real_mode_switch换成如下代码:

?W{Ie0
// For fast reboot support51Testing软件测试网Qk+Pp7h
static unsigned char fast_reboot_switch [] =51Testing软件测试网bA%a'\i:w6j
{51Testing软件测试网;on;Tp-{ b:o7s
0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
~Q? ]'x | s%@ j s^s00x66, 0x25, 0x10, 0x11, 0x11, 0x11, /* andl $0x11111110,%eax */
}-KPw%r+~yI00x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
:jSrb1P9EV0~a00xea, 0x00, 0x00, 0x00, 0x70 /* ljmp $0x7000,$0x0000 */
*]0oB/_f!X:c0};

Jk2k$u$l"Re6L0

(S+W d6@ SO/H0系统就可以切换到实模式中,然后跳转到7000H:0位置开始执行。

FU G @}g0
0X0B3R}!s6f0
51Testing软件测试网xr%vE(ki#FR

a:Fe X-PYc0
回页首
51Testing软件测试网&y&tb h,kg2w6d2X0}/G-M
51Testing软件测试网3i-e$G2B|

d7`3w~P^06.ACPI概述51Testing软件测试网T`-|:wtQ2^gC

51Testing软件测试网3fh"i(\u6M

在2.4.20 内核中ACPI模块被注明为试验和未完成,里面有一部分功能也许没有实现。如果APM和APCI两个模块同时编译进内核,APM在ACPI前被加载, APM起作用使ACPI退出。对于系统电量、电源实践一类的支持(主要是在笔记本上有用),靠的是acpid这个daemon程序。51Testing软件测试网jI9X5\ M(`,N2t

%s3A;U"pB@-vB0没有一个功能类似apm的应用程序切换状态,acpi的程序仅仅完成了对acpi状态的查询。用户实现S0-S4的功能可以直接向/proc/acpi/sleep文件中写入数字来实现。通过读出(cat)其中的内容可以知道系统到底支持那些模式。51Testing软件测试网n$AP%Y1se/^J/F

0p/T7{^9IFZ|.a0acpi 模块的源代码主程序在linux/drivers/acpi/driver.c中,如果向sleep文件写东西,就转到了 linux/drivers/acpi/ospm/system/sm_osl.c文件的sm_osl_proc_write_sleep函数中,这个函 数后来调用了sm_osl_suspend函数。在这个函数里完成了各种功能,包括保护各种状态。最后真正的sleep是通过对 acpi_enter_sleep_state的调用完成的,这个函数在linux/drivers/acpi/hardware/hwsleep.c文 件中,这里写了acpi的寄存器使系统进入sleep状态。写寄存器的指令在这个目录下面的hwregs.c中。51Testing软件测试网xW"W'` \.M]+b3v+m N


1N [z"F1a9Z \0

K+@$H#ly xQ/@0

+U"wr} U,X0
回页首

4m#x(|BH3Yw051Testing软件测试网e5~%ctzw0Ys

],rEHDCqr07.总结51Testing软件测试网m)U!z$S_B6D&Vg#x

51Testing软件测试网'tk)`S1g+WqMm*S/O

本文对acpi的介绍非常简略,实际上ACPI必定会成为将来linux内核中首选的电源管理方式。由于目前官方代码中ACPI版本较低,所以没有太详细的论述,希望将来的内核能有所改变。51Testing软件测试网DyqZ:gptlF,Q

51Testing软件测试网g/p1O+`+h!n%Er`

t[vF0R.o051Testing软件测试网\y@8h2H w

参考资料

"Y~y8\o s0
  • linux-2.4.20源代码

-J_0g]9le0关于作者51Testing软件测试网\ xrX r{I%Mk

%c/nx&^#S0范晓炬,联想(北京)有限公司软件设计中心嵌入式研发处开发工程师,研究兴趣为 Linux 内核,网络安全,XWindow 系统,Linux 桌面应用,人工智能系统。你可以通过xiaoju_f@263.net联系他

'{b Z]%v`0
========================================51Testing软件测试网`;dtKh[IP%Zh
51Testing软件测试网3f$bU+n)@/[

n\!_.eG$sL2g0
n]|,H8I)PJ8Bhc0参考资料:51Testing软件测试网.UYT1C[$dM
www.ibm.com/developerworks/cn/linux/embed/l-rb/index.html51Testing软件测试网T `7buK"rG%}y*r
blog.chinaunix.net/u/7356/showart.php51Testing软件测试网[lMk)h:e-S1I4b W5H
if.ustc.edu.cn/~jbhuang/blog/archives/000265.html51Testing软件测试网)clvG-\6n|(?
www.ahinc.com/linux101/boot.htm51Testing软件测试网L:X4No+n4W/]


TAG: 软件开发 Linux 关机过程

 

评分:0

我来说两句

Open Toolbar