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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 46 | 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 |
| 62 | static void sysrq_handle_SAK(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 63 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | if (tty) |
| 66 | do_SAK(tty); |
| 67 | reset_vc(vc_cons[fg_console].d); |
| 68 | } |
| 69 | static struct sysrq_key_op sysrq_SAK_op = { |
| 70 | .handler = sysrq_handle_SAK, |
| 71 | .help_msg = "saK", |
| 72 | .action_msg = "SAK", |
| 73 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, |
| 74 | }; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 75 | #else |
| 76 | #define sysrq_SAK_op (*(struct sysrq_key_op *)0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | #ifdef CONFIG_VT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static void sysrq_handle_unraw(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 81 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | struct kbd_struct *kbd = &kbd_table[fg_console]; |
| 84 | |
| 85 | if (kbd) |
| 86 | kbd->kbdmode = VC_XLATE; |
| 87 | } |
| 88 | static struct sysrq_key_op sysrq_unraw_op = { |
| 89 | .handler = sysrq_handle_unraw, |
| 90 | .help_msg = "unRaw", |
| 91 | .action_msg = "Keyboard mode set to XLATE", |
| 92 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, |
| 93 | }; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 94 | #else |
| 95 | #define sysrq_unraw_op (*(struct sysrq_key_op *)0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | #endif /* CONFIG_VT */ |
| 97 | |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 98 | #ifdef CONFIG_KEXEC |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 99 | static void sysrq_handle_crashdump(int key, struct pt_regs *pt_regs, |
| 100 | struct tty_struct *tty) |
| 101 | { |
Alexander Nyberg | 6e274d1 | 2005-06-25 14:58:26 -0700 | [diff] [blame] | 102 | crash_kexec(pt_regs); |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 103 | } |
| 104 | static struct sysrq_key_op sysrq_crashdump_op = { |
| 105 | .handler = sysrq_handle_crashdump, |
| 106 | .help_msg = "Crashdump", |
| 107 | .action_msg = "Trigger a crashdump", |
| 108 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 109 | }; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 110 | #else |
| 111 | #define sysrq_crashdump_op (*(struct sysrq_key_op *)0) |
Hariprasad Nellitheertha | 86b1ae3 | 2005-06-25 14:58:25 -0700 | [diff] [blame] | 112 | #endif |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 115 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | static struct sysrq_key_op sysrq_reboot_op = { |
| 121 | .handler = sysrq_handle_reboot, |
| 122 | .help_msg = "reBoot", |
| 123 | .action_msg = "Resetting", |
| 124 | .enable_mask = SYSRQ_ENABLE_BOOT, |
| 125 | }; |
| 126 | |
| 127 | static void sysrq_handle_sync(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 128 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | emergency_sync(); |
| 131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | static struct sysrq_key_op sysrq_sync_op = { |
| 133 | .handler = sysrq_handle_sync, |
| 134 | .help_msg = "Sync", |
| 135 | .action_msg = "Emergency Sync", |
| 136 | .enable_mask = SYSRQ_ENABLE_SYNC, |
| 137 | }; |
| 138 | |
| 139 | static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 140 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
| 142 | emergency_remount(); |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | static struct sysrq_key_op sysrq_mountro_op = { |
| 145 | .handler = sysrq_handle_mountro, |
| 146 | .help_msg = "Unmount", |
| 147 | .action_msg = "Emergency Remount R/O", |
| 148 | .enable_mask = SYSRQ_ENABLE_REMOUNT, |
| 149 | }; |
| 150 | |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 151 | #ifdef CONFIG_DEBUG_MUTEXES |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 152 | static void sysrq_handle_showlocks(int key, struct pt_regs *pt_regs, |
| 153 | struct tty_struct *tty) |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 154 | { |
| 155 | mutex_debug_show_all_locks(); |
| 156 | } |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 157 | static struct sysrq_key_op sysrq_showlocks_op = { |
| 158 | .handler = sysrq_handle_showlocks, |
| 159 | .help_msg = "show-all-locks(D)", |
| 160 | .action_msg = "Show Locks Held", |
| 161 | }; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 162 | #else |
| 163 | #define sysrq_showlocks_op (*(struct sysrq_key_op *)0) |
Ingo Molnar | de5097c | 2006-01-09 15:59:21 -0800 | [diff] [blame] | 164 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 167 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | if (pt_regs) |
| 170 | show_regs(pt_regs); |
| 171 | } |
| 172 | static struct sysrq_key_op sysrq_showregs_op = { |
| 173 | .handler = sysrq_handle_showregs, |
| 174 | .help_msg = "showPc", |
| 175 | .action_msg = "Show Regs", |
| 176 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 177 | }; |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 180 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
| 182 | show_state(); |
| 183 | } |
| 184 | static struct sysrq_key_op sysrq_showstate_op = { |
| 185 | .handler = sysrq_handle_showstate, |
| 186 | .help_msg = "showTasks", |
| 187 | .action_msg = "Show State", |
| 188 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 189 | }; |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 192 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
| 194 | show_mem(); |
| 195 | } |
| 196 | static struct sysrq_key_op sysrq_showmem_op = { |
| 197 | .handler = sysrq_handle_showmem, |
| 198 | .help_msg = "showMem", |
| 199 | .action_msg = "Show Memory", |
| 200 | .enable_mask = SYSRQ_ENABLE_DUMP, |
| 201 | }; |
| 202 | |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 203 | /* |
| 204 | * Signal sysrq helper function. Sends a signal to all user processes. |
| 205 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | static void send_sig_all(int sig) |
| 207 | { |
| 208 | struct task_struct *p; |
| 209 | |
| 210 | for_each_process(p) { |
| 211 | if (p->mm && p->pid != 1) |
| 212 | /* Not swapper, init nor kernel thread */ |
| 213 | force_sig(sig, p); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void sysrq_handle_term(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 218 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
| 220 | send_sig_all(SIGTERM); |
| 221 | console_loglevel = 8; |
| 222 | } |
| 223 | static struct sysrq_key_op sysrq_term_op = { |
| 224 | .handler = sysrq_handle_term, |
| 225 | .help_msg = "tErm", |
| 226 | .action_msg = "Terminate All Tasks", |
| 227 | .enable_mask = SYSRQ_ENABLE_SIGNAL, |
| 228 | }; |
| 229 | |
| 230 | static void moom_callback(void *ignored) |
| 231 | { |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 232 | out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL], |
| 233 | GFP_KERNEL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static DECLARE_WORK(moom_work, moom_callback, NULL); |
| 237 | |
| 238 | static void sysrq_handle_moom(int key, struct pt_regs *pt_regs, |
| 239 | struct tty_struct *tty) |
| 240 | { |
| 241 | schedule_work(&moom_work); |
| 242 | } |
| 243 | static struct sysrq_key_op sysrq_moom_op = { |
| 244 | .handler = sysrq_handle_moom, |
| 245 | .help_msg = "Full", |
| 246 | .action_msg = "Manual OOM execution", |
| 247 | }; |
| 248 | |
| 249 | static void sysrq_handle_kill(int key, struct pt_regs *pt_regs, |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 250 | struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | send_sig_all(SIGKILL); |
| 253 | console_loglevel = 8; |
| 254 | } |
| 255 | static struct sysrq_key_op sysrq_kill_op = { |
| 256 | .handler = sysrq_handle_kill, |
| 257 | .help_msg = "kIll", |
| 258 | .action_msg = "Kill All Tasks", |
| 259 | .enable_mask = SYSRQ_ENABLE_SIGNAL, |
| 260 | }; |
| 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs, |
| 263 | struct tty_struct *tty) |
| 264 | { |
| 265 | normalize_rt_tasks(); |
| 266 | } |
| 267 | static struct sysrq_key_op sysrq_unrt_op = { |
| 268 | .handler = sysrq_handle_unrt, |
| 269 | .help_msg = "Nice", |
| 270 | .action_msg = "Nice All RT Tasks", |
| 271 | .enable_mask = SYSRQ_ENABLE_RTNICE, |
| 272 | }; |
| 273 | |
| 274 | /* Key Operations table and lock */ |
| 275 | static DEFINE_SPINLOCK(sysrq_key_table_lock); |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 276 | |
| 277 | static struct sysrq_key_op *sysrq_key_table[36] = { |
| 278 | &sysrq_loglevel_op, /* 0 */ |
| 279 | &sysrq_loglevel_op, /* 1 */ |
| 280 | &sysrq_loglevel_op, /* 2 */ |
| 281 | &sysrq_loglevel_op, /* 3 */ |
| 282 | &sysrq_loglevel_op, /* 4 */ |
| 283 | &sysrq_loglevel_op, /* 5 */ |
| 284 | &sysrq_loglevel_op, /* 6 */ |
| 285 | &sysrq_loglevel_op, /* 7 */ |
| 286 | &sysrq_loglevel_op, /* 8 */ |
| 287 | &sysrq_loglevel_op, /* 9 */ |
| 288 | |
| 289 | /* |
| 290 | * Don't use for system provided sysrqs, it is handled specially on |
| 291 | * sparc and will never arrive |
| 292 | */ |
| 293 | NULL, /* a */ |
| 294 | &sysrq_reboot_op, /* b */ |
| 295 | &sysrq_crashdump_op, /* c */ |
| 296 | &sysrq_showlocks_op, /* d */ |
| 297 | &sysrq_term_op, /* e */ |
| 298 | &sysrq_moom_op, /* f */ |
| 299 | NULL, /* g */ |
| 300 | NULL, /* h */ |
| 301 | &sysrq_kill_op, /* i */ |
| 302 | NULL, /* j */ |
| 303 | &sysrq_SAK_op, /* k */ |
| 304 | NULL, /* l */ |
| 305 | &sysrq_showmem_op, /* m */ |
| 306 | &sysrq_unrt_op, /* n */ |
| 307 | /* This will often be registered as 'Off' at init time */ |
| 308 | NULL, /* o */ |
| 309 | &sysrq_showregs_op, /* p */ |
| 310 | NULL, /* q */ |
| 311 | &sysrq_unraw_op, /* r */ |
| 312 | &sysrq_sync_op, /* s */ |
| 313 | &sysrq_showstate_op, /* t */ |
| 314 | &sysrq_mountro_op, /* u */ |
| 315 | /* May be assigned at init time by SMP VOYAGER */ |
| 316 | NULL, /* v */ |
| 317 | NULL, /* w */ |
| 318 | NULL, /* x */ |
| 319 | NULL, /* y */ |
| 320 | NULL /* z */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | }; |
| 322 | |
| 323 | /* key2index calculation, -1 on invalid index */ |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 324 | static int sysrq_key_table_key2index(int key) |
| 325 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | int retval; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 327 | |
| 328 | if ((key >= '0') && (key <= '9')) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | retval = key - '0'; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 330 | else if ((key >= 'a') && (key <= 'z')) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | retval = key + 10 - 'a'; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 332 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | retval = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return retval; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * get and put functions for the table, exposed to modules. |
| 339 | */ |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 340 | struct sysrq_key_op *__sysrq_get_key_op(int key) |
| 341 | { |
| 342 | struct sysrq_key_op *op_p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | int i; |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | i = sysrq_key_table_key2index(key); |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 346 | if (i != -1) |
| 347 | op_p = sysrq_key_table[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return op_p; |
| 349 | } |
| 350 | |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 351 | static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p) |
| 352 | { |
| 353 | int i = sysrq_key_table_key2index(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (i != -1) |
| 356 | sysrq_key_table[i] = op_p; |
| 357 | } |
| 358 | |
| 359 | /* |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 360 | * This is the non-locking version of handle_sysrq. It must/can only be called |
| 361 | * by sysrq key handlers, as they are inside of the lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | */ |
Andrew Morton | bf36b90 | 2006-03-25 03:07:08 -0800 | [diff] [blame] | 363 | void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty, |
| 364 | 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; |
| 386 | op_p->handler(key, pt_regs, 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty) |
| 416 | { |
| 417 | if (!sysrq_enabled) |
| 418 | return; |
| 419 | __handle_sysrq(key, pt_regs, tty, 1); |
| 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); |