/* * 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.
*/ int reboot_default = 1; int reboot_cpu; enum reboot_type reboot_type = BOOT_ACPI; int reboot_force;
/* * This variable is used to indicate if a halt was initiated instead of a * reboot when the reboot call was invoked with LINUX_REBOOT_CMD_POWER_OFF, but * the system cannot be powered off. This allowes kernel_halt() to notify users * of that.
*/ staticbool poweroff_fallback_to_halt;
/* * Temporary stub that prevents linkage failure while we're in process * of removing all uses of legacy pm_power_off() around the kernel.
*/ void __weak (*pm_power_off)(void);
/* * Notifier list for kernel code which wants to be called * at shutdown. This is used to stop any idling DMA operations * and the like.
*/ static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
/** * 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.
*/ void emergency_restart(void)
{
kmsg_dump(KMSG_DUMP_EMERG);
system_state = SYSTEM_RESTART;
machine_emergency_restart();
}
EXPORT_SYMBOL_GPL(emergency_restart);
/** * 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.
*/ int register_reboot_notifier(struct notifier_block *nb)
{ return blocking_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.
*/ int unregister_reboot_notifier(struct notifier_block *nb)
{ return blocking_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.
*/ static ATOMIC_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.
*/ int register_restart_handler(struct notifier_block *nb)
{ return atomic_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.
*/ int unregister_restart_handler(struct notifier_block *nb)
{ return atomic_notifier_chain_unregister(&restart_handler_list, nb);
}
EXPORT_SYMBOL(unregister_restart_handler);
/** * do_kernel_restart - Execute kernel restart handler call chain * * @cmd: pointer to buffer containing command to execute for restart * or %NULL * * 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.
*/ void do_kernel_restart(char *cmd)
{
atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
}
void migrate_to_reboot_cpu(void)
{ /* The boot cpu is always logical cpu 0 */ int cpu = reboot_cpu;
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));
}
/* * Notifier list for kernel code which wants to be called * to prepare system for restart.
*/ static BLOCKING_NOTIFIER_HEAD(restart_prep_handler_list);
/** * 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.
*/ void kernel_restart(char *cmd)
{
kernel_restart_prepare(cmd);
do_kernel_restart_prepare();
migrate_to_reboot_cpu();
syscore_shutdown(); if (!cmd)
pr_emerg("Restarting system\n"); else
pr_emerg("Restarting system with command '%s'\n", cmd);
kmsg_dump(KMSG_DUMP_SHUTDOWN);
machine_restart(cmd);
}
EXPORT_SYMBOL_GPL(kernel_restart);
staticvoid kernel_shutdown_prepare(enum system_states state)
{
blocking_notifier_call_chain(&reboot_notifier_list,
(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
system_state = state;
usermodehelper_disable();
device_shutdown();
} /** * kernel_halt - halt the system * * Shutdown everything and perform a clean system halt.
*/ void kernel_halt(void)
{
kernel_shutdown_prepare(SYSTEM_HALT);
migrate_to_reboot_cpu();
syscore_shutdown(); if (poweroff_fallback_to_halt)
pr_emerg("Power off not available: System halted instead\n"); else
pr_emerg("System halted\n");
kmsg_dump(KMSG_DUMP_SHUTDOWN);
machine_halt();
}
EXPORT_SYMBOL_GPL(kernel_halt);
/* * Notifier list for kernel code which wants to be called * to prepare system for power off.
*/ static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list);
/* * Notifier list for kernel code which wants to be called * to power off system.
*/ static ATOMIC_NOTIFIER_HEAD(power_off_handler_list);
/* * Platforms like m68k can't allocate sys_off handler dynamically * at the early boot time because memory allocator isn't available yet.
*/ if (priority == SYS_OFF_PRIO_PLATFORM) {
handler = &platform_sys_off_handler; if (handler->cb_data) return ERR_PTR(-EBUSY);
} else { if (system_state > SYSTEM_RUNNING)
flags = GFP_ATOMIC; else
flags = GFP_KERNEL;
handler = kzalloc(sizeof(*handler), flags); if (!handler) return ERR_PTR(-ENOMEM);
}
/** * register_sys_off_handler - Register sys-off handler * @mode: Sys-off mode * @priority: Handler priority * @callback: Callback function * @cb_data: Callback argument * * Registers system power-off or restart handler that will be invoked * at the step corresponding to the given sys-off mode. Handler's callback * should return NOTIFY_DONE to permit execution of the next handler in * the call chain or NOTIFY_STOP to break the chain (in error case for * example). * * Multiple handlers can be registered at the default priority level. * * Only one handler can be registered at the non-default priority level, * otherwise ERR_PTR(-EBUSY) is returned. * * Returns a new instance of struct sys_off_handler on success, or * an ERR_PTR()-encoded error code otherwise.
*/ struct sys_off_handler *
register_sys_off_handler(enum sys_off_mode mode, int priority, int (*callback)(struct sys_off_data *data), void *cb_data)
{ struct sys_off_handler *handler; int err;
handler = alloc_sys_off_handler(priority); if (IS_ERR(handler)) return handler;
/** * register_platform_power_off - Register platform-level power-off callback * @power_off: Power-off callback * * Registers power-off callback that will be called as last step * of the power-off sequence. This callback is expected to be invoked * for the last resort. Only one platform power-off callback is allowed * to be registered at a time. * * Returns zero on success, or error code on failure.
*/ int register_platform_power_off(void (*power_off)(void))
{ struct sys_off_handler *handler;
handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
SYS_OFF_PRIO_PLATFORM,
platform_power_off_notify,
power_off); if (IS_ERR(handler)) return PTR_ERR(handler);
/** * do_kernel_power_off - Execute kernel power-off handler call chain * * Expected to be called as last step of the power-off sequence. * * Powers off the system immediately if a power-off handler function has * been registered. Otherwise does nothing.
*/ void do_kernel_power_off(void)
{ struct sys_off_handler *sys_off = NULL;
/* * Register sys-off handlers for legacy PM callback. This allows * legacy PM callbacks temporary co-exist with the new sys-off API. * * TODO: Remove legacy handlers once all legacy PM users will be * switched to the sys-off based APIs.
*/ if (pm_power_off)
sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF,
SYS_OFF_PRIO_DEFAULT,
legacy_pm_power_off, NULL);
/** * kernel_can_power_off - check whether system can be powered off * * Returns true if power-off handler is registered and system can be * powered off, false otherwise.
*/ bool kernel_can_power_off(void)
{ return !atomic_notifier_call_chain_is_empty(&power_off_handler_list) ||
pm_power_off;
}
EXPORT_SYMBOL_GPL(kernel_can_power_off);
/** * kernel_power_off - power_off the system * * Shutdown everything and perform a clean system power_off.
*/ void kernel_power_off(void)
{
kernel_shutdown_prepare(SYSTEM_POWER_OFF);
do_kernel_power_off_prepare();
migrate_to_reboot_cpu();
syscore_shutdown();
pr_emerg("Power down\n");
pr_flush(1000, true);
kmsg_dump(KMSG_DUMP_SHUTDOWN);
machine_power_off();
}
EXPORT_SYMBOL_GPL(kernel_power_off);
DEFINE_MUTEX(system_transition_mutex);
/* * 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)
{ 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 &&
magic2 != LINUX_REBOOT_MAGIC2A &&
magic2 != LINUX_REBOOT_MAGIC2B &&
magic2 != LINUX_REBOOT_MAGIC2C)) 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) return ret;
/* 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) && !kernel_can_power_off()) {
poweroff_fallback_to_halt = true;
cmd = LINUX_REBOOT_CMD_HALT;
}
mutex_lock(&system_transition_mutex); switch (cmd) { case LINUX_REBOOT_CMD_RESTART:
kernel_restart(NULL); 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();
do_exit(0);
case LINUX_REBOOT_CMD_POWER_OFF:
kernel_power_off();
do_exit(0); break;
case LINUX_REBOOT_CMD_RESTART2:
ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1); if (ret < 0) {
ret = -EFAULT; break;
}
buffer[sizeof(buffer) - 1] = '\0';
kernel_restart(buffer); break;
#ifdef CONFIG_KEXEC_CORE 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;
}
mutex_unlock(&system_transition_mutex); return ret;
}
/* * 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.
*/ void ctrl_alt_del(void)
{ static DECLARE_WORK(cad_work, deferred_cad);
if (C_A_D)
schedule_work(&cad_work); else
kill_cad_pid(SIGINT, 1);
}
staticint run_cmd(constchar *cmd)
{ char **argv; staticchar *envp[] = { "HOME=/", "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
NULL
}; int ret;
argv = argv_split(GFP_KERNEL, cmd, NULL); if (argv) {
ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
argv_free(argv);
} else {
ret = -ENOMEM;
}
return ret;
}
staticint __orderly_reboot(void)
{ int ret;
ret = run_cmd(reboot_cmd);
if (ret) {
pr_warn("Failed to start orderly reboot: forcing the issue\n");
emergency_sync();
kernel_restart(NULL);
}
return ret;
}
staticint __orderly_poweroff(bool force)
{ int ret;
ret = run_cmd(poweroff_cmd);
if (ret && force) {
pr_warn("Failed to start orderly shutdown: forcing the issue\n");
/* * 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();
}
/** * 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.
*/ void orderly_poweroff(bool force)
{ if (force) /* do not override the pending "true" */
poweroff_force = true;
schedule_work(&poweroff_work);
}
EXPORT_SYMBOL_GPL(orderly_poweroff);
/** * 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.
*/ void orderly_reboot(void)
{
schedule_work(&reboot_work);
}
EXPORT_SYMBOL_GPL(orderly_reboot);
staticconstchar *hw_protection_action_str(enum hw_protection_action action)
{ switch (action) { case HWPROT_ACT_SHUTDOWN: return"shutdown"; case HWPROT_ACT_REBOOT: return"reboot"; default: return"undefined";
}
}
/** * hw_failure_emergency_action_func - emergency action work after a known delay * @work: work_struct associated with the emergency action function * * This function is called in very critical situations to force * a kernel poweroff or reboot after a configurable timeout value.
*/ staticvoid hw_failure_emergency_action_func(struct work_struct *work)
{ constchar *action_str = hw_protection_action_str(hw_failure_emergency_action);
/* * We have reached here after the emergency action waiting period has * expired. This means orderly_poweroff/reboot has not been able to * shut off the system for some reason. * * Try to shut off the system immediately if possible
*/
if (hw_failure_emergency_action == HWPROT_ACT_REBOOT)
kernel_restart(NULL); else
kernel_power_off();
/* * Worst of the worst case trigger emergency restart
*/
pr_emerg("Hardware protection %s failed. Trying emergency restart\n",
action_str);
emergency_restart();
}
/** * hw_failure_emergency_schedule - Schedule an emergency system shutdown or reboot * * @action: The hardware protection action to be taken * @action_delay_ms: Time in milliseconds to elapse before triggering action * * This may be called from any critical situation to trigger a system shutdown * or reboot after a given period of time. * If time is negative this is not scheduled.
*/ staticvoid hw_failure_emergency_schedule(enum hw_protection_action action, int action_delay_ms)
{ if (action_delay_ms <= 0) return;
hw_failure_emergency_action = action;
schedule_delayed_work(&hw_failure_emergency_action_work,
msecs_to_jiffies(action_delay_ms));
}
/** * __hw_protection_trigger - Trigger an emergency system shutdown or reboot * * @reason: Reason of emergency shutdown or reboot to be printed. * @ms_until_forced: Time to wait for orderly shutdown or reboot before * triggering it. Negative value disables the forced * shutdown or reboot. * @action: The hardware protection action to be taken. * * Initiate an emergency system shutdown or reboot in order to protect * hardware from further damage. Usage examples include a thermal protection. * NOTE: The request is ignored if protection shutdown or reboot is already * pending even if the previous request has given a large timeout for forced * shutdown/reboot.
*/ void __hw_protection_trigger(constchar *reason, int ms_until_forced, enum hw_protection_action action)
{ static atomic_t allow_proceed = ATOMIC_INIT(1);
if (action == HWPROT_ACT_DEFAULT)
action = hw_protection_action;
case's': /* * reboot_cpu is s[mp]#### with #### being the processor * to be used for rebooting. Skip 's' or 'smp' prefix.
*/
str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
if (isdigit(str[0])) { int cpu = simple_strtoul(str, NULL, 0);
if (cpu >= num_possible_cpus()) {
pr_err("Ignoring the CPU number in reboot= option. " "CPU %d exceeds possible cpu number %d\n",
cpu, num_possible_cpus()); break;
}
reboot_cpu = cpu;
} else
*mode = REBOOT_SOFT; break;
switch (reboot_mode) { case REBOOT_COLD:
val = REBOOT_COLD_STR; break; case REBOOT_WARM:
val = REBOOT_WARM_STR; break; case REBOOT_HARD:
val = REBOOT_HARD_STR; break; case REBOOT_SOFT:
val = REBOOT_SOFT_STR; break; case REBOOT_GPIO:
val = REBOOT_GPIO_STR; break; default:
val = REBOOT_UNDEFINED_STR;
}
switch (reboot_type) { case BOOT_TRIPLE:
val = BOOT_TRIPLE_STR; break; case BOOT_KBD:
val = BOOT_KBD_STR; break; case BOOT_BIOS:
val = BOOT_BIOS_STR; break; case BOOT_ACPI:
val = BOOT_ACPI_STR; break; case BOOT_EFI:
val = BOOT_EFI_STR; break; case BOOT_CF9_FORCE:
val = BOOT_PCI_STR; break; default:
val = REBOOT_UNDEFINED_STR;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.