reboot.c 9.2 KB

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