diff options
author | Robin Holt <holt@sgi.com> | 2013-07-08 19:01:32 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-07-09 13:33:29 -0400 |
commit | 15d94b82565ebfb0cf27830b96e6cf5ed2d12a9a (patch) | |
tree | 21aad9ff42bf328777956e835ef7962bc307da9a | |
parent | 0efbee70890c992f31a7b294ac654ff6c62d51c5 (diff) |
reboot: move shutdown/reboot related functions to kernel/reboot.c
This patch is preparatory. It moves reboot related syscall, etc
functions from kernel/sys.c to kernel/reboot.c.
Signed-off-by: Robin Holt <holt@sgi.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Russ Anderson <rja@sgi.com>
Cc: Robin Holt <holt@sgi.com>
Cc: Russell King <rmk+kernel@arm.linux.org.uk>
Cc: Guan Xuetao <gxt@mprc.pku.edu.cn>
Cc: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | kernel/Makefile | 2 | ||||
-rw-r--r-- | kernel/reboot.c | 346 | ||||
-rw-r--r-- | kernel/sys.c | 331 |
3 files changed, 347 insertions, 332 deletions
diff --git a/kernel/Makefile b/kernel/Makefile index 271fd3119af9..470839d1a30e 100644 --- a/kernel/Makefile +++ b/kernel/Makefile | |||
@@ -9,7 +9,7 @@ obj-y = fork.o exec_domain.o panic.o printk.o \ | |||
9 | rcupdate.o extable.o params.o posix-timers.o \ | 9 | rcupdate.o extable.o params.o posix-timers.o \ |
10 | kthread.o wait.o sys_ni.o posix-cpu-timers.o mutex.o \ | 10 | kthread.o wait.o sys_ni.o posix-cpu-timers.o mutex.o \ |
11 | hrtimer.o rwsem.o nsproxy.o srcu.o semaphore.o \ | 11 | hrtimer.o rwsem.o nsproxy.o srcu.o semaphore.o \ |
12 | notifier.o ksysfs.o cred.o \ | 12 | notifier.o ksysfs.o cred.o reboot.o \ |
13 | async.o range.o groups.o lglock.o smpboot.o | 13 | async.o range.o groups.o lglock.o smpboot.o |
14 | 14 | ||
15 | ifdef CONFIG_FUNCTION_TRACER | 15 | ifdef CONFIG_FUNCTION_TRACER |
diff --git a/kernel/reboot.c b/kernel/reboot.c new file mode 100644 index 000000000000..37d2636a65c2 --- /dev/null +++ b/kernel/reboot.c | |||
@@ -0,0 +1,346 @@ | |||
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); | ||
diff --git a/kernel/sys.c b/kernel/sys.c index b882440bd0c0..771129b299f8 100644 --- a/kernel/sys.c +++ b/kernel/sys.c | |||
@@ -116,20 +116,6 @@ EXPORT_SYMBOL(fs_overflowuid); | |||
116 | EXPORT_SYMBOL(fs_overflowgid); | 116 | EXPORT_SYMBOL(fs_overflowgid); |
117 | 117 | ||
118 | /* | 118 | /* |
119 | * this indicates whether you can reboot with ctrl-alt-del: the default is yes | ||
120 | */ | ||
121 | |||
122 | int C_A_D = 1; | ||
123 | struct pid *cad_pid; | ||
124 | EXPORT_SYMBOL(cad_pid); | ||
125 | |||
126 | /* | ||
127 | * If set, this is used for preparing the system to power off. | ||
128 | */ | ||
129 | |||
130 | void (*pm_power_off_prepare)(void); | ||
131 | |||
132 | /* | ||
133 | * Returns true if current's euid is same as p's uid or euid, | 119 | * Returns true if current's euid is same as p's uid or euid, |
134 | * or has CAP_SYS_NICE to p's user_ns. | 120 | * or has CAP_SYS_NICE to p's user_ns. |
135 | * | 121 | * |
@@ -308,261 +294,6 @@ out_unlock: | |||
308 | return retval; | 294 | return retval; |
309 | } | 295 | } |
310 | 296 | ||
311 | /** | ||
312 | * emergency_restart - reboot the system | ||
313 | * | ||
314 | * Without shutting down any hardware or taking any locks | ||
315 | * reboot the system. This is called when we know we are in | ||
316 | * trouble so this is our best effort to reboot. This is | ||
317 | * safe to call in interrupt context. | ||
318 | */ | ||
319 | void emergency_restart(void) | ||
320 | { | ||
321 | kmsg_dump(KMSG_DUMP_EMERG); | ||
322 | machine_emergency_restart(); | ||
323 | } | ||
324 | EXPORT_SYMBOL_GPL(emergency_restart); | ||
325 | |||
326 | void kernel_restart_prepare(char *cmd) | ||
327 | { | ||
328 | blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); | ||
329 | system_state = SYSTEM_RESTART; | ||
330 | usermodehelper_disable(); | ||
331 | device_shutdown(); | ||
332 | } | ||
333 | |||
334 | /** | ||
335 | * register_reboot_notifier - Register function to be called at reboot time | ||
336 | * @nb: Info about notifier function to be called | ||
337 | * | ||
338 | * Registers a function with the list of functions | ||
339 | * to be called at reboot time. | ||
340 | * | ||
341 | * Currently always returns zero, as blocking_notifier_chain_register() | ||
342 | * always returns zero. | ||
343 | */ | ||
344 | int register_reboot_notifier(struct notifier_block *nb) | ||
345 | { | ||
346 | return blocking_notifier_chain_register(&reboot_notifier_list, nb); | ||
347 | } | ||
348 | EXPORT_SYMBOL(register_reboot_notifier); | ||
349 | |||
350 | /** | ||
351 | * unregister_reboot_notifier - Unregister previously registered reboot notifier | ||
352 | * @nb: Hook to be unregistered | ||
353 | * | ||
354 | * Unregisters a previously registered reboot | ||
355 | * notifier function. | ||
356 | * | ||
357 | * Returns zero on success, or %-ENOENT on failure. | ||
358 | */ | ||
359 | int unregister_reboot_notifier(struct notifier_block *nb) | ||
360 | { | ||
361 | return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); | ||
362 | } | ||
363 | EXPORT_SYMBOL(unregister_reboot_notifier); | ||
364 | |||
365 | static void migrate_to_reboot_cpu(void) | ||
366 | { | ||
367 | /* The boot cpu is always logical cpu 0 */ | ||
368 | int cpu = 0; | ||
369 | |||
370 | cpu_hotplug_disable(); | ||
371 | |||
372 | /* Make certain the cpu I'm about to reboot on is online */ | ||
373 | if (!cpu_online(cpu)) | ||
374 | cpu = cpumask_first(cpu_online_mask); | ||
375 | |||
376 | /* Prevent races with other tasks migrating this task */ | ||
377 | current->flags |= PF_NO_SETAFFINITY; | ||
378 | |||
379 | /* Make certain I only run on the appropriate processor */ | ||
380 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); | ||
381 | } | ||
382 | |||
383 | /** | ||
384 | * kernel_restart - reboot the system | ||
385 | * @cmd: pointer to buffer containing command to execute for restart | ||
386 | * or %NULL | ||
387 | * | ||
388 | * Shutdown everything and perform a clean reboot. | ||
389 | * This is not safe to call in interrupt context. | ||
390 | */ | ||
391 | void kernel_restart(char *cmd) | ||
392 | { | ||
393 | kernel_restart_prepare(cmd); | ||
394 | migrate_to_reboot_cpu(); | ||
395 | syscore_shutdown(); | ||
396 | if (!cmd) | ||
397 | printk(KERN_EMERG "Restarting system.\n"); | ||
398 | else | ||
399 | printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd); | ||
400 | kmsg_dump(KMSG_DUMP_RESTART); | ||
401 | machine_restart(cmd); | ||
402 | } | ||
403 | EXPORT_SYMBOL_GPL(kernel_restart); | ||
404 | |||
405 | static void kernel_shutdown_prepare(enum system_states state) | ||
406 | { | ||
407 | blocking_notifier_call_chain(&reboot_notifier_list, | ||
408 | (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL); | ||
409 | system_state = state; | ||
410 | usermodehelper_disable(); | ||
411 | device_shutdown(); | ||
412 | } | ||
413 | /** | ||
414 | * kernel_halt - halt the system | ||
415 | * | ||
416 | * Shutdown everything and perform a clean system halt. | ||
417 | */ | ||
418 | void kernel_halt(void) | ||
419 | { | ||
420 | kernel_shutdown_prepare(SYSTEM_HALT); | ||
421 | migrate_to_reboot_cpu(); | ||
422 | syscore_shutdown(); | ||
423 | printk(KERN_EMERG "System halted.\n"); | ||
424 | kmsg_dump(KMSG_DUMP_HALT); | ||
425 | machine_halt(); | ||
426 | } | ||
427 | |||
428 | EXPORT_SYMBOL_GPL(kernel_halt); | ||
429 | |||
430 | /** | ||
431 | * kernel_power_off - power_off the system | ||
432 | * | ||
433 | * Shutdown everything and perform a clean system power_off. | ||
434 | */ | ||
435 | void kernel_power_off(void) | ||
436 | { | ||
437 | kernel_shutdown_prepare(SYSTEM_POWER_OFF); | ||
438 | if (pm_power_off_prepare) | ||
439 | pm_power_off_prepare(); | ||
440 | migrate_to_reboot_cpu(); | ||
441 | syscore_shutdown(); | ||
442 | printk(KERN_EMERG "Power down.\n"); | ||
443 | kmsg_dump(KMSG_DUMP_POWEROFF); | ||
444 | machine_power_off(); | ||
445 | } | ||
446 | EXPORT_SYMBOL_GPL(kernel_power_off); | ||
447 | |||
448 | static DEFINE_MUTEX(reboot_mutex); | ||
449 | |||
450 | /* | ||
451 | * Reboot system call: for obvious reasons only root may call it, | ||
452 | * and even root needs to set up some magic numbers in the registers | ||
453 | * so that some mistake won't make this reboot the whole machine. | ||
454 | * You can also set the meaning of the ctrl-alt-del-key here. | ||
455 | * | ||
456 | * reboot doesn't sync: do that yourself before calling this. | ||
457 | */ | ||
458 | SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, | ||
459 | void __user *, arg) | ||
460 | { | ||
461 | struct pid_namespace *pid_ns = task_active_pid_ns(current); | ||
462 | char buffer[256]; | ||
463 | int ret = 0; | ||
464 | |||
465 | /* We only trust the superuser with rebooting the system. */ | ||
466 | if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT)) | ||
467 | return -EPERM; | ||
468 | |||
469 | /* For safety, we require "magic" arguments. */ | ||
470 | if (magic1 != LINUX_REBOOT_MAGIC1 || | ||
471 | (magic2 != LINUX_REBOOT_MAGIC2 && | ||
472 | magic2 != LINUX_REBOOT_MAGIC2A && | ||
473 | magic2 != LINUX_REBOOT_MAGIC2B && | ||
474 | magic2 != LINUX_REBOOT_MAGIC2C)) | ||
475 | return -EINVAL; | ||
476 | |||
477 | /* | ||
478 | * If pid namespaces are enabled and the current task is in a child | ||
479 | * pid_namespace, the command is handled by reboot_pid_ns() which will | ||
480 | * call do_exit(). | ||
481 | */ | ||
482 | ret = reboot_pid_ns(pid_ns, cmd); | ||
483 | if (ret) | ||
484 | return ret; | ||
485 | |||
486 | /* Instead of trying to make the power_off code look like | ||
487 | * halt when pm_power_off is not set do it the easy way. | ||
488 | */ | ||
489 | if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) | ||
490 | cmd = LINUX_REBOOT_CMD_HALT; | ||
491 | |||
492 | mutex_lock(&reboot_mutex); | ||
493 | switch (cmd) { | ||
494 | case LINUX_REBOOT_CMD_RESTART: | ||
495 | kernel_restart(NULL); | ||
496 | break; | ||
497 | |||
498 | case LINUX_REBOOT_CMD_CAD_ON: | ||
499 | C_A_D = 1; | ||
500 | break; | ||
501 | |||
502 | case LINUX_REBOOT_CMD_CAD_OFF: | ||
503 | C_A_D = 0; | ||
504 | break; | ||
505 | |||
506 | case LINUX_REBOOT_CMD_HALT: | ||
507 | kernel_halt(); | ||
508 | do_exit(0); | ||
509 | panic("cannot halt.\n"); | ||
510 | |||
511 | case LINUX_REBOOT_CMD_POWER_OFF: | ||
512 | kernel_power_off(); | ||
513 | do_exit(0); | ||
514 | break; | ||
515 | |||
516 | case LINUX_REBOOT_CMD_RESTART2: | ||
517 | if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) { | ||
518 | ret = -EFAULT; | ||
519 | break; | ||
520 | } | ||
521 | buffer[sizeof(buffer) - 1] = '\0'; | ||
522 | |||
523 | kernel_restart(buffer); | ||
524 | break; | ||
525 | |||
526 | #ifdef CONFIG_KEXEC | ||
527 | case LINUX_REBOOT_CMD_KEXEC: | ||
528 | ret = kernel_kexec(); | ||
529 | break; | ||
530 | #endif | ||
531 | |||
532 | #ifdef CONFIG_HIBERNATION | ||
533 | case LINUX_REBOOT_CMD_SW_SUSPEND: | ||
534 | ret = hibernate(); | ||
535 | break; | ||
536 | #endif | ||
537 | |||
538 | default: | ||
539 | ret = -EINVAL; | ||
540 | break; | ||
541 | } | ||
542 | mutex_unlock(&reboot_mutex); | ||
543 | return ret; | ||
544 | } | ||
545 | |||
546 | static void deferred_cad(struct work_struct *dummy) | ||
547 | { | ||
548 | kernel_restart(NULL); | ||
549 | } | ||
550 | |||
551 | /* | ||
552 | * This function gets called by ctrl-alt-del - ie the keyboard interrupt. | ||
553 | * As it's called within an interrupt, it may NOT sync: the only choice | ||
554 | * is whether to reboot at once, or just ignore the ctrl-alt-del. | ||
555 | */ | ||
556 | void ctrl_alt_del(void) | ||
557 | { | ||
558 | static DECLARE_WORK(cad_work, deferred_cad); | ||
559 | |||
560 | if (C_A_D) | ||
561 | schedule_work(&cad_work); | ||
562 | else | ||
563 | kill_cad_pid(SIGINT, 1); | ||
564 | } | ||
565 | |||
566 | /* | 297 | /* |
567 | * Unprivileged users may change the real gid to the effective gid | 298 | * Unprivileged users may change the real gid to the effective gid |
568 | * or vice versa. (BSD-style) | 299 | * or vice versa. (BSD-style) |
@@ -2287,68 +2018,6 @@ SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep, | |||
2287 | return err ? -EFAULT : 0; | 2018 | return err ? -EFAULT : 0; |
2288 | } | 2019 | } |
2289 | 2020 | ||
2290 | char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; | ||
2291 | |||
2292 | static int __orderly_poweroff(bool force) | ||
2293 | { | ||
2294 | char **argv; | ||
2295 | static char *envp[] = { | ||
2296 | "HOME=/", | ||
2297 | "PATH=/sbin:/bin:/usr/sbin:/usr/bin", | ||
2298 | NULL | ||
2299 | }; | ||
2300 | int ret; | ||
2301 | |||
2302 | argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL); | ||
2303 | if (argv) { | ||
2304 | ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); | ||
2305 | argv_free(argv); | ||
2306 | } else { | ||
2307 | printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n", | ||
2308 | __func__, poweroff_cmd); | ||
2309 | ret = -ENOMEM; | ||
2310 | } | ||
2311 | |||
2312 | if (ret && force) { | ||
2313 | printk(KERN_WARNING "Failed to start orderly shutdown: " | ||
2314 | "forcing the issue\n"); | ||
2315 | /* | ||
2316 | * I guess this should try to kick off some daemon to sync and | ||
2317 | * poweroff asap. Or not even bother syncing if we're doing an | ||
2318 | * emergency shutdown? | ||
2319 | */ | ||
2320 | emergency_sync(); | ||
2321 | kernel_power_off(); | ||
2322 | } | ||
2323 | |||
2324 | return ret; | ||
2325 | } | ||
2326 | |||
2327 | static bool poweroff_force; | ||
2328 | |||
2329 | static void poweroff_work_func(struct work_struct *work) | ||
2330 | { | ||
2331 | __orderly_poweroff(poweroff_force); | ||
2332 | } | ||
2333 | |||
2334 | static DECLARE_WORK(poweroff_work, poweroff_work_func); | ||
2335 | |||
2336 | /** | ||
2337 | * orderly_poweroff - Trigger an orderly system poweroff | ||
2338 | * @force: force poweroff if command execution fails | ||
2339 | * | ||
2340 | * This may be called from any context to trigger a system shutdown. | ||
2341 | * If the orderly shutdown fails, it will force an immediate shutdown. | ||
2342 | */ | ||
2343 | int orderly_poweroff(bool force) | ||
2344 | { | ||
2345 | if (force) /* do not override the pending "true" */ | ||
2346 | poweroff_force = true; | ||
2347 | schedule_work(&poweroff_work); | ||
2348 | return 0; | ||
2349 | } | ||
2350 | EXPORT_SYMBOL_GPL(orderly_poweroff); | ||
2351 | |||
2352 | /** | 2021 | /** |
2353 | * do_sysinfo - fill in sysinfo struct | 2022 | * do_sysinfo - fill in sysinfo struct |
2354 | * @info: pointer to buffer to fill | 2023 | * @info: pointer to buffer to fill |