/drivers/char/sysrq.c

https://bitbucket.org/evzijst/gittest · C · 432 lines · 332 code · 54 blank · 46 comment · 30 complexity · d93047d7d62d139829aefdb775a052f1 MD5 · raw file

  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 <asm/ptrace.h>
  38. /* Whether we react on sysrq keys or just ignore them */
  39. int sysrq_enabled = 1;
  40. /* Loglevel sysrq handler */
  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. /* SAK sysrq handler */
  57. #ifdef CONFIG_VT
  58. static void sysrq_handle_SAK(int key, struct pt_regs *pt_regs,
  59. struct tty_struct *tty)
  60. {
  61. if (tty)
  62. do_SAK(tty);
  63. reset_vc(vc_cons[fg_console].d);
  64. }
  65. static struct sysrq_key_op sysrq_SAK_op = {
  66. .handler = sysrq_handle_SAK,
  67. .help_msg = "saK",
  68. .action_msg = "SAK",
  69. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  70. };
  71. #endif
  72. #ifdef CONFIG_VT
  73. /* unraw sysrq handler */
  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. #endif /* CONFIG_VT */
  88. /* reboot sysrq handler */
  89. static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs,
  90. struct tty_struct *tty)
  91. {
  92. local_irq_enable();
  93. machine_restart(NULL);
  94. }
  95. static struct sysrq_key_op sysrq_reboot_op = {
  96. .handler = sysrq_handle_reboot,
  97. .help_msg = "reBoot",
  98. .action_msg = "Resetting",
  99. .enable_mask = SYSRQ_ENABLE_BOOT,
  100. };
  101. static void sysrq_handle_sync(int key, struct pt_regs *pt_regs,
  102. struct tty_struct *tty)
  103. {
  104. emergency_sync();
  105. }
  106. static struct sysrq_key_op sysrq_sync_op = {
  107. .handler = sysrq_handle_sync,
  108. .help_msg = "Sync",
  109. .action_msg = "Emergency Sync",
  110. .enable_mask = SYSRQ_ENABLE_SYNC,
  111. };
  112. static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs,
  113. struct tty_struct *tty)
  114. {
  115. emergency_remount();
  116. }
  117. static struct sysrq_key_op sysrq_mountro_op = {
  118. .handler = sysrq_handle_mountro,
  119. .help_msg = "Unmount",
  120. .action_msg = "Emergency Remount R/O",
  121. .enable_mask = SYSRQ_ENABLE_REMOUNT,
  122. };
  123. /* END SYNC SYSRQ HANDLERS BLOCK */
  124. /* SHOW SYSRQ HANDLERS BLOCK */
  125. static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
  126. struct tty_struct *tty)
  127. {
  128. if (pt_regs)
  129. show_regs(pt_regs);
  130. }
  131. static struct sysrq_key_op sysrq_showregs_op = {
  132. .handler = sysrq_handle_showregs,
  133. .help_msg = "showPc",
  134. .action_msg = "Show Regs",
  135. .enable_mask = SYSRQ_ENABLE_DUMP,
  136. };
  137. static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
  138. struct tty_struct *tty)
  139. {
  140. show_state();
  141. }
  142. static struct sysrq_key_op sysrq_showstate_op = {
  143. .handler = sysrq_handle_showstate,
  144. .help_msg = "showTasks",
  145. .action_msg = "Show State",
  146. .enable_mask = SYSRQ_ENABLE_DUMP,
  147. };
  148. static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
  149. struct tty_struct *tty)
  150. {
  151. show_mem();
  152. }
  153. static struct sysrq_key_op sysrq_showmem_op = {
  154. .handler = sysrq_handle_showmem,
  155. .help_msg = "showMem",
  156. .action_msg = "Show Memory",
  157. .enable_mask = SYSRQ_ENABLE_DUMP,
  158. };
  159. /* SHOW SYSRQ HANDLERS BLOCK */
  160. /* SIGNAL SYSRQ HANDLERS BLOCK */
  161. /* signal sysrq helper function
  162. * Sends a signal to all user processes */
  163. static void send_sig_all(int sig)
  164. {
  165. struct task_struct *p;
  166. for_each_process(p) {
  167. if (p->mm && p->pid != 1)
  168. /* Not swapper, init nor kernel thread */
  169. force_sig(sig, p);
  170. }
  171. }
  172. static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
  173. struct tty_struct *tty)
  174. {
  175. send_sig_all(SIGTERM);
  176. console_loglevel = 8;
  177. }
  178. static struct sysrq_key_op sysrq_term_op = {
  179. .handler = sysrq_handle_term,
  180. .help_msg = "tErm",
  181. .action_msg = "Terminate All Tasks",
  182. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  183. };
  184. static void moom_callback(void *ignored)
  185. {
  186. out_of_memory(GFP_KERNEL);
  187. }
  188. static DECLARE_WORK(moom_work, moom_callback, NULL);
  189. static void sysrq_handle_moom(int key, struct pt_regs *pt_regs,
  190. struct tty_struct *tty)
  191. {
  192. schedule_work(&moom_work);
  193. }
  194. static struct sysrq_key_op sysrq_moom_op = {
  195. .handler = sysrq_handle_moom,
  196. .help_msg = "Full",
  197. .action_msg = "Manual OOM execution",
  198. };
  199. static void sysrq_handle_kill(int key, struct pt_regs *pt_regs,
  200. struct tty_struct *tty)
  201. {
  202. send_sig_all(SIGKILL);
  203. console_loglevel = 8;
  204. }
  205. static struct sysrq_key_op sysrq_kill_op = {
  206. .handler = sysrq_handle_kill,
  207. .help_msg = "kIll",
  208. .action_msg = "Kill All Tasks",
  209. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  210. };
  211. /* END SIGNAL SYSRQ HANDLERS BLOCK */
  212. static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
  213. struct tty_struct *tty)
  214. {
  215. normalize_rt_tasks();
  216. }
  217. static struct sysrq_key_op sysrq_unrt_op = {
  218. .handler = sysrq_handle_unrt,
  219. .help_msg = "Nice",
  220. .action_msg = "Nice All RT Tasks",
  221. .enable_mask = SYSRQ_ENABLE_RTNICE,
  222. };
  223. /* Key Operations table and lock */
  224. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  225. #define SYSRQ_KEY_TABLE_LENGTH 36
  226. static struct sysrq_key_op *sysrq_key_table[SYSRQ_KEY_TABLE_LENGTH] = {
  227. /* 0 */ &sysrq_loglevel_op,
  228. /* 1 */ &sysrq_loglevel_op,
  229. /* 2 */ &sysrq_loglevel_op,
  230. /* 3 */ &sysrq_loglevel_op,
  231. /* 4 */ &sysrq_loglevel_op,
  232. /* 5 */ &sysrq_loglevel_op,
  233. /* 6 */ &sysrq_loglevel_op,
  234. /* 7 */ &sysrq_loglevel_op,
  235. /* 8 */ &sysrq_loglevel_op,
  236. /* 9 */ &sysrq_loglevel_op,
  237. /* a */ NULL, /* Don't use for system provided sysrqs,
  238. it is handled specially on the sparc
  239. and will never arrive */
  240. /* b */ &sysrq_reboot_op,
  241. /* c */ NULL,
  242. /* d */ NULL,
  243. /* e */ &sysrq_term_op,
  244. /* f */ &sysrq_moom_op,
  245. /* g */ NULL,
  246. /* h */ NULL,
  247. /* i */ &sysrq_kill_op,
  248. /* j */ NULL,
  249. #ifdef CONFIG_VT
  250. /* k */ &sysrq_SAK_op,
  251. #else
  252. /* k */ NULL,
  253. #endif
  254. /* l */ NULL,
  255. /* m */ &sysrq_showmem_op,
  256. /* n */ &sysrq_unrt_op,
  257. /* o */ NULL, /* This will often be registered
  258. as 'Off' at init time */
  259. /* p */ &sysrq_showregs_op,
  260. /* q */ NULL,
  261. #ifdef CONFIG_VT
  262. /* r */ &sysrq_unraw_op,
  263. #else
  264. /* r */ NULL,
  265. #endif
  266. /* s */ &sysrq_sync_op,
  267. /* t */ &sysrq_showstate_op,
  268. /* u */ &sysrq_mountro_op,
  269. /* v */ NULL, /* May be assigned at init time by SMP VOYAGER */
  270. /* w */ NULL,
  271. /* x */ NULL,
  272. /* y */ NULL,
  273. /* z */ NULL
  274. };
  275. /* key2index calculation, -1 on invalid index */
  276. static int sysrq_key_table_key2index(int key) {
  277. int retval;
  278. if ((key >= '0') && (key <= '9')) {
  279. retval = key - '0';
  280. } else if ((key >= 'a') && (key <= 'z')) {
  281. retval = key + 10 - 'a';
  282. } else {
  283. retval = -1;
  284. }
  285. return retval;
  286. }
  287. /*
  288. * get and put functions for the table, exposed to modules.
  289. */
  290. struct sysrq_key_op *__sysrq_get_key_op (int key) {
  291. struct sysrq_key_op *op_p;
  292. int i;
  293. i = sysrq_key_table_key2index(key);
  294. op_p = (i == -1) ? NULL : sysrq_key_table[i];
  295. return op_p;
  296. }
  297. void __sysrq_put_key_op (int key, struct sysrq_key_op *op_p) {
  298. int i;
  299. i = sysrq_key_table_key2index(key);
  300. if (i != -1)
  301. sysrq_key_table[i] = op_p;
  302. }
  303. /*
  304. * This is the non-locking version of handle_sysrq
  305. * It must/can only be called by sysrq key handlers,
  306. * as they are inside of the lock
  307. */
  308. void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty, int check_mask)
  309. {
  310. struct sysrq_key_op *op_p;
  311. int orig_log_level;
  312. int i, j;
  313. unsigned long flags;
  314. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  315. orig_log_level = console_loglevel;
  316. console_loglevel = 7;
  317. printk(KERN_INFO "SysRq : ");
  318. op_p = __sysrq_get_key_op(key);
  319. if (op_p) {
  320. /* Should we check for enabled operations (/proc/sysrq-trigger should not)
  321. * and is the invoked operation enabled? */
  322. if (!check_mask || sysrq_enabled == 1 ||
  323. (sysrq_enabled & op_p->enable_mask)) {
  324. printk ("%s\n", op_p->action_msg);
  325. console_loglevel = orig_log_level;
  326. op_p->handler(key, pt_regs, tty);
  327. }
  328. else
  329. printk("This sysrq operation is disabled.\n");
  330. } else {
  331. printk("HELP : ");
  332. /* Only print the help msg once per handler */
  333. for (i=0; i<SYSRQ_KEY_TABLE_LENGTH; i++)
  334. if (sysrq_key_table[i]) {
  335. for (j=0; sysrq_key_table[i] != sysrq_key_table[j]; j++);
  336. if (j == i)
  337. printk ("%s ", sysrq_key_table[i]->help_msg);
  338. }
  339. printk ("\n");
  340. console_loglevel = orig_log_level;
  341. }
  342. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  343. }
  344. /*
  345. * This function is called by the keyboard handler when SysRq is pressed
  346. * and any other keycode arrives.
  347. */
  348. void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
  349. {
  350. if (!sysrq_enabled)
  351. return;
  352. __handle_sysrq(key, pt_regs, tty, 1);
  353. }
  354. int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  355. struct sysrq_key_op *remove_op_p) {
  356. int retval;
  357. unsigned long flags;
  358. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  359. if (__sysrq_get_key_op(key) == remove_op_p) {
  360. __sysrq_put_key_op(key, insert_op_p);
  361. retval = 0;
  362. } else {
  363. retval = -1;
  364. }
  365. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  366. return retval;
  367. }
  368. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  369. {
  370. return __sysrq_swap_key_ops(key, op_p, NULL);
  371. }
  372. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  373. {
  374. return __sysrq_swap_key_ops(key, NULL, op_p);
  375. }
  376. EXPORT_SYMBOL(handle_sysrq);
  377. EXPORT_SYMBOL(register_sysrq_key);
  378. EXPORT_SYMBOL(unregister_sysrq_key);