sysrq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* -*- linux-c -*-
  2. *
  3. * $Id: sysrq.c,v 1.15 1998/08/23 14:56:41 mj Exp $
  4. *
  5. * Linux Magic System Request Key Hacks
  6. *
  7. * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  8. * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
  9. *
  10. * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
  11. * overhauled to use key registration
  12. * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
  13. */
  14. #include <linux/config.h>
  15. #include <linux/sched.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/mm.h>
  18. #include <linux/fs.h>
  19. #include <linux/tty.h>
  20. #include <linux/mount.h>
  21. #include <linux/kdev_t.h>
  22. #include <linux/major.h>
  23. #include <linux/reboot.h>
  24. #include <linux/sysrq.h>
  25. #include <linux/kbd_kern.h>
  26. #include <linux/quotaops.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/suspend.h>
  31. #include <linux/writeback.h>
  32. #include <linux/buffer_head.h> /* for fsync_bdev() */
  33. #include <linux/swap.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/vt_kern.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/kexec.h>
  38. #include <asm/ptrace.h>
  39. /* Whether we react on sysrq keys or just ignore them */
  40. int sysrq_enabled = 1;
  41. /* Loglevel sysrq handler */
  42. static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs,
  43. struct tty_struct *tty)
  44. {
  45. int i;
  46. i = key - '0';
  47. console_loglevel = 7;
  48. printk("Loglevel set to %d\n", i);
  49. console_loglevel = i;
  50. }
  51. static struct sysrq_key_op sysrq_loglevel_op = {
  52. .handler = sysrq_handle_loglevel,
  53. .help_msg = "loglevel0-8",
  54. .action_msg = "Changing Loglevel",
  55. .enable_mask = SYSRQ_ENABLE_LOG,
  56. };
  57. /* SAK sysrq handler */
  58. #ifdef CONFIG_VT
  59. static void sysrq_handle_SAK(int key, struct pt_regs *pt_regs,
  60. struct tty_struct *tty)
  61. {
  62. if (tty)
  63. do_SAK(tty);
  64. reset_vc(vc_cons[fg_console].d);
  65. }
  66. static struct sysrq_key_op sysrq_SAK_op = {
  67. .handler = sysrq_handle_SAK,
  68. .help_msg = "saK",
  69. .action_msg = "SAK",
  70. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  71. };
  72. #endif
  73. #ifdef CONFIG_VT
  74. /* unraw sysrq handler */
  75. static void sysrq_handle_unraw(int key, struct pt_regs *pt_regs,
  76. struct tty_struct *tty)
  77. {
  78. struct kbd_struct *kbd = &kbd_table[fg_console];
  79. if (kbd)
  80. kbd->kbdmode = VC_XLATE;
  81. }
  82. static struct sysrq_key_op sysrq_unraw_op = {
  83. .handler = sysrq_handle_unraw,
  84. .help_msg = "unRaw",
  85. .action_msg = "Keyboard mode set to XLATE",
  86. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  87. };
  88. #endif /* CONFIG_VT */
  89. #ifdef CONFIG_KEXEC
  90. /* crashdump sysrq handler */
  91. static void sysrq_handle_crashdump(int key, struct pt_regs *pt_regs,
  92. struct tty_struct *tty)
  93. {
  94. crash_kexec(pt_regs);
  95. }
  96. static struct sysrq_key_op sysrq_crashdump_op = {
  97. .handler = sysrq_handle_crashdump,
  98. .help_msg = "Crashdump",
  99. .action_msg = "Trigger a crashdump",
  100. .enable_mask = SYSRQ_ENABLE_DUMP,
  101. };
  102. #endif
  103. /* reboot sysrq handler */
  104. static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs,
  105. struct tty_struct *tty)
  106. {
  107. local_irq_enable();
  108. emergency_restart();
  109. }
  110. static struct sysrq_key_op sysrq_reboot_op = {
  111. .handler = sysrq_handle_reboot,
  112. .help_msg = "reBoot",
  113. .action_msg = "Resetting",
  114. .enable_mask = SYSRQ_ENABLE_BOOT,
  115. };
  116. static void sysrq_handle_sync(int key, struct pt_regs *pt_regs,
  117. struct tty_struct *tty)
  118. {
  119. emergency_sync();
  120. }
  121. static struct sysrq_key_op sysrq_sync_op = {
  122. .handler = sysrq_handle_sync,
  123. .help_msg = "Sync",
  124. .action_msg = "Emergency Sync",
  125. .enable_mask = SYSRQ_ENABLE_SYNC,
  126. };
  127. static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs,
  128. struct tty_struct *tty)
  129. {
  130. emergency_remount();
  131. }
  132. static struct sysrq_key_op sysrq_mountro_op = {
  133. .handler = sysrq_handle_mountro,
  134. .help_msg = "Unmount",
  135. .action_msg = "Emergency Remount R/O",
  136. .enable_mask = SYSRQ_ENABLE_REMOUNT,
  137. };
  138. /* END SYNC SYSRQ HANDLERS BLOCK */
  139. /* SHOW SYSRQ HANDLERS BLOCK */
  140. static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
  141. struct tty_struct *tty)
  142. {
  143. if (pt_regs)
  144. show_regs(pt_regs);
  145. }
  146. static struct sysrq_key_op sysrq_showregs_op = {
  147. .handler = sysrq_handle_showregs,
  148. .help_msg = "showPc",
  149. .action_msg = "Show Regs",
  150. .enable_mask = SYSRQ_ENABLE_DUMP,
  151. };
  152. static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
  153. struct tty_struct *tty)
  154. {
  155. show_state();
  156. }
  157. static struct sysrq_key_op sysrq_showstate_op = {
  158. .handler = sysrq_handle_showstate,
  159. .help_msg = "showTasks",
  160. .action_msg = "Show State",
  161. .enable_mask = SYSRQ_ENABLE_DUMP,
  162. };
  163. static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
  164. struct tty_struct *tty)
  165. {
  166. show_mem();
  167. }
  168. static struct sysrq_key_op sysrq_showmem_op = {
  169. .handler = sysrq_handle_showmem,
  170. .help_msg = "showMem",
  171. .action_msg = "Show Memory",
  172. .enable_mask = SYSRQ_ENABLE_DUMP,
  173. };
  174. /* SHOW SYSRQ HANDLERS BLOCK */
  175. /* SIGNAL SYSRQ HANDLERS BLOCK */
  176. /* signal sysrq helper function
  177. * Sends a signal to all user processes */
  178. static void send_sig_all(int sig)
  179. {
  180. struct task_struct *p;
  181. for_each_process(p) {
  182. if (p->mm && p->pid != 1)
  183. /* Not swapper, init nor kernel thread */
  184. force_sig(sig, p);
  185. }
  186. }
  187. static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
  188. struct tty_struct *tty)
  189. {
  190. send_sig_all(SIGTERM);
  191. console_loglevel = 8;
  192. }
  193. static struct sysrq_key_op sysrq_term_op = {
  194. .handler = sysrq_handle_term,
  195. .help_msg = "tErm",
  196. .action_msg = "Terminate All Tasks",
  197. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  198. };
  199. static void moom_callback(void *ignored)
  200. {
  201. out_of_memory(GFP_KERNEL, 0);
  202. }
  203. static DECLARE_WORK(moom_work, moom_callback, NULL);
  204. static void sysrq_handle_moom(int key, struct pt_regs *pt_regs,
  205. struct tty_struct *tty)
  206. {
  207. schedule_work(&moom_work);
  208. }
  209. static struct sysrq_key_op sysrq_moom_op = {
  210. .handler = sysrq_handle_moom,
  211. .help_msg = "Full",
  212. .action_msg = "Manual OOM execution",
  213. };
  214. static void sysrq_handle_kill(int key, struct pt_regs *pt_regs,
  215. struct tty_struct *tty)
  216. {
  217. send_sig_all(SIGKILL);
  218. console_loglevel = 8;
  219. }
  220. static struct sysrq_key_op sysrq_kill_op = {
  221. .handler = sysrq_handle_kill,
  222. .help_msg = "kIll",
  223. .action_msg = "Kill All Tasks",
  224. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  225. };
  226. /* END SIGNAL SYSRQ HANDLERS BLOCK */
  227. static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
  228. struct tty_struct *tty)
  229. {
  230. normalize_rt_tasks();
  231. }
  232. static struct sysrq_key_op sysrq_unrt_op = {
  233. .handler = sysrq_handle_unrt,
  234. .help_msg = "Nice",
  235. .action_msg = "Nice All RT Tasks",
  236. .enable_mask = SYSRQ_ENABLE_RTNICE,
  237. };
  238. /* Key Operations table and lock */
  239. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  240. #define SYSRQ_KEY_TABLE_LENGTH 36
  241. static struct sysrq_key_op *sysrq_key_table[SYSRQ_KEY_TABLE_LENGTH] = {
  242. /* 0 */ &sysrq_loglevel_op,
  243. /* 1 */ &sysrq_loglevel_op,
  244. /* 2 */ &sysrq_loglevel_op,
  245. /* 3 */ &sysrq_loglevel_op,
  246. /* 4 */ &sysrq_loglevel_op,
  247. /* 5 */ &sysrq_loglevel_op,
  248. /* 6 */ &sysrq_loglevel_op,
  249. /* 7 */ &sysrq_loglevel_op,
  250. /* 8 */ &sysrq_loglevel_op,
  251. /* 9 */ &sysrq_loglevel_op,
  252. /* a */ NULL, /* Don't use for system provided sysrqs,
  253. it is handled specially on the sparc
  254. and will never arrive */
  255. /* b */ &sysrq_reboot_op,
  256. #ifdef CONFIG_KEXEC
  257. /* c */ &sysrq_crashdump_op,
  258. #else
  259. /* c */ NULL,
  260. #endif
  261. /* d */ NULL,
  262. /* e */ &sysrq_term_op,
  263. /* f */ &sysrq_moom_op,
  264. /* g */ NULL,
  265. /* h */ NULL,
  266. /* i */ &sysrq_kill_op,
  267. /* j */ NULL,
  268. #ifdef CONFIG_VT
  269. /* k */ &sysrq_SAK_op,
  270. #else
  271. /* k */ NULL,
  272. #endif
  273. /* l */ NULL,
  274. /* m */ &sysrq_showmem_op,
  275. /* n */ &sysrq_unrt_op,
  276. /* o */ NULL, /* This will often be registered
  277. as 'Off' at init time */
  278. /* p */ &sysrq_showregs_op,
  279. /* q */ NULL,
  280. #ifdef CONFIG_VT
  281. /* r */ &sysrq_unraw_op,
  282. #else
  283. /* r */ NULL,
  284. #endif
  285. /* s */ &sysrq_sync_op,
  286. /* t */ &sysrq_showstate_op,
  287. /* u */ &sysrq_mountro_op,
  288. /* v */ NULL, /* May be assigned at init time by SMP VOYAGER */
  289. /* w */ NULL,
  290. /* x */ NULL,
  291. /* y */ NULL,
  292. /* z */ NULL
  293. };
  294. /* key2index calculation, -1 on invalid index */
  295. static int sysrq_key_table_key2index(int key) {
  296. int retval;
  297. if ((key >= '0') && (key <= '9')) {
  298. retval = key - '0';
  299. } else if ((key >= 'a') && (key <= 'z')) {
  300. retval = key + 10 - 'a';
  301. } else {
  302. retval = -1;
  303. }
  304. return retval;
  305. }
  306. /*
  307. * get and put functions for the table, exposed to modules.
  308. */
  309. struct sysrq_key_op *__sysrq_get_key_op (int key) {
  310. struct sysrq_key_op *op_p;
  311. int i;
  312. i = sysrq_key_table_key2index(key);
  313. op_p = (i == -1) ? NULL : sysrq_key_table[i];
  314. return op_p;
  315. }
  316. void __sysrq_put_key_op (int key, struct sysrq_key_op *op_p) {
  317. int i;
  318. i = sysrq_key_table_key2index(key);
  319. if (i != -1)
  320. sysrq_key_table[i] = op_p;
  321. }
  322. /*
  323. * This is the non-locking version of handle_sysrq
  324. * It must/can only be called by sysrq key handlers,
  325. * as they are inside of the lock
  326. */
  327. void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty, int check_mask)
  328. {
  329. struct sysrq_key_op *op_p;
  330. int orig_log_level;
  331. int i, j;
  332. unsigned long flags;
  333. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  334. orig_log_level = console_loglevel;
  335. console_loglevel = 7;
  336. printk(KERN_INFO "SysRq : ");
  337. op_p = __sysrq_get_key_op(key);
  338. if (op_p) {
  339. /* Should we check for enabled operations (/proc/sysrq-trigger should not)
  340. * and is the invoked operation enabled? */
  341. if (!check_mask || sysrq_enabled == 1 ||
  342. (sysrq_enabled & op_p->enable_mask)) {
  343. printk ("%s\n", op_p->action_msg);
  344. console_loglevel = orig_log_level;
  345. op_p->handler(key, pt_regs, tty);
  346. }
  347. else
  348. printk("This sysrq operation is disabled.\n");
  349. } else {
  350. printk("HELP : ");
  351. /* Only print the help msg once per handler */
  352. for (i=0; i<SYSRQ_KEY_TABLE_LENGTH; i++)
  353. if (sysrq_key_table[i]) {
  354. for (j=0; sysrq_key_table[i] != sysrq_key_table[j]; j++);
  355. if (j == i)
  356. printk ("%s ", sysrq_key_table[i]->help_msg);
  357. }
  358. printk ("\n");
  359. console_loglevel = orig_log_level;
  360. }
  361. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  362. }
  363. /*
  364. * This function is called by the keyboard handler when SysRq is pressed
  365. * and any other keycode arrives.
  366. */
  367. void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
  368. {
  369. if (!sysrq_enabled)
  370. return;
  371. __handle_sysrq(key, pt_regs, tty, 1);
  372. }
  373. int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  374. struct sysrq_key_op *remove_op_p) {
  375. int retval;
  376. unsigned long flags;
  377. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  378. if (__sysrq_get_key_op(key) == remove_op_p) {
  379. __sysrq_put_key_op(key, insert_op_p);
  380. retval = 0;
  381. } else {
  382. retval = -1;
  383. }
  384. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  385. return retval;
  386. }
  387. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  388. {
  389. return __sysrq_swap_key_ops(key, op_p, NULL);
  390. }
  391. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  392. {
  393. return __sysrq_swap_key_ops(key, NULL, op_p);
  394. }
  395. EXPORT_SYMBOL(handle_sysrq);
  396. EXPORT_SYMBOL(register_sysrq_key);
  397. EXPORT_SYMBOL(unregister_sysrq_key);