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>
24 #include <linux/module.h>
26 #include <asm/ptrace.h>
28 extern void wakeup_bdflush(int);
29 extern void reset_vc(unsigned int);
30 extern struct list_head super_blocks
;
32 /* Whether we react on sysrq keys or just ignore them */
33 int sysrq_enabled
= 1;
35 /* Machine specific power off function */
36 void (*sysrq_power_off
)(void);
38 EXPORT_SYMBOL(sysrq_power_off
);
40 /* Send a signal to all user processes */
42 static void send_sig_all(int sig
, int even_init
)
44 struct task_struct
*p
;
47 if (p
->mm
) { /* Not swapper nor kernel thread */
48 if (p
->pid
== 1 && even_init
) /* Ugly hack to kill init */
56 * This function is called by the keyboard handler when SysRq is pressed
57 * and any other keycode arrives.
60 void handle_sysrq(int key
, struct pt_regs
*pt_regs
,
61 struct kbd_struct
*kbd
, struct tty_struct
*tty
)
63 int orig_log_level
= console_loglevel
;
72 printk(KERN_INFO
"SysRq: ");
74 case 'r': /* R -- Reset raw mode */
76 kbd
->kbdmode
= VC_XLATE
;
77 printk("Keyboard mode set to XLATE\n");
81 case 'k': /* K -- SAK */
88 case 'b': /* B -- boot immediately */
89 printk("Resetting\n");
90 machine_restart(NULL
);
92 case 'o': /* O -- power off */
93 if (sysrq_power_off
) {
94 printk("Power off\n");
98 case 's': /* S -- emergency sync */
99 printk("Emergency Sync\n");
100 emergency_sync_scheduled
= EMERG_SYNC
;
103 case 'u': /* U -- emergency remount R/O */
104 printk("Emergency Remount R/O\n");
105 emergency_sync_scheduled
= EMERG_REMOUNT
;
108 case 'p': /* P -- show PC */
109 printk("Show Regs\n");
113 case 't': /* T -- show task info */
114 printk("Show State\n");
117 case 'm': /* M -- show memory info */
118 printk("Show Memory\n");
121 case '0' ... '9': /* 0-9 -- set console logging level */
122 orig_log_level
= key
- '0';
123 printk("Log level set to %d\n", orig_log_level
);
125 case 'e': /* E -- terminate all user processes */
126 printk("Terminate All Tasks\n");
127 send_sig_all(SIGTERM
, 0);
128 orig_log_level
= 8; /* We probably have killed syslogd */
130 case 'i': /* I -- kill all user processes */
131 printk("Kill All Tasks\n");
132 send_sig_all(SIGKILL
, 0);
135 case 'l': /* L -- kill all processes including init */
136 printk("Kill ALL Tasks (even init)\n");
137 send_sig_all(SIGKILL
, 1);
140 default: /* Unknown: help */
150 printk("Sync Unmount showPc showTasks showMem loglevel0-8 tErm kIll killalL\n");
151 /* Don't use 'A' as it's handled specially on the Sparc */
154 console_loglevel
= orig_log_level
;
157 /* Aux routines for the syncer */
159 static int is_local_disk(kdev_t dev
) /* Guess if the device is a local hard drive */
161 unsigned int major
= MAJOR(dev
);
168 case SCSI_DISK0_MAJOR
:
169 case SCSI_DISK1_MAJOR
:
170 case SCSI_DISK2_MAJOR
:
171 case SCSI_DISK3_MAJOR
:
172 case SCSI_DISK4_MAJOR
:
173 case SCSI_DISK5_MAJOR
:
174 case SCSI_DISK6_MAJOR
:
175 case SCSI_DISK7_MAJOR
:
182 static void go_sync(struct super_block
*sb
, int remount_flag
)
184 printk(KERN_INFO
"%sing device %s ... ",
185 remount_flag
? "Remount" : "Sync",
186 kdevname(sb
->s_dev
));
188 if (remount_flag
) { /* Remount R/O */
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
))
206 fsync_dev(sb
->s_dev
);
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
);
217 printk("nothing to do\n");
219 fsync_dev(sb
->s_dev
); /* Sync only */
225 * Emergency Sync or Unmount. We cannot do it directly, so we set a special
226 * flag and wake up the bdflush kernel thread which immediately calls this function.
227 * We process all mounted hard drives first to recover from crashed experimental
228 * block devices and malfunctional network filesystems.
231 int emergency_sync_scheduled
;
233 void do_emergency_sync(void)
235 struct super_block
*sb
;
239 remount_flag
= (emergency_sync_scheduled
== EMERG_REMOUNT
);
240 emergency_sync_scheduled
= 0;
242 for (sb
= sb_entry(super_blocks
.next
);
243 sb
!= sb_entry(&super_blocks
);
244 sb
= sb_entry(sb
->s_list
.next
))
245 if (is_local_disk(sb
->s_dev
))
246 go_sync(sb
, remount_flag
);
248 for (sb
= sb_entry(super_blocks
.next
);
249 sb
!= sb_entry(&super_blocks
);
250 sb
= sb_entry(sb
->s_list
.next
))
251 if (!is_local_disk(sb
->s_dev
) && MAJOR(sb
->s_dev
))
252 go_sync(sb
, remount_flag
);
255 printk(KERN_INFO
"Done.\n");