sysrq.c 10 KB

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