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/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. static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs,
  42. 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 pt_regs *pt_regs,
  58. struct tty_struct *tty)
  59. {
  60. if (tty)
  61. do_SAK(tty);
  62. reset_vc(vc_cons[fg_console].d);
  63. }
  64. static struct sysrq_key_op sysrq_SAK_op = {
  65. .handler = sysrq_handle_SAK,
  66. .help_msg = "saK",
  67. .action_msg = "SAK",
  68. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  69. };
  70. #else
  71. #define sysrq_SAK_op (*(struct sysrq_key_op *)0)
  72. #endif
  73. #ifdef CONFIG_VT
  74. static void sysrq_handle_unraw(int key, struct pt_regs *pt_regs,
  75. struct tty_struct *tty)
  76. {
  77. struct kbd_struct *kbd = &kbd_table[fg_console];
  78. if (kbd)
  79. kbd->kbdmode = VC_XLATE;
  80. }
  81. static struct sysrq_key_op sysrq_unraw_op = {
  82. .handler = sysrq_handle_unraw,
  83. .help_msg = "unRaw",
  84. .action_msg = "Keyboard mode set to XLATE",
  85. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  86. };
  87. #else
  88. #define sysrq_unraw_op (*(struct sysrq_key_op *)0)
  89. #endif /* CONFIG_VT */
  90. #ifdef CONFIG_KEXEC
  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. #else
  103. #define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
  104. #endif
  105. static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs,
  106. struct tty_struct *tty)
  107. {
  108. local_irq_enable();
  109. emergency_restart();
  110. }
  111. static struct sysrq_key_op sysrq_reboot_op = {
  112. .handler = sysrq_handle_reboot,
  113. .help_msg = "reBoot",
  114. .action_msg = "Resetting",
  115. .enable_mask = SYSRQ_ENABLE_BOOT,
  116. };
  117. static void sysrq_handle_sync(int key, struct pt_regs *pt_regs,
  118. struct tty_struct *tty)
  119. {
  120. emergency_sync();
  121. }
  122. static struct sysrq_key_op sysrq_sync_op = {
  123. .handler = sysrq_handle_sync,
  124. .help_msg = "Sync",
  125. .action_msg = "Emergency Sync",
  126. .enable_mask = SYSRQ_ENABLE_SYNC,
  127. };
  128. static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs,
  129. struct tty_struct *tty)
  130. {
  131. emergency_remount();
  132. }
  133. static struct sysrq_key_op sysrq_mountro_op = {
  134. .handler = sysrq_handle_mountro,
  135. .help_msg = "Unmount",
  136. .action_msg = "Emergency Remount R/O",
  137. .enable_mask = SYSRQ_ENABLE_REMOUNT,
  138. };
  139. #ifdef CONFIG_DEBUG_MUTEXES
  140. static void sysrq_handle_showlocks(int key, struct pt_regs *pt_regs,
  141. 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. #else
  151. #define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
  152. #endif
  153. static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
  154. struct tty_struct *tty)
  155. {
  156. if (pt_regs)
  157. show_regs(pt_regs);
  158. }
  159. static struct sysrq_key_op sysrq_showregs_op = {
  160. .handler = sysrq_handle_showregs,
  161. .help_msg = "showPc",
  162. .action_msg = "Show Regs",
  163. .enable_mask = SYSRQ_ENABLE_DUMP,
  164. };
  165. static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
  166. struct tty_struct *tty)
  167. {
  168. show_state();
  169. }
  170. static struct sysrq_key_op sysrq_showstate_op = {
  171. .handler = sysrq_handle_showstate,
  172. .help_msg = "showTasks",
  173. .action_msg = "Show State",
  174. .enable_mask = SYSRQ_ENABLE_DUMP,
  175. };
  176. static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
  177. struct tty_struct *tty)
  178. {
  179. show_mem();
  180. }
  181. static struct sysrq_key_op sysrq_showmem_op = {
  182. .handler = sysrq_handle_showmem,
  183. .help_msg = "showMem",
  184. .action_msg = "Show Memory",
  185. .enable_mask = SYSRQ_ENABLE_DUMP,
  186. };
  187. /*
  188. * Signal sysrq helper function. Sends a signal to all user processes.
  189. */
  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(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL],
  214. GFP_KERNEL, 0);
  215. }
  216. static DECLARE_WORK(moom_work, moom_callback, NULL);
  217. static void sysrq_handle_moom(int key, struct pt_regs *pt_regs,
  218. 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 pt_regs *pt_regs,
  228. struct tty_struct *tty)
  229. {
  230. send_sig_all(SIGKILL);
  231. console_loglevel = 8;
  232. }
  233. static struct sysrq_key_op sysrq_kill_op = {
  234. .handler = sysrq_handle_kill,
  235. .help_msg = "kIll",
  236. .action_msg = "Kill All Tasks",
  237. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  238. };
  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. static struct sysrq_key_op *sysrq_key_table[36] = {
  253. &sysrq_loglevel_op, /* 0 */
  254. &sysrq_loglevel_op, /* 1 */
  255. &sysrq_loglevel_op, /* 2 */
  256. &sysrq_loglevel_op, /* 3 */
  257. &sysrq_loglevel_op, /* 4 */
  258. &sysrq_loglevel_op, /* 5 */
  259. &sysrq_loglevel_op, /* 6 */
  260. &sysrq_loglevel_op, /* 7 */
  261. &sysrq_loglevel_op, /* 8 */
  262. &sysrq_loglevel_op, /* 9 */
  263. /*
  264. * Don't use for system provided sysrqs, it is handled specially on
  265. * sparc and will never arrive
  266. */
  267. NULL, /* a */
  268. &sysrq_reboot_op, /* b */
  269. &sysrq_crashdump_op, /* c */
  270. &sysrq_showlocks_op, /* d */
  271. &sysrq_term_op, /* e */
  272. &sysrq_moom_op, /* f */
  273. NULL, /* g */
  274. NULL, /* h */
  275. &sysrq_kill_op, /* i */
  276. NULL, /* j */
  277. &sysrq_SAK_op, /* k */
  278. NULL, /* l */
  279. &sysrq_showmem_op, /* m */
  280. &sysrq_unrt_op, /* n */
  281. /* This will often be registered as 'Off' at init time */
  282. NULL, /* o */
  283. &sysrq_showregs_op, /* p */
  284. NULL, /* q */
  285. &sysrq_unraw_op, /* r */
  286. &sysrq_sync_op, /* s */
  287. &sysrq_showstate_op, /* t */
  288. &sysrq_mountro_op, /* u */
  289. /* May be assigned at init time by SMP VOYAGER */
  290. NULL, /* v */
  291. NULL, /* w */
  292. NULL, /* x */
  293. NULL, /* y */
  294. NULL /* z */
  295. };
  296. /* key2index calculation, -1 on invalid index */
  297. static int sysrq_key_table_key2index(int key)
  298. {
  299. int retval;
  300. if ((key >= '0') && (key <= '9'))
  301. retval = key - '0';
  302. else if ((key >= 'a') && (key <= 'z'))
  303. retval = key + 10 - 'a';
  304. else
  305. retval = -1;
  306. return retval;
  307. }
  308. /*
  309. * get and put functions for the table, exposed to modules.
  310. */
  311. struct sysrq_key_op *__sysrq_get_key_op(int key)
  312. {
  313. struct sysrq_key_op *op_p = NULL;
  314. int i;
  315. i = sysrq_key_table_key2index(key);
  316. if (i != -1)
  317. op_p = sysrq_key_table[i];
  318. return op_p;
  319. }
  320. static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
  321. {
  322. int i = sysrq_key_table_key2index(key);
  323. if (i != -1)
  324. sysrq_key_table[i] = op_p;
  325. }
  326. /*
  327. * This is the non-locking version of handle_sysrq. It must/can only be called
  328. * by sysrq key handlers, as they are inside of the lock
  329. */
  330. void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty,
  331. int check_mask)
  332. {
  333. struct sysrq_key_op *op_p;
  334. int orig_log_level;
  335. int i;
  336. unsigned long flags;
  337. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  338. orig_log_level = console_loglevel;
  339. console_loglevel = 7;
  340. printk(KERN_INFO "SysRq : ");
  341. op_p = __sysrq_get_key_op(key);
  342. if (op_p) {
  343. /*
  344. * Should we check for enabled operations (/proc/sysrq-trigger
  345. * should not) and is the invoked operation enabled?
  346. */
  347. if (!check_mask || sysrq_enabled == 1 ||
  348. (sysrq_enabled & op_p->enable_mask)) {
  349. printk("%s\n", op_p->action_msg);
  350. console_loglevel = orig_log_level;
  351. op_p->handler(key, pt_regs, tty);
  352. } else {
  353. printk("This sysrq operation is disabled.\n");
  354. }
  355. } else {
  356. printk("HELP : ");
  357. /* Only print the help msg once per handler */
  358. for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
  359. if (sysrq_key_table[i]) {
  360. int j;
  361. for (j = 0; sysrq_key_table[i] !=
  362. sysrq_key_table[j]; j++)
  363. ;
  364. if (j != i)
  365. continue;
  366. printk("%s ", sysrq_key_table[i]->help_msg);
  367. }
  368. }
  369. printk("\n");
  370. console_loglevel = orig_log_level;
  371. }
  372. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  373. }
  374. /*
  375. * This function is called by the keyboard handler when SysRq is pressed
  376. * and any other keycode arrives.
  377. */
  378. void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
  379. {
  380. if (!sysrq_enabled)
  381. return;
  382. __handle_sysrq(key, pt_regs, tty, 1);
  383. }
  384. EXPORT_SYMBOL(handle_sysrq);
  385. static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  386. struct sysrq_key_op *remove_op_p)
  387. {
  388. int retval;
  389. unsigned long flags;
  390. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  391. if (__sysrq_get_key_op(key) == remove_op_p) {
  392. __sysrq_put_key_op(key, insert_op_p);
  393. retval = 0;
  394. } else {
  395. retval = -1;
  396. }
  397. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  398. return retval;
  399. }
  400. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  401. {
  402. return __sysrq_swap_key_ops(key, op_p, NULL);
  403. }
  404. EXPORT_SYMBOL(register_sysrq_key);
  405. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  406. {
  407. return __sysrq_swap_key_ops(key, NULL, op_p);
  408. }
  409. EXPORT_SYMBOL(unregister_sysrq_key);