reboot.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * linux/kernel/reboot.c
  3. *
  4. * Copyright (C) 2013 Linus Torvalds
  5. */
  6. #define pr_fmt(fmt) "reboot: " fmt
  7. #include <linux/ctype.h>
  8. #include <linux/export.h>
  9. #include <linux/kexec.h>
  10. #include <linux/kmod.h>
  11. #include <linux/kmsg_dump.h>
  12. #include <linux/reboot.h>
  13. #include <linux/suspend.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/syscore_ops.h>
  16. #include <linux/uaccess.h>
  17. /*
  18. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  19. */
  20. int C_A_D = 1;
  21. struct pid *cad_pid;
  22. EXPORT_SYMBOL(cad_pid);
  23. #if defined(CONFIG_ARM) || defined(CONFIG_UNICORE32)
  24. #define DEFAULT_REBOOT_MODE = REBOOT_HARD
  25. #else
  26. #define DEFAULT_REBOOT_MODE
  27. #endif
  28. enum reboot_mode reboot_mode DEFAULT_REBOOT_MODE;
  29. /*
  30. * This variable is used privately to keep track of whether or not
  31. * reboot_type is still set to its default value (i.e., reboot= hasn't
  32. * been set on the command line). This is needed so that we can
  33. * suppress DMI scanning for reboot quirks. Without it, it's
  34. * impossible to override a faulty reboot quirk without recompiling.
  35. */
  36. int reboot_default = 1;
  37. int reboot_cpu;
  38. enum reboot_type reboot_type = BOOT_ACPI;
  39. int reboot_force;
  40. /*
  41. * If set, this is used for preparing the system to power off.
  42. */
  43. void (*pm_power_off_prepare)(void);
  44. /**
  45. * emergency_restart - reboot the system
  46. *
  47. * Without shutting down any hardware or taking any locks
  48. * reboot the system. This is called when we know we are in
  49. * trouble so this is our best effort to reboot. This is
  50. * safe to call in interrupt context.
  51. */
  52. void emergency_restart(void)
  53. {
  54. kmsg_dump(KMSG_DUMP_EMERG);
  55. machine_emergency_restart();
  56. }
  57. EXPORT_SYMBOL_GPL(emergency_restart);
  58. void kernel_restart_prepare(char *cmd)
  59. {
  60. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  61. system_state = SYSTEM_RESTART;
  62. usermodehelper_disable();
  63. device_shutdown();
  64. }
  65. /**
  66. * register_reboot_notifier - Register function to be called at reboot time
  67. * @nb: Info about notifier function to be called
  68. *
  69. * Registers a function with the list of functions
  70. * to be called at reboot time.
  71. *
  72. * Currently always returns zero, as blocking_notifier_chain_register()
  73. * always returns zero.
  74. */
  75. int register_reboot_notifier(struct notifier_block *nb)
  76. {
  77. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  78. }
  79. EXPORT_SYMBOL(register_reboot_notifier);
  80. /**
  81. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  82. * @nb: Hook to be unregistered
  83. *
  84. * Unregisters a previously registered reboot
  85. * notifier function.
  86. *
  87. * Returns zero on success, or %-ENOENT on failure.
  88. */
  89. int unregister_reboot_notifier(struct notifier_block *nb)
  90. {
  91. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  92. }
  93. EXPORT_SYMBOL(unregister_reboot_notifier);
  94. /*
  95. * Notifier list for kernel code which wants to be called
  96. * to restart the system.
  97. */
  98. static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
  99. /**
  100. * register_restart_handler - Register function to be called to reset
  101. * the system
  102. * @nb: Info about handler function to be called
  103. * @nb->priority: Handler priority. Handlers should follow the
  104. * following guidelines for setting priorities.
  105. * 0: Restart handler of last resort,
  106. * with limited restart capabilities
  107. * 128: Default restart handler; use if no other
  108. * restart handler is expected to be available,
  109. * and/or if restart functionality is
  110. * sufficient to restart the entire system
  111. * 255: Highest priority restart handler, will
  112. * preempt all other restart handlers
  113. *
  114. * Registers a function with code to be called to restart the
  115. * system.
  116. *
  117. * Registered functions will be called from machine_restart as last
  118. * step of the restart sequence (if the architecture specific
  119. * machine_restart function calls do_kernel_restart - see below
  120. * for details).
  121. * Registered functions are expected to restart the system immediately.
  122. * If more than one function is registered, the restart handler priority
  123. * selects which function will be called first.
  124. *
  125. * Restart handlers are expected to be registered from non-architecture
  126. * code, typically from drivers. A typical use case would be a system
  127. * where restart functionality is provided through a watchdog. Multiple
  128. * restart handlers may exist; for example, one restart handler might
  129. * restart the entire system, while another only restarts the CPU.
  130. * In such cases, the restart handler which only restarts part of the
  131. * hardware is expected to register with low priority to ensure that
  132. * it only runs if no other means to restart the system is available.
  133. *
  134. * Currently always returns zero, as atomic_notifier_chain_register()
  135. * always returns zero.
  136. */
  137. int register_restart_handler(struct notifier_block *nb)
  138. {
  139. return atomic_notifier_chain_register(&restart_handler_list, nb);
  140. }
  141. EXPORT_SYMBOL(register_restart_handler);
  142. /**
  143. * unregister_restart_handler - Unregister previously registered
  144. * restart handler
  145. * @nb: Hook to be unregistered
  146. *
  147. * Unregisters a previously registered restart handler function.
  148. *
  149. * Returns zero on success, or %-ENOENT on failure.
  150. */
  151. int unregister_restart_handler(struct notifier_block *nb)
  152. {
  153. return atomic_notifier_chain_unregister(&restart_handler_list, nb);
  154. }
  155. EXPORT_SYMBOL(unregister_restart_handler);
  156. /**
  157. * do_kernel_restart - Execute kernel restart handler call chain
  158. *
  159. * Calls functions registered with register_restart_handler.
  160. *
  161. * Expected to be called from machine_restart as last step of the restart
  162. * sequence.
  163. *
  164. * Restarts the system immediately if a restart handler function has been
  165. * registered. Otherwise does nothing.
  166. */
  167. void do_kernel_restart(char *cmd)
  168. {
  169. atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
  170. }
  171. void migrate_to_reboot_cpu(void)
  172. {
  173. /* The boot cpu is always logical cpu 0 */
  174. int cpu = reboot_cpu;
  175. cpu_hotplug_disable();
  176. /* Make certain the cpu I'm about to reboot on is online */
  177. if (!cpu_online(cpu))
  178. cpu = cpumask_first(cpu_online_mask);
  179. /* Prevent races with other tasks migrating this task */
  180. current->flags |= PF_NO_SETAFFINITY;
  181. /* Make certain I only run on the appropriate processor */
  182. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  183. }
  184. /**
  185. * kernel_restart - reboot the system
  186. * @cmd: pointer to buffer containing command to execute for restart
  187. * or %NULL
  188. *
  189. * Shutdown everything and perform a clean reboot.
  190. * This is not safe to call in interrupt context.
  191. */
  192. void kernel_restart(char *cmd)
  193. {
  194. kernel_restart_prepare(cmd);
  195. migrate_to_reboot_cpu();
  196. syscore_shutdown();
  197. if (!cmd)
  198. pr_emerg("Restarting system\n");
  199. else
  200. pr_emerg("Restarting system with command '%s'\n", cmd);
  201. kmsg_dump(KMSG_DUMP_RESTART);
  202. machine_restart(cmd);
  203. }
  204. EXPORT_SYMBOL_GPL(kernel_restart);
  205. static void kernel_shutdown_prepare(enum system_states state)
  206. {
  207. blocking_notifier_call_chain(&reboot_notifier_list,
  208. (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
  209. system_state = state;
  210. usermodehelper_disable();
  211. device_shutdown();
  212. }
  213. /**
  214. * kernel_halt - halt the system
  215. *
  216. * Shutdown everything and perform a clean system halt.
  217. */
  218. void kernel_halt(void)
  219. {
  220. kernel_shutdown_prepare(SYSTEM_HALT);
  221. migrate_to_reboot_cpu();
  222. syscore_shutdown();
  223. pr_emerg("System halted\n");
  224. kmsg_dump(KMSG_DUMP_HALT);
  225. machine_halt();
  226. }
  227. EXPORT_SYMBOL_GPL(kernel_halt);
  228. /**
  229. * kernel_power_off - power_off the system
  230. *
  231. * Shutdown everything and perform a clean system power_off.
  232. */
  233. void kernel_power_off(void)
  234. {
  235. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  236. if (pm_power_off_prepare)
  237. pm_power_off_prepare();
  238. migrate_to_reboot_cpu();
  239. syscore_shutdown();
  240. pr_emerg("Power down\n");
  241. kmsg_dump(KMSG_DUMP_POWEROFF);
  242. machine_power_off();
  243. }
  244. EXPORT_SYMBOL_GPL(kernel_power_off);
  245. static DEFINE_MUTEX(reboot_mutex);
  246. /*
  247. * Reboot system call: for obvious reasons only root may call it,
  248. * and even root needs to set up some magic numbers in the registers
  249. * so that some mistake won't make this reboot the whole machine.
  250. * You can also set the meaning of the ctrl-alt-del-key here.
  251. *
  252. * reboot doesn't sync: do that yourself before calling this.
  253. */
  254. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  255. void __user *, arg)
  256. {
  257. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  258. char buffer[256];
  259. int ret = 0;
  260. /* We only trust the superuser with rebooting the system. */
  261. if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
  262. return -EPERM;
  263. /* For safety, we require "magic" arguments. */
  264. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  265. (magic2 != LINUX_REBOOT_MAGIC2 &&
  266. magic2 != LINUX_REBOOT_MAGIC2A &&
  267. magic2 != LINUX_REBOOT_MAGIC2B &&
  268. magic2 != LINUX_REBOOT_MAGIC2C))
  269. return -EINVAL;
  270. /*
  271. * If pid namespaces are enabled and the current task is in a child
  272. * pid_namespace, the command is handled by reboot_pid_ns() which will
  273. * call do_exit().
  274. */
  275. ret = reboot_pid_ns(pid_ns, cmd);
  276. if (ret)
  277. return ret;
  278. /* Instead of trying to make the power_off code look like
  279. * halt when pm_power_off is not set do it the easy way.
  280. */
  281. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  282. cmd = LINUX_REBOOT_CMD_HALT;
  283. mutex_lock(&reboot_mutex);
  284. switch (cmd) {
  285. case LINUX_REBOOT_CMD_RESTART:
  286. kernel_restart(NULL);
  287. break;
  288. case LINUX_REBOOT_CMD_CAD_ON:
  289. C_A_D = 1;
  290. break;
  291. case LINUX_REBOOT_CMD_CAD_OFF:
  292. C_A_D = 0;
  293. break;
  294. case LINUX_REBOOT_CMD_HALT:
  295. kernel_halt();
  296. do_exit(0);
  297. panic("cannot halt");
  298. case LINUX_REBOOT_CMD_POWER_OFF:
  299. kernel_power_off();
  300. do_exit(0);
  301. break;
  302. case LINUX_REBOOT_CMD_RESTART2:
  303. ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
  304. if (ret < 0) {
  305. ret = -EFAULT;
  306. break;
  307. }
  308. buffer[sizeof(buffer) - 1] = '\0';
  309. kernel_restart(buffer);
  310. break;
  311. #ifdef CONFIG_KEXEC_CORE
  312. case LINUX_REBOOT_CMD_KEXEC:
  313. ret = kernel_kexec();
  314. break;
  315. #endif
  316. #ifdef CONFIG_HIBERNATION
  317. case LINUX_REBOOT_CMD_SW_SUSPEND:
  318. ret = hibernate();
  319. break;
  320. #endif
  321. default:
  322. ret = -EINVAL;
  323. break;
  324. }
  325. mutex_unlock(&reboot_mutex);
  326. return ret;
  327. }
  328. static void deferred_cad(struct work_struct *dummy)
  329. {
  330. kernel_restart(NULL);
  331. }
  332. /*
  333. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  334. * As it's called within an interrupt, it may NOT sync: the only choice
  335. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  336. */
  337. void ctrl_alt_del(void)
  338. {
  339. static DECLARE_WORK(cad_work, deferred_cad);
  340. if (C_A_D)
  341. schedule_work(&cad_work);
  342. else
  343. kill_cad_pid(SIGINT, 1);
  344. }
  345. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  346. static const char reboot_cmd[] = "/sbin/reboot";
  347. static int run_cmd(const char *cmd)
  348. {
  349. char **argv;
  350. static char *envp[] = {
  351. "HOME=/",
  352. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  353. NULL
  354. };
  355. int ret;
  356. argv = argv_split(GFP_KERNEL, cmd, NULL);
  357. if (argv) {
  358. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  359. argv_free(argv);
  360. } else {
  361. ret = -ENOMEM;
  362. }
  363. return ret;
  364. }
  365. static int __orderly_reboot(void)
  366. {
  367. int ret;
  368. ret = run_cmd(reboot_cmd);
  369. if (ret) {
  370. pr_warn("Failed to start orderly reboot: forcing the issue\n");
  371. emergency_sync();
  372. kernel_restart(NULL);
  373. }
  374. return ret;
  375. }
  376. static int __orderly_poweroff(bool force)
  377. {
  378. int ret;
  379. ret = run_cmd(poweroff_cmd);
  380. if (ret && force) {
  381. pr_warn("Failed to start orderly shutdown: forcing the issue\n");
  382. /*
  383. * I guess this should try to kick off some daemon to sync and
  384. * poweroff asap. Or not even bother syncing if we're doing an
  385. * emergency shutdown?
  386. */
  387. emergency_sync();
  388. kernel_power_off();
  389. }
  390. return ret;
  391. }
  392. static bool poweroff_force;
  393. static void poweroff_work_func(struct work_struct *work)
  394. {
  395. __orderly_poweroff(poweroff_force);
  396. }
  397. static DECLARE_WORK(poweroff_work, poweroff_work_func);
  398. /**
  399. * orderly_poweroff - Trigger an orderly system poweroff
  400. * @force: force poweroff if command execution fails
  401. *
  402. * This may be called from any context to trigger a system shutdown.
  403. * If the orderly shutdown fails, it will force an immediate shutdown.
  404. */
  405. void orderly_poweroff(bool force)
  406. {
  407. if (force) /* do not override the pending "true" */
  408. poweroff_force = true;
  409. schedule_work(&poweroff_work);
  410. }
  411. EXPORT_SYMBOL_GPL(orderly_poweroff);
  412. static void reboot_work_func(struct work_struct *work)
  413. {
  414. __orderly_reboot();
  415. }
  416. static DECLARE_WORK(reboot_work, reboot_work_func);
  417. /**
  418. * orderly_reboot - Trigger an orderly system reboot
  419. *
  420. * This may be called from any context to trigger a system reboot.
  421. * If the orderly reboot fails, it will force an immediate reboot.
  422. */
  423. void orderly_reboot(void)
  424. {
  425. schedule_work(&reboot_work);
  426. }
  427. EXPORT_SYMBOL_GPL(orderly_reboot);
  428. static int __init reboot_setup(char *str)
  429. {
  430. for (;;) {
  431. /*
  432. * Having anything passed on the command line via
  433. * reboot= will cause us to disable DMI checking
  434. * below.
  435. */
  436. reboot_default = 0;
  437. switch (*str) {
  438. case 'w':
  439. reboot_mode = REBOOT_WARM;
  440. break;
  441. case 'c':
  442. reboot_mode = REBOOT_COLD;
  443. break;
  444. case 'h':
  445. reboot_mode = REBOOT_HARD;
  446. break;
  447. case 's':
  448. {
  449. int rc;
  450. if (isdigit(*(str+1))) {
  451. rc = kstrtoint(str+1, 0, &reboot_cpu);
  452. if (rc)
  453. return rc;
  454. } else if (str[1] == 'm' && str[2] == 'p' &&
  455. isdigit(*(str+3))) {
  456. rc = kstrtoint(str+3, 0, &reboot_cpu);
  457. if (rc)
  458. return rc;
  459. } else
  460. reboot_mode = REBOOT_SOFT;
  461. break;
  462. }
  463. case 'g':
  464. reboot_mode = REBOOT_GPIO;
  465. break;
  466. case 'b':
  467. case 'a':
  468. case 'k':
  469. case 't':
  470. case 'e':
  471. case 'p':
  472. reboot_type = *str;
  473. break;
  474. case 'f':
  475. reboot_force = 1;
  476. break;
  477. }
  478. str = strchr(str, ',');
  479. if (str)
  480. str++;
  481. else
  482. break;
  483. }
  484. return 1;
  485. }
  486. __setup("reboot=", reboot_setup);