由于本人水平相当有限,不当之处还望大家多多指教。
涉及的内核源码,基于linux-3.10.102。
首先,对于用户态发起的这类操作请求,最终都是通过sys_reboot系统调用(源码在kernel/sys.c)实现的。
其代码如下。如其注释所言,他除了可以重启关机停机,还可以修改ctrl-alt-del组合键的含义。
另外,注释还说到此系统调用不会做sync。即sync需要用户在调用此系统调用之前自己完成。
/*
* 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)
{
struct pid_namespace *pid_ns = task_active_pid_ns(current);
char buffer[256];
int ret = 0;
/* We only trust the superuser with rebooting the system. */
if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
return -EPERM;
/* For safety, we require "magic" arguments. */
if (magic1 != LINUX_REBOOT_MAGIC1 ||
(magic2 != LINUX_REBOOT_MAGIC2 &&