sysrq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #ifdef CONFIG_DEBUG_MUTEXES
  140. static void
  141. sysrq_handle_showlocks(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
  142. {
  143. mutex_debug_show_all_locks();
  144. }
  145. static struct sysrq_key_op sysrq_showlocks_op = {
  146. .handler = sysrq_handle_showlocks,
  147. .help_msg = "show-all-locks(D)",
  148. .action_msg = "Show Locks Held",
  149. };
  150. #endif
  151. /* SHOW SYSRQ HANDLERS BLOCK */
  152. static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
  153. struct tty_struct *tty)
  154. {
  155. if (pt_regs)
  156. show_regs(pt_regs);
  157. }
  158. static struct sysrq_key_op sysrq_showregs_op = {
  159. .handler = sysrq_handle_showregs,
  160. .help_msg = "showPc",
  161. .action_msg = "Show Regs",
  162. .enable_mask = SYSRQ_ENABLE_DUMP,
  163. };
  164. static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
  165. struct tty_struct *tty)
  166. {
  167. show_state();
  168. }
  169. static struct sysrq_key_op sysrq_showstate_op = {
  170. .handler = sysrq_handle_showstate,
  171. .help_msg = "showTasks",
  172. .action_msg = "Show State",
  173. .enable_mask = SYSRQ_ENABLE_DUMP,
  174. };
  175. static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
  176. struct tty_struct *tty)
  177. {
  178. show_mem();
  179. }
  180. static struct sysrq_key_op sysrq_showmem_op = {
  181. .handler = sysrq_handle_showmem,
  182. .help_msg = "showMem",
  183. .action_msg = "Show Memory",
  184. .enable_mask = SYSRQ_ENABLE_DUMP,
  185. };
  186. /* SHOW SYSRQ HANDLERS BLOCK */
  187. /* SIGNAL SYSRQ HANDLERS BLOCK */
  188. /* signal sysrq helper function
  189. * Sends a signal to all user processes */
  190. static void send_sig_all(int sig)
  191. {
  192. struct task_struct *p;
  193. for_each_process(p) {
  194. if (p->mm && p->pid != 1)
  195. /* Not swapper, init nor kernel thread */
  196. force_sig(sig, p);
  197. }
  198. }
  199. static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
  200. struct tty_struct *tty)
  201. {
  202. send_sig_all(SIGTERM);
  203. console_loglevel = 8;
  204. }
  205. static struct sysrq_key_op sysrq_term_op = {
  206. .handler = sysrq_handle_term,
  207. .help_msg = "tErm",
  208. .action_msg = "Terminate All Tasks",
  209. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  210. };
  211. static void moom_callback(void *ignored)
  212. {
  213. out_of_memory(GFP_KERNEL, 0);
  214. }
  215. static DECLARE_WORK(moom_work, moom_callback, NULL);
  216. static void sysrq_handle_moom(int key, struct pt_regs *pt_regs,
  217. struct tty_struct *tty)
  218. {
  219. schedule_work(&moom_work);
  220. }
  221. static struct sysrq_key_op sysrq_moom_op = {
  222. .handler = sysrq_handle_moom,
  223. .help_msg = "Full",
  224. .action_msg = "Manual OOM execution",
  225. };
  226. static void sysrq_handle_kill(int key, struct pt_regs *pt_regs,
  227. struct tty_struct *tty)
  228. {
  229. send_sig_all(SIGKILL);
  230. console_loglevel = 8;
  231. }
  232. static struct sysrq_key_op sysrq_kill_op = {
  233. .handler = sysrq_handle_kill,
  234. .help_msg = "kIll",
  235. .action_msg = "Kill All Tasks",
  236. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  237. };
  238. /* END SIGNAL SYSRQ HANDLERS BLOCK */
  239. static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
  240. struct tty_struct *tty)
  241. {
  242. normalize_rt_tasks();
  243. }
  244. static struct sysrq_key_op sysrq_unrt_op = {
  245. .handler = sysrq_handle_unrt,
  246. .help_msg = "Nice",
  247. .action_msg = "Nice All RT Tasks",
  248. .enable_mask = SYSRQ_ENABLE_RTNICE,
  249. };
  250. /* Key Operations table and lock */
  251. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  252. #define SYSRQ_KEY_TABLE_LENGTH 36
  253. static struct sysrq_key_op *sysrq_key_table[SYSRQ_KEY_TABLE_LENGTH] = {
  254. /* 0 */ &sysrq_loglevel_op,
  255. /* 1 */ &sysrq_loglevel_op,
  256. /* 2 */ &sysrq_loglevel_op,
  257. /* 3 */ &sysrq_loglevel_op,
  258. /* 4 */ &sysrq_loglevel_op,
  259. /* 5 */ &sysrq_loglevel_op,
  260. /* 6 */ &sysrq_loglevel_op,
  261. /* 7 */ &sysrq_loglevel_op,
  262. /* 8 */ &sysrq_loglevel_op,
  263. /* 9 */ &sysrq_loglevel_op,
  264. /* a */ NULL, /* Don't use for system provided sysrqs,
  265. it is handled specially on the sparc
  266. and will never arrive */
  267. /* b */ &sysrq_reboot_op,
  268. #ifdef CONFIG_KEXEC
  269. /* c */ &sysrq_crashdump_op,
  270. #else
  271. /* c */ NULL,
  272. #endif
  273. #ifdef CONFIG_DEBUG_MUTEXES
  274. /* d */ &sysrq_showlocks_op,
  275. #else
  276. /* d */ NULL,
  277. #endif
  278. /* e */ &sysrq_term_op,
  279. /* f */ &sysrq_moom_op,
  280. /* g */ NULL,
  281. /* h */ NULL,
  282. /* i */ &sysrq_kill_op,
  283. /* j */ NULL,
  284. #ifdef CONFIG_VT
  285. /* k */ &sysrq_SAK_op,
  286. #else
  287. /* k */ NULL,
  288. #endif
  289. /* l */ NULL,
  290. /* m */ &sysrq_showmem_op,
  291. /* n */ &sysrq_unrt_op,
  292. /* o */ NULL, /* This will often be registered
  293. as 'Off' at init time */
  294. /* p */ &sysrq_showregs_op,
  295. /* q */ NULL,
  296. #ifdef CONFIG_VT
  297. /* r */ &sysrq_unraw_op,
  298. #else
  299. /* r */ NULL,
  300. #endif
  301. /* s */ &sysrq_sync_op,
  302. /* t */ &sysrq_showstate_op,
  303. /* u */ &sysrq_mountro_op,
  304. /* v */ NULL, /* May be assigned at init time by SMP VOYAGER */
  305. /* w */ NULL,
  306. /* x */ NULL,
  307. /* y */ NULL,
  308. /* z */ NULL
  309. };
  310. /* key2index calculation, -1 on invalid index */
  311. static int sysrq_key_table_key2index(int key) {
  312. int retval;
  313. if ((key >= '0') && (key <= '9')) {
  314. retval = key - '0';
  315. } else if ((key >= 'a') && (key <= 'z')) {
  316. retval = key + 10 - 'a';
  317. } else {
  318. retval = -1;
  319. }
  320. return retval;
  321. }
  322. /*
  323. * get and put functions for the table, exposed to modules.
  324. */
  325. struct sysrq_key_op *__sysrq_get_key_op (int key) {
  326. struct sysrq_key_op *op_p;
  327. int i;
  328. i = sysrq_key_table_key2index(key);
  329. op_p = (i == -1) ? NULL : sysrq_key_table[i];
  330. return op_p;
  331. }
  332. static void __sysrq_put_key_op (int key, struct sysrq_key_op *op_p) {
  333. int i;
  334. i = sysrq_key_table_key2index(key);
  335. if (i != -1)
  336. sysrq_key_table[i] = op_p;
  337. }
  338. /*
  339. * This is the non-locking version of handle_sysrq
  340. * It must/can only be called by sysrq key handlers,
  341. * as they are inside of the lock
  342. */
  343. void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty, int check_mask)
  344. {
  345. struct sysrq_key_op *op_p;
  346. int orig_log_level;
  347. int i, j;
  348. unsigned long flags;
  349. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  350. orig_log_level = console_loglevel;
  351. console_loglevel = 7;
  352. printk(KERN_INFO "SysRq : ");
  353. op_p = __sysrq_get_key_op(key);
  354. if (op_p) {
  355. /* Should we check for enabled operations (/proc/sysrq-trigger should not)
  356. * and is the invoked operation enabled? */
  357. if (!check_mask || sysrq_enabled == 1 ||
  358. (sysrq_enabled & op_p->enable_mask)) {
  359. printk ("%s\n", op_p->action_msg);
  360. console_loglevel = orig_log_level;
  361. op_p->handler(key, pt_regs, tty);
  362. }
  363. else
  364. printk("This sysrq operation is disabled.\n");
  365. } else {
  366. printk("HELP : ");
  367. /* Only print the help msg once per handler */
  368. for (i=0; i<SYSRQ_KEY_TABLE_LENGTH; i++)
  369. if (sysrq_key_table[i]) {
  370. for (j=0; sysrq_key_table[i] != sysrq_key_table[j]; j++);
  371. if (j == i)
  372. printk ("%s ", sysrq_key_table[i]->help_msg);
  373. }
  374. printk ("\n");
  375. console_loglevel = orig_log_level;
  376. }
  377. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  378. }
  379. /*
  380. * This function is called by the keyboard handler when SysRq is pressed
  381. * and any other keycode arrives.
  382. */
  383. void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
  384. {
  385. if (!sysrq_enabled)
  386. return;
  387. __handle_sysrq(key, pt_regs, tty, 1);
  388. }
  389. static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  390. struct sysrq_key_op *remove_op_p) {
  391. int retval;
  392. unsigned long flags;
  393. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  394. if (__sysrq_get_key_op(key) == remove_op_p) {
  395. __sysrq_put_key_op(key, insert_op_p);
  396. retval = 0;
  397. } else {
  398. retval = -1;
  399. }
  400. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  401. return retval;
  402. }
  403. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  404. {
  405. return __sysrq_swap_key_ops(key, op_p, NULL);
  406. }
  407. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  408. {
  409. return __sysrq_swap_key_ops(key, NULL, op_p);
  410. }
  411. EXPORT_SYMBOL(handle_sysrq);
  412. EXPORT_SYMBOL(register_sysrq_key);
  413. EXPORT_SYMBOL(unregister_sysrq_key);