xref: /openbmc/linux/kernel/reboot.c (revision b63adb979583ef185718d774d8162387db5589c0)
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);
5215d94b82SRobin Holt 
5315d94b82SRobin Holt /**
5415d94b82SRobin Holt  *	emergency_restart - reboot the system
5515d94b82SRobin Holt  *
5615d94b82SRobin Holt  *	Without shutting down any hardware or taking any locks
5715d94b82SRobin Holt  *	reboot the system.  This is called when we know we are in
5815d94b82SRobin Holt  *	trouble so this is our best effort to reboot.  This is
5915d94b82SRobin Holt  *	safe to call in interrupt context.
6015d94b82SRobin Holt  */
6115d94b82SRobin Holt void emergency_restart(void)
6215d94b82SRobin Holt {
6315d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_EMERG);
6415d94b82SRobin Holt 	machine_emergency_restart();
6515d94b82SRobin Holt }
6615d94b82SRobin Holt EXPORT_SYMBOL_GPL(emergency_restart);
6715d94b82SRobin Holt 
6815d94b82SRobin Holt void kernel_restart_prepare(char *cmd)
6915d94b82SRobin Holt {
7015d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
7115d94b82SRobin Holt 	system_state = SYSTEM_RESTART;
7215d94b82SRobin Holt 	usermodehelper_disable();
7315d94b82SRobin Holt 	device_shutdown();
7415d94b82SRobin Holt }
7515d94b82SRobin Holt 
7615d94b82SRobin Holt /**
7715d94b82SRobin Holt  *	register_reboot_notifier - Register function to be called at reboot time
7815d94b82SRobin Holt  *	@nb: Info about notifier function to be called
7915d94b82SRobin Holt  *
8015d94b82SRobin Holt  *	Registers a function with the list of functions
8115d94b82SRobin Holt  *	to be called at reboot time.
8215d94b82SRobin Holt  *
8315d94b82SRobin Holt  *	Currently always returns zero, as blocking_notifier_chain_register()
8415d94b82SRobin Holt  *	always returns zero.
8515d94b82SRobin Holt  */
8615d94b82SRobin Holt int register_reboot_notifier(struct notifier_block *nb)
8715d94b82SRobin Holt {
8815d94b82SRobin Holt 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
8915d94b82SRobin Holt }
9015d94b82SRobin Holt EXPORT_SYMBOL(register_reboot_notifier);
9115d94b82SRobin Holt 
9215d94b82SRobin Holt /**
9315d94b82SRobin Holt  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
9415d94b82SRobin Holt  *	@nb: Hook to be unregistered
9515d94b82SRobin Holt  *
9615d94b82SRobin Holt  *	Unregisters a previously registered reboot
9715d94b82SRobin Holt  *	notifier function.
9815d94b82SRobin Holt  *
9915d94b82SRobin Holt  *	Returns zero on success, or %-ENOENT on failure.
10015d94b82SRobin Holt  */
10115d94b82SRobin Holt int unregister_reboot_notifier(struct notifier_block *nb)
10215d94b82SRobin Holt {
10315d94b82SRobin Holt 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
10415d94b82SRobin Holt }
10515d94b82SRobin Holt EXPORT_SYMBOL(unregister_reboot_notifier);
10615d94b82SRobin Holt 
107*b63adb97SGuenter Roeck /*
108*b63adb97SGuenter Roeck  *	Notifier list for kernel code which wants to be called
109*b63adb97SGuenter Roeck  *	to restart the system.
110*b63adb97SGuenter Roeck  */
111*b63adb97SGuenter Roeck static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
112*b63adb97SGuenter Roeck 
113*b63adb97SGuenter Roeck /**
114*b63adb97SGuenter Roeck  *	register_restart_handler - Register function to be called to reset
115*b63adb97SGuenter Roeck  *				   the system
116*b63adb97SGuenter Roeck  *	@nb: Info about handler function to be called
117*b63adb97SGuenter Roeck  *	@nb->priority:	Handler priority. Handlers should follow the
118*b63adb97SGuenter Roeck  *			following guidelines for setting priorities.
119*b63adb97SGuenter Roeck  *			0:	Restart handler of last resort,
120*b63adb97SGuenter Roeck  *				with limited restart capabilities
121*b63adb97SGuenter Roeck  *			128:	Default restart handler; use if no other
122*b63adb97SGuenter Roeck  *				restart handler is expected to be available,
123*b63adb97SGuenter Roeck  *				and/or if restart functionality is
124*b63adb97SGuenter Roeck  *				sufficient to restart the entire system
125*b63adb97SGuenter Roeck  *			255:	Highest priority restart handler, will
126*b63adb97SGuenter Roeck  *				preempt all other restart handlers
127*b63adb97SGuenter Roeck  *
128*b63adb97SGuenter Roeck  *	Registers a function with code to be called to restart the
129*b63adb97SGuenter Roeck  *	system.
130*b63adb97SGuenter Roeck  *
131*b63adb97SGuenter Roeck  *	Registered functions will be called from machine_restart as last
132*b63adb97SGuenter Roeck  *	step of the restart sequence (if the architecture specific
133*b63adb97SGuenter Roeck  *	machine_restart function calls do_kernel_restart - see below
134*b63adb97SGuenter Roeck  *	for details).
135*b63adb97SGuenter Roeck  *	Registered functions are expected to restart the system immediately.
136*b63adb97SGuenter Roeck  *	If more than one function is registered, the restart handler priority
137*b63adb97SGuenter Roeck  *	selects which function will be called first.
138*b63adb97SGuenter Roeck  *
139*b63adb97SGuenter Roeck  *	Restart handlers are expected to be registered from non-architecture
140*b63adb97SGuenter Roeck  *	code, typically from drivers. A typical use case would be a system
141*b63adb97SGuenter Roeck  *	where restart functionality is provided through a watchdog. Multiple
142*b63adb97SGuenter Roeck  *	restart handlers may exist; for example, one restart handler might
143*b63adb97SGuenter Roeck  *	restart the entire system, while another only restarts the CPU.
144*b63adb97SGuenter Roeck  *	In such cases, the restart handler which only restarts part of the
145*b63adb97SGuenter Roeck  *	hardware is expected to register with low priority to ensure that
146*b63adb97SGuenter Roeck  *	it only runs if no other means to restart the system is available.
147*b63adb97SGuenter Roeck  *
148*b63adb97SGuenter Roeck  *	Currently always returns zero, as atomic_notifier_chain_register()
149*b63adb97SGuenter Roeck  *	always returns zero.
150*b63adb97SGuenter Roeck  */
151*b63adb97SGuenter Roeck int register_restart_handler(struct notifier_block *nb)
152*b63adb97SGuenter Roeck {
153*b63adb97SGuenter Roeck 	return atomic_notifier_chain_register(&restart_handler_list, nb);
154*b63adb97SGuenter Roeck }
155*b63adb97SGuenter Roeck EXPORT_SYMBOL(register_restart_handler);
156*b63adb97SGuenter Roeck 
157*b63adb97SGuenter Roeck /**
158*b63adb97SGuenter Roeck  *	unregister_restart_handler - Unregister previously registered
159*b63adb97SGuenter Roeck  *				     restart handler
160*b63adb97SGuenter Roeck  *	@nb: Hook to be unregistered
161*b63adb97SGuenter Roeck  *
162*b63adb97SGuenter Roeck  *	Unregisters a previously registered restart handler function.
163*b63adb97SGuenter Roeck  *
164*b63adb97SGuenter Roeck  *	Returns zero on success, or %-ENOENT on failure.
165*b63adb97SGuenter Roeck  */
166*b63adb97SGuenter Roeck int unregister_restart_handler(struct notifier_block *nb)
167*b63adb97SGuenter Roeck {
168*b63adb97SGuenter Roeck 	return atomic_notifier_chain_unregister(&restart_handler_list, nb);
169*b63adb97SGuenter Roeck }
170*b63adb97SGuenter Roeck EXPORT_SYMBOL(unregister_restart_handler);
171*b63adb97SGuenter Roeck 
172*b63adb97SGuenter Roeck /**
173*b63adb97SGuenter Roeck  *	do_kernel_restart - Execute kernel restart handler call chain
174*b63adb97SGuenter Roeck  *
175*b63adb97SGuenter Roeck  *	Calls functions registered with register_restart_handler.
176*b63adb97SGuenter Roeck  *
177*b63adb97SGuenter Roeck  *	Expected to be called from machine_restart as last step of the restart
178*b63adb97SGuenter Roeck  *	sequence.
179*b63adb97SGuenter Roeck  *
180*b63adb97SGuenter Roeck  *	Restarts the system immediately if a restart handler function has been
181*b63adb97SGuenter Roeck  *	registered. Otherwise does nothing.
182*b63adb97SGuenter Roeck  */
183*b63adb97SGuenter Roeck void do_kernel_restart(char *cmd)
184*b63adb97SGuenter Roeck {
185*b63adb97SGuenter Roeck 	atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
186*b63adb97SGuenter Roeck }
187*b63adb97SGuenter Roeck 
188c97102baSVivek Goyal void migrate_to_reboot_cpu(void)
18915d94b82SRobin Holt {
19015d94b82SRobin Holt 	/* The boot cpu is always logical cpu 0 */
1911b3a5d02SRobin Holt 	int cpu = reboot_cpu;
19215d94b82SRobin Holt 
19315d94b82SRobin Holt 	cpu_hotplug_disable();
19415d94b82SRobin Holt 
19515d94b82SRobin Holt 	/* Make certain the cpu I'm about to reboot on is online */
19615d94b82SRobin Holt 	if (!cpu_online(cpu))
19715d94b82SRobin Holt 		cpu = cpumask_first(cpu_online_mask);
19815d94b82SRobin Holt 
19915d94b82SRobin Holt 	/* Prevent races with other tasks migrating this task */
20015d94b82SRobin Holt 	current->flags |= PF_NO_SETAFFINITY;
20115d94b82SRobin Holt 
20215d94b82SRobin Holt 	/* Make certain I only run on the appropriate processor */
20315d94b82SRobin Holt 	set_cpus_allowed_ptr(current, cpumask_of(cpu));
20415d94b82SRobin Holt }
20515d94b82SRobin Holt 
20615d94b82SRobin Holt /**
20715d94b82SRobin Holt  *	kernel_restart - reboot the system
20815d94b82SRobin Holt  *	@cmd: pointer to buffer containing command to execute for restart
20915d94b82SRobin Holt  *		or %NULL
21015d94b82SRobin Holt  *
21115d94b82SRobin Holt  *	Shutdown everything and perform a clean reboot.
21215d94b82SRobin Holt  *	This is not safe to call in interrupt context.
21315d94b82SRobin Holt  */
21415d94b82SRobin Holt void kernel_restart(char *cmd)
21515d94b82SRobin Holt {
21615d94b82SRobin Holt 	kernel_restart_prepare(cmd);
21715d94b82SRobin Holt 	migrate_to_reboot_cpu();
21815d94b82SRobin Holt 	syscore_shutdown();
21915d94b82SRobin Holt 	if (!cmd)
220972ee83dSRobin Holt 		pr_emerg("Restarting system\n");
22115d94b82SRobin Holt 	else
222972ee83dSRobin Holt 		pr_emerg("Restarting system with command '%s'\n", cmd);
22315d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_RESTART);
22415d94b82SRobin Holt 	machine_restart(cmd);
22515d94b82SRobin Holt }
22615d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_restart);
22715d94b82SRobin Holt 
22815d94b82SRobin Holt static void kernel_shutdown_prepare(enum system_states state)
22915d94b82SRobin Holt {
23015d94b82SRobin Holt 	blocking_notifier_call_chain(&reboot_notifier_list,
23115d94b82SRobin Holt 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
23215d94b82SRobin Holt 	system_state = state;
23315d94b82SRobin Holt 	usermodehelper_disable();
23415d94b82SRobin Holt 	device_shutdown();
23515d94b82SRobin Holt }
23615d94b82SRobin Holt /**
23715d94b82SRobin Holt  *	kernel_halt - halt the system
23815d94b82SRobin Holt  *
23915d94b82SRobin Holt  *	Shutdown everything and perform a clean system halt.
24015d94b82SRobin Holt  */
24115d94b82SRobin Holt void kernel_halt(void)
24215d94b82SRobin Holt {
24315d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_HALT);
24415d94b82SRobin Holt 	migrate_to_reboot_cpu();
24515d94b82SRobin Holt 	syscore_shutdown();
246972ee83dSRobin Holt 	pr_emerg("System halted\n");
24715d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_HALT);
24815d94b82SRobin Holt 	machine_halt();
24915d94b82SRobin Holt }
25015d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_halt);
25115d94b82SRobin Holt 
25215d94b82SRobin Holt /**
25315d94b82SRobin Holt  *	kernel_power_off - power_off the system
25415d94b82SRobin Holt  *
25515d94b82SRobin Holt  *	Shutdown everything and perform a clean system power_off.
25615d94b82SRobin Holt  */
25715d94b82SRobin Holt void kernel_power_off(void)
25815d94b82SRobin Holt {
25915d94b82SRobin Holt 	kernel_shutdown_prepare(SYSTEM_POWER_OFF);
26015d94b82SRobin Holt 	if (pm_power_off_prepare)
26115d94b82SRobin Holt 		pm_power_off_prepare();
26215d94b82SRobin Holt 	migrate_to_reboot_cpu();
26315d94b82SRobin Holt 	syscore_shutdown();
264972ee83dSRobin Holt 	pr_emerg("Power down\n");
26515d94b82SRobin Holt 	kmsg_dump(KMSG_DUMP_POWEROFF);
26615d94b82SRobin Holt 	machine_power_off();
26715d94b82SRobin Holt }
26815d94b82SRobin Holt EXPORT_SYMBOL_GPL(kernel_power_off);
26915d94b82SRobin Holt 
27015d94b82SRobin Holt static DEFINE_MUTEX(reboot_mutex);
27115d94b82SRobin Holt 
27215d94b82SRobin Holt /*
27315d94b82SRobin Holt  * Reboot system call: for obvious reasons only root may call it,
27415d94b82SRobin Holt  * and even root needs to set up some magic numbers in the registers
27515d94b82SRobin Holt  * so that some mistake won't make this reboot the whole machine.
27615d94b82SRobin Holt  * You can also set the meaning of the ctrl-alt-del-key here.
27715d94b82SRobin Holt  *
27815d94b82SRobin Holt  * reboot doesn't sync: do that yourself before calling this.
27915d94b82SRobin Holt  */
28015d94b82SRobin Holt SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
28115d94b82SRobin Holt 		void __user *, arg)
28215d94b82SRobin Holt {
28315d94b82SRobin Holt 	struct pid_namespace *pid_ns = task_active_pid_ns(current);
28415d94b82SRobin Holt 	char buffer[256];
28515d94b82SRobin Holt 	int ret = 0;
28615d94b82SRobin Holt 
28715d94b82SRobin Holt 	/* We only trust the superuser with rebooting the system. */
28815d94b82SRobin Holt 	if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
28915d94b82SRobin Holt 		return -EPERM;
29015d94b82SRobin Holt 
29115d94b82SRobin Holt 	/* For safety, we require "magic" arguments. */
29215d94b82SRobin Holt 	if (magic1 != LINUX_REBOOT_MAGIC1 ||
29315d94b82SRobin Holt 			(magic2 != LINUX_REBOOT_MAGIC2 &&
29415d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2A &&
29515d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2B &&
29615d94b82SRobin Holt 			magic2 != LINUX_REBOOT_MAGIC2C))
29715d94b82SRobin Holt 		return -EINVAL;
29815d94b82SRobin Holt 
29915d94b82SRobin Holt 	/*
30015d94b82SRobin Holt 	 * If pid namespaces are enabled and the current task is in a child
30115d94b82SRobin Holt 	 * pid_namespace, the command is handled by reboot_pid_ns() which will
30215d94b82SRobin Holt 	 * call do_exit().
30315d94b82SRobin Holt 	 */
30415d94b82SRobin Holt 	ret = reboot_pid_ns(pid_ns, cmd);
30515d94b82SRobin Holt 	if (ret)
30615d94b82SRobin Holt 		return ret;
30715d94b82SRobin Holt 
30815d94b82SRobin Holt 	/* Instead of trying to make the power_off code look like
30915d94b82SRobin Holt 	 * halt when pm_power_off is not set do it the easy way.
31015d94b82SRobin Holt 	 */
31115d94b82SRobin Holt 	if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
31215d94b82SRobin Holt 		cmd = LINUX_REBOOT_CMD_HALT;
31315d94b82SRobin Holt 
31415d94b82SRobin Holt 	mutex_lock(&reboot_mutex);
31515d94b82SRobin Holt 	switch (cmd) {
31615d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART:
31715d94b82SRobin Holt 		kernel_restart(NULL);
31815d94b82SRobin Holt 		break;
31915d94b82SRobin Holt 
32015d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_ON:
32115d94b82SRobin Holt 		C_A_D = 1;
32215d94b82SRobin Holt 		break;
32315d94b82SRobin Holt 
32415d94b82SRobin Holt 	case LINUX_REBOOT_CMD_CAD_OFF:
32515d94b82SRobin Holt 		C_A_D = 0;
32615d94b82SRobin Holt 		break;
32715d94b82SRobin Holt 
32815d94b82SRobin Holt 	case LINUX_REBOOT_CMD_HALT:
32915d94b82SRobin Holt 		kernel_halt();
33015d94b82SRobin Holt 		do_exit(0);
33115d94b82SRobin Holt 		panic("cannot halt");
33215d94b82SRobin Holt 
33315d94b82SRobin Holt 	case LINUX_REBOOT_CMD_POWER_OFF:
33415d94b82SRobin Holt 		kernel_power_off();
33515d94b82SRobin Holt 		do_exit(0);
33615d94b82SRobin Holt 		break;
33715d94b82SRobin Holt 
33815d94b82SRobin Holt 	case LINUX_REBOOT_CMD_RESTART2:
339972ee83dSRobin Holt 		ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
340972ee83dSRobin Holt 		if (ret < 0) {
34115d94b82SRobin Holt 			ret = -EFAULT;
34215d94b82SRobin Holt 			break;
34315d94b82SRobin Holt 		}
34415d94b82SRobin Holt 		buffer[sizeof(buffer) - 1] = '\0';
34515d94b82SRobin Holt 
34615d94b82SRobin Holt 		kernel_restart(buffer);
34715d94b82SRobin Holt 		break;
34815d94b82SRobin Holt 
34915d94b82SRobin Holt #ifdef CONFIG_KEXEC
35015d94b82SRobin Holt 	case LINUX_REBOOT_CMD_KEXEC:
35115d94b82SRobin Holt 		ret = kernel_kexec();
35215d94b82SRobin Holt 		break;
35315d94b82SRobin Holt #endif
35415d94b82SRobin Holt 
35515d94b82SRobin Holt #ifdef CONFIG_HIBERNATION
35615d94b82SRobin Holt 	case LINUX_REBOOT_CMD_SW_SUSPEND:
35715d94b82SRobin Holt 		ret = hibernate();
35815d94b82SRobin Holt 		break;
35915d94b82SRobin Holt #endif
36015d94b82SRobin Holt 
36115d94b82SRobin Holt 	default:
36215d94b82SRobin Holt 		ret = -EINVAL;
36315d94b82SRobin Holt 		break;
36415d94b82SRobin Holt 	}
36515d94b82SRobin Holt 	mutex_unlock(&reboot_mutex);
36615d94b82SRobin Holt 	return ret;
36715d94b82SRobin Holt }
36815d94b82SRobin Holt 
36915d94b82SRobin Holt static void deferred_cad(struct work_struct *dummy)
37015d94b82SRobin Holt {
37115d94b82SRobin Holt 	kernel_restart(NULL);
37215d94b82SRobin Holt }
37315d94b82SRobin Holt 
37415d94b82SRobin Holt /*
37515d94b82SRobin Holt  * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
37615d94b82SRobin Holt  * As it's called within an interrupt, it may NOT sync: the only choice
37715d94b82SRobin Holt  * is whether to reboot at once, or just ignore the ctrl-alt-del.
37815d94b82SRobin Holt  */
37915d94b82SRobin Holt void ctrl_alt_del(void)
38015d94b82SRobin Holt {
38115d94b82SRobin Holt 	static DECLARE_WORK(cad_work, deferred_cad);
38215d94b82SRobin Holt 
38315d94b82SRobin Holt 	if (C_A_D)
38415d94b82SRobin Holt 		schedule_work(&cad_work);
38515d94b82SRobin Holt 	else
38615d94b82SRobin Holt 		kill_cad_pid(SIGINT, 1);
38715d94b82SRobin Holt }
38815d94b82SRobin Holt 
38915d94b82SRobin Holt char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
39015d94b82SRobin Holt 
39115d94b82SRobin Holt static int __orderly_poweroff(bool force)
39215d94b82SRobin Holt {
39315d94b82SRobin Holt 	char **argv;
39415d94b82SRobin Holt 	static char *envp[] = {
39515d94b82SRobin Holt 		"HOME=/",
39615d94b82SRobin Holt 		"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
39715d94b82SRobin Holt 		NULL
39815d94b82SRobin Holt 	};
39915d94b82SRobin Holt 	int ret;
40015d94b82SRobin Holt 
40115d94b82SRobin Holt 	argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
40215d94b82SRobin Holt 	if (argv) {
40315d94b82SRobin Holt 		ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
40415d94b82SRobin Holt 		argv_free(argv);
40515d94b82SRobin Holt 	} else {
40615d94b82SRobin Holt 		ret = -ENOMEM;
40715d94b82SRobin Holt 	}
40815d94b82SRobin Holt 
40915d94b82SRobin Holt 	if (ret && force) {
410972ee83dSRobin Holt 		pr_warn("Failed to start orderly shutdown: forcing the issue\n");
41115d94b82SRobin Holt 		/*
41215d94b82SRobin Holt 		 * I guess this should try to kick off some daemon to sync and
41315d94b82SRobin Holt 		 * poweroff asap.  Or not even bother syncing if we're doing an
41415d94b82SRobin Holt 		 * emergency shutdown?
41515d94b82SRobin Holt 		 */
41615d94b82SRobin Holt 		emergency_sync();
41715d94b82SRobin Holt 		kernel_power_off();
41815d94b82SRobin Holt 	}
41915d94b82SRobin Holt 
42015d94b82SRobin Holt 	return ret;
42115d94b82SRobin Holt }
42215d94b82SRobin Holt 
42315d94b82SRobin Holt static bool poweroff_force;
42415d94b82SRobin Holt 
42515d94b82SRobin Holt static void poweroff_work_func(struct work_struct *work)
42615d94b82SRobin Holt {
42715d94b82SRobin Holt 	__orderly_poweroff(poweroff_force);
42815d94b82SRobin Holt }
42915d94b82SRobin Holt 
43015d94b82SRobin Holt static DECLARE_WORK(poweroff_work, poweroff_work_func);
43115d94b82SRobin Holt 
43215d94b82SRobin Holt /**
43315d94b82SRobin Holt  * orderly_poweroff - Trigger an orderly system poweroff
43415d94b82SRobin Holt  * @force: force poweroff if command execution fails
43515d94b82SRobin Holt  *
43615d94b82SRobin Holt  * This may be called from any context to trigger a system shutdown.
43715d94b82SRobin Holt  * If the orderly shutdown fails, it will force an immediate shutdown.
43815d94b82SRobin Holt  */
43915d94b82SRobin Holt int orderly_poweroff(bool force)
44015d94b82SRobin Holt {
44115d94b82SRobin Holt 	if (force) /* do not override the pending "true" */
44215d94b82SRobin Holt 		poweroff_force = true;
44315d94b82SRobin Holt 	schedule_work(&poweroff_work);
44415d94b82SRobin Holt 	return 0;
44515d94b82SRobin Holt }
44615d94b82SRobin Holt EXPORT_SYMBOL_GPL(orderly_poweroff);
4471b3a5d02SRobin Holt 
4481b3a5d02SRobin Holt static int __init reboot_setup(char *str)
4491b3a5d02SRobin Holt {
4501b3a5d02SRobin Holt 	for (;;) {
4511b3a5d02SRobin Holt 		/*
4521b3a5d02SRobin Holt 		 * Having anything passed on the command line via
4531b3a5d02SRobin Holt 		 * reboot= will cause us to disable DMI checking
4541b3a5d02SRobin Holt 		 * below.
4551b3a5d02SRobin Holt 		 */
4561b3a5d02SRobin Holt 		reboot_default = 0;
4571b3a5d02SRobin Holt 
4581b3a5d02SRobin Holt 		switch (*str) {
4591b3a5d02SRobin Holt 		case 'w':
4601b3a5d02SRobin Holt 			reboot_mode = REBOOT_WARM;
4611b3a5d02SRobin Holt 			break;
4621b3a5d02SRobin Holt 
4631b3a5d02SRobin Holt 		case 'c':
4641b3a5d02SRobin Holt 			reboot_mode = REBOOT_COLD;
4651b3a5d02SRobin Holt 			break;
4661b3a5d02SRobin Holt 
4671b3a5d02SRobin Holt 		case 'h':
4681b3a5d02SRobin Holt 			reboot_mode = REBOOT_HARD;
4691b3a5d02SRobin Holt 			break;
4701b3a5d02SRobin Holt 
4711b3a5d02SRobin Holt 		case 's':
472616feab7SFabian Frederick 		{
473616feab7SFabian Frederick 			int rc;
474616feab7SFabian Frederick 
475616feab7SFabian Frederick 			if (isdigit(*(str+1))) {
476616feab7SFabian Frederick 				rc = kstrtoint(str+1, 0, &reboot_cpu);
477616feab7SFabian Frederick 				if (rc)
478616feab7SFabian Frederick 					return rc;
479616feab7SFabian Frederick 			} else if (str[1] == 'm' && str[2] == 'p' &&
480616feab7SFabian Frederick 				   isdigit(*(str+3))) {
481616feab7SFabian Frederick 				rc = kstrtoint(str+3, 0, &reboot_cpu);
482616feab7SFabian Frederick 				if (rc)
483616feab7SFabian Frederick 					return rc;
484616feab7SFabian Frederick 			} else
4851b3a5d02SRobin Holt 				reboot_mode = REBOOT_SOFT;
4861b3a5d02SRobin Holt 			break;
487616feab7SFabian Frederick 		}
4881b3a5d02SRobin Holt 		case 'g':
4891b3a5d02SRobin Holt 			reboot_mode = REBOOT_GPIO;
4901b3a5d02SRobin Holt 			break;
4911b3a5d02SRobin Holt 
4921b3a5d02SRobin Holt 		case 'b':
4931b3a5d02SRobin Holt 		case 'a':
4941b3a5d02SRobin Holt 		case 'k':
4951b3a5d02SRobin Holt 		case 't':
4961b3a5d02SRobin Holt 		case 'e':
4971b3a5d02SRobin Holt 		case 'p':
4981b3a5d02SRobin Holt 			reboot_type = *str;
4991b3a5d02SRobin Holt 			break;
5001b3a5d02SRobin Holt 
5011b3a5d02SRobin Holt 		case 'f':
5021b3a5d02SRobin Holt 			reboot_force = 1;
5031b3a5d02SRobin Holt 			break;
5041b3a5d02SRobin Holt 		}
5051b3a5d02SRobin Holt 
5061b3a5d02SRobin Holt 		str = strchr(str, ',');
5071b3a5d02SRobin Holt 		if (str)
5081b3a5d02SRobin Holt 			str++;
5091b3a5d02SRobin Holt 		else
5101b3a5d02SRobin Holt 			break;
5111b3a5d02SRobin Holt 	}
5121b3a5d02SRobin Holt 	return 1;
5131b3a5d02SRobin Holt }
5141b3a5d02SRobin Holt __setup("reboot=", reboot_setup);
515