sysrq.c 21 KB

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