3 * $Id: sysrq.c,v 1.1.1.1 2005/04/11 02:50:19 jack 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>
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/module.h>
31 #include <linux/spinlock.h>
33 #include <asm/ptrace.h>
35 extern void reset_vc(unsigned int);
36 extern struct list_head super_blocks;
38 /* Whether we react on sysrq keys or just ignore them */
39 int sysrq_enabled = 1;
41 /* Machine specific power off function */
42 void (*sysrq_power_off)(void);
44 /* Loglevel sysrq handler */
45 static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs,
46 struct kbd_struct *kbd, struct tty_struct *tty) {
50 printk("Loglevel set to %d\n", i);
53 static struct sysrq_key_op sysrq_loglevel_op = {
54 handler: sysrq_handle_loglevel,
55 help_msg: "loglevel0-8",
56 action_msg: "Changing Loglevel",
60 /* SAK sysrq handler */
62 static void sysrq_handle_SAK(int key, struct pt_regs *pt_regs,
63 struct kbd_struct *kbd, struct tty_struct *tty) {
68 static struct sysrq_key_op sysrq_SAK_op = {
69 handler: sysrq_handle_SAK,
76 /* unraw sysrq handler */
77 static void sysrq_handle_unraw(int key, struct pt_regs *pt_regs,
78 struct kbd_struct *kbd, struct tty_struct *tty) {
80 kbd->kbdmode = VC_XLATE;
82 static struct sysrq_key_op sysrq_unraw_op = {
83 handler: sysrq_handle_unraw,
85 action_msg: "Keyboard mode set to XLATE",
89 /* reboot sysrq handler */
90 static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs,
91 struct kbd_struct *kbd, struct tty_struct *tty) {
92 machine_restart(NULL);
94 static struct sysrq_key_op sysrq_reboot_op = {
95 handler: sysrq_handle_reboot,
97 action_msg: "Resetting",
102 /* SYNC SYSRQ HANDLERS BLOCK */
104 /* do_emergency_sync helper function */
105 /* Guesses if the device is a local hard drive */
106 static int is_local_disk(kdev_t dev) {
121 case SCSI_DISK0_MAJOR:
122 case SCSI_DISK1_MAJOR:
123 case SCSI_DISK2_MAJOR:
124 case SCSI_DISK3_MAJOR:
125 case SCSI_DISK4_MAJOR:
126 case SCSI_DISK5_MAJOR:
127 case SCSI_DISK6_MAJOR:
128 case SCSI_DISK7_MAJOR:
136 /* do_emergency_sync helper function */
137 static void go_sync(struct super_block *sb, int remount_flag)
140 orig_loglevel = console_loglevel;
141 console_loglevel = 7;
142 printk(KERN_INFO "%sing device %s ... ",
143 remount_flag ? "Remount" : "Sync",
144 kdevname(sb->s_dev));
146 if (remount_flag) { /* Remount R/O */
150 if (sb->s_flags & MS_RDONLY) {
156 for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
157 struct file *file = list_entry(p, struct file, f_list);
158 if (file->f_dentry && file_count(file)
159 && S_ISREG(file->f_dentry->d_inode->i_mode))
164 fsync_dev(sb->s_dev);
166 if (sb->s_op && sb->s_op->remount_fs) {
167 ret = sb->s_op->remount_fs(sb, &flags, NULL);
169 printk("error %d\n", ret);
171 sb->s_flags = (sb->s_flags & ~MS_RMT_MASK) | (flags & MS_RMT_MASK);
175 printk("nothing to do\n");
176 } else { /* Sync only */
177 fsync_dev(sb->s_dev);
180 console_loglevel = orig_loglevel;
183 * Emergency Sync or Unmount. We cannot do it directly, so we set a special
184 * flag and wake up the bdflush kernel thread which immediately calls this function.
185 * We process all mounted hard drives first to recover from crashed experimental
186 * block devices and malfunctional network filesystems.
189 volatile int emergency_sync_scheduled;
191 void do_emergency_sync(void) {
192 struct super_block *sb;
197 remount_flag = (emergency_sync_scheduled == EMERG_REMOUNT);
198 emergency_sync_scheduled = 0;
200 for (sb = sb_entry(super_blocks.next);
201 sb != sb_entry(&super_blocks);
202 sb = sb_entry(sb->s_list.next))
203 if (is_local_disk(sb->s_dev))
204 go_sync(sb, remount_flag);
206 for (sb = sb_entry(super_blocks.next);
207 sb != sb_entry(&super_blocks);
208 sb = sb_entry(sb->s_list.next))
209 if (!is_local_disk(sb->s_dev) && MAJOR(sb->s_dev))
210 go_sync(sb, remount_flag);
214 orig_loglevel = console_loglevel;
215 console_loglevel = 7;
216 printk(KERN_INFO "Done.\n");
217 console_loglevel = orig_loglevel;
220 static void sysrq_handle_sync(int key, struct pt_regs *pt_regs,
221 struct kbd_struct *kbd, struct tty_struct *tty) {
222 emergency_sync_scheduled = EMERG_SYNC;
225 static struct sysrq_key_op sysrq_sync_op = {
226 handler: sysrq_handle_sync,
228 action_msg: "Emergency Sync",
231 static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs,
232 struct kbd_struct *kbd, struct tty_struct *tty) {
233 emergency_sync_scheduled = EMERG_REMOUNT;
236 static struct sysrq_key_op sysrq_mountro_op = {
237 handler: sysrq_handle_mountro,
239 action_msg: "Emergency Remount R/O",
242 /* END SYNC SYSRQ HANDLERS BLOCK */
245 /* SHOW SYSRQ HANDLERS BLOCK */
247 static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
248 struct kbd_struct *kbd, struct tty_struct *tty) {
252 static struct sysrq_key_op sysrq_showregs_op = {
253 handler: sysrq_handle_showregs,
255 action_msg: "Show Regs",
259 static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
260 struct kbd_struct *kbd, struct tty_struct *tty) {
263 static struct sysrq_key_op sysrq_showstate_op = {
264 handler: sysrq_handle_showstate,
265 help_msg: "showTasks",
266 action_msg: "Show State",
270 static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
271 struct kbd_struct *kbd, struct tty_struct *tty) {
274 static struct sysrq_key_op sysrq_showmem_op = {
275 handler: sysrq_handle_showmem,
277 action_msg: "Show Memory",
280 /* SHOW SYSRQ HANDLERS BLOCK */
283 /* SIGNAL SYSRQ HANDLERS BLOCK */
285 /* signal sysrq helper function
286 * Sends a signal to all user processes */
287 static void send_sig_all(int sig)
289 struct task_struct *p;
292 if (p->mm && p->pid != 1)
293 /* Not swapper, init nor kernel thread */
298 static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
299 struct kbd_struct *kbd, struct tty_struct *tty) {
300 send_sig_all(SIGTERM);
301 console_loglevel = 8;
303 static struct sysrq_key_op sysrq_term_op = {
304 handler: sysrq_handle_term,
306 action_msg: "Terminate All Tasks",
309 static void sysrq_handle_kill(int key, struct pt_regs *pt_regs,
310 struct kbd_struct *kbd, struct tty_struct *tty) {
311 send_sig_all(SIGKILL);
312 console_loglevel = 8;
314 static struct sysrq_key_op sysrq_kill_op = {
315 handler: sysrq_handle_kill,
317 action_msg: "Kill All Tasks",
320 /* END SIGNAL SYSRQ HANDLERS BLOCK */
323 /* Key Operations table and lock */
324 static spinlock_t sysrq_key_table_lock = SPIN_LOCK_UNLOCKED;
325 #define SYSRQ_KEY_TABLE_LENGTH 36
326 static struct sysrq_key_op *sysrq_key_table[SYSRQ_KEY_TABLE_LENGTH] = {
327 /* 0 */ &sysrq_loglevel_op,
328 /* 1 */ &sysrq_loglevel_op,
329 /* 2 */ &sysrq_loglevel_op,
330 /* 3 */ &sysrq_loglevel_op,
331 /* 4 */ &sysrq_loglevel_op,
332 /* 5 */ &sysrq_loglevel_op,
333 /* 6 */ &sysrq_loglevel_op,
334 /* 7 */ &sysrq_loglevel_op,
335 /* 8 */ &sysrq_loglevel_op,
336 /* 9 */ &sysrq_loglevel_op,
337 /* a */ NULL, /* Don't use for system provided sysrqs,
338 it is handled specially on the spark
339 and will never arive */
340 /* b */ &sysrq_reboot_op,
343 /* e */ &sysrq_term_op,
347 /* i */ &sysrq_kill_op,
350 /* k */ &sysrq_SAK_op,
355 /* m */ &sysrq_showmem_op,
357 /* o */ NULL, /* This will often be registered
358 as 'Off' at init time */
359 /* p */ &sysrq_showregs_op,
361 /* r */ &sysrq_unraw_op,
362 /* s */ &sysrq_sync_op,
363 /* t */ &sysrq_showstate_op,
364 /* u */ &sysrq_mountro_op,
372 /* key2index calculation, -1 on invalid index */
373 static __inline__ int sysrq_key_table_key2index(int key) {
375 if ((key >= '0') & (key <= '9')) {
377 } else if ((key >= 'a') & (key <= 'z')) {
378 retval = key + 10 - 'a';
386 * table lock and unlocking functions, exposed to modules
389 void __sysrq_lock_table (void) { spin_lock(&sysrq_key_table_lock); }
391 void __sysrq_unlock_table (void) { spin_unlock(&sysrq_key_table_lock); }
394 * get and put functions for the table, exposed to modules.
397 struct sysrq_key_op *__sysrq_get_key_op (int key) {
398 struct sysrq_key_op *op_p;
401 i = sysrq_key_table_key2index(key);
402 op_p = (i == -1) ? NULL : sysrq_key_table[i];
406 void __sysrq_put_key_op (int key, struct sysrq_key_op *op_p) {
409 i = sysrq_key_table_key2index(key);
411 sysrq_key_table[i] = op_p;
415 * This function is called by the keyboard handler when SysRq is pressed
416 * and any other keycode arrives.
419 void handle_sysrq(int key, struct pt_regs *pt_regs,
420 struct kbd_struct *kbd, struct tty_struct *tty) {
424 __sysrq_lock_table();
425 __handle_sysrq_nolock(key, pt_regs, kbd, tty);
426 __sysrq_unlock_table();
430 * This is the non-locking version of handle_sysrq
431 * It must/can only be called by sysrq key handlers,
432 * as they are inside of the lock
435 void __handle_sysrq_nolock(int key, struct pt_regs *pt_regs,
436 struct kbd_struct *kbd, struct tty_struct *tty) {
437 struct sysrq_key_op *op_p;
444 orig_log_level = console_loglevel;
445 console_loglevel = 7;
446 printk(KERN_INFO "SysRq : ");
448 op_p = __sysrq_get_key_op(key);
450 printk ("%s\n", op_p->action_msg);
451 console_loglevel = orig_log_level;
452 op_p->handler(key, pt_regs, kbd, tty);
455 /* Only print the help msg once per handler */
456 for (i=0; i<SYSRQ_KEY_TABLE_LENGTH; i++)
457 if (sysrq_key_table[i]) {
458 for (j=0; sysrq_key_table[i] != sysrq_key_table[j]; j++);
460 printk ("%s ", sysrq_key_table[i]->help_msg);
463 console_loglevel = orig_log_level;
467 EXPORT_SYMBOL(handle_sysrq);
468 EXPORT_SYMBOL(__handle_sysrq_nolock);
469 EXPORT_SYMBOL(__sysrq_lock_table);
470 EXPORT_SYMBOL(__sysrq_unlock_table);
471 EXPORT_SYMBOL(__sysrq_get_key_op);
472 EXPORT_SYMBOL(__sysrq_put_key_op);