115d94b82SRobin Holt /* 215d94b82SRobin Holt * linux/kernel/reboot.c 315d94b82SRobin Holt * 415d94b82SRobin Holt * Copyright (C) 2013 Linus Torvalds 515d94b82SRobin Holt */ 615d94b82SRobin Holt 7972ee83dSRobin Holt #define pr_fmt(fmt) "reboot: " fmt 8972ee83dSRobin Holt 91b3a5d02SRobin Holt #include <linux/ctype.h> 1015d94b82SRobin Holt #include <linux/export.h> 1115d94b82SRobin Holt #include <linux/kexec.h> 1215d94b82SRobin Holt #include <linux/kmod.h> 1315d94b82SRobin Holt #include <linux/kmsg_dump.h> 1415d94b82SRobin Holt #include <linux/reboot.h> 1515d94b82SRobin Holt #include <linux/suspend.h> 1615d94b82SRobin Holt #include <linux/syscalls.h> 1715d94b82SRobin Holt #include <linux/syscore_ops.h> 1815d94b82SRobin Holt #include <linux/uaccess.h> 1915d94b82SRobin Holt 2015d94b82SRobin Holt /* 2115d94b82SRobin Holt * this indicates whether you can reboot with ctrl-alt-del: the default is yes 2215d94b82SRobin Holt */ 2315d94b82SRobin Holt 2415d94b82SRobin Holt int C_A_D = 1; 2515d94b82SRobin Holt struct pid *cad_pid; 2615d94b82SRobin Holt EXPORT_SYMBOL(cad_pid); 2715d94b82SRobin Holt 281b3a5d02SRobin Holt #if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32) 291b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE = REBOOT_HARD 301b3a5d02SRobin Holt #else 311b3a5d02SRobin Holt #define DEFAULT_REBOOT_MODE 321b3a5d02SRobin Holt #endif 331b3a5d02SRobin Holt enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE; 341b3a5d02SRobin Holt 35e2f0b88eSChuansheng Liu /* 36e2f0b88eSChuansheng Liu * This variable is used privately to keep track of whether or not 37e2f0b88eSChuansheng Liu * reboot_type is still set to its default value (i.e., reboot= hasn't 38e2f0b88eSChuansheng Liu * been set on the command line). This is needed so that we can 39e2f0b88eSChuansheng Liu * suppress DMI scanning for reboot quirks. Without it, it's 40e2f0b88eSChuansheng Liu * impossible to override a faulty reboot quirk without recompiling. 41e2f0b88eSChuansheng Liu */ 42e2f0b88eSChuansheng Liu int reboot_default = 1; 431b3a5d02SRobin Holt int reboot_cpu; 441b3a5d02SRobin Holt enum reboot_type reboot_type = BOOT_ACPI; 451b3a5d02SRobin Holt int reboot_force; 461b3a5d02SRobin Holt 4715d94b82SRobin Holt /* 4815d94b82SRobin Holt * If set, this is used for preparing the system to power off. 4915d94b82SRobin Holt */ 5015d94b82SRobin Holt 5115d94b82SRobin Holt void (*pm_power_off_prepare)(void); 52*74f008f2SOleksij Rempel EXPORT_SYMBOL_GPL(pm_power_off_prepare); 5315d94b82SRobin Holt 5415d94b82SRobin Holt /** 5515d94b82SRobin Holt * emergency_restart - reboot the system 5615d94b82SRobin Holt * 5715d94b82SRobin Holt * Without shutting down any hardware or taking any locks 5815d94b82SRobin Holt * reboot the system. This is called when we know we are in 5915d94b82SRobin Holt * trouble so this is our best effort to reboot. This is 6015d94b82SRobin Holt * safe to call in interrupt context. 6115d94b82SRobin Holt */ 6215d94b82SRobin Holt void emergency_restart(void) 6315d94b82SRobin Holt { 6415d94b82SRobin Holt kmsg_dump(KMSG_DUMP_EMERG); 6515d94b82SRobin Holt machine_emergency_restart(); 6615d94b82SRobin Holt } 6715d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart); 6815d94b82SRobin Holt 6915d94b82SRobin Holt void kernel_restart_prepare(char *cmd) 7015d94b82SRobin Holt { 7115d94b82SRobin Holt blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); 7215d94b82SRobin Holt system_state = SYSTEM_RESTART; 7315d94b82SRobin Holt usermodehelper_disable(); 7415d94b82SRobin Holt device_shutdown(); 7515d94b82SRobin Holt } 7615d94b82SRobin Holt 7715d94b82SRobin Holt /** 7815d94b82SRobin Holt * register_reboot_notifier - Register function to be called at reboot time 7915d94b82SRobin Holt * @nb: Info about notifier function to be called 8015d94b82SRobin Holt * 8115d94b82SRobin Holt * Registers a function with the list of functions 8215d94b82SRobin Holt * to be called at reboot time. 8315d94b82SRobin Holt * 8415d94b82SRobin Holt * Currently always returns zero, as blocking_notifier_chain_register() 8515d94b82SRobin Holt * always returns zero. 8615d94b82SRobin Holt */ 8715d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb) 8815d94b82SRobin Holt { 8915d94b82SRobin Holt return blocking_notifier_chain_register(&reboot_notifier_list, nb); 9015d94b82SRobin Holt } 9115d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier); 9215d94b82SRobin Holt 9315d94b82SRobin Holt /** 9415d94b82SRobin Holt * unregister_reboot_notifier - Unregister previously registered reboot notifier 9515d94b82SRobin Holt * @nb: Hook to be unregistered 9615d94b82SRobin Holt * 9715d94b82SRobin Holt * Unregisters a previously registered reboot 9815d94b82SRobin Holt * notifier function. 9915d94b82SRobin Holt * 10015d94b82SRobin Holt * Returns zero on success, or %-ENOENT on failure. 10115d94b82SRobin Holt */ 10215d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb) 10315d94b82SRobin Holt { 10415d94b82SRobin Holt return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); 10515d94b82SRobin Holt } 10615d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier); 10715d94b82SRobin Holt 1082d8364baSAndrey Smirnov static void devm_unregister_reboot_notifier(struct device *dev, void *res) 1092d8364baSAndrey Smirnov { 1102d8364baSAndrey Smirnov WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res)); 1112d8364baSAndrey Smirnov } 1122d8364baSAndrey Smirnov 1132d8364baSAndrey Smirnov int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb) 1142d8364baSAndrey Smirnov { 1152d8364baSAndrey Smirnov struct notifier_block **rcnb; 1162d8364baSAndrey Smirnov int ret; 1172d8364baSAndrey Smirnov 1182d8364baSAndrey Smirnov rcnb = devres_alloc(devm_unregister_reboot_notifier, 1192d8364baSAndrey Smirnov sizeof(*rcnb), GFP_KERNEL); 1202d8364baSAndrey Smirnov if (!rcnb) 1212d8364baSAndrey Smirnov return -ENOMEM; 1222d8364baSAndrey Smirnov 1232d8364baSAndrey Smirnov ret = register_reboot_notifier(nb); 1242d8364baSAndrey Smirnov if (!ret) { 1252d8364baSAndrey Smirnov *rcnb = nb; 1262d8364baSAndrey Smirnov devres_add(dev, rcnb); 1272d8364baSAndrey Smirnov } else { 1282d8364baSAndrey Smirnov devres_free(rcnb); 1292d8364baSAndrey Smirnov } 1302d8364baSAndrey Smirnov 1312d8364baSAndrey Smirnov return ret; 1322d8364baSAndrey Smirnov } 1332d8364baSAndrey Smirnov EXPORT_SYMBOL(devm_register_reboot_notifier); 1342d8364baSAndrey Smirnov 135b63adb97SGuenter Roeck /* 136b63adb97SGuenter Roeck * Notifier list for kernel code which wants to be called 137b63adb97SGuenter Roeck * to restart the system. 138b63adb97SGuenter Roeck */ 139b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list); 140b63adb97SGuenter Roeck 141b63adb97SGuenter Roeck /** 142b63adb97SGuenter Roeck * register_restart_handler - Register function to be called to reset 143b63adb97SGuenter Roeck * the system 144b63adb97SGuenter Roeck * @nb: Info about handler function to be called 145b63adb97SGuenter Roeck * @nb->priority: Handler priority. Handlers should follow the 146b63adb97SGuenter Roeck * following guidelines for setting priorities. 147b63adb97SGuenter Roeck * 0: Restart handler of last resort, 148b63adb97SGuenter Roeck * with limited restart capabilities 149b63adb97SGuenter Roeck * 128: Default restart handler; use if no other 150b63adb97SGuenter Roeck * restart handler is expected to be available, 151b63adb97SGuenter Roeck * and/or if restart functionality is 152b63adb97SGuenter Roeck * sufficient to restart the entire system 153b63adb97SGuenter Roeck * 255: Highest priority restart handler, will 154b63adb97SGuenter Roeck * preempt all other restart handlers 155b63adb97SGuenter Roeck * 156b63adb97SGuenter Roeck * Registers a function with code to be called to restart the 157b63adb97SGuenter Roeck * system. 158b63adb97SGuenter Roeck * 159b63adb97SGuenter Roeck * Registered functions will be called from machine_restart as last 160b63adb97SGuenter Roeck * step of the restart sequence (if the architecture specific 161b63adb97SGuenter Roeck * machine_restart function calls do_kernel_restart - see below 162b63adb97SGuenter Roeck * for details). 163b63adb97SGuenter Roeck * Registered functions are expected to restart the system immediately. 164b63adb97SGuenter Roeck * If more than one function is registered, the restart handler priority 165b63adb97SGuenter Roeck * selects which function will be called first. 166b63adb97SGuenter Roeck * 167b63adb97SGuenter Roeck * Restart handlers are expected to be registered from non-architecture 168b63adb97SGuenter Roeck * code, typically from drivers. A typical use case would be a system 169b63adb97SGuenter Roeck * where restart functionality is provided through a watchdog. Multiple 170b63adb97SGuenter Roeck * restart handlers may exist; for example, one restart handler might 171b63adb97SGuenter Roeck * restart the entire system, while another only restarts the CPU. 172b63adb97SGuenter Roeck * In such cases, the restart handler which only restarts part of the 173b63adb97SGuenter Roeck * hardware is expected to register with low priority to ensure that 174b63adb97SGuenter Roeck * it only runs if no other means to restart the system is available. 175b63adb97SGuenter Roeck * 176b63adb97SGuenter Roeck * Currently always returns zero, as atomic_notifier_chain_register() 177b63adb97SGuenter Roeck * always returns zero. 178b63adb97SGuenter Roeck */ 179b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb) 180b63adb97SGuenter Roeck { 181b63adb97SGuenter Roeck return atomic_notifier_chain_register(&restart_handler_list, nb); 182b63adb97SGuenter Roeck } 183b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler); 184b63adb97SGuenter Roeck 185b63adb97SGuenter Roeck /** 186b63adb97SGuenter Roeck * unregister_restart_handler - Unregister previously registered 187b63adb97SGuenter Roeck * restart handler 188b63adb97SGuenter Roeck * @nb: Hook to be unregistered 189b63adb97SGuenter Roeck * 190b63adb97SGuenter Roeck * Unregisters a previously registered restart handler function. 191b63adb97SGuenter Roeck * 192b63adb97SGuenter Roeck * Returns zero on success, or %-ENOENT on failure. 193b63adb97SGuenter Roeck */ 194b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb) 195b63adb97SGuenter Roeck { 196b63adb97SGuenter Roeck return atomic_notifier_chain_unregister(&restart_handler_list, nb); 197b63adb97SGuenter Roeck } 198b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler); 199b63adb97SGuenter Roeck 200b63adb97SGuenter Roeck /** 201b63adb97SGuenter Roeck * do_kernel_restart - Execute kernel restart handler call chain 202b63adb97SGuenter Roeck * 203b63adb97SGuenter Roeck * Calls functions registered with register_restart_handler. 204b63adb97SGuenter Roeck * 205b63adb97SGuenter Roeck * Expected to be called from machine_restart as last step of the restart 206b63adb97SGuenter Roeck * sequence. 207b63adb97SGuenter Roeck * 208b63adb97SGuenter Roeck * Restarts the system immediately if a restart handler function has been 209b63adb97SGuenter Roeck * registered. Otherwise does nothing. 210b63adb97SGuenter Roeck */ 211b63adb97SGuenter Roeck void do_kernel_restart(char *cmd) 212b63adb97SGuenter Roeck { 213b63adb97SGuenter Roeck atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd); 214b63adb97SGuenter Roeck } 215b63adb97SGuenter Roeck 216c97102baSVivek Goyal void migrate_to_reboot_cpu(void) 21715d94b82SRobin Holt { 21815d94b82SRobin Holt /* The boot cpu is always logical cpu 0 */ 2191b3a5d02SRobin Holt int cpu = reboot_cpu; 22015d94b82SRobin Holt 22115d94b82SRobin Holt cpu_hotplug_disable(); 22215d94b82SRobin Holt 22315d94b82SRobin Holt /* Make certain the cpu I'm about to reboot on is online */ 22415d94b82SRobin Holt if (!cpu_online(cpu)) 22515d94b82SRobin Holt cpu = cpumask_first(cpu_online_mask); 22615d94b82SRobin Holt 22715d94b82SRobin Holt /* Prevent races with other tasks migrating this task */ 22815d94b82SRobin Holt current->flags |= PF_NO_SETAFFINITY; 22915d94b82SRobin Holt 23015d94b82SRobin Holt /* Make certain I only run on the appropriate processor */ 23115d94b82SRobin Holt set_cpus_allowed_ptr(current, cpumask_of(cpu)); 23215d94b82SRobin Holt } 23315d94b82SRobin Holt 23415d94b82SRobin Holt /** 23515d94b82SRobin Holt * kernel_restart - reboot the system 23615d94b82SRobin Holt * @cmd: pointer to buffer containing command to execute for restart 23715d94b82SRobin Holt * or %NULL 23815d94b82SRobin Holt * 23915d94b82SRobin Holt * Shutdown everything and perform a clean reboot. 24015d94b82SRobin Holt * This is not safe to call in interrupt context. 24115d94b82SRobin Holt */ 24215d94b82SRobin Holt void kernel_restart(char *cmd) 24315d94b82SRobin Holt { 24415d94b82SRobin Holt kernel_restart_prepare(cmd); 24515d94b82SRobin Holt migrate_to_reboot_cpu(); 24615d94b82SRobin Holt syscore_shutdown(); 24715d94b82SRobin Holt if (!cmd) 248972ee83dSRobin Holt pr_emerg("Restarting system\n"); 24915d94b82SRobin Holt else 250972ee83dSRobin Holt pr_emerg("Restarting system with command '%s'\n", cmd); 25115d94b82SRobin Holt kmsg_dump(KMSG_DUMP_RESTART); 25215d94b82SRobin Holt machine_restart(cmd); 25315d94b82SRobin Holt } 25415d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart); 25515d94b82SRobin Holt 25615d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state) 25715d94b82SRobin Holt { 25815d94b82SRobin Holt blocking_notifier_call_chain(&reboot_notifier_list, 25915d94b82SRobin Holt (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL); 26015d94b82SRobin Holt system_state = state; 26115d94b82SRobin Holt usermodehelper_disable(); 26215d94b82SRobin Holt device_shutdown(); 26315d94b82SRobin Holt } 26415d94b82SRobin Holt /** 26515d94b82SRobin Holt * kernel_halt - halt the system 26615d94b82SRobin Holt * 26715d94b82SRobin Holt * Shutdown everything and perform a clean system halt. 26815d94b82SRobin Holt */ 26915d94b82SRobin Holt void kernel_halt(void) 27015d94b82SRobin Holt { 27115d94b82SRobin Holt kernel_shutdown_prepare(SYSTEM_HALT); 27215d94b82SRobin Holt migrate_to_reboot_cpu(); 27315d94b82SRobin Holt syscore_shutdown(); 274972ee83dSRobin Holt pr_emerg("System halted\n"); 27515d94b82SRobin Holt kmsg_dump(KMSG_DUMP_HALT); 27615d94b82SRobin Holt machine_halt(); 27715d94b82SRobin Holt } 27815d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt); 27915d94b82SRobin Holt 28015d94b82SRobin Holt /** 28115d94b82SRobin Holt * kernel_power_off - power_off the system 28215d94b82SRobin Holt * 28315d94b82SRobin Holt * Shutdown everything and perform a clean system power_off. 28415d94b82SRobin Holt */ 28515d94b82SRobin Holt void kernel_power_off(void) 28615d94b82SRobin Holt { 28715d94b82SRobin Holt kernel_shutdown_prepare(SYSTEM_POWER_OFF); 28815d94b82SRobin Holt if (pm_power_off_prepare) 28915d94b82SRobin Holt pm_power_off_prepare(); 29015d94b82SRobin Holt migrate_to_reboot_cpu(); 29115d94b82SRobin Holt syscore_shutdown(); 292972ee83dSRobin Holt pr_emerg("Power down\n"); 29315d94b82SRobin Holt kmsg_dump(KMSG_DUMP_POWEROFF); 29415d94b82SRobin Holt machine_power_off(); 29515d94b82SRobin Holt } 29615d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off); 29715d94b82SRobin Holt 29855f2503cSPingfan Liu DEFINE_MUTEX(system_transition_mutex); 29915d94b82SRobin Holt 30015d94b82SRobin Holt /* 30115d94b82SRobin Holt * Reboot system call: for obvious reasons only root may call it, 30215d94b82SRobin Holt * and even root needs to set up some magic numbers in the registers 30315d94b82SRobin Holt * so that some mistake won't make this reboot the whole machine. 30415d94b82SRobin Holt * You can also set the meaning of the ctrl-alt-del-key here. 30515d94b82SRobin Holt * 30615d94b82SRobin Holt * reboot doesn't sync: do that yourself before calling this. 30715d94b82SRobin Holt */ 30815d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, 30915d94b82SRobin Holt void __user *, arg) 31015d94b82SRobin Holt { 31115d94b82SRobin Holt struct pid_namespace *pid_ns = task_active_pid_ns(current); 31215d94b82SRobin Holt char buffer[256]; 31315d94b82SRobin Holt int ret = 0; 31415d94b82SRobin Holt 31515d94b82SRobin Holt /* We only trust the superuser with rebooting the system. */ 31615d94b82SRobin Holt if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT)) 31715d94b82SRobin Holt return -EPERM; 31815d94b82SRobin Holt 31915d94b82SRobin Holt /* For safety, we require "magic" arguments. */ 32015d94b82SRobin Holt if (magic1 != LINUX_REBOOT_MAGIC1 || 32115d94b82SRobin Holt (magic2 != LINUX_REBOOT_MAGIC2 && 32215d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2A && 32315d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2B && 32415d94b82SRobin Holt magic2 != LINUX_REBOOT_MAGIC2C)) 32515d94b82SRobin Holt return -EINVAL; 32615d94b82SRobin Holt 32715d94b82SRobin Holt /* 32815d94b82SRobin Holt * If pid namespaces are enabled and the current task is in a child 32915d94b82SRobin Holt * pid_namespace, the command is handled by reboot_pid_ns() which will 33015d94b82SRobin Holt * call do_exit(). 33115d94b82SRobin Holt */ 33215d94b82SRobin Holt ret = reboot_pid_ns(pid_ns, cmd); 33315d94b82SRobin Holt if (ret) 33415d94b82SRobin Holt return ret; 33515d94b82SRobin Holt 33615d94b82SRobin Holt /* Instead of trying to make the power_off code look like 33715d94b82SRobin Holt * halt when pm_power_off is not set do it the easy way. 33815d94b82SRobin Holt */ 33915d94b82SRobin Holt if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) 34015d94b82SRobin Holt cmd = LINUX_REBOOT_CMD_HALT; 34115d94b82SRobin Holt 34255f2503cSPingfan Liu mutex_lock(&system_transition_mutex); 34315d94b82SRobin Holt switch (cmd) { 34415d94b82SRobin Holt case LINUX_REBOOT_CMD_RESTART: 34515d94b82SRobin Holt kernel_restart(NULL); 34615d94b82SRobin Holt break; 34715d94b82SRobin Holt 34815d94b82SRobin Holt case LINUX_REBOOT_CMD_CAD_ON: 34915d94b82SRobin Holt C_A_D = 1; 35015d94b82SRobin Holt break; 35115d94b82SRobin Holt 35215d94b82SRobin Holt case LINUX_REBOOT_CMD_CAD_OFF: 35315d94b82SRobin Holt C_A_D = 0; 35415d94b82SRobin Holt break; 35515d94b82SRobin Holt 35615d94b82SRobin Holt case LINUX_REBOOT_CMD_HALT: 35715d94b82SRobin Holt kernel_halt(); 35815d94b82SRobin Holt do_exit(0); 35915d94b82SRobin Holt panic("cannot halt"); 36015d94b82SRobin Holt 36115d94b82SRobin Holt case LINUX_REBOOT_CMD_POWER_OFF: 36215d94b82SRobin Holt kernel_power_off(); 36315d94b82SRobin Holt do_exit(0); 36415d94b82SRobin Holt break; 36515d94b82SRobin Holt 36615d94b82SRobin Holt case LINUX_REBOOT_CMD_RESTART2: 367972ee83dSRobin Holt ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1); 368972ee83dSRobin Holt if (ret < 0) { 36915d94b82SRobin Holt ret = -EFAULT; 37015d94b82SRobin Holt break; 37115d94b82SRobin Holt } 37215d94b82SRobin Holt buffer[sizeof(buffer) - 1] = '\0'; 37315d94b82SRobin Holt 37415d94b82SRobin Holt kernel_restart(buffer); 37515d94b82SRobin Holt break; 37615d94b82SRobin Holt 3772965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 37815d94b82SRobin Holt case LINUX_REBOOT_CMD_KEXEC: 37915d94b82SRobin Holt ret = kernel_kexec(); 38015d94b82SRobin Holt break; 38115d94b82SRobin Holt #endif 38215d94b82SRobin Holt 38315d94b82SRobin Holt #ifdef CONFIG_HIBERNATION 38415d94b82SRobin Holt case LINUX_REBOOT_CMD_SW_SUSPEND: 38515d94b82SRobin Holt ret = hibernate(); 38615d94b82SRobin Holt break; 38715d94b82SRobin Holt #endif 38815d94b82SRobin Holt 38915d94b82SRobin Holt default: 39015d94b82SRobin Holt ret = -EINVAL; 39115d94b82SRobin Holt break; 39215d94b82SRobin Holt } 39355f2503cSPingfan Liu mutex_unlock(&system_transition_mutex); 39415d94b82SRobin Holt return ret; 39515d94b82SRobin Holt } 39615d94b82SRobin Holt 39715d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy) 39815d94b82SRobin Holt { 39915d94b82SRobin Holt kernel_restart(NULL); 40015d94b82SRobin Holt } 40115d94b82SRobin Holt 40215d94b82SRobin Holt /* 40315d94b82SRobin Holt * This function gets called by ctrl-alt-del - ie the keyboard interrupt. 40415d94b82SRobin Holt * As it's called within an interrupt, it may NOT sync: the only choice 40515d94b82SRobin Holt * is whether to reboot at once, or just ignore the ctrl-alt-del. 40615d94b82SRobin Holt */ 40715d94b82SRobin Holt void ctrl_alt_del(void) 40815d94b82SRobin Holt { 40915d94b82SRobin Holt static DECLARE_WORK(cad_work, deferred_cad); 41015d94b82SRobin Holt 41115d94b82SRobin Holt if (C_A_D) 41215d94b82SRobin Holt schedule_work(&cad_work); 41315d94b82SRobin Holt else 41415d94b82SRobin Holt kill_cad_pid(SIGINT, 1); 41515d94b82SRobin Holt } 41615d94b82SRobin Holt 41715d94b82SRobin Holt char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; 4187a54f46bSJoel Stanley static const char reboot_cmd[] = "/sbin/reboot"; 41915d94b82SRobin Holt 4207a54f46bSJoel Stanley static int run_cmd(const char *cmd) 42115d94b82SRobin Holt { 42215d94b82SRobin Holt char **argv; 42315d94b82SRobin Holt static char *envp[] = { 42415d94b82SRobin Holt "HOME=/", 42515d94b82SRobin Holt "PATH=/sbin:/bin:/usr/sbin:/usr/bin", 42615d94b82SRobin Holt NULL 42715d94b82SRobin Holt }; 42815d94b82SRobin Holt int ret; 4297a54f46bSJoel Stanley argv = argv_split(GFP_KERNEL, cmd, NULL); 43015d94b82SRobin Holt if (argv) { 43115d94b82SRobin Holt ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); 43215d94b82SRobin Holt argv_free(argv); 43315d94b82SRobin Holt } else { 43415d94b82SRobin Holt ret = -ENOMEM; 43515d94b82SRobin Holt } 43615d94b82SRobin Holt 4377a54f46bSJoel Stanley return ret; 4387a54f46bSJoel Stanley } 4397a54f46bSJoel Stanley 4407a54f46bSJoel Stanley static int __orderly_reboot(void) 4417a54f46bSJoel Stanley { 4427a54f46bSJoel Stanley int ret; 4437a54f46bSJoel Stanley 4447a54f46bSJoel Stanley ret = run_cmd(reboot_cmd); 4457a54f46bSJoel Stanley 4467a54f46bSJoel Stanley if (ret) { 4477a54f46bSJoel Stanley pr_warn("Failed to start orderly reboot: forcing the issue\n"); 4487a54f46bSJoel Stanley emergency_sync(); 4497a54f46bSJoel Stanley kernel_restart(NULL); 4507a54f46bSJoel Stanley } 4517a54f46bSJoel Stanley 4527a54f46bSJoel Stanley return ret; 4537a54f46bSJoel Stanley } 4547a54f46bSJoel Stanley 4557a54f46bSJoel Stanley static int __orderly_poweroff(bool force) 4567a54f46bSJoel Stanley { 4577a54f46bSJoel Stanley int ret; 4587a54f46bSJoel Stanley 4597a54f46bSJoel Stanley ret = run_cmd(poweroff_cmd); 4607a54f46bSJoel Stanley 46115d94b82SRobin Holt if (ret && force) { 462972ee83dSRobin Holt pr_warn("Failed to start orderly shutdown: forcing the issue\n"); 4637a54f46bSJoel Stanley 46415d94b82SRobin Holt /* 46515d94b82SRobin Holt * I guess this should try to kick off some daemon to sync and 46615d94b82SRobin Holt * poweroff asap. Or not even bother syncing if we're doing an 46715d94b82SRobin Holt * emergency shutdown? 46815d94b82SRobin Holt */ 46915d94b82SRobin Holt emergency_sync(); 47015d94b82SRobin Holt kernel_power_off(); 47115d94b82SRobin Holt } 47215d94b82SRobin Holt 47315d94b82SRobin Holt return ret; 47415d94b82SRobin Holt } 47515d94b82SRobin Holt 47615d94b82SRobin Holt static bool poweroff_force; 47715d94b82SRobin Holt 47815d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work) 47915d94b82SRobin Holt { 48015d94b82SRobin Holt __orderly_poweroff(poweroff_force); 48115d94b82SRobin Holt } 48215d94b82SRobin Holt 48315d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func); 48415d94b82SRobin Holt 48515d94b82SRobin Holt /** 48615d94b82SRobin Holt * orderly_poweroff - Trigger an orderly system poweroff 48715d94b82SRobin Holt * @force: force poweroff if command execution fails 48815d94b82SRobin Holt * 48915d94b82SRobin Holt * This may be called from any context to trigger a system shutdown. 49015d94b82SRobin Holt * If the orderly shutdown fails, it will force an immediate shutdown. 49115d94b82SRobin Holt */ 4927a54f46bSJoel Stanley void orderly_poweroff(bool force) 49315d94b82SRobin Holt { 49415d94b82SRobin Holt if (force) /* do not override the pending "true" */ 49515d94b82SRobin Holt poweroff_force = true; 49615d94b82SRobin Holt schedule_work(&poweroff_work); 49715d94b82SRobin Holt } 49815d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff); 4991b3a5d02SRobin Holt 5007a54f46bSJoel Stanley static void reboot_work_func(struct work_struct *work) 5017a54f46bSJoel Stanley { 5027a54f46bSJoel Stanley __orderly_reboot(); 5037a54f46bSJoel Stanley } 5047a54f46bSJoel Stanley 5057a54f46bSJoel Stanley static DECLARE_WORK(reboot_work, reboot_work_func); 5067a54f46bSJoel Stanley 5077a54f46bSJoel Stanley /** 5087a54f46bSJoel Stanley * orderly_reboot - Trigger an orderly system reboot 5097a54f46bSJoel Stanley * 5107a54f46bSJoel Stanley * This may be called from any context to trigger a system reboot. 5117a54f46bSJoel Stanley * If the orderly reboot fails, it will force an immediate reboot. 5127a54f46bSJoel Stanley */ 5137a54f46bSJoel Stanley void orderly_reboot(void) 5147a54f46bSJoel Stanley { 5157a54f46bSJoel Stanley schedule_work(&reboot_work); 5167a54f46bSJoel Stanley } 5177a54f46bSJoel Stanley EXPORT_SYMBOL_GPL(orderly_reboot); 5187a54f46bSJoel Stanley 5191b3a5d02SRobin Holt static int __init reboot_setup(char *str) 5201b3a5d02SRobin Holt { 5211b3a5d02SRobin Holt for (;;) { 5221b3a5d02SRobin Holt /* 5231b3a5d02SRobin Holt * Having anything passed on the command line via 5241b3a5d02SRobin Holt * reboot= will cause us to disable DMI checking 5251b3a5d02SRobin Holt * below. 5261b3a5d02SRobin Holt */ 5271b3a5d02SRobin Holt reboot_default = 0; 5281b3a5d02SRobin Holt 5291b3a5d02SRobin Holt switch (*str) { 5301b3a5d02SRobin Holt case 'w': 5311b3a5d02SRobin Holt reboot_mode = REBOOT_WARM; 5321b3a5d02SRobin Holt break; 5331b3a5d02SRobin Holt 5341b3a5d02SRobin Holt case 'c': 5351b3a5d02SRobin Holt reboot_mode = REBOOT_COLD; 5361b3a5d02SRobin Holt break; 5371b3a5d02SRobin Holt 5381b3a5d02SRobin Holt case 'h': 5391b3a5d02SRobin Holt reboot_mode = REBOOT_HARD; 5401b3a5d02SRobin Holt break; 5411b3a5d02SRobin Holt 5421b3a5d02SRobin Holt case 's': 543616feab7SFabian Frederick { 544616feab7SFabian Frederick int rc; 545616feab7SFabian Frederick 546616feab7SFabian Frederick if (isdigit(*(str+1))) { 547616feab7SFabian Frederick rc = kstrtoint(str+1, 0, &reboot_cpu); 548616feab7SFabian Frederick if (rc) 549616feab7SFabian Frederick return rc; 550616feab7SFabian Frederick } else if (str[1] == 'm' && str[2] == 'p' && 551616feab7SFabian Frederick isdigit(*(str+3))) { 552616feab7SFabian Frederick rc = kstrtoint(str+3, 0, &reboot_cpu); 553616feab7SFabian Frederick if (rc) 554616feab7SFabian Frederick return rc; 555616feab7SFabian Frederick } else 5561b3a5d02SRobin Holt reboot_mode = REBOOT_SOFT; 5571b3a5d02SRobin Holt break; 558616feab7SFabian Frederick } 5591b3a5d02SRobin Holt case 'g': 5601b3a5d02SRobin Holt reboot_mode = REBOOT_GPIO; 5611b3a5d02SRobin Holt break; 5621b3a5d02SRobin Holt 5631b3a5d02SRobin Holt case 'b': 5641b3a5d02SRobin Holt case 'a': 5651b3a5d02SRobin Holt case 'k': 5661b3a5d02SRobin Holt case 't': 5671b3a5d02SRobin Holt case 'e': 5681b3a5d02SRobin Holt case 'p': 5691b3a5d02SRobin Holt reboot_type = *str; 5701b3a5d02SRobin Holt break; 5711b3a5d02SRobin Holt 5721b3a5d02SRobin Holt case 'f': 5731b3a5d02SRobin Holt reboot_force = 1; 5741b3a5d02SRobin Holt break; 5751b3a5d02SRobin Holt } 5761b3a5d02SRobin Holt 5771b3a5d02SRobin Holt str = strchr(str, ','); 5781b3a5d02SRobin Holt if (str) 5791b3a5d02SRobin Holt str++; 5801b3a5d02SRobin Holt else 5811b3a5d02SRobin Holt break; 5821b3a5d02SRobin Holt } 5831b3a5d02SRobin Holt return 1; 5841b3a5d02SRobin Holt } 5851b3a5d02SRobin Holt __setup("reboot=", reboot_setup); 586