2 * linux/kernel/reboot.c
4 * Copyright (C) 2013 Linus Torvalds
7 #include <linux/export.h>
8 #include <linux/kexec.h>
9 #include <linux/kmod.h>
10 #include <linux/kmsg_dump.h>
11 #include <linux/reboot.h>
12 #include <linux/suspend.h>
13 #include <linux/syscalls.h>
14 #include <linux/syscore_ops.h>
15 #include <linux/uaccess.h>
18 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
23 EXPORT_SYMBOL(cad_pid);
26 * If set, this is used for preparing the system to power off.
29 void (*pm_power_off_prepare)(void);
32 * emergency_restart - reboot the system
34 * Without shutting down any hardware or taking any locks
35 * reboot the system. This is called when we know we are in
36 * trouble so this is our best effort to reboot. This is
37 * safe to call in interrupt context.
39 void emergency_restart(void)
41 kmsg_dump(KMSG_DUMP_EMERG);
42 machine_emergency_restart();
44 EXPORT_SYMBOL_GPL(emergency_restart);
46 void kernel_restart_prepare(char *cmd)
48 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
49 system_state = SYSTEM_RESTART;
50 usermodehelper_disable();
55 * register_reboot_notifier - Register function to be called at reboot time
56 * @nb: Info about notifier function to be called
58 * Registers a function with the list of functions
59 * to be called at reboot time.
61 * Currently always returns zero, as blocking_notifier_chain_register()
62 * always returns zero.
64 int register_reboot_notifier(struct notifier_block *nb)
66 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
68 EXPORT_SYMBOL(register_reboot_notifier);
71 * unregister_reboot_notifier - Unregister previously registered reboot notifier
72 * @nb: Hook to be unregistered
74 * Unregisters a previously registered reboot
77 * Returns zero on success, or %-ENOENT on failure.
79 int unregister_reboot_notifier(struct notifier_block *nb)
81 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
83 EXPORT_SYMBOL(unregister_reboot_notifier);
85 static void migrate_to_reboot_cpu(void)
87 /* The boot cpu is always logical cpu 0 */
90 cpu_hotplug_disable();
92 /* Make certain the cpu I'm about to reboot on is online */
94 cpu = cpumask_first(cpu_online_mask);
96 /* Prevent races with other tasks migrating this task */
97 current->flags |= PF_NO_SETAFFINITY;
99 /* Make certain I only run on the appropriate processor */
100 set_cpus_allowed_ptr(current, cpumask_of(cpu));
104 * kernel_restart - reboot the system
105 * @cmd: pointer to buffer containing command to execute for restart
108 * Shutdown everything and perform a clean reboot.
109 * This is not safe to call in interrupt context.
111 void kernel_restart(char *cmd)
113 kernel_restart_prepare(cmd);
114 migrate_to_reboot_cpu();
117 printk(KERN_EMERG "Restarting system.\n");
119 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
120 kmsg_dump(KMSG_DUMP_RESTART);
121 machine_restart(cmd);
123 EXPORT_SYMBOL_GPL(kernel_restart);
125 static void kernel_shutdown_prepare(enum system_states state)
127 blocking_notifier_call_chain(&reboot_notifier_list,
128 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
129 system_state = state;
130 usermodehelper_disable();
134 * kernel_halt - halt the system
136 * Shutdown everything and perform a clean system halt.
138 void kernel_halt(void)
140 kernel_shutdown_prepare(SYSTEM_HALT);
141 migrate_to_reboot_cpu();
143 printk(KERN_EMERG "System halted.\n");
144 kmsg_dump(KMSG_DUMP_HALT);
148 EXPORT_SYMBOL_GPL(kernel_halt);
151 * kernel_power_off - power_off the system
153 * Shutdown everything and perform a clean system power_off.
155 void kernel_power_off(void)
157 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
158 if (pm_power_off_prepare)
159 pm_power_off_prepare();
160 migrate_to_reboot_cpu();
162 printk(KERN_EMERG "Power down.\n");
163 kmsg_dump(KMSG_DUMP_POWEROFF);
166 EXPORT_SYMBOL_GPL(kernel_power_off);
168 static DEFINE_MUTEX(reboot_mutex);
171 * Reboot system call: for obvious reasons only root may call it,
172 * and even root needs to set up some magic numbers in the registers
173 * so that some mistake won't make this reboot the whole machine.
174 * You can also set the meaning of the ctrl-alt-del-key here.
176 * reboot doesn't sync: do that yourself before calling this.
178 SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
181 struct pid_namespace *pid_ns = task_active_pid_ns(current);
185 /* We only trust the superuser with rebooting the system. */
186 if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
189 /* For safety, we require "magic" arguments. */
190 if (magic1 != LINUX_REBOOT_MAGIC1 ||
191 (magic2 != LINUX_REBOOT_MAGIC2 &&
192 magic2 != LINUX_REBOOT_MAGIC2A &&
193 magic2 != LINUX_REBOOT_MAGIC2B &&
194 magic2 != LINUX_REBOOT_MAGIC2C))
198 * If pid namespaces are enabled and the current task is in a child
199 * pid_namespace, the command is handled by reboot_pid_ns() which will
202 ret = reboot_pid_ns(pid_ns, cmd);
206 /* Instead of trying to make the power_off code look like
207 * halt when pm_power_off is not set do it the easy way.
209 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
210 cmd = LINUX_REBOOT_CMD_HALT;
212 mutex_lock(&reboot_mutex);
214 case LINUX_REBOOT_CMD_RESTART:
215 kernel_restart(NULL);
218 case LINUX_REBOOT_CMD_CAD_ON:
222 case LINUX_REBOOT_CMD_CAD_OFF:
226 case LINUX_REBOOT_CMD_HALT:
229 panic("cannot halt");
231 case LINUX_REBOOT_CMD_POWER_OFF:
236 case LINUX_REBOOT_CMD_RESTART2:
237 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
241 buffer[sizeof(buffer) - 1] = '\0';
243 kernel_restart(buffer);
247 case LINUX_REBOOT_CMD_KEXEC:
248 ret = kernel_kexec();
252 #ifdef CONFIG_HIBERNATION
253 case LINUX_REBOOT_CMD_SW_SUSPEND:
262 mutex_unlock(&reboot_mutex);
266 static void deferred_cad(struct work_struct *dummy)
268 kernel_restart(NULL);
272 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
273 * As it's called within an interrupt, it may NOT sync: the only choice
274 * is whether to reboot at once, or just ignore the ctrl-alt-del.
276 void ctrl_alt_del(void)
278 static DECLARE_WORK(cad_work, deferred_cad);
281 schedule_work(&cad_work);
283 kill_cad_pid(SIGINT, 1);
286 char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
288 static int __orderly_poweroff(bool force)
291 static char *envp[] = {
293 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
298 argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
300 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
303 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
304 __func__, poweroff_cmd);
309 printk(KERN_WARNING "Failed to start orderly shutdown: "
310 "forcing the issue\n");
312 * I guess this should try to kick off some daemon to sync and
313 * poweroff asap. Or not even bother syncing if we're doing an
314 * emergency shutdown?
323 static bool poweroff_force;
325 static void poweroff_work_func(struct work_struct *work)
327 __orderly_poweroff(poweroff_force);
330 static DECLARE_WORK(poweroff_work, poweroff_work_func);
333 * orderly_poweroff - Trigger an orderly system poweroff
334 * @force: force poweroff if command execution fails
336 * This may be called from any context to trigger a system shutdown.
337 * If the orderly shutdown fails, it will force an immediate shutdown.
339 int orderly_poweroff(bool force)
341 if (force) /* do not override the pending "true" */
342 poweroff_force = true;
343 schedule_work(&poweroff_work);
346 EXPORT_SYMBOL_GPL(orderly_poweroff);