Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* -*- linux-c -*- |
| 2 | * |
| 3 | * $Id: sysrq.c,v 1.15 1998/08/23 14:56:41 mj Exp $ |
| 4 | * |
| 5 | * Linux Magic System Request Key Hacks |
| 6 | * |
| 7 | * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz> |
| 8 | * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz> |
| 9 | * |
| 10 | * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
| 11 | * overhauled to use key registration |
| 12 | * based upon discusions in irc://irc.openprojects.net/#kernelnewbies |
| 13 | */ |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/sched.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/tty.h> |
| 20 | #include <linux/mount.h> |
| 21 | #include <linux/kdev_t.h> |
| 22 | #include <linux/major.h> |
| 23 | #include <linux/reboot.h> |
| 24 | #include <linux/sysrq.h> |
| 25 | #include <linux/kbd_kern.h> |
| 26 | #include <linux/quotaops.h> |
| 27 | #include <linux/smp_lock.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/suspend.h> |
| 31 | #include <linux/writeback.h> |
| 32 | #include <linux/buffer_head.h> /* for fsync_bdev() */ |
| 33 | #include <linux/swap.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/vt_kern.h> |
| 36 | #include <linux/workqueue.h> |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 37 | #include <linux/kexec.h> |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 38 | #include <linux/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <asm/ptrace.h> |
Heiko Carstens | 2033b0c | 2006-10-06 16:38:42 +0200 | [diff] [blame] | 41 | #include <asm/irq_regs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* Whether we react on sysrq keys or just ignore them */ |
| 44 | int sysrq_enabled = 1; |
| 45 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 46 | static void sysrq_handle_loglevel(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | int i; |
| 49 | i = key - '0'; |
| 50 | console_loglevel = 7; |
| 51 | printk("Loglevel set to %d\n", i); |
| 52 | console_loglevel = i; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 53 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static struct sysrq_key_op sysrq_loglevel_op = { |
| 55 | .handler = sysrq_handle_loglevel, |
| 56 | .help_msg = "loglevel0-8", |
| 57 | .action_msg = "Changing Loglevel", |
| 58 | .enable_mask = SYSRQ_ENABLE_LOG, |
| 59 | }; |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #ifdef CONFIG_VT |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 62 | static void sysrq_handle_SAK(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
| 64 | if (tty) |
| 65 | do_SAK(tty); |
| 66 | reset_vc(vc_cons[fg_console].d); |
| 67 | } |
| 68 | static struct sysrq_key_op sysrq_SAK_op = { |
| 69 | .handler = sysrq_handle_SAK, |
| 70 | .help_msg = "saK", |
| 71 | .action_msg = "SAK", |
| 72 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, |
| 73 | }; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 74 | #else |
| 75 | #define sysrq_SAK_op (*(struct sysrq_key_op *)0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #endif |
| 77 | |
| 78 | #ifdef CONFIG_VT |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 79 | static void sysrq_handle_unraw(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | struct kbd_struct *kbd = &kbd_table[fg_console]; |
| 82 | |
| 83 | if (kbd) |
| 84 | kbd->kbdmode = VC_XLATE; |
| 85 | } |
| 86 | static struct sysrq_key_op sysrq_unraw_op = { |
| 87 | .handler = sysrq_handle_unraw, |
| 88 | .help_msg = "unRaw", |
| 89 | .action_msg = "Keyboard mode set to XLATE", |
| 90 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, |
| 91 | }; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 92 | #else |
| 93 | #define sysrq_unraw_op (*(struct sysrq_key_op *)0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #endif /* CONFIG_VT */ |
| 95 | |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 96 | #ifdef CONFIG_KEXEC |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 97 | static void sysrq_handle_crashdump(int key, struct tty_struct *tty) |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 98 | { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 99 | crash_kexec(get_irq_regs()); |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 100 | } |
| 101 | static struct sysrq_key_op sysrq_crashdump_op = { |
| 102 | .handler = sysrq_handle_crashdump, |
| 103 | .help_msg = "Crashdump", |
| 104 | .action_msg = "Trigger a crashdump", |
| 105 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 106 | }; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 107 | #else |
| 108 | #define sysrq_crashdump_op (*(struct sysrq_key_op *)0) |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 109 | #endif |
| 110 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 111 | static void sysrq_handle_reboot(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Peter Zijlstra | b2e9c7d | 2006-09-30 23:28:02 -0700 | [diff] [blame] | 113 | lockdep_off(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | local_irq_enable(); |
Eric W. Biederman | 4de8b9b | 2005-07-26 11:51:06 -0600 | [diff] [blame] | 115 | emergency_restart(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | static struct sysrq_key_op sysrq_reboot_op = { |
| 118 | .handler = sysrq_handle_reboot, |
| 119 | .help_msg = "reBoot", |
| 120 | .action_msg = "Resetting", |
| 121 | .enable_mask = SYSRQ_ENABLE_BOOT, |
| 122 | }; |
| 123 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 124 | static void sysrq_handle_sync(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | emergency_sync(); |
| 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | static struct sysrq_key_op sysrq_sync_op = { |
| 129 | .handler = sysrq_handle_sync, |
| 130 | .help_msg = "Sync", |
| 131 | .action_msg = "Emergency Sync", |
| 132 | .enable_mask = SYSRQ_ENABLE_SYNC, |
| 133 | }; |
| 134 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 135 | static void sysrq_handle_mountro(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | emergency_remount(); |
| 138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | static struct sysrq_key_op sysrq_mountro_op = { |
| 140 | .handler = sysrq_handle_mountro, |
| 141 | .help_msg = "Unmount", |
| 142 | .action_msg = "Emergency Remount R/O", |
| 143 | .enable_mask = SYSRQ_ENABLE_REMOUNT, |
| 144 | }; |
| 145 | |
Ingo Molnar | 8c64580 | 2006-07-03 00:24:56 -0700 | [diff] [blame] | 146 | #ifdef CONFIG_LOCKDEP |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 147 | static void sysrq_handle_showlocks(int key, struct tty_struct *tty) |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 148 | { |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 149 | debug_show_all_locks(); |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 150 | } |
Ingo Molnar | 8c64580 | 2006-07-03 00:24:56 -0700 | [diff] [blame] | 151 | |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 152 | static struct sysrq_key_op sysrq_showlocks_op = { |
| 153 | .handler = sysrq_handle_showlocks, |
| 154 | .help_msg = "show-all-locks(D)", |
| 155 | .action_msg = "Show Locks Held", |
| 156 | }; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 157 | #else |
| 158 | #define sysrq_showlocks_op (*(struct sysrq_key_op *)0) |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 159 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 161 | static void sysrq_handle_showregs(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 163 | struct pt_regs *regs = get_irq_regs(); |
| 164 | if (regs) |
| 165 | show_regs(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | static struct sysrq_key_op sysrq_showregs_op = { |
| 168 | .handler = sysrq_handle_showregs, |
| 169 | .help_msg = "showPc", |
| 170 | .action_msg = "Show Regs", |
| 171 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 172 | }; |
| 173 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 174 | static void sysrq_handle_showstate(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | show_state(); |
| 177 | } |
| 178 | static struct sysrq_key_op sysrq_showstate_op = { |
| 179 | .handler = sysrq_handle_showstate, |
| 180 | .help_msg = "showTasks", |
| 181 | .action_msg = "Show State", |
| 182 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 183 | }; |
| 184 | |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame^] | 185 | static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty) |
| 186 | { |
| 187 | show_state_filter(TASK_UNINTERRUPTIBLE); |
| 188 | } |
| 189 | static struct sysrq_key_op sysrq_showstate_blocked_op = { |
| 190 | .handler = sysrq_handle_showstate_blocked, |
| 191 | .help_msg = "showBlockedTasks", |
| 192 | .action_msg = "Show Blocked State", |
| 193 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 194 | }; |
| 195 | |
| 196 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 197 | static void sysrq_handle_showmem(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | show_mem(); |
| 200 | } |
| 201 | static struct sysrq_key_op sysrq_showmem_op = { |
| 202 | .handler = sysrq_handle_showmem, |
| 203 | .help_msg = "showMem", |
| 204 | .action_msg = "Show Memory", |
| 205 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 206 | }; |
| 207 | |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 208 | /* |
| 209 | * Signal sysrq helper function. Sends a signal to all user processes. |
| 210 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | static void send_sig_all(int sig) |
| 212 | { |
| 213 | struct task_struct *p; |
| 214 | |
| 215 | for_each_process(p) { |
Sukadev Bhattiprolu | f400e19 | 2006-09-29 02:00:07 -0700 | [diff] [blame] | 216 | if (p->mm && !is_init(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* Not swapper, init nor kernel thread */ |
| 218 | force_sig(sig, p); |
| 219 | } |
| 220 | } |
| 221 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 222 | static void sysrq_handle_term(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | send_sig_all(SIGTERM); |
| 225 | console_loglevel = 8; |
| 226 | } |
| 227 | static struct sysrq_key_op sysrq_term_op = { |
| 228 | .handler = sysrq_handle_term, |
| 229 | .help_msg = "tErm", |
| 230 | .action_msg = "Terminate All Tasks", |
| 231 | .enable_mask = SYSRQ_ENABLE_SIGNAL, |
| 232 | }; |
| 233 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 234 | static void moom_callback(struct work_struct *ignored) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 236 | out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL], |
| 237 | GFP_KERNEL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 240 | static DECLARE_WORK(moom_work, moom_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 242 | static void sysrq_handle_moom(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
| 244 | schedule_work(&moom_work); |
| 245 | } |
| 246 | static struct sysrq_key_op sysrq_moom_op = { |
| 247 | .handler = sysrq_handle_moom, |
| 248 | .help_msg = "Full", |
| 249 | .action_msg = "Manual OOM execution", |
| 250 | }; |
| 251 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 252 | static void sysrq_handle_kill(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
| 254 | send_sig_all(SIGKILL); |
| 255 | console_loglevel = 8; |
| 256 | } |
| 257 | static struct sysrq_key_op sysrq_kill_op = { |
| 258 | .handler = sysrq_handle_kill, |
| 259 | .help_msg = "kIll", |
| 260 | .action_msg = "Kill All Tasks", |
| 261 | .enable_mask = SYSRQ_ENABLE_SIGNAL, |
| 262 | }; |
| 263 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 264 | static void sysrq_handle_unrt(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
| 266 | normalize_rt_tasks(); |
| 267 | } |
| 268 | static struct sysrq_key_op sysrq_unrt_op = { |
| 269 | .handler = sysrq_handle_unrt, |
| 270 | .help_msg = "Nice", |
| 271 | .action_msg = "Nice All RT Tasks", |
| 272 | .enable_mask = SYSRQ_ENABLE_RTNICE, |
| 273 | }; |
| 274 | |
| 275 | /* Key Operations table and lock */ |
| 276 | static DEFINE_SPINLOCK(sysrq_key_table_lock); |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 277 | |
| 278 | static struct sysrq_key_op *sysrq_key_table[36] = { |
| 279 | &sysrq_loglevel_op, /* 0 */ |
| 280 | &sysrq_loglevel_op, /* 1 */ |
| 281 | &sysrq_loglevel_op, /* 2 */ |
| 282 | &sysrq_loglevel_op, /* 3 */ |
| 283 | &sysrq_loglevel_op, /* 4 */ |
| 284 | &sysrq_loglevel_op, /* 5 */ |
| 285 | &sysrq_loglevel_op, /* 6 */ |
| 286 | &sysrq_loglevel_op, /* 7 */ |
| 287 | &sysrq_loglevel_op, /* 8 */ |
| 288 | &sysrq_loglevel_op, /* 9 */ |
| 289 | |
| 290 | /* |
| 291 | * Don't use for system provided sysrqs, it is handled specially on |
| 292 | * sparc and will never arrive |
| 293 | */ |
| 294 | NULL, /* a */ |
| 295 | &sysrq_reboot_op, /* b */ |
| 296 | &sysrq_crashdump_op, /* c */ |
| 297 | &sysrq_showlocks_op, /* d */ |
| 298 | &sysrq_term_op, /* e */ |
| 299 | &sysrq_moom_op, /* f */ |
| 300 | NULL, /* g */ |
| 301 | NULL, /* h */ |
| 302 | &sysrq_kill_op, /* i */ |
| 303 | NULL, /* j */ |
| 304 | &sysrq_SAK_op, /* k */ |
| 305 | NULL, /* l */ |
| 306 | &sysrq_showmem_op, /* m */ |
| 307 | &sysrq_unrt_op, /* n */ |
| 308 | /* This will often be registered as 'Off' at init time */ |
| 309 | NULL, /* o */ |
| 310 | &sysrq_showregs_op, /* p */ |
| 311 | NULL, /* q */ |
| 312 | &sysrq_unraw_op, /* r */ |
| 313 | &sysrq_sync_op, /* s */ |
| 314 | &sysrq_showstate_op, /* t */ |
| 315 | &sysrq_mountro_op, /* u */ |
| 316 | /* May be assigned at init time by SMP VOYAGER */ |
| 317 | NULL, /* v */ |
| 318 | NULL, /* w */ |
Ingo Molnar | e59e2ae | 2006-12-06 20:35:59 -0800 | [diff] [blame^] | 319 | &sysrq_showstate_blocked_op, /* x */ |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 320 | NULL, /* y */ |
| 321 | NULL /* z */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | /* key2index calculation, -1 on invalid index */ |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 325 | static int sysrq_key_table_key2index(int key) |
| 326 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | int retval; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 328 | |
| 329 | if ((key >= '0') && (key <= '9')) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | retval = key - '0'; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 331 | else if ((key >= 'a') && (key <= 'z')) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | retval = key + 10 - 'a'; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 333 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | retval = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return retval; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * get and put functions for the table, exposed to modules. |
| 340 | */ |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 341 | struct sysrq_key_op *__sysrq_get_key_op(int key) |
| 342 | { |
| 343 | struct sysrq_key_op *op_p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | int i; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | i = sysrq_key_table_key2index(key); |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 347 | if (i != -1) |
| 348 | op_p = sysrq_key_table[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return op_p; |
| 350 | } |
| 351 | |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 352 | static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p) |
| 353 | { |
| 354 | int i = sysrq_key_table_key2index(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | if (i != -1) |
| 357 | sysrq_key_table[i] = op_p; |
| 358 | } |
| 359 | |
| 360 | /* |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 361 | * This is the non-locking version of handle_sysrq. It must/can only be called |
| 362 | * by sysrq key handlers, as they are inside of the lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 364 | void __handle_sysrq(int key, struct tty_struct *tty, int check_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { |
| 366 | struct sysrq_key_op *op_p; |
| 367 | int orig_log_level; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 368 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | unsigned long flags; |
| 370 | |
| 371 | spin_lock_irqsave(&sysrq_key_table_lock, flags); |
| 372 | orig_log_level = console_loglevel; |
| 373 | console_loglevel = 7; |
| 374 | printk(KERN_INFO "SysRq : "); |
| 375 | |
| 376 | op_p = __sysrq_get_key_op(key); |
| 377 | if (op_p) { |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 378 | /* |
| 379 | * Should we check for enabled operations (/proc/sysrq-trigger |
| 380 | * should not) and is the invoked operation enabled? |
| 381 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | if (!check_mask || sysrq_enabled == 1 || |
| 383 | (sysrq_enabled & op_p->enable_mask)) { |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 384 | printk("%s\n", op_p->action_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | console_loglevel = orig_log_level; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 386 | op_p->handler(key, tty); |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 387 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | printk("This sysrq operation is disabled.\n"); |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } else { |
| 391 | printk("HELP : "); |
| 392 | /* Only print the help msg once per handler */ |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 393 | for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) { |
| 394 | if (sysrq_key_table[i]) { |
| 395 | int j; |
| 396 | |
| 397 | for (j = 0; sysrq_key_table[i] != |
| 398 | sysrq_key_table[j]; j++) |
| 399 | ; |
| 400 | if (j != i) |
| 401 | continue; |
| 402 | printk("%s ", sysrq_key_table[i]->help_msg); |
| 403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 405 | printk("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | console_loglevel = orig_log_level; |
| 407 | } |
| 408 | spin_unlock_irqrestore(&sysrq_key_table_lock, flags); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * This function is called by the keyboard handler when SysRq is pressed |
| 413 | * and any other keycode arrives. |
| 414 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 415 | void handle_sysrq(int key, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
| 417 | if (!sysrq_enabled) |
| 418 | return; |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 419 | __handle_sysrq(key, tty, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 421 | EXPORT_SYMBOL(handle_sysrq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Adrian Bunk | cf62ddc | 2005-11-08 21:39:47 -0800 | [diff] [blame] | 423 | static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 424 | struct sysrq_key_op *remove_op_p) |
| 425 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | int retval; |
| 428 | unsigned long flags; |
| 429 | |
| 430 | spin_lock_irqsave(&sysrq_key_table_lock, flags); |
| 431 | if (__sysrq_get_key_op(key) == remove_op_p) { |
| 432 | __sysrq_put_key_op(key, insert_op_p); |
| 433 | retval = 0; |
| 434 | } else { |
| 435 | retval = -1; |
| 436 | } |
| 437 | spin_unlock_irqrestore(&sysrq_key_table_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return retval; |
| 439 | } |
| 440 | |
| 441 | int register_sysrq_key(int key, struct sysrq_key_op *op_p) |
| 442 | { |
| 443 | return __sysrq_swap_key_ops(key, op_p, NULL); |
| 444 | } |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 445 | EXPORT_SYMBOL(register_sysrq_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | int unregister_sysrq_key(int key, struct sysrq_key_op *op_p) |
| 448 | { |
| 449 | return __sysrq_swap_key_ops(key, NULL, op_p); |
| 450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | EXPORT_SYMBOL(unregister_sysrq_key); |