sysrq.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. Linux Magic System Request Key Hacks
  2. Documentation for sysrq.c
  3. * What is the magic SysRq key?
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. It is a 'magical' key combo you can hit which the kernel will respond to
  6. regardless of whatever else it is doing, unless it is completely locked up.
  7. * How do I enable the magic SysRq key?
  8. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. You need to say "yes" to 'Magic SysRq key (CONFIG_MAGIC_SYSRQ)' when
  10. configuring the kernel. When running a kernel with SysRq compiled in,
  11. /proc/sys/kernel/sysrq controls the functions allowed to be invoked via
  12. the SysRq key. By default the file contains 1 which means that every
  13. possible SysRq request is allowed (in older versions SysRq was disabled
  14. by default, and you were required to specifically enable it at run-time
  15. but this is not the case any more). Here is the list of possible values
  16. in /proc/sys/kernel/sysrq:
  17. 0 - disable sysrq completely
  18. 1 - enable all functions of sysrq
  19. >1 - bitmask of allowed sysrq functions (see below for detailed function
  20. description):
  21. 2 - enable control of console logging level
  22. 4 - enable control of keyboard (SAK, unraw)
  23. 8 - enable debugging dumps of processes etc.
  24. 16 - enable sync command
  25. 32 - enable remount read-only
  26. 64 - enable signalling of processes (term, kill, oom-kill)
  27. 128 - allow reboot/poweroff
  28. 256 - allow nicing of all RT tasks
  29. You can set the value in the file by the following command:
  30. echo "number" >/proc/sys/kernel/sysrq
  31. Note that the value of /proc/sys/kernel/sysrq influences only the invocation
  32. via a keyboard. Invocation of any operation via /proc/sysrq-trigger is always
  33. allowed (by a user with admin privileges).
  34. * How do I use the magic SysRq key?
  35. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  36. On x86 - You press the key combo 'ALT-SysRq-<command key>'. Note - Some
  37. keyboards may not have a key labeled 'SysRq'. The 'SysRq' key is
  38. also known as the 'Print Screen' key. Also some keyboards cannot
  39. handle so many keys being pressed at the same time, so you might
  40. have better luck with "press Alt", "press SysRq", "release SysRq",
  41. "press <command key>", release everything.
  42. On SPARC - You press 'ALT-STOP-<command key>', I believe.
  43. On the serial console (PC style standard serial ports only) -
  44. You send a BREAK, then within 5 seconds a command key. Sending
  45. BREAK twice is interpreted as a normal BREAK.
  46. On PowerPC - Press 'ALT - Print Screen (or F13) - <command key>,
  47. Print Screen (or F13) - <command key> may suffice.
  48. On other - If you know of the key combos for other architectures, please
  49. let me know so I can add them to this section.
  50. On all - write a character to /proc/sysrq-trigger. e.g.:
  51. echo t > /proc/sysrq-trigger
  52. * What are the 'command' keys?
  53. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  54. 'b' - Will immediately reboot the system without syncing or unmounting
  55. your disks.
  56. 'c' - Will perform a system crash by a NULL pointer dereference.
  57. A crashdump will be taken if configured.
  58. 'd' - Shows all locks that are held.
  59. 'e' - Send a SIGTERM to all processes, except for init.
  60. 'f' - Will call oom_kill to kill a memory hog process.
  61. 'g' - Used by kgdb (kernel debugger)
  62. 'h' - Will display help (actually any other key than those listed
  63. here will display help. but 'h' is easy to remember :-)
  64. 'i' - Send a SIGKILL to all processes, except for init.
  65. 'j' - Forcibly "Just thaw it" - filesystems frozen by the FIFREEZE ioctl.
  66. 'k' - Secure Access Key (SAK) Kills all programs on the current virtual
  67. console. NOTE: See important comments below in SAK section.
  68. 'l' - Shows a stack backtrace for all active CPUs.
  69. 'm' - Will dump current memory info to your console.
  70. 'n' - Used to make RT tasks nice-able
  71. 'o' - Will shut your system off (if configured and supported).
  72. 'p' - Will dump the current registers and flags to your console.
  73. 'q' - Will dump per CPU lists of all armed hrtimers (but NOT regular
  74. timer_list timers) and detailed information about all
  75. clockevent devices.
  76. 'r' - Turns off keyboard raw mode and sets it to XLATE.
  77. 's' - Will attempt to sync all mounted filesystems.
  78. 't' - Will dump a list of current tasks and their information to your
  79. console.
  80. 'u' - Will attempt to remount all mounted filesystems read-only.
  81. 'v' - Forcefully restores framebuffer console
  82. 'v' - Causes ETM buffer dump [ARM-specific]
  83. 'w' - Dumps tasks that are in uninterruptable (blocked) state.
  84. 'x' - Used by xmon interface on ppc/powerpc platforms.
  85. 'y' - Show global CPU Registers [SPARC-64 specific]
  86. 'z' - Dump the ftrace buffer
  87. '0'-'9' - Sets the console log level, controlling which kernel messages
  88. will be printed to your console. ('0', for example would make
  89. it so that only emergency messages like PANICs or OOPSes would
  90. make it to your console.)
  91. * Okay, so what can I use them for?
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. Well, un'R'aw is very handy when your X server or a svgalib program crashes.
  94. sa'K' (Secure Access Key) is useful when you want to be sure there is no
  95. trojan program running at console which could grab your password
  96. when you would try to login. It will kill all programs on given console,
  97. thus letting you make sure that the login prompt you see is actually
  98. the one from init, not some trojan program.
  99. IMPORTANT: In its true form it is not a true SAK like the one in a :IMPORTANT
  100. IMPORTANT: c2 compliant system, and it should not be mistaken as :IMPORTANT
  101. IMPORTANT: such. :IMPORTANT
  102. It seems others find it useful as (System Attention Key) which is
  103. useful when you want to exit a program that will not let you switch consoles.
  104. (For example, X or a svgalib program.)
  105. re'B'oot is good when you're unable to shut down. But you should also 'S'ync
  106. and 'U'mount first.
  107. 'C'rash can be used to manually trigger a crashdump when the system is hung.
  108. Note that this just triggers a crash if there is no dump mechanism available.
  109. 'S'ync is great when your system is locked up, it allows you to sync your
  110. disks and will certainly lessen the chance of data loss and fscking. Note
  111. that the sync hasn't taken place until you see the "OK" and "Done" appear
  112. on the screen. (If the kernel is really in strife, you may not ever get the
  113. OK or Done message...)
  114. 'U'mount is basically useful in the same ways as 'S'ync. I generally 'S'ync,
  115. 'U'mount, then re'B'oot when my system locks. It's saved me many a fsck.
  116. Again, the unmount (remount read-only) hasn't taken place until you see the
  117. "OK" and "Done" message appear on the screen.
  118. The loglevels '0'-'9' are useful when your console is being flooded with
  119. kernel messages you do not want to see. Selecting '0' will prevent all but
  120. the most urgent kernel messages from reaching your console. (They will
  121. still be logged if syslogd/klogd are alive, though.)
  122. t'E'rm and k'I'll are useful if you have some sort of runaway process you
  123. are unable to kill any other way, especially if it's spawning other
  124. processes.
  125. "'J'ust thaw it" is useful if your system becomes unresponsive due to a frozen
  126. (probably root) filesystem via the FIFREEZE ioctl.
  127. * Sometimes SysRq seems to get 'stuck' after using it, what can I do?
  128. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  129. That happens to me, also. I've found that tapping shift, alt, and control
  130. on both sides of the keyboard, and hitting an invalid sysrq sequence again
  131. will fix the problem. (i.e., something like alt-sysrq-z). Switching to another
  132. virtual console (ALT+Fn) and then back again should also help.
  133. * I hit SysRq, but nothing seems to happen, what's wrong?
  134. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  135. There are some keyboards that produce a different keycode for SysRq than the
  136. pre-defined value of 99 (see KEY_SYSRQ in include/linux/input.h), or which
  137. don't have a SysRq key at all. In these cases, run 'showkey -s' to find an
  138. appropriate scancode sequence, and use 'setkeycodes <sequence> 99' to map
  139. this sequence to the usual SysRq code (e.g., 'setkeycodes e05b 99'). It's
  140. probably best to put this command in a boot script. Oh, and by the way, you
  141. exit 'showkey' by not typing anything for ten seconds.
  142. * I want to add SysRQ key events to a module, how does it work?
  143. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144. In order to register a basic function with the table, you must first include
  145. the header 'include/linux/sysrq.h', this will define everything else you need.
  146. Next, you must create a sysrq_key_op struct, and populate it with A) the key
  147. handler function you will use, B) a help_msg string, that will print when SysRQ
  148. prints help, and C) an action_msg string, that will print right before your
  149. handler is called. Your handler must conform to the prototype in 'sysrq.h'.
  150. After the sysrq_key_op is created, you can call the kernel function
  151. register_sysrq_key(int key, struct sysrq_key_op *op_p); this will
  152. register the operation pointed to by 'op_p' at table key 'key',
  153. if that slot in the table is blank. At module unload time, you must call
  154. the function unregister_sysrq_key(int key, struct sysrq_key_op *op_p), which
  155. will remove the key op pointed to by 'op_p' from the key 'key', if and only if
  156. it is currently registered in that slot. This is in case the slot has been
  157. overwritten since you registered it.
  158. The Magic SysRQ system works by registering key operations against a key op
  159. lookup table, which is defined in 'drivers/char/sysrq.c'. This key table has
  160. a number of operations registered into it at compile time, but is mutable,
  161. and 2 functions are exported for interface to it:
  162. register_sysrq_key and unregister_sysrq_key.
  163. Of course, never ever leave an invalid pointer in the table. I.e., when
  164. your module that called register_sysrq_key() exits, it must call
  165. unregister_sysrq_key() to clean up the sysrq key table entry that it used.
  166. Null pointers in the table are always safe. :)
  167. If for some reason you feel the need to call the handle_sysrq function from
  168. within a function called by handle_sysrq, you must be aware that you are in
  169. a lock (you are also in an interrupt handler, which means don't sleep!), so
  170. you must call __handle_sysrq_nolock instead.
  171. * When I hit a SysRq key combination only the header appears on the console?
  172. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  173. Sysrq output is subject to the same console loglevel control as all
  174. other console output. This means that if the kernel was booted 'quiet'
  175. as is common on distro kernels the output may not appear on the actual
  176. console, even though it will appear in the dmesg buffer, and be accessible
  177. via the dmesg command and to the consumers of /proc/kmsg. As a specific
  178. exception the header line from the sysrq command is passed to all console
  179. consumers as if the current loglevel was maximum. If only the header
  180. is emitted it is almost certain that the kernel loglevel is too low.
  181. Should you require the output on the console channel then you will need
  182. to temporarily up the console loglevel using alt-sysrq-8 or:
  183. echo 8 > /proc/sysrq-trigger
  184. Remember to return the loglevel to normal after triggering the sysrq
  185. command you are interested in.
  186. * I have more questions, who can I ask?
  187. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  188. Just ask them on the linux-kernel mailing list:
  189. linux-kernel@vger.kernel.org
  190. * Credits
  191. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  192. Written by Mydraal <vulpyne@vulpyne.net>
  193. Updated by Adam Sulmicki <adam@cfar.umd.edu>
  194. Updated by Jeremy M. Dolan <jmd@turbogeek.org> 2001/01/28 10:15:59
  195. Added to by Crutcher Dunnavant <crutcher+kernel@datastacks.com>