]>
Commit | Line | Data |
---|---|---|
15d94b82 RH |
1 | /* |
2 | * linux/kernel/reboot.c | |
3 | * | |
4 | * Copyright (C) 2013 Linus Torvalds | |
5 | */ | |
6 | ||
972ee83d RH |
7 | #define pr_fmt(fmt) "reboot: " fmt |
8 | ||
15d94b82 RH |
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> | |
18 | ||
19 | /* | |
20 | * this indicates whether you can reboot with ctrl-alt-del: the default is yes | |
21 | */ | |
22 | ||
23 | int C_A_D = 1; | |
24 | struct pid *cad_pid; | |
25 | EXPORT_SYMBOL(cad_pid); | |
26 | ||
27 | /* | |
28 | * If set, this is used for preparing the system to power off. | |
29 | */ | |
30 | ||
31 | void (*pm_power_off_prepare)(void); | |
32 | ||
33 | /** | |
34 | * emergency_restart - reboot the system | |
35 | * | |
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. | |
40 | */ | |
41 | void emergency_restart(void) | |
42 | { | |
43 | kmsg_dump(KMSG_DUMP_EMERG); | |
44 | machine_emergency_restart(); | |
45 | } | |
46 | EXPORT_SYMBOL_GPL(emergency_restart); | |
47 | ||
48 | void kernel_restart_prepare(char *cmd) | |
49 | { | |
50 | blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); | |
51 | system_state = SYSTEM_RESTART; | |
52 | usermodehelper_disable(); | |
53 | device_shutdown(); | |
54 | } | |
55 | ||
56 | /** | |
57 | * register_reboot_notifier - Register function to be called at reboot time | |
58 | * @nb: Info about notifier function to be called | |
59 | * | |
60 | * Registers a function with the list of functions | |
61 | * to be called at reboot time. | |
62 | * | |
63 | * Currently always returns zero, as blocking_notifier_chain_register() | |
64 | * always returns zero. | |
65 | */ | |
66 | int register_reboot_notifier(struct notifier_block *nb) | |
67 | { | |
68 | return blocking_notifier_chain_register(&reboot_notifier_list, nb); | |
69 | } | |
70 | EXPORT_SYMBOL(register_reboot_notifier); | |
71 | ||
72 | /** | |
73 | * unregister_reboot_notifier - Unregister previously registered reboot notifier | |
74 | * @nb: Hook to be unregistered | |
75 | * | |
76 | * Unregisters a previously registered reboot | |
77 | * notifier function. | |
78 | * | |
79 | * Returns zero on success, or %-ENOENT on failure. | |
80 | */ | |
81 | int unregister_reboot_notifier(struct notifier_block *nb) | |
82 | { | |
83 | return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); | |
84 | } | |
85 | EXPORT_SYMBOL(unregister_reboot_notifier); | |
86 | ||
87 | static void migrate_to_reboot_cpu(void) | |
88 | { | |
89 | /* The boot cpu is always logical cpu 0 */ | |
90 | int cpu = 0; | |
91 | ||
92 | cpu_hotplug_disable(); | |
93 | ||
94 | /* Make certain the cpu I'm about to reboot on is online */ | |
95 | if (!cpu_online(cpu)) | |
96 | cpu = cpumask_first(cpu_online_mask); | |
97 | ||
98 | /* Prevent races with other tasks migrating this task */ | |
99 | current->flags |= PF_NO_SETAFFINITY; | |
100 | ||
101 | /* Make certain I only run on the appropriate processor */ | |
102 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); | |
103 | } | |
104 | ||
105 | /** | |
106 | * kernel_restart - reboot the system | |
107 | * @cmd: pointer to buffer containing command to execute for restart | |
108 | * or %NULL | |
109 | * | |
110 | * Shutdown everything and perform a clean reboot. | |
111 | * This is not safe to call in interrupt context. | |
112 | */ | |
113 | void kernel_restart(char *cmd) | |
114 | { | |
115 | kernel_restart_prepare(cmd); | |
116 | migrate_to_reboot_cpu(); | |
117 | syscore_shutdown(); | |
118 | if (!cmd) | |
972ee83d | 119 | pr_emerg("Restarting system\n"); |
15d94b82 | 120 | else |
972ee83d | 121 | pr_emerg("Restarting system with command '%s'\n", cmd); |
15d94b82 RH |
122 | kmsg_dump(KMSG_DUMP_RESTART); |
123 | machine_restart(cmd); | |
124 | } | |
125 | EXPORT_SYMBOL_GPL(kernel_restart); | |
126 | ||
127 | static void kernel_shutdown_prepare(enum system_states state) | |
128 | { | |
129 | blocking_notifier_call_chain(&reboot_notifier_list, | |
972ee83d | 130 | (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL); |
15d94b82 RH |
131 | system_state = state; |
132 | usermodehelper_disable(); | |
133 | device_shutdown(); | |
134 | } | |
135 | /** | |
136 | * kernel_halt - halt the system | |
137 | * | |
138 | * Shutdown everything and perform a clean system halt. | |
139 | */ | |
140 | void kernel_halt(void) | |
141 | { | |
142 | kernel_shutdown_prepare(SYSTEM_HALT); | |
143 | migrate_to_reboot_cpu(); | |
144 | syscore_shutdown(); | |
972ee83d | 145 | pr_emerg("System halted\n"); |
15d94b82 RH |
146 | kmsg_dump(KMSG_DUMP_HALT); |
147 | machine_halt(); | |
148 | } | |
15d94b82 RH |
149 | EXPORT_SYMBOL_GPL(kernel_halt); |
150 | ||
151 | /** | |
152 | * kernel_power_off - power_off the system | |
153 | * | |
154 | * Shutdown everything and perform a clean system power_off. | |
155 | */ | |
156 | void kernel_power_off(void) | |
157 | { | |
158 | kernel_shutdown_prepare(SYSTEM_POWER_OFF); | |
159 | if (pm_power_off_prepare) | |
160 | pm_power_off_prepare(); | |
161 | migrate_to_reboot_cpu(); | |
162 | syscore_shutdown(); | |
972ee83d | 163 | pr_emerg("Power down\n"); |
15d94b82 RH |
164 | kmsg_dump(KMSG_DUMP_POWEROFF); |
165 | machine_power_off(); | |
166 | } | |
167 | EXPORT_SYMBOL_GPL(kernel_power_off); | |
168 | ||
169 | static DEFINE_MUTEX(reboot_mutex); | |
170 | ||
171 | /* | |
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. | |
176 | * | |
177 | * reboot doesn't sync: do that yourself before calling this. | |
178 | */ | |
179 | SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, | |
180 | void __user *, arg) | |
181 | { | |
182 | struct pid_namespace *pid_ns = task_active_pid_ns(current); | |
183 | char buffer[256]; | |
184 | int ret = 0; | |
185 | ||
186 | /* We only trust the superuser with rebooting the system. */ | |
187 | if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT)) | |
188 | return -EPERM; | |
189 | ||
190 | /* For safety, we require "magic" arguments. */ | |
191 | if (magic1 != LINUX_REBOOT_MAGIC1 || | |
972ee83d RH |
192 | (magic2 != LINUX_REBOOT_MAGIC2 && |
193 | magic2 != LINUX_REBOOT_MAGIC2A && | |
15d94b82 | 194 | magic2 != LINUX_REBOOT_MAGIC2B && |
972ee83d | 195 | magic2 != LINUX_REBOOT_MAGIC2C)) |
15d94b82 RH |
196 | return -EINVAL; |
197 | ||
198 | /* | |
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 | |
201 | * call do_exit(). | |
202 | */ | |
203 | ret = reboot_pid_ns(pid_ns, cmd); | |
204 | if (ret) | |
205 | return ret; | |
206 | ||
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. | |
209 | */ | |
210 | if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) | |
211 | cmd = LINUX_REBOOT_CMD_HALT; | |
212 | ||
213 | mutex_lock(&reboot_mutex); | |
214 | switch (cmd) { | |
215 | case LINUX_REBOOT_CMD_RESTART: | |
216 | kernel_restart(NULL); | |
217 | break; | |
218 | ||
219 | case LINUX_REBOOT_CMD_CAD_ON: | |
220 | C_A_D = 1; | |
221 | break; | |
222 | ||
223 | case LINUX_REBOOT_CMD_CAD_OFF: | |
224 | C_A_D = 0; | |
225 | break; | |
226 | ||
227 | case LINUX_REBOOT_CMD_HALT: | |
228 | kernel_halt(); | |
229 | do_exit(0); | |
230 | panic("cannot halt"); | |
231 | ||
232 | case LINUX_REBOOT_CMD_POWER_OFF: | |
233 | kernel_power_off(); | |
234 | do_exit(0); | |
235 | break; | |
236 | ||
237 | case LINUX_REBOOT_CMD_RESTART2: | |
972ee83d RH |
238 | ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1); |
239 | if (ret < 0) { | |
15d94b82 RH |
240 | ret = -EFAULT; |
241 | break; | |
242 | } | |
243 | buffer[sizeof(buffer) - 1] = '\0'; | |
244 | ||
245 | kernel_restart(buffer); | |
246 | break; | |
247 | ||
248 | #ifdef CONFIG_KEXEC | |
249 | case LINUX_REBOOT_CMD_KEXEC: | |
250 | ret = kernel_kexec(); | |
251 | break; | |
252 | #endif | |
253 | ||
254 | #ifdef CONFIG_HIBERNATION | |
255 | case LINUX_REBOOT_CMD_SW_SUSPEND: | |
256 | ret = hibernate(); | |
257 | break; | |
258 | #endif | |
259 | ||
260 | default: | |
261 | ret = -EINVAL; | |
262 | break; | |
263 | } | |
264 | mutex_unlock(&reboot_mutex); | |
265 | return ret; | |
266 | } | |
267 | ||
268 | static void deferred_cad(struct work_struct *dummy) | |
269 | { | |
270 | kernel_restart(NULL); | |
271 | } | |
272 | ||
273 | /* | |
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. | |
277 | */ | |
278 | void ctrl_alt_del(void) | |
279 | { | |
280 | static DECLARE_WORK(cad_work, deferred_cad); | |
281 | ||
282 | if (C_A_D) | |
283 | schedule_work(&cad_work); | |
284 | else | |
285 | kill_cad_pid(SIGINT, 1); | |
286 | } | |
287 | ||
288 | char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; | |
289 | ||
290 | static int __orderly_poweroff(bool force) | |
291 | { | |
292 | char **argv; | |
293 | static char *envp[] = { | |
294 | "HOME=/", | |
295 | "PATH=/sbin:/bin:/usr/sbin:/usr/bin", | |
296 | NULL | |
297 | }; | |
298 | int ret; | |
299 | ||
300 | argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL); | |
301 | if (argv) { | |
302 | ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); | |
303 | argv_free(argv); | |
304 | } else { | |
15d94b82 RH |
305 | ret = -ENOMEM; |
306 | } | |
307 | ||
308 | if (ret && force) { | |
972ee83d | 309 | pr_warn("Failed to start orderly shutdown: forcing the issue\n"); |
15d94b82 RH |
310 | /* |
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? | |
314 | */ | |
315 | emergency_sync(); | |
316 | kernel_power_off(); | |
317 | } | |
318 | ||
319 | return ret; | |
320 | } | |
321 | ||
322 | static bool poweroff_force; | |
323 | ||
324 | static void poweroff_work_func(struct work_struct *work) | |
325 | { | |
326 | __orderly_poweroff(poweroff_force); | |
327 | } | |
328 | ||
329 | static DECLARE_WORK(poweroff_work, poweroff_work_func); | |
330 | ||
331 | /** | |
332 | * orderly_poweroff - Trigger an orderly system poweroff | |
333 | * @force: force poweroff if command execution fails | |
334 | * | |
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. | |
337 | */ | |
338 | int orderly_poweroff(bool force) | |
339 | { | |
340 | if (force) /* do not override the pending "true" */ | |
341 | poweroff_force = true; | |
342 | schedule_work(&poweroff_work); | |
343 | return 0; | |
344 | } | |
345 | EXPORT_SYMBOL_GPL(orderly_poweroff); |