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 <asm/ptrace.h>
  38. /* Whether we react on sysrq keys or just ignore them */
  39. int sysrq_enabled = 1;
  40. static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs,
  41. struct tty_struct *tty)
  42. {
  43. int i;
  44. i = key - '0';
  45. console_loglevel = 7;
  46. printk("Loglevel set to %d\n", i);
  47. console_loglevel = i;
  48. }
  49. static struct sysrq_key_op sysrq_loglevel_op = {
  50. .handler = sysrq_handle_loglevel,
  51. .help_msg = "loglevel0-8",
  52. .action_msg = "Changing Loglevel",
  53. .enable_mask = SYSRQ_ENABLE_LOG,
  54. };
  55. #ifdef CONFIG_VT
  56. static void sysrq_handle_SAK(int key, struct pt_regs *pt_regs,
  57. 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 pt_regs *pt_regs,
  74. struct tty_struct *tty)
  75. {
  76. struct kbd_struct *kbd = &kbd_table[fg_console];
  77. if (kbd)
  78. kbd->kbdmode = VC_XLATE;
  79. }
  80. static struct sysrq_key_op sysrq_unraw_op = {
  81. .handler = sysrq_handle_unraw,
  82. .help_msg = "unRaw",
  83. .action_msg = "Keyboard mode set to XLATE",
  84. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  85. };
  86. #else
  87. #define sysrq_unraw_op (*(struct sysrq_key_op *)0)
  88. #endif /* CONFIG_VT */
  89. #ifdef CONFIG_KEXEC
  90. static void sysrq_handle_crashdump(int key, struct pt_regs *pt_regs,
  91. struct tty_struct *tty)
  92. {
  93. crash_kexec(pt_regs);
  94. }
  95. static struct sysrq_key_op sysrq_crashdump_op = {
  96. .handler = sysrq_handle_crashdump,
  97. .help_msg = "Crashdump",
  98. .action_msg = "Trigger a crashdump",
  99. .enable_mask = SYSRQ_ENABLE_DUMP,
  100. };
  101. #else
  102. #define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
  103. #endif
  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. #ifdef CONFIG_LOCKDEP
  139. static void sysrq_handle_showlocks(int key, struct pt_regs *pt_regs,
  140. struct tty_struct *tty)
  141. {
  142. debug_show_all_locks();
  143. }
  144. static struct sysrq_key_op sysrq_showlocks_op = {
  145. .handler = sysrq_handle_showlocks,
  146. .help_msg = "show-all-locks(D)",
  147. .action_msg = "Show Locks Held",
  148. };
  149. #else
  150. #define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
  151. #endif
  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. /*
  187. * Signal sysrq helper function. Sends a signal to all user processes.
  188. */
  189. static void send_sig_all(int sig)
  190. {
  191. struct task_struct *p;
  192. for_each_process(p) {
  193. if (p->mm && p->pid != 1)
  194. /* Not swapper, init nor kernel thread */
  195. force_sig(sig, p);
  196. }
  197. }
  198. static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
  199. struct tty_struct *tty)
  200. {
  201. send_sig_all(SIGTERM);
  202. console_loglevel = 8;
  203. }
  204. static struct sysrq_key_op sysrq_term_op = {
  205. .handler = sysrq_handle_term,
  206. .help_msg = "tErm",
  207. .action_msg = "Terminate All Tasks",
  208. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  209. };
  210. static void moom_callback(void *ignored)
  211. {
  212. out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL],
  213. 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. static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
  239. struct tty_struct *tty)
  240. {
  241. normalize_rt_tasks();
  242. }
  243. static struct sysrq_key_op sysrq_unrt_op = {
  244. .handler = sysrq_handle_unrt,
  245. .help_msg = "Nice",
  246. .action_msg = "Nice All RT Tasks",
  247. .enable_mask = SYSRQ_ENABLE_RTNICE,
  248. };
  249. /* Key Operations table and lock */
  250. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  251. static struct sysrq_key_op *sysrq_key_table[36] = {
  252. &sysrq_loglevel_op, /* 0 */
  253. &sysrq_loglevel_op, /* 1 */
  254. &sysrq_loglevel_op, /* 2 */
  255. &sysrq_loglevel_op, /* 3 */
  256. &sysrq_loglevel_op, /* 4 */
  257. &sysrq_loglevel_op, /* 5 */
  258. &sysrq_loglevel_op, /* 6 */
  259. &sysrq_loglevel_op, /* 7 */
  260. &sysrq_loglevel_op, /* 8 */
  261. &sysrq_loglevel_op, /* 9 */
  262. /*
  263. * Don't use for system provided sysrqs, it is handled specially on
  264. * sparc and will never arrive
  265. */
  266. NULL, /* a */
  267. &sysrq_reboot_op, /* b */
  268. &sysrq_crashdump_op, /* c */
  269. &sysrq_showlocks_op, /* d */
  270. &sysrq_term_op, /* e */
  271. &sysrq_moom_op, /* f */
  272. NULL, /* g */
  273. NULL, /* h */
  274. &sysrq_kill_op, /* i */
  275. NULL, /* j */
  276. &sysrq_SAK_op, /* k */
  277. NULL, /* l */
  278. &sysrq_showmem_op, /* m */
  279. &sysrq_unrt_op, /* n */
  280. /* This will often be registered as 'Off' at init time */
  281. NULL, /* o */
  282. &sysrq_showregs_op, /* p */
  283. NULL, /* q */
  284. &sysrq_unraw_op, /* r */
  285. &sysrq_sync_op, /* s */
  286. &sysrq_showstate_op, /* t */
  287. &sysrq_mountro_op, /* u */
  288. /* May be assigned at init time by SMP VOYAGER */
  289. NULL, /* v */
  290. NULL, /* w */
  291. NULL, /* x */
  292. NULL, /* y */
  293. NULL /* z */
  294. };
  295. /* key2index calculation, -1 on invalid index */
  296. static int sysrq_key_table_key2index(int key)
  297. {
  298. int retval;
  299. if ((key >= '0') && (key <= '9'))
  300. retval = key - '0';
  301. else if ((key >= 'a') && (key <= 'z'))
  302. retval = key + 10 - 'a';
  303. else
  304. retval = -1;
  305. return retval;
  306. }
  307. /*
  308. * get and put functions for the table, exposed to modules.
  309. */
  310. struct sysrq_key_op *__sysrq_get_key_op(int key)
  311. {
  312. struct sysrq_key_op *op_p = NULL;
  313. int i;
  314. i = sysrq_key_table_key2index(key);
  315. if (i != -1)
  316. op_p = sysrq_key_table[i];
  317. return op_p;
  318. }
  319. static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
  320. {
  321. int i = sysrq_key_table_key2index(key);
  322. if (i != -1)
  323. sysrq_key_table[i] = op_p;
  324. }
  325. /*
  326. * This is the non-locking version of handle_sysrq. It must/can only be called
  327. * by sysrq key handlers, as they are inside of the lock
  328. */
  329. void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty,
  330. int check_mask)
  331. {
  332. struct sysrq_key_op *op_p;
  333. int orig_log_level;
  334. int i;
  335. unsigned long flags;
  336. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  337. orig_log_level = console_loglevel;
  338. console_loglevel = 7;
  339. printk(KERN_INFO "SysRq : ");
  340. op_p = __sysrq_get_key_op(key);
  341. if (op_p) {
  342. /*
  343. * Should we check for enabled operations (/proc/sysrq-trigger
  344. * should not) and is the invoked operation enabled?
  345. */
  346. if (!check_mask || sysrq_enabled == 1 ||
  347. (sysrq_enabled & op_p->enable_mask)) {
  348. printk("%s\n", op_p->action_msg);
  349. console_loglevel = orig_log_level;
  350. op_p->handler(key, pt_regs, tty);
  351. } else {
  352. printk("This sysrq operation is disabled.\n");
  353. }
  354. } else {
  355. printk("HELP : ");
  356. /* Only print the help msg once per handler */
  357. for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
  358. if (sysrq_key_table[i]) {
  359. int j;
  360. for (j = 0; sysrq_key_table[i] !=
  361. sysrq_key_table[j]; j++)
  362. ;
  363. if (j != i)
  364. continue;
  365. printk("%s ", sysrq_key_table[i]->help_msg);
  366. }
  367. }
  368. printk("\n");
  369. console_loglevel = orig_log_level;
  370. }
  371. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  372. }
  373. /*
  374. * This function is called by the keyboard handler when SysRq is pressed
  375. * and any other keycode arrives.
  376. */
  377. void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
  378. {
  379. if (!sysrq_enabled)
  380. return;
  381. __handle_sysrq(key, pt_regs, tty, 1);
  382. }
  383. EXPORT_SYMBOL(handle_sysrq);
  384. static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  385. struct sysrq_key_op *remove_op_p)
  386. {
  387. int retval;
  388. unsigned long flags;
  389. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  390. if (__sysrq_get_key_op(key) == remove_op_p) {
  391. __sysrq_put_key_op(key, insert_op_p);
  392. retval = 0;
  393. } else {
  394. retval = -1;
  395. }
  396. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  397. return retval;
  398. }
  399. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  400. {
  401. return __sysrq_swap_key_ops(key, op_p, NULL);
  402. }
  403. EXPORT_SYMBOL(register_sysrq_key);
  404. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  405. {
  406. return __sysrq_swap_key_ops(key, NULL, op_p);
  407. }
  408. EXPORT_SYMBOL(unregister_sysrq_key);