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>
11 #include <linux/config.h>
12 #include <linux/sched.h>
13 #include <linux/interrupt.h>
16 #include <linux/mount.h>
17 #include <linux/kdev_t.h>
18 #include <linux/major.h>
19 #include <linux/reboot.h>
20 #include <linux/sysrq.h>
21 #include <linux/kbd_kern.h>
22 #include <linux/quotaops.h>
23 #include <linux/smp_lock.h>
25 #include <asm/ptrace.h>
28 #include <linux/apm_bios.h>
31 extern void wakeup_bdflush(int);
32 extern void reset_vc(unsigned int);
33 extern int console_loglevel
;
34 extern struct vfsmount
*vfsmntlist
;
36 /* Send a signal to all user processes */
38 static void send_sig_all(int sig
, int even_init
)
40 struct task_struct
*p
;
43 if (p
->mm
) { /* Not swapper nor kernel thread */
44 if (p
->pid
== 1 && even_init
) /* Ugly hack to kill init */
52 * This function is called by the keyboard handler when SysRq is pressed
53 * and any other keycode arrives.
56 void handle_sysrq(int key
, struct pt_regs
*pt_regs
,
57 struct kbd_struct
*kbd
, struct tty_struct
*tty
)
59 int orig_log_level
= console_loglevel
;
65 printk(KERN_INFO
"SysRq: ");
67 case 'r': /* R -- Reset raw mode */
69 kbd
->kbdmode
= VC_XLATE
;
70 printk("Keyboard mode set to XLATE\n");
74 case 'k': /* K -- SAK */
81 case 'b': /* B -- boot immediately */
82 printk("Resetting\n");
83 machine_restart(NULL
);
86 case 'o': /* O -- power off */
87 printk("Power off\n");
91 case 's': /* S -- emergency sync */
92 printk("Emergency Sync\n");
93 emergency_sync_scheduled
= EMERG_SYNC
;
96 case 'u': /* U -- emergency remount R/O */
97 printk("Emergency Remount R/O\n");
98 emergency_sync_scheduled
= EMERG_REMOUNT
;
101 case 'p': /* P -- show PC */
102 printk("Show Regs\n");
106 case 't': /* T -- show task info */
107 printk("Show State\n");
110 case 'm': /* M -- show memory info */
111 printk("Show Memory\n");
114 case '0' ... '9': /* 0-9 -- set console logging level */
115 orig_log_level
= key
- '0';
116 printk("Log level set to %d\n", orig_log_level
);
118 case 'e': /* E -- terminate all user processes */
119 printk("Terminate All Tasks\n");
120 send_sig_all(SIGTERM
, 0);
121 orig_log_level
= 8; /* We probably have killed syslogd */
123 case 'i': /* I -- kill all user processes */
124 printk("Kill All Tasks\n");
125 send_sig_all(SIGKILL
, 0);
128 case 'l': /* L -- kill all processes including init */
129 printk("Kill ALL Tasks (even init)\n");
130 send_sig_all(SIGKILL
, 1);
133 default: /* Unknown: help */
144 "Sync Unmount showPc showTasks showMem loglevel0-8 tErm kIll killalL\n");
145 /* Don't use 'A' as it's handled specially on the Sparc */
148 console_loglevel
= orig_log_level
;
151 /* Aux routines for the syncer */
153 static int is_local_disk(kdev_t dev
) /* Guess if the device is a local hard drive */
155 unsigned int major
= MAJOR(dev
);
162 case SCSI_DISK0_MAJOR
:
163 case SCSI_DISK1_MAJOR
:
164 case SCSI_DISK2_MAJOR
:
165 case SCSI_DISK3_MAJOR
:
166 case SCSI_DISK4_MAJOR
:
167 case SCSI_DISK5_MAJOR
:
168 case SCSI_DISK6_MAJOR
:
169 case SCSI_DISK7_MAJOR
:
176 static void go_sync(kdev_t dev
, int remount_flag
)
178 printk(KERN_INFO
"%sing device %s ... ",
179 remount_flag
? "Remount" : "Sync",
182 if (remount_flag
) { /* Remount R/O */
183 struct super_block
*sb
= get_super(dev
);
184 struct vfsmount
*vfsmnt
;
189 printk("Superblock not found\n");
192 if (sb
->s_flags
& MS_RDONLY
) {
198 for (p
= sb
->s_files
.next
; p
!= &sb
->s_files
; p
= p
->next
) {
199 struct file
*file
= list_entry(p
, struct file
, f_list
);
200 if (file
->f_dentry
&& file_count(file
)
201 && S_ISREG(file
->f_dentry
->d_inode
->i_mode
))
208 if (sb
->s_op
&& sb
->s_op
->remount_fs
) {
209 ret
= sb
->s_op
->remount_fs(sb
, &flags
, NULL
);
211 printk("error %d\n", ret
);
213 sb
->s_flags
= (sb
->s_flags
& ~MS_RMT_MASK
) | (flags
& MS_RMT_MASK
);
214 if ((vfsmnt
= lookup_vfsmnt(sb
->s_dev
)))
215 vfsmnt
->mnt_flags
= sb
->s_flags
;
219 printk("nothing to do\n");
221 fsync_dev(dev
); /* Sync only */
227 * Emergency Sync or Unmount. We cannot do it directly, so we set a special
228 * flag and wake up the bdflush kernel thread which immediately calls this function.
229 * We process all mounted hard drives first to recover from crashed experimental
230 * block devices and malfunctional network filesystems.
233 int emergency_sync_scheduled
;
235 void do_emergency_sync(void)
237 struct vfsmount
*mnt
;
241 remount_flag
= (emergency_sync_scheduled
== EMERG_REMOUNT
);
242 emergency_sync_scheduled
= 0;
244 for (mnt
= vfsmntlist
; mnt
; mnt
= mnt
->mnt_next
)
245 if (is_local_disk(mnt
->mnt_dev
))
246 go_sync(mnt
->mnt_dev
, remount_flag
);
248 for (mnt
= vfsmntlist
; mnt
; mnt
= mnt
->mnt_next
)
249 if (!is_local_disk(mnt
->mnt_dev
) && MAJOR(mnt
->mnt_dev
))
250 go_sync(mnt
->mnt_dev
, remount_flag
);
253 printk(KERN_INFO
"Done.\n");