[NETFILTER]: Move ip6_masked_addrcmp to include/net/ipv6.h
[linux-2.6/verdex.git] / drivers / char / sysrq.c
blobd58f823188539651f6e6eabd76ac091cae7f835b
1 /* -*- linux-c -*-
3 * $Id: sysrq.c,v 1.15 1998/08/23 14:56:41 mj Exp $
5 * Linux Magic System Request Key Hacks
7 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
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
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>
38 #include <linux/kexec.h>
40 #include <asm/ptrace.h>
42 /* Whether we react on sysrq keys or just ignore them */
43 int sysrq_enabled = 1;
45 /* Loglevel sysrq handler */
46 static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs,
47 struct tty_struct *tty)
49 int i;
50 i = key - '0';
51 console_loglevel = 7;
52 printk("Loglevel set to %d\n", i);
53 console_loglevel = i;
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,
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)
68 if (tty)
69 do_SAK(tty);
70 reset_vc(vc_cons[fg_console].d);
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,
78 #endif
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)
85 struct kbd_struct *kbd = &kbd_table[fg_console];
87 if (kbd)
88 kbd->kbdmode = VC_XLATE;
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,
96 #endif /* CONFIG_VT */
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)
103 crash_kexec(pt_regs);
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,
111 #endif
113 /* reboot sysrq handler */
114 static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs,
115 struct tty_struct *tty)
117 local_irq_enable();
118 emergency_restart();
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,
128 static void sysrq_handle_sync(int key, struct pt_regs *pt_regs,
129 struct tty_struct *tty)
131 emergency_sync();
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,
141 static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs,
142 struct tty_struct *tty)
144 emergency_remount();
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,
154 /* END SYNC SYSRQ HANDLERS BLOCK */
156 #ifdef CONFIG_DEBUG_MUTEXES
158 static void
159 sysrq_handle_showlocks(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
161 mutex_debug_show_all_locks();
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",
170 #endif
172 /* SHOW SYSRQ HANDLERS BLOCK */
174 static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
175 struct tty_struct *tty)
177 if (pt_regs)
178 show_regs(pt_regs);
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,
188 static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
189 struct tty_struct *tty)
191 show_state();
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,
201 static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
202 struct tty_struct *tty)
204 show_mem();
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,
213 /* SHOW SYSRQ HANDLERS BLOCK */
216 /* SIGNAL SYSRQ HANDLERS BLOCK */
218 /* signal sysrq helper function
219 * Sends a signal to all user processes */
220 static void send_sig_all(int sig)
222 struct task_struct *p;
224 for_each_process(p) {
225 if (p->mm && p->pid != 1)
226 /* Not swapper, init nor kernel thread */
227 force_sig(sig, p);
231 static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
232 struct tty_struct *tty)
234 send_sig_all(SIGTERM);
235 console_loglevel = 8;
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,
244 static void moom_callback(void *ignored)
246 out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL], GFP_KERNEL, 0);
249 static DECLARE_WORK(moom_work, moom_callback, NULL);
251 static void sysrq_handle_moom(int key, struct pt_regs *pt_regs,
252 struct tty_struct *tty)
254 schedule_work(&moom_work);
256 static struct sysrq_key_op sysrq_moom_op = {
257 .handler = sysrq_handle_moom,
258 .help_msg = "Full",
259 .action_msg = "Manual OOM execution",
262 static void sysrq_handle_kill(int key, struct pt_regs *pt_regs,
263 struct tty_struct *tty)
265 send_sig_all(SIGKILL);
266 console_loglevel = 8;
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,
275 /* END SIGNAL SYSRQ HANDLERS BLOCK */
277 static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
278 struct tty_struct *tty)
280 normalize_rt_tasks();
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,
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,
307 #ifdef CONFIG_KEXEC
308 /* c */ &sysrq_crashdump_op,
309 #else
310 /* c */ NULL,
311 #endif
312 #ifdef CONFIG_DEBUG_MUTEXES
313 /* d */ &sysrq_showlocks_op,
314 #else
315 /* d */ NULL,
316 #endif
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
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;
360 return retval;
364 * get and put functions for the table, exposed to modules.
367 struct sysrq_key_op *__sysrq_get_key_op (int key) {
368 struct sysrq_key_op *op_p;
369 int i;
371 i = sysrq_key_table_key2index(key);
372 op_p = (i == -1) ? NULL : sysrq_key_table[i];
373 return op_p;
376 static void __sysrq_put_key_op (int key, struct sysrq_key_op *op_p) {
377 int i;
379 i = sysrq_key_table_key2index(key);
380 if (i != -1)
381 sysrq_key_table[i] = op_p;
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
390 void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty, int check_mask)
392 struct sysrq_key_op *op_p;
393 int orig_log_level;
394 int i, j;
395 unsigned long flags;
397 spin_lock_irqsave(&sysrq_key_table_lock, flags);
398 orig_log_level = console_loglevel;
399 console_loglevel = 7;
400 printk(KERN_INFO "SysRq : ");
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);
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);
423 printk ("\n");
424 console_loglevel = orig_log_level;
426 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
430 * This function is called by the keyboard handler when SysRq is pressed
431 * and any other keycode arrives.
434 void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
436 if (!sysrq_enabled)
437 return;
438 __handle_sysrq(key, pt_regs, tty, 1);
441 static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
442 struct sysrq_key_op *remove_op_p) {
444 int retval;
445 unsigned long flags;
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;
454 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
456 return retval;
459 int register_sysrq_key(int key, struct sysrq_key_op *op_p)
461 return __sysrq_swap_key_ops(key, op_p, NULL);
464 int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
466 return __sysrq_swap_key_ops(key, NULL, op_p);
469 EXPORT_SYMBOL(handle_sysrq);
470 EXPORT_SYMBOL(register_sysrq_key);
471 EXPORT_SYMBOL(unregister_sysrq_key);