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