sysrq.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 on ppc and sh platforms.
  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' - Dumps Voyager SMP processor info to your console.
  82. 'w' - Dumps tasks that are in uninterruptable (blocked) state.
  83. 'x' - Used by xmon interface on ppc/powerpc platforms.
  84. 'z' - Dump the ftrace buffer
  85. '0'-'9' - Sets the console log level, controlling which kernel messages
  86. will be printed to your console. ('0', for example would make
  87. it so that only emergency messages like PANICs or OOPSes would
  88. make it to your console.)
  89. * Okay, so what can I use them for?
  90. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91. Well, un'R'aw is very handy when your X server or a svgalib program crashes.
  92. sa'K' (Secure Access Key) is useful when you want to be sure there is no
  93. trojan program running at console which could grab your password
  94. when you would try to login. It will kill all programs on given console,
  95. thus letting you make sure that the login prompt you see is actually
  96. the one from init, not some trojan program.
  97. IMPORTANT: In its true form it is not a true SAK like the one in a :IMPORTANT
  98. IMPORTANT: c2 compliant system, and it should not be mistaken as :IMPORTANT
  99. IMPORTANT: such. :IMPORTANT
  100. It seems others find it useful as (System Attention Key) which is
  101. useful when you want to exit a program that will not let you switch consoles.
  102. (For example, X or a svgalib program.)
  103. re'B'oot is good when you're unable to shut down. But you should also 'S'ync
  104. and 'U'mount first.
  105. 'C'rash can be used to manually trigger a crashdump when the system is hung.
  106. Note that this just triggers a crash if there is no dump mechanism available.
  107. 'S'ync is great when your system is locked up, it allows you to sync your
  108. disks and will certainly lessen the chance of data loss and fscking. Note
  109. that the sync hasn't taken place until you see the "OK" and "Done" appear
  110. on the screen. (If the kernel is really in strife, you may not ever get the
  111. OK or Done message...)
  112. 'U'mount is basically useful in the same ways as 'S'ync. I generally 'S'ync,
  113. 'U'mount, then re'B'oot when my system locks. It's saved me many a fsck.
  114. Again, the unmount (remount read-only) hasn't taken place until you see the
  115. "OK" and "Done" message appear on the screen.
  116. The loglevels '0'-'9' are useful when your console is being flooded with
  117. kernel messages you do not want to see. Selecting '0' will prevent all but
  118. the most urgent kernel messages from reaching your console. (They will
  119. still be logged if syslogd/klogd are alive, though.)
  120. t'E'rm and k'I'll are useful if you have some sort of runaway process you
  121. are unable to kill any other way, especially if it's spawning other
  122. processes.
  123. "'J'ust thaw it" is useful if your system becomes unresponsive due to a frozen
  124. (probably root) filesystem via the FIFREEZE ioctl.
  125. * Sometimes SysRq seems to get 'stuck' after using it, what can I do?
  126. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  127. That happens to me, also. I've found that tapping shift, alt, and control
  128. on both sides of the keyboard, and hitting an invalid sysrq sequence again
  129. will fix the problem. (i.e., something like alt-sysrq-z). Switching to another
  130. virtual console (ALT+Fn) and then back again should also help.
  131. * I hit SysRq, but nothing seems to happen, what's wrong?
  132. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. There are some keyboards that send different scancodes for SysRq than the
  134. pre-defined 0x54. So if SysRq doesn't work out of the box for a certain
  135. keyboard, run 'showkey -s' to find out the proper scancode sequence. Then
  136. use 'setkeycodes <sequence> 84' to define this sequence to the usual SysRq
  137. code (84 is decimal for 0x54). It's probably best to put this command in a
  138. boot script. Oh, and by the way, you exit 'showkey' by not typing anything
  139. for ten seconds.
  140. * I want to add SysRQ key events to a module, how does it work?
  141. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  142. In order to register a basic function with the table, you must first include
  143. the header 'include/linux/sysrq.h', this will define everything else you need.
  144. Next, you must create a sysrq_key_op struct, and populate it with A) the key
  145. handler function you will use, B) a help_msg string, that will print when SysRQ
  146. prints help, and C) an action_msg string, that will print right before your
  147. handler is called. Your handler must conform to the prototype in 'sysrq.h'.
  148. After the sysrq_key_op is created, you can call the kernel function
  149. register_sysrq_key(int key, struct sysrq_key_op *op_p); this will
  150. register the operation pointed to by 'op_p' at table key 'key',
  151. if that slot in the table is blank. At module unload time, you must call
  152. the function unregister_sysrq_key(int key, struct sysrq_key_op *op_p), which
  153. will remove the key op pointed to by 'op_p' from the key 'key', if and only if
  154. it is currently registered in that slot. This is in case the slot has been
  155. overwritten since you registered it.
  156. The Magic SysRQ system works by registering key operations against a key op
  157. lookup table, which is defined in 'drivers/char/sysrq.c'. This key table has
  158. a number of operations registered into it at compile time, but is mutable,
  159. and 2 functions are exported for interface to it:
  160. register_sysrq_key and unregister_sysrq_key.
  161. Of course, never ever leave an invalid pointer in the table. I.e., when
  162. your module that called register_sysrq_key() exits, it must call
  163. unregister_sysrq_key() to clean up the sysrq key table entry that it used.
  164. Null pointers in the table are always safe. :)
  165. If for some reason you feel the need to call the handle_sysrq function from
  166. within a function called by handle_sysrq, you must be aware that you are in
  167. a lock (you are also in an interrupt handler, which means don't sleep!), so
  168. you must call __handle_sysrq_nolock instead.
  169. * When I hit a SysRq key combination only the header appears on the console?
  170. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  171. Sysrq output is subject to the same console loglevel control as all
  172. other console output. This means that if the kernel was booted 'quiet'
  173. as is common on distro kernels the output may not appear on the actual
  174. console, even though it will appear in the dmesg buffer, and be accessible
  175. via the dmesg command and to the consumers of /proc/kmsg. As a specific
  176. exception the header line from the sysrq command is passed to all console
  177. consumers as if the current loglevel was maximum. If only the header
  178. is emitted it is almost certain that the kernel loglevel is too low.
  179. Should you require the output on the console channel then you will need
  180. to temporarily up the console loglevel using alt-sysrq-8 or:
  181. echo 8 > /proc/sysrq-trigger
  182. Remember to return the loglevel to normal after triggering the sysrq
  183. command you are interested in.
  184. * I have more questions, who can I ask?
  185. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  186. And I'll answer any questions about the registration system you got, also
  187. responding as soon as possible.
  188. -Crutcher
  189. * Credits
  190. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  191. Written by Mydraal <vulpyne@vulpyne.net>
  192. Updated by Adam Sulmicki <adam@cfar.umd.edu>
  193. Updated by Jeremy M. Dolan <jmd@turbogeek.org> 2001/01/28 10:15:59
  194. Added to by Crutcher Dunnavant <crutcher+kernel@datastacks.com>