sysrq.c 11 KB

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