一:Busyobx層的分析
這段時(shí)間,在忙到一個(gè)項(xiàng)目時(shí),需要在busybox中用到reboot命令,開(kāi)始在busybox中的shell中輸入reboot命令,始終如下的信息,然后就停止在那里了,無(wú)法重啟...為了徹底的弄明白這個(gè)問(wèn)題,我在網(wǎng)絡(luò)上找了很久,終于有個(gè)人寫(xiě)的一個(gè)reboot流程分析,我就借花獻(xiàn)佛.在這里重新分析下busybox是如何運(yùn)行這個(gè)命令,同時(shí)又是如何調(diào)用到Linux內(nèi)核中的mach_reset中的arch_reset,當(dāng)針對(duì)不同的ARM芯片時(shí),作為L(zhǎng)inux內(nèi)核開(kāi)發(fā)和驅(qū)動(dòng)開(kāi)發(fā)的朋友,對(duì)于這個(gè)流程還是一定要了解的。要不,出現(xiàn)問(wèn)題,又如何找出問(wèn)題呢。忘記了reboot的打印信息了,如下:
[plain]?view plain?copy
print?
The?system?is?going?down?NOW?!!??
Sending?SIGTERM?to?all?processes.??
Sending?SIGKILL?to?all?processes.??
Please?stand?by?while?rebooting?the?system.??
Restarting?system.??
.??
通過(guò)分析busybox1.20.0的代碼可以看出在init.c中有這樣一行的代碼,如下:
[cpp]?view plain?copy
print?
int?init_main(int?argc,?char?**argv)?MAIN_EXTERNALLY_VISIBLE;??
int?init_main(int?argc?UNUSED_PARAM,?char?**argv)??
{??
static?const?int?magic[]?=?{??
RB_HALT_SYSTEM,??
RB_POWER_OFF,??
RB_AUTOBOOT??
};??
static?const?smallint?signals[]?=?{?SIGUSR1,?SIGUSR2,?SIGTERM?};??
......??
/*?struct?sysinfo?is?linux-specific?*/??
#ifdef?__linux__??
/*?Make?sure?there?is?enough?memory?to?do?something?useful.?*/??
if?(ENABLE_SWAPONOFF)?{?//是否配置了swapoff命令??
struct?sysinfo?info;??
if?(sysinfo(&info)?==?0??
&&?(info.mem_unit???info.mem_unit?:?1)?*?(long?long)info.totalram?1024*1024??
)?{??
message(L_CONSOLE,?"Low?memory,?forcing?swapon");??
/*?swapon?-a?requires?/proc?typically?*/??
new_init_action(SYSINIT,?"mount?-t?proc?proc?/proc",?"");??
/*?Try?to?turn?on?swap?*/??
new_init_action(SYSINIT,?"swapon?-a",?"");??
run_actions(SYSINIT);???/*?wait?and?removing?*/??
}??
}??
#endif??
......??
/*?Make?the?command?line?just?say?"init"??-?thats?all,?nothing?else?*/??
strncpy(argv[0],?"init",?strlen(argv[0]));??
/*?Wipe?argv[1]-argv[N]?so?they?don't?clutter?the?ps?listing?*/??
while?(*++argv)??
memset(*argv,?0,?strlen(*argv));??
/*?Set?up?signal?handlers?*/??
/*?Set?up?signal?handlers?*/??
if?(!DEBUG_INIT)?{??
struct?sigaction?sa;??
bb_signals(0??
+?(1?<
+?(1?<
+?(1?<
,?halt_reboot_pwoff);//看到這個(gè)halt_reboot_pwoff??
signal(SIGQUIT,?restart_handler);?/*?re-exec?another?init?*/?//看到這個(gè)restart_handler函數(shù),這是我們需要分析的??
/*?Stop?handler?must?allow?only?SIGCONT?inside?itself?*/??
memset(&sa,?0,?sizeof(sa));??
sigfillset(&sa.sa_mask);??
sigdelset(&sa.sa_mask,?SIGCONT);??
sa.sa_handler?=?stop_handler;??
/*?NB:?sa_flags?doesn't?have?SA_RESTART.?
*?It?must?be?able?to?interrupt?wait().?
*/??
sigaction_set(SIGTSTP,?&sa);?/*?pause?*/??
/*?Does?not?work?as?intended,?at?least?in?2.6.20.?
*?SIGSTOP?is?simply?ignored?by?init:?
*/??
sigaction_set(SIGSTOP,?&sa);?/*?pause?*/??
/*?SIGINT?(Ctrl-Alt-Del)?must?interrupt?wait(),?
*?setting?handler?without?SA_RESTART?flag.?
*/??
bb_signals_recursive_norestart((1?<
}??
......??
}??
單獨(dú)拿出halt_reboot_pwoff和restart_handler這個(gè)函數(shù)來(lái)看看
[cpp]?view plain?copy
print?
/*?The?SIGUSR[12]/SIGTERM?handler?*/??
static?void?halt_reboot_pwoff(int?sig)?NORETURN;??
static?void?halt_reboot_pwoff(int?sig)??
{??
const?char?*m;??
unsigned?rb;??
/*?We?may?call?run()?and?it?unmasks?signals,?
*?including?the?one?masked?inside?this?signal?handler.?
*?Testcase?which?would?start?multiple?reboot?scripts:?
*??while?true;?do?reboot;?done?
*?Preventing?it:?
*/??
reset_sighandlers_and_unblock_sigs();??
run_shutdown_and_kill_processes();??
m?=?"halt";??
rb?=?RB_HALT_SYSTEM;??
if?(sig?==?SIGTERM)?{??
m?=?"reboot";??
rb?=?RB_AUTOBOOT;??
}?else?if?(sig?==?SIGUSR2)?{??
m?=?"poweroff";??
rb?=?RB_POWER_OFF;??
}??
message(L_CONSOLE,?"Requesting?system?%s",?m);??
pause_and_low_level_reboot(rb);??
/*?not?reached?*/??
}??
restart_handler函數(shù)如下:
[cpp]?view plain?copy
print?
/*?Handler?for?QUIT?-?exec?"restart"?action,?
*?else?(no?such?action?defined)?do?nothing?*/??
static?void?restart_handler(int?sig?UNUSED_PARAM)??
{??
struct?init_action?*a;??
for?(a?=?init_action_list;?a;?a?=?a->next)?{??
if?(!(a->action_type?&?RESTART))??
continue;??
/*?Starting?from?here,?we?won't?return.?
*?Thus?don't?need?to?worry?about?preserving?errno?
*?and?such.?
*/??
reset_sighandlers_and_unblock_sigs();??
run_shutdown_and_kill_processes();??
#ifdef?RB_ENABLE_CAD??
/*?Allow?Ctrl-Alt-Del?to?reboot?the?system.?
*?This?is?how?kernel?sets?it?up?for?init,?we?follow?suit.?
*/??
reboot(RB_ENABLE_CAD);?/*?misnomer?*/??
#endif??
if?(open_stdio_to_tty(a->terminal))?{??
dbg_message(L_CONSOLE,?"Trying?to?re-exec?%s",?a->command);??
/*?Theoretically?should?be?safe.?
*?But?in?practice,?kernel?bugs?may?leave?
*?unkillable?processes,?and?wait()?may?block?forever.?
*?Oh?well.?Hoping?"new"?init?won't?be?too?surprised?
*?by?having?children?it?didn't?create.?
*/??
//while?(wait(NULL)?>?0)??
//??continue;??
init_exec(a->command);??
}??
/*?Open?or?exec?failed?*/??
pause_and_low_level_reboot(RB_HALT_SYSTEM);??
/*?not?reached?*/??
}??
}??
通過(guò)分析,我們看到他們都會(huì)有調(diào)用這兩個(gè)函數(shù):reset_sighandlers_and_unblock_sigs();以及?run_shutdown_and_kill_processes();,我們重點(diǎn)關(guān)注如下這個(gè)函數(shù):
[cpp]?view plain?copy
print?
static?void?run_shutdown_and_kill_processes(void)??
{??
/*?Run?everything?to?be?run?at?"shutdown".??This?is?done?_prior_?
*?to?killing?everything,?in?case?people?wish?to?use?scripts?to?
*?shut?things?down?gracefully...?*/??
run_actions(SHUTDOWN);??
message(L_CONSOLE?|?L_LOG,?"The?system?is?going?down?NOW!");??
/*?Send?signals?to?every?process?_except_?pid?1?*/??
kill(-1,?SIGTERM);??
message(L_CONSOLE?|?L_LOG,?"Sent?SIG%s?to?all?processes",?"TERM");??
sync();??
sleep(1);??
kill(-1,?SIGKILL);??
message(L_CONSOLE,?"Sent?SIG%s?to?all?processes",?"KILL");??
sync();??
/*sleep(1);?-?callers?take?care?about?making?a?pause?*/??
}??
嘿嘿,終于看到了上面的打印信息:The system is going down NOW !! 以及Sending SIGTERM to all processes. 同時(shí)在上面的halt_reboot_pwoff和restart_handler中都會(huì)調(diào)用這樣一個(gè)函數(shù),如下:
[cpp]?view plain?copy
print?
static?void?pause_and_low_level_reboot(unsigned?magic)?NORETURN;??
static?void?pause_and_low_level_reboot(unsigned?magic)??
{??
pid_t?pid;??
/*?Allow?time?for?last?message?to?reach?serial?console,?etc?*/??
sleep(1);??
/*?We?have?to?fork?here,?since?the?kernel?calls?do_exit(EXIT_SUCCESS)?
*?in?linux/kernel/sys.c,?which?can?cause?the?machine?to?panic?when?
*?the?init?process?exits...?*/??
pid?=?vfork();??
if?(pid?==?0)?{?/*?child?*/??
reboot(magic);??
_exit(EXIT_SUCCESS);??
}??
while?(1)??
sleep(1);??
}??
看到了嗎?有一個(gè)reboot(magic)函數(shù),對(duì)于vfork函數(shù),請(qǐng)參考fork函數(shù)。這里不多說(shuō)了.... 我們現(xiàn)在來(lái)看看reboot.h文件,如下:
[cpp]?view plain?copy
print?
/*?
*?Definitions?related?to?the?reboot()?system?call,?
*?shared?between?init.c?and?halt.c.?
*/??
#include???
#ifndef?RB_HALT_SYSTEM??
#?if?defined(__linux__)??
#??define?RB_HALT_SYSTEM??0xcdef0123??
#??define?RB_ENABLE_CAD???0x89abcdef??
#??define?RB_DISABLE_CAD??0??
#??define?RB_POWER_OFF????0x4321fedc??
#??define?RB_AUTOBOOT?????0x01234567??
#?elif?defined(RB_HALT)??
#??define?RB_HALT_SYSTEM??RB_HALT??
#?endif??
#endif??
/*?Stop?system?and?switch?power?off?if?possible.??*/??
#ifndef?RB_POWER_OFF??
#?if?defined(RB_POWERDOWN)??
#??define?RB_POWER_OFF??RB_POWERDOWN??
#?elif?defined(__linux__)??
#??define?RB_POWER_OFF??0x4321fedc??
#?else??
#??warning?"poweroff?unsupported,?using?halt?as?fallback"??
#??define?RB_POWER_OFF??RB_HALT_SYSTEM??
#?endif??
#endif??
而在linux的內(nèi)核中的定義如下:
busybox和linux內(nèi)核中的REBOOT的定義值是一樣的。看到了沒(méi)有了。這個(gè)很重要的哦,否則busybox是無(wú)法調(diào)用linux內(nèi)核的reboot函數(shù)。
二:Linux內(nèi)核層的分析
Linux內(nèi)核是如何銜接busybox的reboot函數(shù)的呢,如下代碼:
[cpp]?view plain?copy
print?
/*?
*?Reboot?system?call:?for?obvious?reasons?only?root?may?call?it,?
*?and?even?root?needs?to?set?up?some?magic?numbers?in?the?registers?
*?so?that?some?mistake?won't?make?this?reboot?the?whole?machine.?
*?You?can?also?set?the?meaning?of?the?ctrl-alt-del-key?here.?
*?
*?reboot?doesn't?sync:?do?that?yourself?before?calling?this.?
*/??
SYSCALL_DEFINE4(reboot,?int,?magic1,?int,?magic2,?unsigned?int,?cmd,??
void?__user?*,?arg)??
{??
char?buffer[256];??
int?ret?=?0;??
/*?We?only?trust?the?superuser?with?rebooting?the?system.?*/??
if?(!capable(CAP_SYS_BOOT))??
return?-EPERM;??
/*?For?safety,?we?require?"magic"?arguments.?*/??
if?(magic1?!=?LINUX_REBOOT_MAGIC1?||??
(magic2?!=?LINUX_REBOOT_MAGIC2?&&??
magic2?!=?LINUX_REBOOT_MAGIC2A?&&??
magic2?!=?LINUX_REBOOT_MAGIC2B?&&??
magic2?!=?LINUX_REBOOT_MAGIC2C))??
return?-EINVAL;??
/*?Instead?of?trying?to?make?the?power_off?code?look?like?
*?halt?when?pm_power_off?is?not?set?do?it?the?easy?way.?
*/??
if?((cmd?==?LINUX_REBOOT_CMD_POWER_OFF)?&&?!pm_power_off)??
cmd?=?LINUX_REBOOT_CMD_HALT;??
lock_kernel();??
switch?(cmd)?{??
case?LINUX_REBOOT_CMD_RESTART:??
kernel_restart(NULL);?//這個(gè)就是重新啟動(dòng)Linx的命令??
break;??
case?LINUX_REBOOT_CMD_CAD_ON:??
C_A_D?=?1;??
break;??
case?LINUX_REBOOT_CMD_CAD_OFF:??
C_A_D?=?0;??
break;??
case?LINUX_REBOOT_CMD_HALT:??
kernel_halt();??
unlock_kernel();??
do_exit(0);??
panic("cannot?halt");??
case?LINUX_REBOOT_CMD_POWER_OFF:??
kernel_power_off();??
unlock_kernel();??
do_exit(0);??
break;??
case?LINUX_REBOOT_CMD_RESTART2:??
if?(strncpy_from_user(&buffer[0],?arg,?sizeof(buffer)?-?1)?0)?{??
unlock_kernel();??
return?-EFAULT;??
}??
buffer[sizeof(buffer)?-?1]?=?'\0';??
kernel_restart(buffer);??
break;??
#ifdef?CONFIG_KEXEC??
case?LINUX_REBOOT_CMD_KEXEC:??
ret?=?kernel_kexec();??
break;??
#endif??
#ifdef?CONFIG_HIBERNATION??
case?LINUX_REBOOT_CMD_SW_SUSPEND:??
ret?=?hibernate();??
break;??
#endif??
default:??
ret?=?-EINVAL;??
break;??
}??
unlock_kernel();??
return?ret;??
}??
繼續(xù)跟蹤kernel_restart()函數(shù),如下:
最終會(huì)調(diào)用一個(gè)machine_restart(cmd)函數(shù),這個(gè)是跟具體的芯片有很大的關(guān)系的,我們進(jìn)一步的分析如下:
看到了嗎,最終是調(diào)用arch_reset來(lái)復(fù)位整個(gè)系統(tǒng)的。同時(shí)我們也看到了S3C2440的reset的函數(shù)如下:
在arm_pm_restart = s3c24xx_pm_restart()函數(shù),最終也是調(diào)用arm_machine_restart(mod, cmd)來(lái)實(shí)現(xiàn)的。而在arm_machine_restart()函數(shù)中,最終也是調(diào)用arch_reset()函數(shù)來(lái)實(shí)現(xiàn),而這個(gè)函數(shù)是在哪里呢。在S3C2440沒(méi)有看到arch_reset函數(shù)的實(shí)現(xiàn),因此從S3C2410中找到了如下的代碼,請(qǐng)繼續(xù)看下面的代碼:
終于看到了arch_reset函數(shù),最終是采用S3C2410或者S3C2440的WatchDog來(lái)實(shí)現(xiàn)reboot的命令的。大家可以想想,busybox的poweroff命令,是如何實(shí)現(xiàn)通過(guò)Linux系統(tǒng)關(guān)閉整個(gè)系統(tǒng)的電源呢,其實(shí)很簡(jiǎn)單,只需要實(shí)現(xiàn)下面的函數(shù)中的pm_power_off的回調(diào)函數(shù)即可。
我們可以通過(guò)一個(gè)GPIO來(lái)控制整個(gè)系統(tǒng)的電源,而通過(guò)上面的pm_power_off的回調(diào)函數(shù)來(lái)實(shí)現(xiàn),只需要在pm_power_off函數(shù)對(duì)GPIO進(jìn)行操作就可以了。你看不是很簡(jiǎn)單嗎?
?
評(píng)論
查看更多