reboot.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. static void migrate_to_reboot_cpu(void)
  95. {
  96. /* The boot cpu is always logical cpu 0 */
  97. int cpu = reboot_cpu;
  98. cpu_hotplug_disable();
  99. /* Make certain the cpu I'm about to reboot on is online */
  100. if (!cpu_online(cpu))
  101. cpu = cpumask_first(cpu_online_mask);
  102. /* Prevent races with other tasks migrating this task */
  103. current->flags |= PF_NO_SETAFFINITY;
  104. /* Make certain I only run on the appropriate processor */
  105. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  106. }
  107. /**
  108. * kernel_restart - reboot the system
  109. * @cmd: pointer to buffer containing command to execute for restart
  110. * or %NULL
  111. *
  112. * Shutdown everything and perform a clean reboot.
  113. * This is not safe to call in interrupt context.
  114. */
  115. void kernel_restart(char *cmd)
  116. {
  117. kernel_restart_prepare(cmd);
  118. migrate_to_reboot_cpu();
  119. syscore_shutdown();
  120. if (!cmd)
  121. pr_emerg("Restarting system\n");
  122. else
  123. pr_emerg("Restarting system with command '%s'\n", cmd);
  124. kmsg_dump(KMSG_DUMP_RESTART);
  125. machine_restart(cmd);
  126. }
  127. EXPORT_SYMBOL_GPL(kernel_restart);
  128. static void kernel_shutdown_prepare(enum system_states state)
  129. {
  130. blocking_notifier_call_chain(&reboot_notifier_list,
  131. (state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
  132. system_state = state;
  133. usermodehelper_disable();
  134. device_shutdown();
  135. }
  136. /**
  137. * kernel_halt - halt the system
  138. *
  139. * Shutdown everything and perform a clean system halt.
  140. */
  141. void kernel_halt(void)
  142. {
  143. kernel_shutdown_prepare(SYSTEM_HALT);
  144. migrate_to_reboot_cpu();
  145. syscore_shutdown();
  146. pr_emerg("System halted\n");
  147. kmsg_dump(KMSG_DUMP_HALT);
  148. machine_halt();
  149. }
  150. EXPORT_SYMBOL_GPL(kernel_halt);
  151. /**
  152. * kernel_power_off - power_off the system
  153. *
  154. * Shutdown everything and perform a clean system power_off.
  155. */
  156. void kernel_power_off(void)
  157. {
  158. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  159. if (pm_power_off_prepare)
  160. pm_power_off_prepare();
  161. migrate_to_reboot_cpu();
  162. syscore_shutdown();
  163. pr_emerg("Power down\n");
  164. kmsg_dump(KMSG_DUMP_POWEROFF);
  165. machine_power_off();
  166. }
  167. EXPORT_SYMBOL_GPL(kernel_power_off);
  168. static DEFINE_MUTEX(reboot_mutex);
  169. /*
  170. * Reboot system call: for obvious reasons only root may call it,
  171. * and even root needs to set up some magic numbers in the registers
  172. * so that some mistake won't make this reboot the whole machine.
  173. * You can also set the meaning of the ctrl-alt-del-key here.
  174. *
  175. * reboot doesn't sync: do that yourself before calling this.
  176. */
  177. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  178. void __user *, arg)
  179. {
  180. struct pid_namespace *pid_ns = task_active_pid_ns(current);
  181. char buffer[256];
  182. int ret = 0;
  183. /* We only trust the superuser with rebooting the system. */
  184. if (!ns_capable(pid_ns->user_ns, CAP_SYS_BOOT))
  185. return -EPERM;
  186. /* For safety, we require "magic" arguments. */
  187. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  188. (magic2 != LINUX_REBOOT_MAGIC2 &&
  189. magic2 != LINUX_REBOOT_MAGIC2A &&
  190. magic2 != LINUX_REBOOT_MAGIC2B &&
  191. magic2 != LINUX_REBOOT_MAGIC2C))
  192. return -EINVAL;
  193. /*
  194. * If pid namespaces are enabled and the current task is in a child
  195. * pid_namespace, the command is handled by reboot_pid_ns() which will
  196. * call do_exit().
  197. */
  198. ret = reboot_pid_ns(pid_ns, cmd);
  199. if (ret)
  200. return ret;
  201. /* Instead of trying to make the power_off code look like
  202. * halt when pm_power_off is not set do it the easy way.
  203. */
  204. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  205. cmd = LINUX_REBOOT_CMD_HALT;
  206. mutex_lock(&reboot_mutex);
  207. switch (cmd) {
  208. case LINUX_REBOOT_CMD_RESTART:
  209. kernel_restart(NULL);
  210. break;
  211. case LINUX_REBOOT_CMD_CAD_ON:
  212. C_A_D = 1;
  213. break;
  214. case LINUX_REBOOT_CMD_CAD_OFF:
  215. C_A_D = 0;
  216. break;
  217. case LINUX_REBOOT_CMD_HALT:
  218. kernel_halt();
  219. do_exit(0);
  220. panic("cannot halt");
  221. case LINUX_REBOOT_CMD_POWER_OFF:
  222. kernel_power_off();
  223. do_exit(0);
  224. break;
  225. case LINUX_REBOOT_CMD_RESTART2:
  226. ret = strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1);
  227. if (ret < 0) {
  228. ret = -EFAULT;
  229. break;
  230. }
  231. buffer[sizeof(buffer) - 1] = '\0';
  232. kernel_restart(buffer);
  233. break;
  234. #ifdef CONFIG_KEXEC
  235. case LINUX_REBOOT_CMD_KEXEC:
  236. ret = kernel_kexec();
  237. break;
  238. #endif
  239. #ifdef CONFIG_HIBERNATION
  240. case LINUX_REBOOT_CMD_SW_SUSPEND:
  241. ret = hibernate();
  242. break;
  243. #endif
  244. default:
  245. ret = -EINVAL;
  246. break;
  247. }
  248. mutex_unlock(&reboot_mutex);
  249. return ret;
  250. }
  251. static void deferred_cad(struct work_struct *dummy)
  252. {
  253. kernel_restart(NULL);
  254. }
  255. /*
  256. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  257. * As it's called within an interrupt, it may NOT sync: the only choice
  258. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  259. */
  260. void ctrl_alt_del(void)
  261. {
  262. static DECLARE_WORK(cad_work, deferred_cad);
  263. if (C_A_D)
  264. schedule_work(&cad_work);
  265. else
  266. kill_cad_pid(SIGINT, 1);
  267. }
  268. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  269. static int __orderly_poweroff(bool force)
  270. {
  271. char **argv;
  272. static char *envp[] = {
  273. "HOME=/",
  274. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  275. NULL
  276. };
  277. int ret;
  278. argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
  279. if (argv) {
  280. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  281. argv_free(argv);
  282. } else {
  283. ret = -ENOMEM;
  284. }
  285. if (ret && force) {
  286. pr_warn("Failed to start orderly shutdown: forcing the issue\n");
  287. /*
  288. * I guess this should try to kick off some daemon to sync and
  289. * poweroff asap. Or not even bother syncing if we're doing an
  290. * emergency shutdown?
  291. */
  292. emergency_sync();
  293. kernel_power_off();
  294. }
  295. return ret;
  296. }
  297. static bool poweroff_force;
  298. static void poweroff_work_func(struct work_struct *work)
  299. {
  300. __orderly_poweroff(poweroff_force);
  301. }
  302. static DECLARE_WORK(poweroff_work, poweroff_work_func);
  303. /**
  304. * orderly_poweroff - Trigger an orderly system poweroff
  305. * @force: force poweroff if command execution fails
  306. *
  307. * This may be called from any context to trigger a system shutdown.
  308. * If the orderly shutdown fails, it will force an immediate shutdown.
  309. */
  310. int orderly_poweroff(bool force)
  311. {
  312. if (force) /* do not override the pending "true" */
  313. poweroff_force = true;
  314. schedule_work(&poweroff_work);
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(orderly_poweroff);
  318. static int __init reboot_setup(char *str)
  319. {
  320. for (;;) {
  321. /*
  322. * Having anything passed on the command line via
  323. * reboot= will cause us to disable DMI checking
  324. * below.
  325. */
  326. reboot_default = 0;
  327. switch (*str) {
  328. case 'w':
  329. reboot_mode = REBOOT_WARM;
  330. break;
  331. case 'c':
  332. reboot_mode = REBOOT_COLD;
  333. break;
  334. case 'h':
  335. reboot_mode = REBOOT_HARD;
  336. break;
  337. case 's':
  338. if (isdigit(*(str+1)))
  339. reboot_cpu = simple_strtoul(str+1, NULL, 0);
  340. else if (str[1] == 'm' && str[2] == 'p' &&
  341. isdigit(*(str+3)))
  342. reboot_cpu = simple_strtoul(str+3, NULL, 0);
  343. else
  344. reboot_mode = REBOOT_SOFT;
  345. break;
  346. case 'g':
  347. reboot_mode = REBOOT_GPIO;
  348. break;
  349. case 'b':
  350. case 'a':
  351. case 'k':
  352. case 't':
  353. case 'e':
  354. case 'p':
  355. reboot_type = *str;
  356. break;
  357. case 'f':
  358. reboot_force = 1;
  359. break;
  360. }
  361. str = strchr(str, ',');
  362. if (str)
  363. str++;
  364. else
  365. break;
  366. }
  367. return 1;
  368. }
  369. __setup("reboot=", reboot_setup);