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