/* * This variable is used privately to keep track of whether or not * reboot_type is still set to its default value (i.e., reboot= hasn't * been set on the command line). This is needed so that we can * suppress DMI scanning for reboot quirks. Without it, it's * impossible to override a faulty reboot quirk without recompiling. */intreboot_default=1;

/** * emergency_restart - reboot the system * * Without shutting down any hardware or taking any locks * reboot the system. This is called when we know we are in * trouble so this is our best effort to reboot. This is * safe to call in interrupt context. */voidemergency_restart(void){kmsg_dump(KMSG_DUMP_EMERG);machine_emergency_restart();}EXPORT_SYMBOL_GPL(emergency_restart);voidkernel_restart_prepare(char*cmd){blocking_notifier_call_chain(&reboot_notifier_list,SYS_RESTART,cmd);system_state=SYSTEM_RESTART;usermodehelper_disable();device_shutdown();}/** * register_reboot_notifier - Register function to be called at reboot time * @nb: Info about notifier function to be called * * Registers a function with the list of functions * to be called at reboot time. * * Currently always returns zero, as blocking_notifier_chain_register() * always returns zero. */intregister_reboot_notifier(structnotifier_block*nb){returnblocking_notifier_chain_register(&reboot_notifier_list,nb);}EXPORT_SYMBOL(register_reboot_notifier);/** * unregister_reboot_notifier - Unregister previously registered reboot notifier * @nb: Hook to be unregistered * * Unregisters a previously registered reboot * notifier function. * * Returns zero on success, or %-ENOENT on failure. */intunregister_reboot_notifier(structnotifier_block*nb){returnblocking_notifier_chain_unregister(&reboot_notifier_list,nb);}EXPORT_SYMBOL(unregister_reboot_notifier);

/* * Notifier list for kernel code which wants to be called * to restart the system. */staticATOMIC_NOTIFIER_HEAD(restart_handler_list);/** * register_restart_handler - Register function to be called to reset * the system * @nb: Info about handler function to be called * @nb->priority: Handler priority. Handlers should follow the * following guidelines for setting priorities. * 0: Restart handler of last resort, * with limited restart capabilities * 128: Default restart handler; use if no other * restart handler is expected to be available, * and/or if restart functionality is * sufficient to restart the entire system * 255: Highest priority restart handler, will * preempt all other restart handlers * * Registers a function with code to be called to restart the * system. * * Registered functions will be called from machine_restart as last * step of the restart sequence (if the architecture specific * machine_restart function calls do_kernel_restart - see below * for details). * Registered functions are expected to restart the system immediately. * If more than one function is registered, the restart handler priority * selects which function will be called first. * * Restart handlers are expected to be registered from non-architecture * code, typically from drivers. A typical use case would be a system * where restart functionality is provided through a watchdog. Multiple * restart handlers may exist; for example, one restart handler might * restart the entire system, while another only restarts the CPU. * In such cases, the restart handler which only restarts part of the * hardware is expected to register with low priority to ensure that * it only runs if no other means to restart the system is available. * * Currently always returns zero, as atomic_notifier_chain_register() * always returns zero. */intregister_restart_handler(structnotifier_block*nb){returnatomic_notifier_chain_register(&restart_handler_list,nb);}EXPORT_SYMBOL(register_restart_handler);/** * unregister_restart_handler - Unregister previously registered * restart handler * @nb: Hook to be unregistered * * Unregisters a previously registered restart handler function. * * Returns zero on success, or %-ENOENT on failure. */intunregister_restart_handler(structnotifier_block*nb){returnatomic_notifier_chain_unregister(&restart_handler_list,nb);}EXPORT_SYMBOL(unregister_restart_handler);/** * do_kernel_restart - Execute kernel restart handler call chain * * Calls functions registered with register_restart_handler. * * Expected to be called from machine_restart as last step of the restart * sequence. * * Restarts the system immediately if a restart handler function has been * registered. Otherwise does nothing. */voiddo_kernel_restart(char*cmd){atomic_notifier_call_chain(&restart_handler_list,reboot_mode,cmd);}

cpu_hotplug_disable();/* Make certain the cpu I'm about to reboot on is online */if(!cpu_online(cpu))cpu=cpumask_first(cpu_online_mask);/* Prevent races with other tasks migrating this task */current->flags|=PF_NO_SETAFFINITY;/* Make certain I only run on the appropriate processor */set_cpus_allowed_ptr(current,cpumask_of(cpu));}/** * kernel_restart - reboot the system * @cmd: pointer to buffer containing command to execute for restart * or %NULL * * Shutdown everything and perform a clean reboot. * This is not safe to call in interrupt context. */voidkernel_restart(char*cmd){kernel_restart_prepare(cmd);migrate_to_reboot_cpu();syscore_shutdown();if(!cmd)

/* * 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,unsignedint,cmd,void__user*,arg){structpid_namespace*pid_ns=task_active_pid_ns(current);charbuffer[256];intret=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||

return-EINVAL;/* * If pid namespaces are enabled and the current task is in a child * pid_namespace, the command is handled by reboot_pid_ns() which will * call do_exit(). */ret=reboot_pid_ns(pid_ns,cmd);if(ret)returnret;/* 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;

returnret;}staticvoiddeferred_cad(structwork_struct*dummy){kernel_restart(NULL);}/* * This function gets called by ctrl-alt-del - ie the keyboard interrupt. * As it's called within an interrupt, it may NOT sync: the only choice * is whether to reboot at once, or just ignore the ctrl-alt-del. */voidctrl_alt_del(void){staticDECLARE_WORK(cad_work,deferred_cad);if(C_A_D)schedule_work(&cad_work);elsekill_cad_pid(SIGINT,1);}charpoweroff_cmd[POWEROFF_CMD_PATH_LEN]="/sbin/poweroff";

/* * I guess this should try to kick off some daemon to sync and * poweroff asap. Or not even bother syncing if we're doing an * emergency shutdown? */emergency_sync();kernel_power_off();}returnret;}staticboolpoweroff_force;staticvoidpoweroff_work_func(structwork_struct*work){__orderly_poweroff(poweroff_force);}staticDECLARE_WORK(poweroff_work,poweroff_work_func);/** * orderly_poweroff - Trigger an orderly system poweroff * @force: force poweroff if command execution fails * * This may be called from any context to trigger a system shutdown. * If the orderly shutdown fails, it will force an immediate shutdown. */

staticvoidreboot_work_func(structwork_struct*work){__orderly_reboot();}staticDECLARE_WORK(reboot_work,reboot_work_func);/** * orderly_reboot - Trigger an orderly system reboot * * This may be called from any context to trigger a system reboot. * If the orderly reboot fails, it will force an immediate reboot. */voidorderly_reboot(void){schedule_work(&reboot_work);}EXPORT_SYMBOL_GPL(orderly_reboot);