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