sysrq.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * Linux Magic System Request Key Hacks
  3. *
  4. * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  5. * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
  6. *
  7. * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
  8. * overhauled to use key registration
  9. * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
  10. *
  11. * Copyright (c) 2010 Dmitry Torokhov
  12. * Input handler conversion
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/sched.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/mm.h>
  18. #include <linux/fs.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/proc_fs.h>
  26. #include <linux/nmi.h>
  27. #include <linux/quotaops.h>
  28. #include <linux/perf_event.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/suspend.h>
  32. #include <linux/writeback.h>
  33. #include <linux/swap.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/vt_kern.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/hrtimer.h>
  38. #include <linux/oom.h>
  39. #include <linux/slab.h>
  40. #include <linux/input.h>
  41. #include <linux/uaccess.h>
  42. #include <asm/ptrace.h>
  43. #include <asm/irq_regs.h>
  44. /* Whether we react on sysrq keys or just ignore them */
  45. static int __read_mostly sysrq_enabled = SYSRQ_DEFAULT_ENABLE;
  46. static bool __read_mostly sysrq_always_enabled;
  47. bool sysrq_on(void)
  48. {
  49. return sysrq_enabled || sysrq_always_enabled;
  50. }
  51. EXPORT_SYMBOL(sysrq_on);
  52. /*
  53. * A value of 1 means 'all', other nonzero values are an op mask:
  54. */
  55. static bool sysrq_on_mask(int mask)
  56. {
  57. return sysrq_always_enabled ||
  58. sysrq_enabled == 1 ||
  59. (sysrq_enabled & mask);
  60. }
  61. static int __init sysrq_always_enabled_setup(char *str)
  62. {
  63. sysrq_always_enabled = true;
  64. pr_info("sysrq always enabled.\n");
  65. return 1;
  66. }
  67. __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
  68. static void sysrq_handle_loglevel(int key)
  69. {
  70. int i;
  71. i = key - '0';
  72. console_loglevel = 7;
  73. printk("Loglevel set to %d\n", i);
  74. console_loglevel = i;
  75. }
  76. static struct sysrq_key_op sysrq_loglevel_op = {
  77. .handler = sysrq_handle_loglevel,
  78. .help_msg = "loglevel(0-9)",
  79. .action_msg = "Changing Loglevel",
  80. .enable_mask = SYSRQ_ENABLE_LOG,
  81. };
  82. #ifdef CONFIG_VT
  83. static void sysrq_handle_SAK(int key)
  84. {
  85. struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
  86. schedule_work(SAK_work);
  87. }
  88. static struct sysrq_key_op sysrq_SAK_op = {
  89. .handler = sysrq_handle_SAK,
  90. .help_msg = "saK",
  91. .action_msg = "SAK",
  92. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  93. };
  94. #else
  95. #define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
  96. #endif
  97. #ifdef CONFIG_VT
  98. static void sysrq_handle_unraw(int key)
  99. {
  100. vt_reset_unicode(fg_console);
  101. }
  102. static struct sysrq_key_op sysrq_unraw_op = {
  103. .handler = sysrq_handle_unraw,
  104. .help_msg = "unRaw",
  105. .action_msg = "Keyboard mode set to system default",
  106. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  107. };
  108. #else
  109. #define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
  110. #endif /* CONFIG_VT */
  111. static void sysrq_handle_crash(int key)
  112. {
  113. char *killer = NULL;
  114. panic_on_oops = 1; /* force panic */
  115. wmb();
  116. *killer = 1;
  117. }
  118. static struct sysrq_key_op sysrq_crash_op = {
  119. .handler = sysrq_handle_crash,
  120. .help_msg = "Crash",
  121. .action_msg = "Trigger a crash",
  122. .enable_mask = SYSRQ_ENABLE_DUMP,
  123. };
  124. static void sysrq_handle_reboot(int key)
  125. {
  126. lockdep_off();
  127. local_irq_enable();
  128. emergency_restart();
  129. }
  130. static struct sysrq_key_op sysrq_reboot_op = {
  131. .handler = sysrq_handle_reboot,
  132. .help_msg = "reBoot",
  133. .action_msg = "Resetting",
  134. .enable_mask = SYSRQ_ENABLE_BOOT,
  135. };
  136. static void sysrq_handle_sync(int key)
  137. {
  138. emergency_sync();
  139. }
  140. static struct sysrq_key_op sysrq_sync_op = {
  141. .handler = sysrq_handle_sync,
  142. .help_msg = "Sync",
  143. .action_msg = "Emergency Sync",
  144. .enable_mask = SYSRQ_ENABLE_SYNC,
  145. };
  146. static void sysrq_handle_show_timers(int key)
  147. {
  148. sysrq_timer_list_show();
  149. }
  150. static struct sysrq_key_op sysrq_show_timers_op = {
  151. .handler = sysrq_handle_show_timers,
  152. .help_msg = "show-all-timers(Q)",
  153. .action_msg = "Show clockevent devices & pending hrtimers (no others)",
  154. };
  155. static void sysrq_handle_mountro(int key)
  156. {
  157. emergency_remount();
  158. }
  159. static struct sysrq_key_op sysrq_mountro_op = {
  160. .handler = sysrq_handle_mountro,
  161. .help_msg = "Unmount",
  162. .action_msg = "Emergency Remount R/O",
  163. .enable_mask = SYSRQ_ENABLE_REMOUNT,
  164. };
  165. #ifdef CONFIG_LOCKDEP
  166. static void sysrq_handle_showlocks(int key)
  167. {
  168. debug_show_all_locks();
  169. }
  170. static struct sysrq_key_op sysrq_showlocks_op = {
  171. .handler = sysrq_handle_showlocks,
  172. .help_msg = "show-all-locks(D)",
  173. .action_msg = "Show Locks Held",
  174. };
  175. #else
  176. #define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
  177. #endif
  178. #ifdef CONFIG_SMP
  179. static DEFINE_SPINLOCK(show_lock);
  180. static void showacpu(void *dummy)
  181. {
  182. unsigned long flags;
  183. /* Idle CPUs have no interesting backtrace. */
  184. if (idle_cpu(smp_processor_id()))
  185. return;
  186. spin_lock_irqsave(&show_lock, flags);
  187. printk(KERN_INFO "CPU%d:\n", smp_processor_id());
  188. show_stack(NULL, NULL);
  189. spin_unlock_irqrestore(&show_lock, flags);
  190. }
  191. static void sysrq_showregs_othercpus(struct work_struct *dummy)
  192. {
  193. smp_call_function(showacpu, NULL, 0);
  194. }
  195. static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
  196. static void sysrq_handle_showallcpus(int key)
  197. {
  198. /*
  199. * Fall back to the workqueue based printing if the
  200. * backtrace printing did not succeed or the
  201. * architecture has no support for it:
  202. */
  203. if (!trigger_all_cpu_backtrace()) {
  204. struct pt_regs *regs = get_irq_regs();
  205. if (regs) {
  206. printk(KERN_INFO "CPU%d:\n", smp_processor_id());
  207. show_regs(regs);
  208. }
  209. schedule_work(&sysrq_showallcpus);
  210. }
  211. }
  212. static struct sysrq_key_op sysrq_showallcpus_op = {
  213. .handler = sysrq_handle_showallcpus,
  214. .help_msg = "show-backtrace-all-active-cpus(L)",
  215. .action_msg = "Show backtrace of all active CPUs",
  216. .enable_mask = SYSRQ_ENABLE_DUMP,
  217. };
  218. #endif
  219. static void sysrq_handle_showregs(int key)
  220. {
  221. struct pt_regs *regs = get_irq_regs();
  222. if (regs)
  223. show_regs(regs);
  224. perf_event_print_debug();
  225. }
  226. static struct sysrq_key_op sysrq_showregs_op = {
  227. .handler = sysrq_handle_showregs,
  228. .help_msg = "show-registers(P)",
  229. .action_msg = "Show Regs",
  230. .enable_mask = SYSRQ_ENABLE_DUMP,
  231. };
  232. static void sysrq_handle_showstate(int key)
  233. {
  234. show_state();
  235. }
  236. static struct sysrq_key_op sysrq_showstate_op = {
  237. .handler = sysrq_handle_showstate,
  238. .help_msg = "show-task-states(T)",
  239. .action_msg = "Show State",
  240. .enable_mask = SYSRQ_ENABLE_DUMP,
  241. };
  242. static void sysrq_handle_showstate_blocked(int key)
  243. {
  244. show_state_filter(TASK_UNINTERRUPTIBLE);
  245. }
  246. static struct sysrq_key_op sysrq_showstate_blocked_op = {
  247. .handler = sysrq_handle_showstate_blocked,
  248. .help_msg = "show-blocked-tasks(W)",
  249. .action_msg = "Show Blocked State",
  250. .enable_mask = SYSRQ_ENABLE_DUMP,
  251. };
  252. #ifdef CONFIG_TRACING
  253. #include <linux/ftrace.h>
  254. static void sysrq_ftrace_dump(int key)
  255. {
  256. ftrace_dump(DUMP_ALL);
  257. }
  258. static struct sysrq_key_op sysrq_ftrace_dump_op = {
  259. .handler = sysrq_ftrace_dump,
  260. .help_msg = "dump-ftrace-buffer(Z)",
  261. .action_msg = "Dump ftrace buffer",
  262. .enable_mask = SYSRQ_ENABLE_DUMP,
  263. };
  264. #else
  265. #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
  266. #endif
  267. static void sysrq_handle_showmem(int key)
  268. {
  269. show_mem(0);
  270. }
  271. static struct sysrq_key_op sysrq_showmem_op = {
  272. .handler = sysrq_handle_showmem,
  273. .help_msg = "show-memory-usage(M)",
  274. .action_msg = "Show Memory",
  275. .enable_mask = SYSRQ_ENABLE_DUMP,
  276. };
  277. /*
  278. * Signal sysrq helper function. Sends a signal to all user processes.
  279. */
  280. static void send_sig_all(int sig)
  281. {
  282. struct task_struct *p;
  283. read_lock(&tasklist_lock);
  284. for_each_process(p) {
  285. if (p->flags & PF_KTHREAD)
  286. continue;
  287. if (is_global_init(p))
  288. continue;
  289. do_send_sig_info(sig, SEND_SIG_FORCED, p, true);
  290. }
  291. read_unlock(&tasklist_lock);
  292. }
  293. static void sysrq_handle_term(int key)
  294. {
  295. send_sig_all(SIGTERM);
  296. console_loglevel = 8;
  297. }
  298. static struct sysrq_key_op sysrq_term_op = {
  299. .handler = sysrq_handle_term,
  300. .help_msg = "terminate-all-tasks(E)",
  301. .action_msg = "Terminate All Tasks",
  302. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  303. };
  304. static void moom_callback(struct work_struct *ignored)
  305. {
  306. out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0, NULL, true);
  307. }
  308. static DECLARE_WORK(moom_work, moom_callback);
  309. static void sysrq_handle_moom(int key)
  310. {
  311. schedule_work(&moom_work);
  312. }
  313. static struct sysrq_key_op sysrq_moom_op = {
  314. .handler = sysrq_handle_moom,
  315. .help_msg = "memory-full-oom-kill(F)",
  316. .action_msg = "Manual OOM execution",
  317. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  318. };
  319. #ifdef CONFIG_BLOCK
  320. static void sysrq_handle_thaw(int key)
  321. {
  322. emergency_thaw_all();
  323. }
  324. static struct sysrq_key_op sysrq_thaw_op = {
  325. .handler = sysrq_handle_thaw,
  326. .help_msg = "thaw-filesystems(J)",
  327. .action_msg = "Emergency Thaw of all frozen filesystems",
  328. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  329. };
  330. #endif
  331. static void sysrq_handle_kill(int key)
  332. {
  333. send_sig_all(SIGKILL);
  334. console_loglevel = 8;
  335. }
  336. static struct sysrq_key_op sysrq_kill_op = {
  337. .handler = sysrq_handle_kill,
  338. .help_msg = "kill-all-tasks(I)",
  339. .action_msg = "Kill All Tasks",
  340. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  341. };
  342. static void sysrq_handle_unrt(int key)
  343. {
  344. normalize_rt_tasks();
  345. }
  346. static struct sysrq_key_op sysrq_unrt_op = {
  347. .handler = sysrq_handle_unrt,
  348. .help_msg = "nice-all-RT-tasks(N)",
  349. .action_msg = "Nice All RT Tasks",
  350. .enable_mask = SYSRQ_ENABLE_RTNICE,
  351. };
  352. /* Key Operations table and lock */
  353. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  354. static struct sysrq_key_op *sysrq_key_table[36] = {
  355. &sysrq_loglevel_op, /* 0 */
  356. &sysrq_loglevel_op, /* 1 */
  357. &sysrq_loglevel_op, /* 2 */
  358. &sysrq_loglevel_op, /* 3 */
  359. &sysrq_loglevel_op, /* 4 */
  360. &sysrq_loglevel_op, /* 5 */
  361. &sysrq_loglevel_op, /* 6 */
  362. &sysrq_loglevel_op, /* 7 */
  363. &sysrq_loglevel_op, /* 8 */
  364. &sysrq_loglevel_op, /* 9 */
  365. /*
  366. * a: Don't use for system provided sysrqs, it is handled specially on
  367. * sparc and will never arrive.
  368. */
  369. NULL, /* a */
  370. &sysrq_reboot_op, /* b */
  371. &sysrq_crash_op, /* c & ibm_emac driver debug */
  372. &sysrq_showlocks_op, /* d */
  373. &sysrq_term_op, /* e */
  374. &sysrq_moom_op, /* f */
  375. /* g: May be registered for the kernel debugger */
  376. NULL, /* g */
  377. NULL, /* h - reserved for help */
  378. &sysrq_kill_op, /* i */
  379. #ifdef CONFIG_BLOCK
  380. &sysrq_thaw_op, /* j */
  381. #else
  382. NULL, /* j */
  383. #endif
  384. &sysrq_SAK_op, /* k */
  385. #ifdef CONFIG_SMP
  386. &sysrq_showallcpus_op, /* l */
  387. #else
  388. NULL, /* l */
  389. #endif
  390. &sysrq_showmem_op, /* m */
  391. &sysrq_unrt_op, /* n */
  392. /* o: This will often be registered as 'Off' at init time */
  393. NULL, /* o */
  394. &sysrq_showregs_op, /* p */
  395. &sysrq_show_timers_op, /* q */
  396. &sysrq_unraw_op, /* r */
  397. &sysrq_sync_op, /* s */
  398. &sysrq_showstate_op, /* t */
  399. &sysrq_mountro_op, /* u */
  400. /* v: May be registered for frame buffer console restore */
  401. NULL, /* v */
  402. &sysrq_showstate_blocked_op, /* w */
  403. /* x: May be registered on ppc/powerpc for xmon */
  404. NULL, /* x */
  405. /* y: May be registered on sparc64 for global register dump */
  406. NULL, /* y */
  407. &sysrq_ftrace_dump_op, /* z */
  408. };
  409. /* key2index calculation, -1 on invalid index */
  410. static int sysrq_key_table_key2index(int key)
  411. {
  412. int retval;
  413. if ((key >= '0') && (key <= '9'))
  414. retval = key - '0';
  415. else if ((key >= 'a') && (key <= 'z'))
  416. retval = key + 10 - 'a';
  417. else
  418. retval = -1;
  419. return retval;
  420. }
  421. /*
  422. * get and put functions for the table, exposed to modules.
  423. */
  424. struct sysrq_key_op *__sysrq_get_key_op(int key)
  425. {
  426. struct sysrq_key_op *op_p = NULL;
  427. int i;
  428. i = sysrq_key_table_key2index(key);
  429. if (i != -1)
  430. op_p = sysrq_key_table[i];
  431. return op_p;
  432. }
  433. static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
  434. {
  435. int i = sysrq_key_table_key2index(key);
  436. if (i != -1)
  437. sysrq_key_table[i] = op_p;
  438. }
  439. void __handle_sysrq(int key, bool check_mask)
  440. {
  441. struct sysrq_key_op *op_p;
  442. int orig_log_level;
  443. int i;
  444. unsigned long flags;
  445. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  446. /*
  447. * Raise the apparent loglevel to maximum so that the sysrq header
  448. * is shown to provide the user with positive feedback. We do not
  449. * simply emit this at KERN_EMERG as that would change message
  450. * routing in the consumers of /proc/kmsg.
  451. */
  452. orig_log_level = console_loglevel;
  453. console_loglevel = 7;
  454. op_p = __sysrq_get_key_op(key);
  455. if (op_p) {
  456. /*
  457. * Should we check for enabled operations (/proc/sysrq-trigger
  458. * should not) and is the invoked operation enabled?
  459. */
  460. if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
  461. pr_info("%s\n", op_p->action_msg);
  462. console_loglevel = orig_log_level;
  463. op_p->handler(key);
  464. } else {
  465. pr_info("This sysrq operation is disabled.\n");
  466. console_loglevel = orig_log_level;
  467. }
  468. } else {
  469. pr_info("HELP : ");
  470. /* Only print the help msg once per handler */
  471. for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
  472. if (sysrq_key_table[i]) {
  473. int j;
  474. for (j = 0; sysrq_key_table[i] !=
  475. sysrq_key_table[j]; j++)
  476. ;
  477. if (j != i)
  478. continue;
  479. printk("%s ", sysrq_key_table[i]->help_msg);
  480. }
  481. }
  482. printk("\n");
  483. console_loglevel = orig_log_level;
  484. }
  485. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  486. }
  487. void handle_sysrq(int key)
  488. {
  489. if (sysrq_on())
  490. __handle_sysrq(key, true);
  491. }
  492. EXPORT_SYMBOL(handle_sysrq);
  493. #ifdef CONFIG_INPUT
  494. /* Simple translation table for the SysRq keys */
  495. static const unsigned char sysrq_xlate[KEY_CNT] =
  496. "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
  497. "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
  498. "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
  499. "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
  500. "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
  501. "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
  502. "\r\000/"; /* 0x60 - 0x6f */
  503. struct sysrq_state {
  504. struct input_handle handle;
  505. struct work_struct reinject_work;
  506. unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
  507. unsigned int alt;
  508. unsigned int alt_use;
  509. bool active;
  510. bool need_reinject;
  511. bool reinjecting;
  512. };
  513. static void sysrq_reinject_alt_sysrq(struct work_struct *work)
  514. {
  515. struct sysrq_state *sysrq =
  516. container_of(work, struct sysrq_state, reinject_work);
  517. struct input_handle *handle = &sysrq->handle;
  518. unsigned int alt_code = sysrq->alt_use;
  519. if (sysrq->need_reinject) {
  520. /* we do not want the assignment to be reordered */
  521. sysrq->reinjecting = true;
  522. mb();
  523. /* Simulate press and release of Alt + SysRq */
  524. input_inject_event(handle, EV_KEY, alt_code, 1);
  525. input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
  526. input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
  527. input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
  528. input_inject_event(handle, EV_KEY, alt_code, 0);
  529. input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
  530. mb();
  531. sysrq->reinjecting = false;
  532. }
  533. }
  534. static bool sysrq_filter(struct input_handle *handle,
  535. unsigned int type, unsigned int code, int value)
  536. {
  537. struct sysrq_state *sysrq = handle->private;
  538. bool was_active = sysrq->active;
  539. bool suppress;
  540. /*
  541. * Do not filter anything if we are in the process of re-injecting
  542. * Alt+SysRq combination.
  543. */
  544. if (sysrq->reinjecting)
  545. return false;
  546. switch (type) {
  547. case EV_SYN:
  548. suppress = false;
  549. break;
  550. case EV_KEY:
  551. switch (code) {
  552. case KEY_LEFTALT:
  553. case KEY_RIGHTALT:
  554. if (!value) {
  555. /* One of ALTs is being released */
  556. if (sysrq->active && code == sysrq->alt_use)
  557. sysrq->active = false;
  558. sysrq->alt = KEY_RESERVED;
  559. } else if (value != 2) {
  560. sysrq->alt = code;
  561. sysrq->need_reinject = false;
  562. }
  563. break;
  564. case KEY_SYSRQ:
  565. if (value == 1 && sysrq->alt != KEY_RESERVED) {
  566. sysrq->active = true;
  567. sysrq->alt_use = sysrq->alt;
  568. /*
  569. * If nothing else will be pressed we'll need
  570. * to re-inject Alt-SysRq keysroke.
  571. */
  572. sysrq->need_reinject = true;
  573. }
  574. /*
  575. * Pretend that sysrq was never pressed at all. This
  576. * is needed to properly handle KGDB which will try
  577. * to release all keys after exiting debugger. If we
  578. * do not clear key bit it KGDB will end up sending
  579. * release events for Alt and SysRq, potentially
  580. * triggering print screen function.
  581. */
  582. if (sysrq->active)
  583. clear_bit(KEY_SYSRQ, handle->dev->key);
  584. break;
  585. default:
  586. if (sysrq->active && value && value != 2) {
  587. sysrq->need_reinject = false;
  588. __handle_sysrq(sysrq_xlate[code], true);
  589. }
  590. break;
  591. }
  592. suppress = sysrq->active;
  593. if (!sysrq->active) {
  594. /*
  595. * If we are not suppressing key presses keep track of
  596. * keyboard state so we can release keys that have been
  597. * pressed before entering SysRq mode.
  598. */
  599. if (value)
  600. set_bit(code, sysrq->key_down);
  601. else
  602. clear_bit(code, sysrq->key_down);
  603. if (was_active)
  604. schedule_work(&sysrq->reinject_work);
  605. } else if (value == 0 &&
  606. test_and_clear_bit(code, sysrq->key_down)) {
  607. /*
  608. * Pass on release events for keys that was pressed before
  609. * entering SysRq mode.
  610. */
  611. suppress = false;
  612. }
  613. break;
  614. default:
  615. suppress = sysrq->active;
  616. break;
  617. }
  618. return suppress;
  619. }
  620. static int sysrq_connect(struct input_handler *handler,
  621. struct input_dev *dev,
  622. const struct input_device_id *id)
  623. {
  624. struct sysrq_state *sysrq;
  625. int error;
  626. sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
  627. if (!sysrq)
  628. return -ENOMEM;
  629. INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
  630. sysrq->handle.dev = dev;
  631. sysrq->handle.handler = handler;
  632. sysrq->handle.name = "sysrq";
  633. sysrq->handle.private = sysrq;
  634. error = input_register_handle(&sysrq->handle);
  635. if (error) {
  636. pr_err("Failed to register input sysrq handler, error %d\n",
  637. error);
  638. goto err_free;
  639. }
  640. error = input_open_device(&sysrq->handle);
  641. if (error) {
  642. pr_err("Failed to open input device, error %d\n", error);
  643. goto err_unregister;
  644. }
  645. return 0;
  646. err_unregister:
  647. input_unregister_handle(&sysrq->handle);
  648. err_free:
  649. kfree(sysrq);
  650. return error;
  651. }
  652. static void sysrq_disconnect(struct input_handle *handle)
  653. {
  654. struct sysrq_state *sysrq = handle->private;
  655. input_close_device(handle);
  656. cancel_work_sync(&sysrq->reinject_work);
  657. input_unregister_handle(handle);
  658. kfree(sysrq);
  659. }
  660. /*
  661. * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
  662. * keyboards have SysRq key predefined and so user may add it to keymap
  663. * later, but we expect all such keyboards to have left alt.
  664. */
  665. static const struct input_device_id sysrq_ids[] = {
  666. {
  667. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  668. INPUT_DEVICE_ID_MATCH_KEYBIT,
  669. .evbit = { BIT_MASK(EV_KEY) },
  670. .keybit = { BIT_MASK(KEY_LEFTALT) },
  671. },
  672. { },
  673. };
  674. static struct input_handler sysrq_handler = {
  675. .filter = sysrq_filter,
  676. .connect = sysrq_connect,
  677. .disconnect = sysrq_disconnect,
  678. .name = "sysrq",
  679. .id_table = sysrq_ids,
  680. };
  681. static bool sysrq_handler_registered;
  682. static inline void sysrq_register_handler(void)
  683. {
  684. int error;
  685. error = input_register_handler(&sysrq_handler);
  686. if (error)
  687. pr_err("Failed to register input handler, error %d", error);
  688. else
  689. sysrq_handler_registered = true;
  690. }
  691. static inline void sysrq_unregister_handler(void)
  692. {
  693. if (sysrq_handler_registered) {
  694. input_unregister_handler(&sysrq_handler);
  695. sysrq_handler_registered = false;
  696. }
  697. }
  698. #else
  699. static inline void sysrq_register_handler(void)
  700. {
  701. }
  702. static inline void sysrq_unregister_handler(void)
  703. {
  704. }
  705. #endif /* CONFIG_INPUT */
  706. int sysrq_toggle_support(int enable_mask)
  707. {
  708. bool was_enabled = sysrq_on();
  709. sysrq_enabled = enable_mask;
  710. if (was_enabled != sysrq_on()) {
  711. if (sysrq_on())
  712. sysrq_register_handler();
  713. else
  714. sysrq_unregister_handler();
  715. }
  716. return 0;
  717. }
  718. static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  719. struct sysrq_key_op *remove_op_p)
  720. {
  721. int retval;
  722. unsigned long flags;
  723. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  724. if (__sysrq_get_key_op(key) == remove_op_p) {
  725. __sysrq_put_key_op(key, insert_op_p);
  726. retval = 0;
  727. } else {
  728. retval = -1;
  729. }
  730. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  731. return retval;
  732. }
  733. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  734. {
  735. return __sysrq_swap_key_ops(key, op_p, NULL);
  736. }
  737. EXPORT_SYMBOL(register_sysrq_key);
  738. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  739. {
  740. return __sysrq_swap_key_ops(key, NULL, op_p);
  741. }
  742. EXPORT_SYMBOL(unregister_sysrq_key);
  743. #ifdef CONFIG_PROC_FS
  744. /*
  745. * writing 'C' to /proc/sysrq-trigger is like sysrq-C
  746. */
  747. static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
  748. size_t count, loff_t *ppos)
  749. {
  750. if (count) {
  751. char c;
  752. if (get_user(c, buf))
  753. return -EFAULT;
  754. __handle_sysrq(c, false);
  755. }
  756. return count;
  757. }
  758. static const struct file_operations proc_sysrq_trigger_operations = {
  759. .write = write_sysrq_trigger,
  760. .llseek = noop_llseek,
  761. };
  762. static void sysrq_init_procfs(void)
  763. {
  764. if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
  765. &proc_sysrq_trigger_operations))
  766. pr_err("Failed to register proc interface\n");
  767. }
  768. #else
  769. static inline void sysrq_init_procfs(void)
  770. {
  771. }
  772. #endif /* CONFIG_PROC_FS */
  773. static int __init sysrq_init(void)
  774. {
  775. sysrq_init_procfs();
  776. if (sysrq_on())
  777. sysrq_register_handler();
  778. return 0;
  779. }
  780. module_init(sysrq_init);