Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
15d94b82 RH |
2 | /* |
3 | * linux/kernel/reboot.c | |
4 | * | |
5 | * Copyright (C) 2013 Linus Torvalds | |
6 | */ | |
7 | ||
972ee83d RH |
8 | #define pr_fmt(fmt) "reboot: " fmt |
9 | ||
dfa19b11 | 10 | #include <linux/atomic.h> |
1b3a5d02 | 11 | #include <linux/ctype.h> |
15d94b82 RH |
12 | #include <linux/export.h> |
13 | #include <linux/kexec.h> | |
14 | #include <linux/kmod.h> | |
15 | #include <linux/kmsg_dump.h> | |
16 | #include <linux/reboot.h> | |
17 | #include <linux/suspend.h> | |
18 | #include <linux/syscalls.h> | |
19 | #include <linux/syscore_ops.h> | |
20 | #include <linux/uaccess.h> | |
21 | ||
22 | /* | |
23 | * this indicates whether you can reboot with ctrl-alt-del: the default is yes | |
24 | */ | |
25 | ||
06d17766 | 26 | static int C_A_D = 1; |
15d94b82 RH |
27 | struct pid *cad_pid; |
28 | EXPORT_SYMBOL(cad_pid); | |
29 | ||
fb37409a | 30 | #if defined(CONFIG_ARM) |
1b3a5d02 RH |
31 | #define DEFAULT_REBOOT_MODE = REBOOT_HARD |
32 | #else | |
33 | #define DEFAULT_REBOOT_MODE | |
34 | #endif | |
35 | enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE; | |
d46b3f5b | 36 | EXPORT_SYMBOL_GPL(reboot_mode); |
b287a25a | 37 | enum reboot_mode panic_reboot_mode = REBOOT_UNDEFINED; |
1b3a5d02 | 38 | |
e2f0b88e CL |
39 | /* |
40 | * This variable is used privately to keep track of whether or not | |
41 | * reboot_type is still set to its default value (i.e., reboot= hasn't | |
42 | * been set on the command line). This is needed so that we can | |
43 | * suppress DMI scanning for reboot quirks. Without it, it's | |
44 | * impossible to override a faulty reboot quirk without recompiling. | |
45 | */ | |
46 | int reboot_default = 1; | |
1b3a5d02 RH |
47 | int reboot_cpu; |
48 | enum reboot_type reboot_type = BOOT_ACPI; | |
49 | int reboot_force; | |
50 | ||
232edc2f DO |
51 | struct sys_off_handler { |
52 | struct notifier_block nb; | |
53 | int (*sys_off_cb)(struct sys_off_data *data); | |
54 | void *cb_data; | |
55 | enum sys_off_mode mode; | |
56 | bool blocking; | |
57 | void *list; | |
58 | }; | |
59 | ||
15d94b82 | 60 | /* |
5d34b41a DO |
61 | * Temporary stub that prevents linkage failure while we're in process |
62 | * of removing all uses of legacy pm_power_off() around the kernel. | |
15d94b82 | 63 | */ |
5d34b41a | 64 | void __weak (*pm_power_off)(void); |
15d94b82 RH |
65 | |
66 | /** | |
67 | * emergency_restart - reboot the system | |
68 | * | |
69 | * Without shutting down any hardware or taking any locks | |
70 | * reboot the system. This is called when we know we are in | |
71 | * trouble so this is our best effort to reboot. This is | |
72 | * safe to call in interrupt context. | |
73 | */ | |
74 | void emergency_restart(void) | |
75 | { | |
76 | kmsg_dump(KMSG_DUMP_EMERG); | |
77 | machine_emergency_restart(); | |
78 | } | |
79 | EXPORT_SYMBOL_GPL(emergency_restart); | |
80 | ||
81 | void kernel_restart_prepare(char *cmd) | |
82 | { | |
83 | blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); | |
84 | system_state = SYSTEM_RESTART; | |
85 | usermodehelper_disable(); | |
86 | device_shutdown(); | |
87 | } | |
88 | ||
89 | /** | |
90 | * register_reboot_notifier - Register function to be called at reboot time | |
91 | * @nb: Info about notifier function to be called | |
92 | * | |
93 | * Registers a function with the list of functions | |
94 | * to be called at reboot time. | |
95 | * | |
96 | * Currently always returns zero, as blocking_notifier_chain_register() | |
97 | * always returns zero. | |
98 | */ | |
99 | int register_reboot_notifier(struct notifier_block *nb) | |
100 | { | |
101 | return blocking_notifier_chain_register(&reboot_notifier_list, nb); | |
102 | } | |
103 | EXPORT_SYMBOL(register_reboot_notifier); | |
104 | ||
105 | /** | |
106 | * unregister_reboot_notifier - Unregister previously registered reboot notifier | |
107 | * @nb: Hook to be unregistered | |
108 | * | |
109 | * Unregisters a previously registered reboot | |
110 | * notifier function. | |
111 | * | |
112 | * Returns zero on success, or %-ENOENT on failure. | |
113 | */ | |
114 | int unregister_reboot_notifier(struct notifier_block *nb) | |
115 | { | |
116 | return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); | |
117 | } | |
118 | EXPORT_SYMBOL(unregister_reboot_notifier); | |
119 | ||
2d8364ba AS |
120 | static void devm_unregister_reboot_notifier(struct device *dev, void *res) |
121 | { | |
122 | WARN_ON(unregister_reboot_notifier(*(struct notifier_block **)res)); | |
123 | } | |
124 | ||
125 | int devm_register_reboot_notifier(struct device *dev, struct notifier_block *nb) | |
126 | { | |
127 | struct notifier_block **rcnb; | |
128 | int ret; | |
129 | ||
130 | rcnb = devres_alloc(devm_unregister_reboot_notifier, | |
131 | sizeof(*rcnb), GFP_KERNEL); | |
132 | if (!rcnb) | |
133 | return -ENOMEM; | |
134 | ||
135 | ret = register_reboot_notifier(nb); | |
136 | if (!ret) { | |
137 | *rcnb = nb; | |
138 | devres_add(dev, rcnb); | |
139 | } else { | |
140 | devres_free(rcnb); | |
141 | } | |
142 | ||
143 | return ret; | |
144 | } | |
145 | EXPORT_SYMBOL(devm_register_reboot_notifier); | |
146 | ||
b63adb97 GR |
147 | /* |
148 | * Notifier list for kernel code which wants to be called | |
149 | * to restart the system. | |
150 | */ | |
151 | static ATOMIC_NOTIFIER_HEAD(restart_handler_list); | |
152 | ||
153 | /** | |
154 | * register_restart_handler - Register function to be called to reset | |
155 | * the system | |
156 | * @nb: Info about handler function to be called | |
157 | * @nb->priority: Handler priority. Handlers should follow the | |
158 | * following guidelines for setting priorities. | |
159 | * 0: Restart handler of last resort, | |
160 | * with limited restart capabilities | |
161 | * 128: Default restart handler; use if no other | |
162 | * restart handler is expected to be available, | |
163 | * and/or if restart functionality is | |
164 | * sufficient to restart the entire system | |
165 | * 255: Highest priority restart handler, will | |
166 | * preempt all other restart handlers | |
167 | * | |
168 | * Registers a function with code to be called to restart the | |
169 | * system. | |
170 | * | |
171 | * Registered functions will be called from machine_restart as last | |
172 | * step of the restart sequence (if the architecture specific | |
173 | * machine_restart function calls do_kernel_restart - see below | |
174 | * for details). | |
175 | * Registered functions are expected to restart the system immediately. | |
176 | * If more than one function is registered, the restart handler priority | |
177 | * selects which function will be called first. | |
178 | * | |
179 | * Restart handlers are expected to be registered from non-architecture | |
180 | * code, typically from drivers. A typical use case would be a system | |
181 | * where restart functionality is provided through a watchdog. Multiple | |
182 | * restart handlers may exist; for example, one restart handler might | |
183 | * restart the entire system, while another only restarts the CPU. | |
184 | * In such cases, the restart handler which only restarts part of the | |
185 | * hardware is expected to register with low priority to ensure that | |
186 | * it only runs if no other means to restart the system is available. | |
187 | * | |
188 | * Currently always returns zero, as atomic_notifier_chain_register() | |
189 | * always returns zero. | |
190 | */ | |
191 | int register_restart_handler(struct notifier_block *nb) | |
192 | { | |
193 | return atomic_notifier_chain_register(&restart_handler_list, nb); | |
194 | } | |
195 | EXPORT_SYMBOL(register_restart_handler); | |
196 | ||
197 | /** | |
198 | * unregister_restart_handler - Unregister previously registered | |
199 | * restart handler | |
200 | * @nb: Hook to be unregistered | |
201 | * | |
202 | * Unregisters a previously registered restart handler function. | |
203 | * | |
204 | * Returns zero on success, or %-ENOENT on failure. | |
205 | */ | |
206 | int unregister_restart_handler(struct notifier_block *nb) | |
207 | { | |
208 | return atomic_notifier_chain_unregister(&restart_handler_list, nb); | |
209 | } | |
210 | EXPORT_SYMBOL(unregister_restart_handler); | |
211 | ||
212 | /** | |
213 | * do_kernel_restart - Execute kernel restart handler call chain | |
214 | * | |
215 | * Calls functions registered with register_restart_handler. | |
216 | * | |
217 | * Expected to be called from machine_restart as last step of the restart | |
218 | * sequence. | |
219 | * | |
220 | * Restarts the system immediately if a restart handler function has been | |
221 | * registered. Otherwise does nothing. | |
222 | */ | |
223 | void do_kernel_restart(char *cmd) | |
224 | { | |
225 | atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd); | |
226 | } | |
227 | ||
c97102ba | 228 | void migrate_to_reboot_cpu(void) |
15d94b82 RH |
229 | { |
230 | /* The boot cpu is always logical cpu 0 */ | |
1b3a5d02 | 231 | int cpu = reboot_cpu; |
15d94b82 RH |
232 | |
233 | cpu_hotplug_disable(); | |
234 | ||
235 | /* Make certain the cpu I'm about to reboot on is online */ | |
236 | if (!cpu_online(cpu)) | |
237 | cpu = cpumask_first(cpu_online_mask); | |
238 | ||
239 | /* Prevent races with other tasks migrating this task */ | |
240 | current->flags |= PF_NO_SETAFFINITY; | |
241 | ||
242 | /* Make certain I only run on the appropriate processor */ | |
243 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); | |
244 | } | |
245 | ||
246 | /** | |
247 | * kernel_restart - reboot the system | |
248 | * @cmd: pointer to buffer containing command to execute for restart | |
249 | * or %NULL | |
250 | * | |
251 | * Shutdown everything and perform a clean reboot. | |
252 | * This is not safe to call in interrupt context. | |
253 | */ | |
254 | void kernel_restart(char *cmd) | |
255 | { | |
256 | kernel_restart_prepare(cmd); | |
257 | migrate_to_reboot_cpu(); | |
258 | syscore_shutdown(); | |
259 | if (!cmd) | |
972ee83d | 260 | pr_emerg("Restarting system\n"); |
15d94b82 | 261 | else |
972ee83d | 262 | pr_emerg("Restarting system with command '%s'\n", cmd); |
6d3cf962 | 263 | kmsg_dump(KMSG_DUMP_SHUTDOWN); |
15d94b82 RH |
264 | machine_restart(cmd); |
265 | } | |
266 | EXPORT_SYMBOL_GPL(kernel_restart); | |
267 | ||
268 | static void kernel_shutdown_prepare(enum system_states state) | |
269 | { | |
270 | blocking_notifier_call_chain(&reboot_notifier_list, | |
972ee83d | 271 | (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL); |
15d94b82 RH |
272 | system_state = state; |
273 | usermodehelper_disable(); | |
274 | device_shutdown(); | |
275 | } | |
276 | /** | |
277 | * kernel_halt - halt the system | |
278 | * | |
279 | * Shutdown everything and perform a clean system halt. | |
280 | */ | |
281 | void kernel_halt(void) | |
282 | { | |
283 | kernel_shutdown_prepare(SYSTEM_HALT); | |
284 | migrate_to_reboot_cpu(); | |
285 | syscore_shutdown(); | |
972ee83d | 286 | pr_emerg("System halted\n"); |
6d3cf962 | 287 | kmsg_dump(KMSG_DUMP_SHUTDOWN); |
15d94b82 RH |
288 | machine_halt(); |
289 | } | |
15d94b82 RH |
290 | EXPORT_SYMBOL_GPL(kernel_halt); |
291 | ||
232edc2f DO |
292 | /* |
293 | * Notifier list for kernel code which wants to be called | |
294 | * to prepare system for power off. | |
295 | */ | |
296 | static BLOCKING_NOTIFIER_HEAD(power_off_prep_handler_list); | |
297 | ||
298 | /* | |
299 | * Notifier list for kernel code which wants to be called | |
300 | * to power off system. | |
301 | */ | |
302 | static ATOMIC_NOTIFIER_HEAD(power_off_handler_list); | |
303 | ||
304 | static int sys_off_notify(struct notifier_block *nb, | |
305 | unsigned long mode, void *cmd) | |
306 | { | |
307 | struct sys_off_handler *handler; | |
308 | struct sys_off_data data = {}; | |
309 | ||
310 | handler = container_of(nb, struct sys_off_handler, nb); | |
311 | data.cb_data = handler->cb_data; | |
312 | data.mode = mode; | |
313 | data.cmd = cmd; | |
314 | ||
315 | return handler->sys_off_cb(&data); | |
316 | } | |
317 | ||
587b9bfe DO |
318 | static struct sys_off_handler platform_sys_off_handler; |
319 | ||
320 | static struct sys_off_handler *alloc_sys_off_handler(int priority) | |
321 | { | |
322 | struct sys_off_handler *handler; | |
323 | ||
324 | /* | |
325 | * Platforms like m68k can't allocate sys_off handler dynamically | |
326 | * at the early boot time because memory allocator isn't available yet. | |
327 | */ | |
328 | if (priority == SYS_OFF_PRIO_PLATFORM) { | |
329 | handler = &platform_sys_off_handler; | |
330 | if (handler->cb_data) | |
331 | return ERR_PTR(-EBUSY); | |
332 | } else { | |
333 | handler = kzalloc(sizeof(*handler), GFP_KERNEL); | |
334 | if (!handler) | |
335 | return ERR_PTR(-ENOMEM); | |
336 | } | |
337 | ||
338 | return handler; | |
339 | } | |
340 | ||
341 | static void free_sys_off_handler(struct sys_off_handler *handler) | |
342 | { | |
343 | if (handler == &platform_sys_off_handler) | |
344 | memset(handler, 0, sizeof(*handler)); | |
345 | else | |
346 | kfree(handler); | |
347 | } | |
348 | ||
232edc2f DO |
349 | /** |
350 | * register_sys_off_handler - Register sys-off handler | |
351 | * @mode: Sys-off mode | |
352 | * @priority: Handler priority | |
353 | * @callback: Callback function | |
354 | * @cb_data: Callback argument | |
355 | * | |
356 | * Registers system power-off or restart handler that will be invoked | |
357 | * at the step corresponding to the given sys-off mode. Handler's callback | |
358 | * should return NOTIFY_DONE to permit execution of the next handler in | |
359 | * the call chain or NOTIFY_STOP to break the chain (in error case for | |
360 | * example). | |
361 | * | |
362 | * Multiple handlers can be registered at the default priority level. | |
363 | * | |
364 | * Only one handler can be registered at the non-default priority level, | |
365 | * otherwise ERR_PTR(-EBUSY) is returned. | |
366 | * | |
367 | * Returns a new instance of struct sys_off_handler on success, or | |
368 | * an ERR_PTR()-encoded error code otherwise. | |
369 | */ | |
370 | struct sys_off_handler * | |
371 | register_sys_off_handler(enum sys_off_mode mode, | |
372 | int priority, | |
373 | int (*callback)(struct sys_off_data *data), | |
374 | void *cb_data) | |
375 | { | |
376 | struct sys_off_handler *handler; | |
377 | int err; | |
378 | ||
587b9bfe DO |
379 | handler = alloc_sys_off_handler(priority); |
380 | if (IS_ERR(handler)) | |
381 | return handler; | |
232edc2f DO |
382 | |
383 | switch (mode) { | |
384 | case SYS_OFF_MODE_POWER_OFF_PREPARE: | |
385 | handler->list = &power_off_prep_handler_list; | |
386 | handler->blocking = true; | |
387 | break; | |
388 | ||
389 | case SYS_OFF_MODE_POWER_OFF: | |
390 | handler->list = &power_off_handler_list; | |
391 | break; | |
392 | ||
393 | case SYS_OFF_MODE_RESTART: | |
394 | handler->list = &restart_handler_list; | |
395 | break; | |
396 | ||
397 | default: | |
587b9bfe | 398 | free_sys_off_handler(handler); |
232edc2f DO |
399 | return ERR_PTR(-EINVAL); |
400 | } | |
401 | ||
402 | handler->nb.notifier_call = sys_off_notify; | |
403 | handler->nb.priority = priority; | |
404 | handler->sys_off_cb = callback; | |
405 | handler->cb_data = cb_data; | |
406 | handler->mode = mode; | |
407 | ||
408 | if (handler->blocking) { | |
409 | if (priority == SYS_OFF_PRIO_DEFAULT) | |
410 | err = blocking_notifier_chain_register(handler->list, | |
411 | &handler->nb); | |
412 | else | |
413 | err = blocking_notifier_chain_register_unique_prio(handler->list, | |
414 | &handler->nb); | |
415 | } else { | |
416 | if (priority == SYS_OFF_PRIO_DEFAULT) | |
417 | err = atomic_notifier_chain_register(handler->list, | |
418 | &handler->nb); | |
419 | else | |
420 | err = atomic_notifier_chain_register_unique_prio(handler->list, | |
421 | &handler->nb); | |
422 | } | |
423 | ||
424 | if (err) { | |
587b9bfe | 425 | free_sys_off_handler(handler); |
232edc2f DO |
426 | return ERR_PTR(err); |
427 | } | |
428 | ||
429 | return handler; | |
430 | } | |
431 | EXPORT_SYMBOL_GPL(register_sys_off_handler); | |
432 | ||
433 | /** | |
434 | * unregister_sys_off_handler - Unregister sys-off handler | |
435 | * @handler: Sys-off handler | |
436 | * | |
437 | * Unregisters given sys-off handler. | |
438 | */ | |
439 | void unregister_sys_off_handler(struct sys_off_handler *handler) | |
440 | { | |
441 | int err; | |
442 | ||
443 | if (!handler) | |
444 | return; | |
445 | ||
446 | if (handler->blocking) | |
447 | err = blocking_notifier_chain_unregister(handler->list, | |
448 | &handler->nb); | |
449 | else | |
450 | err = atomic_notifier_chain_unregister(handler->list, | |
451 | &handler->nb); | |
452 | ||
453 | /* sanity check, shall never happen */ | |
454 | WARN_ON(err); | |
455 | ||
587b9bfe | 456 | free_sys_off_handler(handler); |
232edc2f DO |
457 | } |
458 | EXPORT_SYMBOL_GPL(unregister_sys_off_handler); | |
459 | ||
460 | static void devm_unregister_sys_off_handler(void *data) | |
461 | { | |
462 | struct sys_off_handler *handler = data; | |
463 | ||
464 | unregister_sys_off_handler(handler); | |
465 | } | |
466 | ||
467 | /** | |
468 | * devm_register_sys_off_handler - Register sys-off handler | |
469 | * @dev: Device that registers handler | |
470 | * @mode: Sys-off mode | |
471 | * @priority: Handler priority | |
472 | * @callback: Callback function | |
473 | * @cb_data: Callback argument | |
474 | * | |
475 | * Registers resource-managed sys-off handler. | |
476 | * | |
477 | * Returns zero on success, or error code on failure. | |
478 | */ | |
479 | int devm_register_sys_off_handler(struct device *dev, | |
480 | enum sys_off_mode mode, | |
481 | int priority, | |
482 | int (*callback)(struct sys_off_data *data), | |
483 | void *cb_data) | |
484 | { | |
485 | struct sys_off_handler *handler; | |
486 | ||
487 | handler = register_sys_off_handler(mode, priority, callback, cb_data); | |
488 | if (IS_ERR(handler)) | |
489 | return PTR_ERR(handler); | |
490 | ||
491 | return devm_add_action_or_reset(dev, devm_unregister_sys_off_handler, | |
492 | handler); | |
493 | } | |
494 | EXPORT_SYMBOL_GPL(devm_register_sys_off_handler); | |
495 | ||
d2c54153 DO |
496 | /** |
497 | * devm_register_power_off_handler - Register power-off handler | |
498 | * @dev: Device that registers callback | |
499 | * @callback: Callback function | |
500 | * @cb_data: Callback's argument | |
501 | * | |
502 | * Registers resource-managed sys-off handler with a default priority | |
503 | * and using power-off mode. | |
504 | * | |
505 | * Returns zero on success, or error code on failure. | |
506 | */ | |
507 | int devm_register_power_off_handler(struct device *dev, | |
508 | int (*callback)(struct sys_off_data *data), | |
509 | void *cb_data) | |
510 | { | |
511 | return devm_register_sys_off_handler(dev, | |
512 | SYS_OFF_MODE_POWER_OFF, | |
513 | SYS_OFF_PRIO_DEFAULT, | |
514 | callback, cb_data); | |
515 | } | |
516 | EXPORT_SYMBOL_GPL(devm_register_power_off_handler); | |
517 | ||
6779db97 DO |
518 | /** |
519 | * devm_register_restart_handler - Register restart handler | |
520 | * @dev: Device that registers callback | |
521 | * @callback: Callback function | |
522 | * @cb_data: Callback's argument | |
523 | * | |
524 | * Registers resource-managed sys-off handler with a default priority | |
525 | * and using restart mode. | |
526 | * | |
527 | * Returns zero on success, or error code on failure. | |
528 | */ | |
529 | int devm_register_restart_handler(struct device *dev, | |
530 | int (*callback)(struct sys_off_data *data), | |
531 | void *cb_data) | |
532 | { | |
533 | return devm_register_sys_off_handler(dev, | |
534 | SYS_OFF_MODE_RESTART, | |
535 | SYS_OFF_PRIO_DEFAULT, | |
536 | callback, cb_data); | |
537 | } | |
538 | EXPORT_SYMBOL_GPL(devm_register_restart_handler); | |
539 | ||
fb61375e DO |
540 | static struct sys_off_handler *platform_power_off_handler; |
541 | ||
542 | static int platform_power_off_notify(struct sys_off_data *data) | |
543 | { | |
544 | void (*platform_power_power_off_cb)(void) = data->cb_data; | |
545 | ||
546 | platform_power_power_off_cb(); | |
547 | ||
548 | return NOTIFY_DONE; | |
549 | } | |
550 | ||
551 | /** | |
552 | * register_platform_power_off - Register platform-level power-off callback | |
553 | * @power_off: Power-off callback | |
554 | * | |
555 | * Registers power-off callback that will be called as last step | |
556 | * of the power-off sequence. This callback is expected to be invoked | |
557 | * for the last resort. Only one platform power-off callback is allowed | |
558 | * to be registered at a time. | |
559 | * | |
560 | * Returns zero on success, or error code on failure. | |
561 | */ | |
562 | int register_platform_power_off(void (*power_off)(void)) | |
563 | { | |
564 | struct sys_off_handler *handler; | |
565 | ||
566 | handler = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, | |
567 | SYS_OFF_PRIO_PLATFORM, | |
568 | platform_power_off_notify, | |
569 | power_off); | |
570 | if (IS_ERR(handler)) | |
571 | return PTR_ERR(handler); | |
572 | ||
573 | platform_power_off_handler = handler; | |
574 | ||
575 | return 0; | |
576 | } | |
577 | EXPORT_SYMBOL_GPL(register_platform_power_off); | |
578 | ||
579 | /** | |
580 | * unregister_platform_power_off - Unregister platform-level power-off callback | |
581 | * @power_off: Power-off callback | |
582 | * | |
583 | * Unregisters previously registered platform power-off callback. | |
584 | */ | |
585 | void unregister_platform_power_off(void (*power_off)(void)) | |
586 | { | |
587 | if (platform_power_off_handler && | |
588 | platform_power_off_handler->cb_data == power_off) { | |
589 | unregister_sys_off_handler(platform_power_off_handler); | |
590 | platform_power_off_handler = NULL; | |
591 | } | |
592 | } | |
593 | EXPORT_SYMBOL_GPL(unregister_platform_power_off); | |
594 | ||
7b9a3de9 DO |
595 | static int legacy_pm_power_off(struct sys_off_data *data) |
596 | { | |
597 | if (pm_power_off) | |
598 | pm_power_off(); | |
599 | ||
600 | return NOTIFY_DONE; | |
601 | } | |
602 | ||
7b9a3de9 DO |
603 | static void do_kernel_power_off_prepare(void) |
604 | { | |
605 | blocking_notifier_call_chain(&power_off_prep_handler_list, 0, NULL); | |
606 | } | |
607 | ||
2b6aa733 DO |
608 | /** |
609 | * do_kernel_power_off - Execute kernel power-off handler call chain | |
610 | * | |
611 | * Expected to be called as last step of the power-off sequence. | |
612 | * | |
613 | * Powers off the system immediately if a power-off handler function has | |
614 | * been registered. Otherwise does nothing. | |
615 | */ | |
616 | void do_kernel_power_off(void) | |
617 | { | |
618 | atomic_notifier_call_chain(&power_off_handler_list, 0, NULL); | |
619 | } | |
620 | ||
0e2110d2 DO |
621 | /** |
622 | * kernel_can_power_off - check whether system can be powered off | |
623 | * | |
624 | * Returns true if power-off handler is registered and system can be | |
625 | * powered off, false otherwise. | |
626 | */ | |
627 | bool kernel_can_power_off(void) | |
628 | { | |
629 | return !atomic_notifier_call_chain_is_empty(&power_off_handler_list); | |
630 | } | |
631 | EXPORT_SYMBOL_GPL(kernel_can_power_off); | |
632 | ||
15d94b82 RH |
633 | /** |
634 | * kernel_power_off - power_off the system | |
635 | * | |
636 | * Shutdown everything and perform a clean system power_off. | |
637 | */ | |
638 | void kernel_power_off(void) | |
639 | { | |
640 | kernel_shutdown_prepare(SYSTEM_POWER_OFF); | |
7b9a3de9 | 641 | do_kernel_power_off_prepare(); |
15d94b82 RH |
642 | migrate_to_reboot_cpu(); |
643 | syscore_shutdown(); | |
972ee83d | 644 | pr_emerg("Power down\n"); |
6d3cf962 | 645 | kmsg_dump(KMSG_DUMP_SHUTDOWN); |
15d94b82 RH |
646 | machine_power_off(); |
647 | } | |
648 | EXPORT_SYMBOL_GPL(kernel_power_off); | |
649 | ||
55f2503c | 650 | DEFINE_MUTEX(system_transition_mutex); |
15d94b82 RH |
651 | |
652 | /* | |
653 | * Reboot system call: for obvious reasons only root may call it, | |
654 | * and even root needs to set up some magic numbers in the registers | |
655 | * so that some mistake won't make this reboot the whole machine. | |
656 | * You can also set the meaning of the ctrl-alt-del-key here. | |
657 | * | |
658 | * reboot doesn't sync: do that yourself before calling this. | |
659 | */ | |
660 | SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, | |
661 | void __user *, arg) | |
662 | { | |
663 | struct pid_namespace *pid_ns = task_active_pid_ns(current); | |
da007f17 | 664 | struct sys_off_handler *sys_off = NULL; |
15d94b82 RH |
665 | char buffer[256]; |
666 | int ret = 0; | |
667 | ||
668 | /* We only trust the superuser with rebooting the system. */ | |
669 | if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT)) | |
670 | return -EPERM; | |
671 | ||
672 | /* For safety, we require "magic" arguments. */ | |
673 | if (magic1 != LINUX_REBOOT_MAGIC1 || | |
972ee83d RH |
674 | (magic2 != LINUX_REBOOT_MAGIC2 && |
675 | magic2 != LINUX_REBOOT_MAGIC2A && | |
15d94b82 | 676 | magic2 != LINUX_REBOOT_MAGIC2B && |
972ee83d | 677 | magic2 != LINUX_REBOOT_MAGIC2C)) |
15d94b82 RH |
678 | return -EINVAL; |
679 | ||
680 | /* | |
681 | * If pid namespaces are enabled and the current task is in a child | |
682 | * pid_namespace, the command is handled by reboot_pid_ns() which will | |
683 | * call do_exit(). | |
684 | */ | |
685 | ret = reboot_pid_ns(pid_ns, cmd); | |
686 | if (ret) | |
687 | return ret; | |
688 | ||
da007f17 DO |
689 | /* |
690 | * Register sys-off handlers for legacy PM callback. This allows | |
691 | * legacy PM callbacks temporary co-exist with the new sys-off API. | |
692 | * | |
693 | * TODO: Remove legacy handlers once all legacy PM users will be | |
694 | * switched to the sys-off based APIs. | |
695 | */ | |
696 | if (pm_power_off) { | |
697 | sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, | |
698 | SYS_OFF_PRIO_DEFAULT, | |
699 | legacy_pm_power_off, NULL); | |
700 | if (IS_ERR(sys_off)) | |
701 | return PTR_ERR(sys_off); | |
702 | } | |
703 | ||
15d94b82 RH |
704 | /* Instead of trying to make the power_off code look like |
705 | * halt when pm_power_off is not set do it the easy way. | |
706 | */ | |
0e2110d2 | 707 | if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !kernel_can_power_off()) |
15d94b82 RH |
708 | cmd = LINUX_REBOOT_CMD_HALT; |
709 | ||
55f2503c | 710 | mutex_lock(&system_transition_mutex); |
15d94b82 RH |
711 | switch (cmd) { |
712 | case LINUX_REBOOT_CMD_RESTART: | |
713 | kernel_restart(NULL); | |
714 | break; | |
715 | ||
716 | case LINUX_REBOOT_CMD_CAD_ON: | |
717 | C_A_D = 1; | |
718 | break; | |
719 | ||
720 | case LINUX_REBOOT_CMD_CAD_OFF: | |
721 | C_A_D = 0; | |
722 | break; | |
723 | ||
724 | case LINUX_REBOOT_CMD_HALT: | |
725 | kernel_halt(); | |
726 | do_exit(0); | |
15d94b82 RH |
727 | |
728 | case LINUX_REBOOT_CMD_POWER_OFF: | |
729 | kernel_power_off(); | |
730 | do_exit(0); | |
731 | break; | |
732 | ||
733 | case LINUX_REBOOT_CMD_RESTART2: | |
972ee83d RH |
734 | ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1); |
735 | if (ret < 0) { | |
15d94b82 RH |
736 | ret = -EFAULT; |
737 | break; | |
738 | } | |
739 | buffer[sizeof(buffer) - 1] = '\0'; | |
740 | ||
741 | kernel_restart(buffer); | |
742 | break; | |
743 | ||
2965faa5 | 744 | #ifdef CONFIG_KEXEC_CORE |
15d94b82 RH |
745 | case LINUX_REBOOT_CMD_KEXEC: |
746 | ret = kernel_kexec(); | |
747 | break; | |
748 | #endif | |
749 | ||
750 | #ifdef CONFIG_HIBERNATION | |
751 | case LINUX_REBOOT_CMD_SW_SUSPEND: | |
752 | ret = hibernate(); | |
753 | break; | |
754 | #endif | |
755 | ||
756 | default: | |
757 | ret = -EINVAL; | |
758 | break; | |
759 | } | |
55f2503c | 760 | mutex_unlock(&system_transition_mutex); |
da007f17 | 761 | unregister_sys_off_handler(sys_off); |
15d94b82 RH |
762 | return ret; |
763 | } | |
764 | ||
765 | static void deferred_cad(struct work_struct *dummy) | |
766 | { | |
767 | kernel_restart(NULL); | |
768 | } | |
769 | ||
770 | /* | |
771 | * This function gets called by ctrl-alt-del - ie the keyboard interrupt. | |
772 | * As it's called within an interrupt, it may NOT sync: the only choice | |
773 | * is whether to reboot at once, or just ignore the ctrl-alt-del. | |
774 | */ | |
775 | void ctrl_alt_del(void) | |
776 | { | |
777 | static DECLARE_WORK(cad_work, deferred_cad); | |
778 | ||
779 | if (C_A_D) | |
780 | schedule_work(&cad_work); | |
781 | else | |
782 | kill_cad_pid(SIGINT, 1); | |
783 | } | |
784 | ||
06d17766 | 785 | #define POWEROFF_CMD_PATH_LEN 256 |
786 | static char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; | |
7a54f46b | 787 | static const char reboot_cmd[] = "/sbin/reboot"; |
15d94b82 | 788 | |
7a54f46b | 789 | static int run_cmd(const char *cmd) |
15d94b82 RH |
790 | { |
791 | char **argv; | |
792 | static char *envp[] = { | |
793 | "HOME=/", | |
794 | "PATH=/sbin:/bin:/usr/sbin:/usr/bin", | |
795 | NULL | |
796 | }; | |
797 | int ret; | |
7a54f46b | 798 | argv = argv_split(GFP_KERNEL, cmd, NULL); |
15d94b82 RH |
799 | if (argv) { |
800 | ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC); | |
801 | argv_free(argv); | |
802 | } else { | |
15d94b82 RH |
803 | ret = -ENOMEM; |
804 | } | |
805 | ||
7a54f46b JS |
806 | return ret; |
807 | } | |
808 | ||
809 | static int __orderly_reboot(void) | |
810 | { | |
811 | int ret; | |
812 | ||
813 | ret = run_cmd(reboot_cmd); | |
814 | ||
815 | if (ret) { | |
2bb2b7b5 | 816 | printk_prefer_direct_enter(); |
7a54f46b JS |
817 | pr_warn("Failed to start orderly reboot: forcing the issue\n"); |
818 | emergency_sync(); | |
819 | kernel_restart(NULL); | |
2bb2b7b5 | 820 | printk_prefer_direct_exit(); |
7a54f46b JS |
821 | } |
822 | ||
823 | return ret; | |
824 | } | |
825 | ||
826 | static int __orderly_poweroff(bool force) | |
827 | { | |
828 | int ret; | |
829 | ||
830 | ret = run_cmd(poweroff_cmd); | |
831 | ||
15d94b82 | 832 | if (ret && force) { |
2bb2b7b5 | 833 | printk_prefer_direct_enter(); |
972ee83d | 834 | pr_warn("Failed to start orderly shutdown: forcing the issue\n"); |
7a54f46b | 835 | |
15d94b82 RH |
836 | /* |
837 | * I guess this should try to kick off some daemon to sync and | |
838 | * poweroff asap. Or not even bother syncing if we're doing an | |
839 | * emergency shutdown? | |
840 | */ | |
841 | emergency_sync(); | |
842 | kernel_power_off(); | |
2bb2b7b5 | 843 | printk_prefer_direct_exit(); |
15d94b82 RH |
844 | } |
845 | ||
846 | return ret; | |
847 | } | |
848 | ||
849 | static bool poweroff_force; | |
850 | ||
851 | static void poweroff_work_func(struct work_struct *work) | |
852 | { | |
853 | __orderly_poweroff(poweroff_force); | |
854 | } | |
855 | ||
856 | static DECLARE_WORK(poweroff_work, poweroff_work_func); | |
857 | ||
858 | /** | |
859 | * orderly_poweroff - Trigger an orderly system poweroff | |
860 | * @force: force poweroff if command execution fails | |
861 | * | |
862 | * This may be called from any context to trigger a system shutdown. | |
863 | * If the orderly shutdown fails, it will force an immediate shutdown. | |
864 | */ | |
7a54f46b | 865 | void orderly_poweroff(bool force) |
15d94b82 RH |
866 | { |
867 | if (force) /* do not override the pending "true" */ | |
868 | poweroff_force = true; | |
869 | schedule_work(&poweroff_work); | |
15d94b82 RH |
870 | } |
871 | EXPORT_SYMBOL_GPL(orderly_poweroff); | |
1b3a5d02 | 872 | |
7a54f46b JS |
873 | static void reboot_work_func(struct work_struct *work) |
874 | { | |
875 | __orderly_reboot(); | |
876 | } | |
877 | ||
878 | static DECLARE_WORK(reboot_work, reboot_work_func); | |
879 | ||
880 | /** | |
881 | * orderly_reboot - Trigger an orderly system reboot | |
882 | * | |
883 | * This may be called from any context to trigger a system reboot. | |
884 | * If the orderly reboot fails, it will force an immediate reboot. | |
885 | */ | |
886 | void orderly_reboot(void) | |
887 | { | |
888 | schedule_work(&reboot_work); | |
889 | } | |
890 | EXPORT_SYMBOL_GPL(orderly_reboot); | |
891 | ||
dfa19b11 MV |
892 | /** |
893 | * hw_failure_emergency_poweroff_func - emergency poweroff work after a known delay | |
894 | * @work: work_struct associated with the emergency poweroff function | |
895 | * | |
896 | * This function is called in very critical situations to force | |
897 | * a kernel poweroff after a configurable timeout value. | |
898 | */ | |
899 | static void hw_failure_emergency_poweroff_func(struct work_struct *work) | |
900 | { | |
2bb2b7b5 JO |
901 | printk_prefer_direct_enter(); |
902 | ||
dfa19b11 MV |
903 | /* |
904 | * We have reached here after the emergency shutdown waiting period has | |
905 | * expired. This means orderly_poweroff has not been able to shut off | |
906 | * the system for some reason. | |
907 | * | |
908 | * Try to shut down the system immediately using kernel_power_off | |
909 | * if populated | |
910 | */ | |
911 | pr_emerg("Hardware protection timed-out. Trying forced poweroff\n"); | |
912 | kernel_power_off(); | |
913 | ||
914 | /* | |
915 | * Worst of the worst case trigger emergency restart | |
916 | */ | |
917 | pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n"); | |
918 | emergency_restart(); | |
2bb2b7b5 JO |
919 | |
920 | printk_prefer_direct_exit(); | |
dfa19b11 MV |
921 | } |
922 | ||
923 | static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work, | |
924 | hw_failure_emergency_poweroff_func); | |
925 | ||
926 | /** | |
927 | * hw_failure_emergency_poweroff - Trigger an emergency system poweroff | |
928 | * | |
929 | * This may be called from any critical situation to trigger a system shutdown | |
930 | * after a given period of time. If time is negative this is not scheduled. | |
931 | */ | |
932 | static void hw_failure_emergency_poweroff(int poweroff_delay_ms) | |
933 | { | |
934 | if (poweroff_delay_ms <= 0) | |
935 | return; | |
936 | schedule_delayed_work(&hw_failure_emergency_poweroff_work, | |
937 | msecs_to_jiffies(poweroff_delay_ms)); | |
938 | } | |
939 | ||
940 | /** | |
941 | * hw_protection_shutdown - Trigger an emergency system poweroff | |
942 | * | |
943 | * @reason: Reason of emergency shutdown to be printed. | |
944 | * @ms_until_forced: Time to wait for orderly shutdown before tiggering a | |
945 | * forced shudown. Negative value disables the forced | |
946 | * shutdown. | |
947 | * | |
948 | * Initiate an emergency system shutdown in order to protect hardware from | |
949 | * further damage. Usage examples include a thermal protection or a voltage or | |
950 | * current regulator failures. | |
951 | * NOTE: The request is ignored if protection shutdown is already pending even | |
952 | * if the previous request has given a large timeout for forced shutdown. | |
953 | * Can be called from any context. | |
954 | */ | |
955 | void hw_protection_shutdown(const char *reason, int ms_until_forced) | |
956 | { | |
957 | static atomic_t allow_proceed = ATOMIC_INIT(1); | |
958 | ||
2bb2b7b5 JO |
959 | printk_prefer_direct_enter(); |
960 | ||
dfa19b11 MV |
961 | pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason); |
962 | ||
963 | /* Shutdown should be initiated only once. */ | |
964 | if (!atomic_dec_and_test(&allow_proceed)) | |
2bb2b7b5 | 965 | goto out; |
dfa19b11 MV |
966 | |
967 | /* | |
968 | * Queue a backup emergency shutdown in the event of | |
969 | * orderly_poweroff failure | |
970 | */ | |
971 | hw_failure_emergency_poweroff(ms_until_forced); | |
972 | orderly_poweroff(true); | |
2bb2b7b5 JO |
973 | out: |
974 | printk_prefer_direct_exit(); | |
dfa19b11 MV |
975 | } |
976 | EXPORT_SYMBOL_GPL(hw_protection_shutdown); | |
977 | ||
1b3a5d02 RH |
978 | static int __init reboot_setup(char *str) |
979 | { | |
980 | for (;;) { | |
b287a25a AK |
981 | enum reboot_mode *mode; |
982 | ||
1b3a5d02 RH |
983 | /* |
984 | * Having anything passed on the command line via | |
985 | * reboot= will cause us to disable DMI checking | |
986 | * below. | |
987 | */ | |
988 | reboot_default = 0; | |
989 | ||
b287a25a AK |
990 | if (!strncmp(str, "panic_", 6)) { |
991 | mode = &panic_reboot_mode; | |
992 | str += 6; | |
993 | } else { | |
994 | mode = &reboot_mode; | |
995 | } | |
996 | ||
1b3a5d02 RH |
997 | switch (*str) { |
998 | case 'w': | |
b287a25a | 999 | *mode = REBOOT_WARM; |
1b3a5d02 RH |
1000 | break; |
1001 | ||
1002 | case 'c': | |
b287a25a | 1003 | *mode = REBOOT_COLD; |
1b3a5d02 RH |
1004 | break; |
1005 | ||
1006 | case 'h': | |
b287a25a | 1007 | *mode = REBOOT_HARD; |
1b3a5d02 RH |
1008 | break; |
1009 | ||
1010 | case 's': | |
f9a90501 MC |
1011 | /* |
1012 | * reboot_cpu is s[mp]#### with #### being the processor | |
1013 | * to be used for rebooting. Skip 's' or 'smp' prefix. | |
1014 | */ | |
1015 | str += str[1] == 'm' && str[2] == 'p' ? 3 : 1; | |
1016 | ||
1017 | if (isdigit(str[0])) { | |
1018 | int cpu = simple_strtoul(str, NULL, 0); | |
1019 | ||
1020 | if (cpu >= num_possible_cpus()) { | |
1021 | pr_err("Ignoring the CPU number in reboot= option. " | |
1022 | "CPU %d exceeds possible cpu number %d\n", | |
1023 | cpu, num_possible_cpus()); | |
1024 | break; | |
1025 | } | |
1026 | reboot_cpu = cpu; | |
1027 | } else | |
b287a25a | 1028 | *mode = REBOOT_SOFT; |
1b3a5d02 | 1029 | break; |
8b92c4ff | 1030 | |
1b3a5d02 | 1031 | case 'g': |
b287a25a | 1032 | *mode = REBOOT_GPIO; |
1b3a5d02 RH |
1033 | break; |
1034 | ||
1035 | case 'b': | |
1036 | case 'a': | |
1037 | case 'k': | |
1038 | case 't': | |
1039 | case 'e': | |
1040 | case 'p': | |
1041 | reboot_type = *str; | |
1042 | break; | |
1043 | ||
1044 | case 'f': | |
1045 | reboot_force = 1; | |
1046 | break; | |
1047 | } | |
1048 | ||
1049 | str = strchr(str, ','); | |
1050 | if (str) | |
1051 | str++; | |
1052 | else | |
1053 | break; | |
1054 | } | |
1055 | return 1; | |
1056 | } | |
1057 | __setup("reboot=", reboot_setup); | |
2c622ed0 MC |
1058 | |
1059 | #ifdef CONFIG_SYSFS | |
1060 | ||
1061 | #define REBOOT_COLD_STR "cold" | |
1062 | #define REBOOT_WARM_STR "warm" | |
1063 | #define REBOOT_HARD_STR "hard" | |
1064 | #define REBOOT_SOFT_STR "soft" | |
1065 | #define REBOOT_GPIO_STR "gpio" | |
1066 | #define REBOOT_UNDEFINED_STR "undefined" | |
1067 | ||
1068 | #define BOOT_TRIPLE_STR "triple" | |
1069 | #define BOOT_KBD_STR "kbd" | |
1070 | #define BOOT_BIOS_STR "bios" | |
1071 | #define BOOT_ACPI_STR "acpi" | |
1072 | #define BOOT_EFI_STR "efi" | |
0c5c0179 | 1073 | #define BOOT_PCI_STR "pci" |
2c622ed0 MC |
1074 | |
1075 | static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) | |
1076 | { | |
1077 | const char *val; | |
1078 | ||
1079 | switch (reboot_mode) { | |
1080 | case REBOOT_COLD: | |
1081 | val = REBOOT_COLD_STR; | |
1082 | break; | |
1083 | case REBOOT_WARM: | |
1084 | val = REBOOT_WARM_STR; | |
1085 | break; | |
1086 | case REBOOT_HARD: | |
1087 | val = REBOOT_HARD_STR; | |
1088 | break; | |
1089 | case REBOOT_SOFT: | |
1090 | val = REBOOT_SOFT_STR; | |
1091 | break; | |
1092 | case REBOOT_GPIO: | |
1093 | val = REBOOT_GPIO_STR; | |
1094 | break; | |
1095 | default: | |
1096 | val = REBOOT_UNDEFINED_STR; | |
1097 | } | |
1098 | ||
1099 | return sprintf(buf, "%s\n", val); | |
1100 | } | |
1101 | static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr, | |
1102 | const char *buf, size_t count) | |
1103 | { | |
1104 | if (!capable(CAP_SYS_BOOT)) | |
1105 | return -EPERM; | |
1106 | ||
1107 | if (!strncmp(buf, REBOOT_COLD_STR, strlen(REBOOT_COLD_STR))) | |
1108 | reboot_mode = REBOOT_COLD; | |
1109 | else if (!strncmp(buf, REBOOT_WARM_STR, strlen(REBOOT_WARM_STR))) | |
1110 | reboot_mode = REBOOT_WARM; | |
1111 | else if (!strncmp(buf, REBOOT_HARD_STR, strlen(REBOOT_HARD_STR))) | |
1112 | reboot_mode = REBOOT_HARD; | |
1113 | else if (!strncmp(buf, REBOOT_SOFT_STR, strlen(REBOOT_SOFT_STR))) | |
1114 | reboot_mode = REBOOT_SOFT; | |
1115 | else if (!strncmp(buf, REBOOT_GPIO_STR, strlen(REBOOT_GPIO_STR))) | |
1116 | reboot_mode = REBOOT_GPIO; | |
1117 | else | |
1118 | return -EINVAL; | |
1119 | ||
1a9d079f MC |
1120 | reboot_default = 0; |
1121 | ||
2c622ed0 MC |
1122 | return count; |
1123 | } | |
1124 | static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode); | |
1125 | ||
40247e55 MC |
1126 | #ifdef CONFIG_X86 |
1127 | static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) | |
1128 | { | |
1129 | return sprintf(buf, "%d\n", reboot_force); | |
1130 | } | |
1131 | static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr, | |
1132 | const char *buf, size_t count) | |
1133 | { | |
1134 | bool res; | |
1135 | ||
1136 | if (!capable(CAP_SYS_BOOT)) | |
1137 | return -EPERM; | |
1138 | ||
1139 | if (kstrtobool(buf, &res)) | |
1140 | return -EINVAL; | |
1141 | ||
1142 | reboot_default = 0; | |
1143 | reboot_force = res; | |
1144 | ||
1145 | return count; | |
1146 | } | |
1147 | static struct kobj_attribute reboot_force_attr = __ATTR_RW(force); | |
1148 | ||
2c622ed0 MC |
1149 | static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
1150 | { | |
1151 | const char *val; | |
1152 | ||
1153 | switch (reboot_type) { | |
1154 | case BOOT_TRIPLE: | |
1155 | val = BOOT_TRIPLE_STR; | |
1156 | break; | |
1157 | case BOOT_KBD: | |
1158 | val = BOOT_KBD_STR; | |
1159 | break; | |
1160 | case BOOT_BIOS: | |
1161 | val = BOOT_BIOS_STR; | |
1162 | break; | |
1163 | case BOOT_ACPI: | |
1164 | val = BOOT_ACPI_STR; | |
1165 | break; | |
1166 | case BOOT_EFI: | |
1167 | val = BOOT_EFI_STR; | |
1168 | break; | |
1169 | case BOOT_CF9_FORCE: | |
0c5c0179 | 1170 | val = BOOT_PCI_STR; |
2c622ed0 MC |
1171 | break; |
1172 | default: | |
1173 | val = REBOOT_UNDEFINED_STR; | |
1174 | } | |
1175 | ||
1176 | return sprintf(buf, "%s\n", val); | |
1177 | } | |
1178 | static ssize_t type_store(struct kobject *kobj, struct kobj_attribute *attr, | |
1179 | const char *buf, size_t count) | |
1180 | { | |
1181 | if (!capable(CAP_SYS_BOOT)) | |
1182 | return -EPERM; | |
1183 | ||
1184 | if (!strncmp(buf, BOOT_TRIPLE_STR, strlen(BOOT_TRIPLE_STR))) | |
1185 | reboot_type = BOOT_TRIPLE; | |
1186 | else if (!strncmp(buf, BOOT_KBD_STR, strlen(BOOT_KBD_STR))) | |
1187 | reboot_type = BOOT_KBD; | |
1188 | else if (!strncmp(buf, BOOT_BIOS_STR, strlen(BOOT_BIOS_STR))) | |
1189 | reboot_type = BOOT_BIOS; | |
1190 | else if (!strncmp(buf, BOOT_ACPI_STR, strlen(BOOT_ACPI_STR))) | |
1191 | reboot_type = BOOT_ACPI; | |
1192 | else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR))) | |
1193 | reboot_type = BOOT_EFI; | |
0c5c0179 | 1194 | else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR))) |
2c622ed0 | 1195 | reboot_type = BOOT_CF9_FORCE; |
2c622ed0 MC |
1196 | else |
1197 | return -EINVAL; | |
1198 | ||
1a9d079f MC |
1199 | reboot_default = 0; |
1200 | ||
2c622ed0 MC |
1201 | return count; |
1202 | } | |
1203 | static struct kobj_attribute reboot_type_attr = __ATTR_RW(type); | |
40247e55 | 1204 | #endif |
2c622ed0 | 1205 | |
40247e55 | 1206 | #ifdef CONFIG_SMP |
2c622ed0 MC |
1207 | static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
1208 | { | |
1209 | return sprintf(buf, "%d\n", reboot_cpu); | |
1210 | } | |
1211 | static ssize_t cpu_store(struct kobject *kobj, struct kobj_attribute *attr, | |
1212 | const char *buf, size_t count) | |
1213 | { | |
1214 | unsigned int cpunum; | |
1215 | int rc; | |
1216 | ||
1217 | if (!capable(CAP_SYS_BOOT)) | |
1218 | return -EPERM; | |
1219 | ||
1220 | rc = kstrtouint(buf, 0, &cpunum); | |
1221 | ||
1222 | if (rc) | |
1223 | return rc; | |
1224 | ||
1225 | if (cpunum >= num_possible_cpus()) | |
1226 | return -ERANGE; | |
1227 | ||
1a9d079f | 1228 | reboot_default = 0; |
2c622ed0 MC |
1229 | reboot_cpu = cpunum; |
1230 | ||
1231 | return count; | |
1232 | } | |
1233 | static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu); | |
40247e55 | 1234 | #endif |
2c622ed0 MC |
1235 | |
1236 | static struct attribute *reboot_attrs[] = { | |
1237 | &reboot_mode_attr.attr, | |
40247e55 MC |
1238 | #ifdef CONFIG_X86 |
1239 | &reboot_force_attr.attr, | |
2c622ed0 | 1240 | &reboot_type_attr.attr, |
40247e55 MC |
1241 | #endif |
1242 | #ifdef CONFIG_SMP | |
2c622ed0 | 1243 | &reboot_cpu_attr.attr, |
40247e55 | 1244 | #endif |
2c622ed0 MC |
1245 | NULL, |
1246 | }; | |
1247 | ||
764aaf44 Y |
1248 | #ifdef CONFIG_SYSCTL |
1249 | static struct ctl_table kern_reboot_table[] = { | |
1250 | { | |
1251 | .procname = "poweroff_cmd", | |
1252 | .data = &poweroff_cmd, | |
1253 | .maxlen = POWEROFF_CMD_PATH_LEN, | |
1254 | .mode = 0644, | |
1255 | .proc_handler = proc_dostring, | |
1256 | }, | |
1257 | { | |
1258 | .procname = "ctrl-alt-del", | |
1259 | .data = &C_A_D, | |
1260 | .maxlen = sizeof(int), | |
1261 | .mode = 0644, | |
1262 | .proc_handler = proc_dointvec, | |
1263 | }, | |
1264 | { } | |
1265 | }; | |
1266 | ||
1267 | static void __init kernel_reboot_sysctls_init(void) | |
1268 | { | |
1269 | register_sysctl_init("kernel", kern_reboot_table); | |
1270 | } | |
1271 | #else | |
1272 | #define kernel_reboot_sysctls_init() do { } while (0) | |
1273 | #endif /* CONFIG_SYSCTL */ | |
1274 | ||
2c622ed0 MC |
1275 | static const struct attribute_group reboot_attr_group = { |
1276 | .attrs = reboot_attrs, | |
1277 | }; | |
1278 | ||
1279 | static int __init reboot_ksysfs_init(void) | |
1280 | { | |
1281 | struct kobject *reboot_kobj; | |
1282 | int ret; | |
1283 | ||
1284 | reboot_kobj = kobject_create_and_add("reboot", kernel_kobj); | |
1285 | if (!reboot_kobj) | |
1286 | return -ENOMEM; | |
1287 | ||
1288 | ret = sysfs_create_group(reboot_kobj, &reboot_attr_group); | |
1289 | if (ret) { | |
1290 | kobject_put(reboot_kobj); | |
1291 | return ret; | |
1292 | } | |
1293 | ||
06d17766 | 1294 | kernel_reboot_sysctls_init(); |
1295 | ||
2c622ed0 MC |
1296 | return 0; |
1297 | } | |
1298 | late_initcall(reboot_ksysfs_init); | |
1299 | ||
1300 | #endif |