2 * linux/kernel/reboot.c
4 * Copyright (C) 2013 Linus Torvalds
7 #define pr_fmt(fmt) "reboot: " fmt
9 #include <linux/export.h>
10 #include <linux/kexec.h>
11 #include <linux/kmod.h>
12 #include <linux/kmsg_dump.h>
13 #include <linux/reboot.h>
14 #include <linux/suspend.h>
15 #include <linux/syscalls.h>
16 #include <linux/syscore_ops.h>
17 #include <linux/uaccess.h>
20 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
25 EXPORT_SYMBOL(cad_pid
);
28 * If set, this is used for preparing the system to power off.
31 void (*pm_power_off_prepare
)(void);
34 * emergency_restart - reboot the system
36 * Without shutting down any hardware or taking any locks
37 * reboot the system. This is called when we know we are in
38 * trouble so this is our best effort to reboot. This is
39 * safe to call in interrupt context.
41 void emergency_restart(void)
43 kmsg_dump(KMSG_DUMP_EMERG
);
44 machine_emergency_restart();
46 EXPORT_SYMBOL_GPL(emergency_restart
);
48 void kernel_restart_prepare(char *cmd
)
50 blocking_notifier_call_chain(&reboot_notifier_list
, SYS_RESTART
, cmd
);
51 system_state
= SYSTEM_RESTART
;
52 usermodehelper_disable();
57 * register_reboot_notifier - Register function to be called at reboot time
58 * @nb: Info about notifier function to be called
60 * Registers a function with the list of functions
61 * to be called at reboot time.
63 * Currently always returns zero, as blocking_notifier_chain_register()
64 * always returns zero.
66 int register_reboot_notifier(struct notifier_block
*nb
)
68 return blocking_notifier_chain_register(&reboot_notifier_list
, nb
);
70 EXPORT_SYMBOL(register_reboot_notifier
);
73 * unregister_reboot_notifier - Unregister previously registered reboot notifier
74 * @nb: Hook to be unregistered
76 * Unregisters a previously registered reboot
79 * Returns zero on success, or %-ENOENT on failure.
81 int unregister_reboot_notifier(struct notifier_block
*nb
)
83 return blocking_notifier_chain_unregister(&reboot_notifier_list
, nb
);
85 EXPORT_SYMBOL(unregister_reboot_notifier
);
87 static void migrate_to_reboot_cpu(void)
89 /* The boot cpu is always logical cpu 0 */
92 cpu_hotplug_disable();
94 /* Make certain the cpu I'm about to reboot on is online */
96 cpu
= cpumask_first(cpu_online_mask
);
98 /* Prevent races with other tasks migrating this task */
99 current
->flags
|= PF_NO_SETAFFINITY
;
101 /* Make certain I only run on the appropriate processor */
102 set_cpus_allowed_ptr(current
, cpumask_of(cpu
));
106 * kernel_restart - reboot the system
107 * @cmd: pointer to buffer containing command to execute for restart
110 * Shutdown everything and perform a clean reboot.
111 * This is not safe to call in interrupt context.
113 void kernel_restart(char *cmd
)
115 kernel_restart_prepare(cmd
);
116 migrate_to_reboot_cpu();
119 pr_emerg("Restarting system\n");
121 pr_emerg("Restarting system with command '%s'\n", cmd
);
122 kmsg_dump(KMSG_DUMP_RESTART
);
123 machine_restart(cmd
);
125 EXPORT_SYMBOL_GPL(kernel_restart
);
127 static void kernel_shutdown_prepare(enum system_states state
)
129 blocking_notifier_call_chain(&reboot_notifier_list
,
130 (state
== SYSTEM_HALT
) ? SYS_HALT
: SYS_POWER_OFF
, NULL
);
131 system_state
= state
;
132 usermodehelper_disable();
136 * kernel_halt - halt the system
138 * Shutdown everything and perform a clean system halt.
140 void kernel_halt(void)
142 kernel_shutdown_prepare(SYSTEM_HALT
);
143 migrate_to_reboot_cpu();
145 pr_emerg("System halted\n");
146 kmsg_dump(KMSG_DUMP_HALT
);
149 EXPORT_SYMBOL_GPL(kernel_halt
);
152 * kernel_power_off - power_off the system
154 * Shutdown everything and perform a clean system power_off.
156 void kernel_power_off(void)
158 kernel_shutdown_prepare(SYSTEM_POWER_OFF
);
159 if (pm_power_off_prepare
)
160 pm_power_off_prepare();
161 migrate_to_reboot_cpu();
163 pr_emerg("Power down\n");
164 kmsg_dump(KMSG_DUMP_POWEROFF
);
167 EXPORT_SYMBOL_GPL(kernel_power_off
);
169 static DEFINE_MUTEX(reboot_mutex
);
172 * Reboot system call: for obvious reasons only root may call it,
173 * and even root needs to set up some magic numbers in the registers
174 * so that some mistake won't make this reboot the whole machine.
175 * You can also set the meaning of the ctrl-alt-del-key here.
177 * reboot doesn't sync: do that yourself before calling this.
179 SYSCALL_DEFINE4(reboot
, int, magic1
, int, magic2
, unsigned int, cmd
,
182 struct pid_namespace
*pid_ns
= task_active_pid_ns(current
);
186 /* We only trust the superuser with rebooting the system. */
187 if (!ns_capable(pid_ns
->user_ns
, CAP_SYS_BOOT
))
190 /* For safety, we require "magic" arguments. */
191 if (magic1
!= LINUX_REBOOT_MAGIC1
||
192 (magic2
!= LINUX_REBOOT_MAGIC2
&&
193 magic2
!= LINUX_REBOOT_MAGIC2A
&&
194 magic2
!= LINUX_REBOOT_MAGIC2B
&&
195 magic2
!= LINUX_REBOOT_MAGIC2C
))
199 * If pid namespaces are enabled and the current task is in a child
200 * pid_namespace, the command is handled by reboot_pid_ns() which will
203 ret
= reboot_pid_ns(pid_ns
, cmd
);
207 /* Instead of trying to make the power_off code look like
208 * halt when pm_power_off is not set do it the easy way.
210 if ((cmd
== LINUX_REBOOT_CMD_POWER_OFF
) && !pm_power_off
)
211 cmd
= LINUX_REBOOT_CMD_HALT
;
213 mutex_lock(&reboot_mutex
);
215 case LINUX_REBOOT_CMD_RESTART
:
216 kernel_restart(NULL
);
219 case LINUX_REBOOT_CMD_CAD_ON
:
223 case LINUX_REBOOT_CMD_CAD_OFF
:
227 case LINUX_REBOOT_CMD_HALT
:
230 panic("cannot halt");
232 case LINUX_REBOOT_CMD_POWER_OFF
:
237 case LINUX_REBOOT_CMD_RESTART2
:
238 ret
= strncpy_from_user(&buffer
[0], arg
, sizeof(buffer
) - 1);
243 buffer
[sizeof(buffer
) - 1] = '\0';
245 kernel_restart(buffer
);
249 case LINUX_REBOOT_CMD_KEXEC
:
250 ret
= kernel_kexec();
254 #ifdef CONFIG_HIBERNATION
255 case LINUX_REBOOT_CMD_SW_SUSPEND
:
264 mutex_unlock(&reboot_mutex
);
268 static void deferred_cad(struct work_struct
*dummy
)
270 kernel_restart(NULL
);
274 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
275 * As it's called within an interrupt, it may NOT sync: the only choice
276 * is whether to reboot at once, or just ignore the ctrl-alt-del.
278 void ctrl_alt_del(void)
280 static DECLARE_WORK(cad_work
, deferred_cad
);
283 schedule_work(&cad_work
);
285 kill_cad_pid(SIGINT
, 1);
288 char poweroff_cmd
[POWEROFF_CMD_PATH_LEN
] = "/sbin/poweroff";
290 static int __orderly_poweroff(bool force
)
293 static char *envp
[] = {
295 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
300 argv
= argv_split(GFP_KERNEL
, poweroff_cmd
, NULL
);
302 ret
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
309 pr_warn("Failed to start orderly shutdown: forcing the issue\n");
311 * I guess this should try to kick off some daemon to sync and
312 * poweroff asap. Or not even bother syncing if we're doing an
313 * emergency shutdown?
322 static bool poweroff_force
;
324 static void poweroff_work_func(struct work_struct
*work
)
326 __orderly_poweroff(poweroff_force
);
329 static DECLARE_WORK(poweroff_work
, poweroff_work_func
);
332 * orderly_poweroff - Trigger an orderly system poweroff
333 * @force: force poweroff if command execution fails
335 * This may be called from any context to trigger a system shutdown.
336 * If the orderly shutdown fails, it will force an immediate shutdown.
338 int orderly_poweroff(bool force
)
340 if (force
) /* do not override the pending "true" */
341 poweroff_force
= true;
342 schedule_work(&poweroff_work
);
345 EXPORT_SYMBOL_GPL(orderly_poweroff
);