sysrq.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 = 1;
  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();
  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_MAX + 1] =
  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. static bool sysrq_down;
  501. static int sysrq_alt_use;
  502. static int sysrq_alt;
  503. static bool sysrq_filter(struct input_handle *handle, unsigned int type,
  504. unsigned int code, int value)
  505. {
  506. if (type != EV_KEY)
  507. goto out;
  508. switch (code) {
  509. case KEY_LEFTALT:
  510. case KEY_RIGHTALT:
  511. if (value)
  512. sysrq_alt = code;
  513. else {
  514. if (sysrq_down && code == sysrq_alt_use)
  515. sysrq_down = false;
  516. sysrq_alt = 0;
  517. }
  518. break;
  519. case KEY_SYSRQ:
  520. if (value == 1 && sysrq_alt) {
  521. sysrq_down = true;
  522. sysrq_alt_use = sysrq_alt;
  523. }
  524. break;
  525. default:
  526. if (sysrq_down && value && value != 2)
  527. __handle_sysrq(sysrq_xlate[code], true);
  528. break;
  529. }
  530. out:
  531. return sysrq_down;
  532. }
  533. static int sysrq_connect(struct input_handler *handler,
  534. struct input_dev *dev,
  535. const struct input_device_id *id)
  536. {
  537. struct input_handle *handle;
  538. int error;
  539. sysrq_down = false;
  540. sysrq_alt = 0;
  541. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  542. if (!handle)
  543. return -ENOMEM;
  544. handle->dev = dev;
  545. handle->handler = handler;
  546. handle->name = "sysrq";
  547. error = input_register_handle(handle);
  548. if (error) {
  549. pr_err("Failed to register input sysrq handler, error %d\n",
  550. error);
  551. goto err_free;
  552. }
  553. error = input_open_device(handle);
  554. if (error) {
  555. pr_err("Failed to open input device, error %d\n", error);
  556. goto err_unregister;
  557. }
  558. return 0;
  559. err_unregister:
  560. input_unregister_handle(handle);
  561. err_free:
  562. kfree(handle);
  563. return error;
  564. }
  565. static void sysrq_disconnect(struct input_handle *handle)
  566. {
  567. input_close_device(handle);
  568. input_unregister_handle(handle);
  569. kfree(handle);
  570. }
  571. /*
  572. * We are matching on KEY_LEFTALT insteard of KEY_SYSRQ because not all
  573. * keyboards have SysRq ikey predefined and so user may add it to keymap
  574. * later, but we expect all such keyboards to have left alt.
  575. */
  576. static const struct input_device_id sysrq_ids[] = {
  577. {
  578. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  579. INPUT_DEVICE_ID_MATCH_KEYBIT,
  580. .evbit = { BIT_MASK(EV_KEY) },
  581. .keybit = { BIT_MASK(KEY_LEFTALT) },
  582. },
  583. { },
  584. };
  585. static struct input_handler sysrq_handler = {
  586. .filter = sysrq_filter,
  587. .connect = sysrq_connect,
  588. .disconnect = sysrq_disconnect,
  589. .name = "sysrq",
  590. .id_table = sysrq_ids,
  591. };
  592. static bool sysrq_handler_registered;
  593. static inline void sysrq_register_handler(void)
  594. {
  595. int error;
  596. error = input_register_handler(&sysrq_handler);
  597. if (error)
  598. pr_err("Failed to register input handler, error %d", error);
  599. else
  600. sysrq_handler_registered = true;
  601. }
  602. static inline void sysrq_unregister_handler(void)
  603. {
  604. if (sysrq_handler_registered) {
  605. input_unregister_handler(&sysrq_handler);
  606. sysrq_handler_registered = false;
  607. }
  608. }
  609. #else
  610. static inline void sysrq_register_handler(void)
  611. {
  612. }
  613. static inline void sysrq_unregister_handler(void)
  614. {
  615. }
  616. #endif /* CONFIG_INPUT */
  617. int sysrq_toggle_support(int enable_mask)
  618. {
  619. bool was_enabled = sysrq_on();
  620. sysrq_enabled = enable_mask;
  621. if (was_enabled != sysrq_on()) {
  622. if (sysrq_on())
  623. sysrq_register_handler();
  624. else
  625. sysrq_unregister_handler();
  626. }
  627. return 0;
  628. }
  629. static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  630. struct sysrq_key_op *remove_op_p)
  631. {
  632. int retval;
  633. unsigned long flags;
  634. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  635. if (__sysrq_get_key_op(key) == remove_op_p) {
  636. __sysrq_put_key_op(key, insert_op_p);
  637. retval = 0;
  638. } else {
  639. retval = -1;
  640. }
  641. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  642. return retval;
  643. }
  644. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  645. {
  646. return __sysrq_swap_key_ops(key, op_p, NULL);
  647. }
  648. EXPORT_SYMBOL(register_sysrq_key);
  649. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  650. {
  651. return __sysrq_swap_key_ops(key, NULL, op_p);
  652. }
  653. EXPORT_SYMBOL(unregister_sysrq_key);
  654. #ifdef CONFIG_PROC_FS
  655. /*
  656. * writing 'C' to /proc/sysrq-trigger is like sysrq-C
  657. */
  658. static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
  659. size_t count, loff_t *ppos)
  660. {
  661. if (count) {
  662. char c;
  663. if (get_user(c, buf))
  664. return -EFAULT;
  665. __handle_sysrq(c, false);
  666. }
  667. return count;
  668. }
  669. static const struct file_operations proc_sysrq_trigger_operations = {
  670. .write = write_sysrq_trigger,
  671. };
  672. static void sysrq_init_procfs(void)
  673. {
  674. if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
  675. &proc_sysrq_trigger_operations))
  676. pr_err("Failed to register proc interface\n");
  677. }
  678. #else
  679. static inline void sysrq_init_procfs(void)
  680. {
  681. }
  682. #endif /* CONFIG_PROC_FS */
  683. static int __init sysrq_init(void)
  684. {
  685. sysrq_init_procfs();
  686. if (sysrq_on())
  687. sysrq_register_handler();
  688. return 0;
  689. }
  690. module_init(sysrq_init);