]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
15 | #include <linux/sched.h> |
16 | #include <linux/interrupt.h> | |
17 | #include <linux/mm.h> | |
18 | #include <linux/fs.h> | |
19 | #include <linux/tty.h> | |
20 | #include <linux/mount.h> | |
21 | #include <linux/kdev_t.h> | |
22 | #include <linux/major.h> | |
23 | #include <linux/reboot.h> | |
24 | #include <linux/sysrq.h> | |
25 | #include <linux/kbd_kern.h> | |
f40cbaa5 | 26 | #include <linux/proc_fs.h> |
1da177e4 | 27 | #include <linux/quotaops.h> |
1da177e4 LT |
28 | #include <linux/kernel.h> |
29 | #include <linux/module.h> | |
30 | #include <linux/suspend.h> | |
31 | #include <linux/writeback.h> | |
32 | #include <linux/buffer_head.h> /* for fsync_bdev() */ | |
33 | #include <linux/swap.h> | |
34 | #include <linux/spinlock.h> | |
35 | #include <linux/vt_kern.h> | |
36 | #include <linux/workqueue.h> | |
86b1ae38 | 37 | #include <linux/kexec.h> |
7d12e780 | 38 | #include <linux/irq.h> |
88ad0bf6 | 39 | #include <linux/hrtimer.h> |
5a3135c2 | 40 | #include <linux/oom.h> |
1da177e4 LT |
41 | |
42 | #include <asm/ptrace.h> | |
2033b0c3 | 43 | #include <asm/irq_regs.h> |
1da177e4 LT |
44 | |
45 | /* Whether we react on sysrq keys or just ignore them */ | |
5d6f647f IM |
46 | int __read_mostly __sysrq_enabled = 1; |
47 | ||
48 | static int __read_mostly sysrq_always_enabled; | |
49 | ||
50 | int sysrq_on(void) | |
51 | { | |
52 | return __sysrq_enabled || sysrq_always_enabled; | |
53 | } | |
54 | ||
55 | /* | |
56 | * A value of 1 means 'all', other nonzero values are an op mask: | |
57 | */ | |
58 | static inline int sysrq_on_mask(int mask) | |
59 | { | |
60 | return sysrq_always_enabled || __sysrq_enabled == 1 || | |
61 | (__sysrq_enabled & mask); | |
62 | } | |
63 | ||
64 | static int __init sysrq_always_enabled_setup(char *str) | |
65 | { | |
66 | sysrq_always_enabled = 1; | |
67 | printk(KERN_INFO "debug: sysrq always enabled.\n"); | |
68 | ||
69 | return 1; | |
70 | } | |
71 | ||
72 | __setup("sysrq_always_enabled", sysrq_always_enabled_setup); | |
73 | ||
1da177e4 | 74 | |
7d12e780 | 75 | static void sysrq_handle_loglevel(int key, struct tty_struct *tty) |
1da177e4 LT |
76 | { |
77 | int i; | |
78 | i = key - '0'; | |
79 | console_loglevel = 7; | |
80 | printk("Loglevel set to %d\n", i); | |
81 | console_loglevel = i; | |
bf36b901 | 82 | } |
1da177e4 LT |
83 | static struct sysrq_key_op sysrq_loglevel_op = { |
84 | .handler = sysrq_handle_loglevel, | |
208b95ce | 85 | .help_msg = "loglevel(0-9)", |
1da177e4 LT |
86 | .action_msg = "Changing Loglevel", |
87 | .enable_mask = SYSRQ_ENABLE_LOG, | |
88 | }; | |
89 | ||
1da177e4 | 90 | #ifdef CONFIG_VT |
7d12e780 | 91 | static void sysrq_handle_SAK(int key, struct tty_struct *tty) |
1da177e4 | 92 | { |
8b6312f4 | 93 | struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work; |
8b6312f4 | 94 | schedule_work(SAK_work); |
1da177e4 LT |
95 | } |
96 | static struct sysrq_key_op sysrq_SAK_op = { | |
97 | .handler = sysrq_handle_SAK, | |
98 | .help_msg = "saK", | |
99 | .action_msg = "SAK", | |
100 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, | |
101 | }; | |
bf36b901 AM |
102 | #else |
103 | #define sysrq_SAK_op (*(struct sysrq_key_op *)0) | |
1da177e4 LT |
104 | #endif |
105 | ||
106 | #ifdef CONFIG_VT | |
7d12e780 | 107 | static void sysrq_handle_unraw(int key, struct tty_struct *tty) |
1da177e4 LT |
108 | { |
109 | struct kbd_struct *kbd = &kbd_table[fg_console]; | |
110 | ||
111 | if (kbd) | |
2e8ecb9d | 112 | kbd->kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; |
1da177e4 LT |
113 | } |
114 | static struct sysrq_key_op sysrq_unraw_op = { | |
115 | .handler = sysrq_handle_unraw, | |
116 | .help_msg = "unRaw", | |
2e8ecb9d | 117 | .action_msg = "Keyboard mode set to system default", |
1da177e4 LT |
118 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, |
119 | }; | |
bf36b901 AM |
120 | #else |
121 | #define sysrq_unraw_op (*(struct sysrq_key_op *)0) | |
1da177e4 LT |
122 | #endif /* CONFIG_VT */ |
123 | ||
86b1ae38 | 124 | #ifdef CONFIG_KEXEC |
7d12e780 | 125 | static void sysrq_handle_crashdump(int key, struct tty_struct *tty) |
86b1ae38 | 126 | { |
7d12e780 | 127 | crash_kexec(get_irq_regs()); |
86b1ae38 HN |
128 | } |
129 | static struct sysrq_key_op sysrq_crashdump_op = { | |
130 | .handler = sysrq_handle_crashdump, | |
131 | .help_msg = "Crashdump", | |
132 | .action_msg = "Trigger a crashdump", | |
133 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
134 | }; | |
bf36b901 AM |
135 | #else |
136 | #define sysrq_crashdump_op (*(struct sysrq_key_op *)0) | |
86b1ae38 HN |
137 | #endif |
138 | ||
7d12e780 | 139 | static void sysrq_handle_reboot(int key, struct tty_struct *tty) |
1da177e4 | 140 | { |
b2e9c7d0 | 141 | lockdep_off(); |
1da177e4 | 142 | local_irq_enable(); |
4de8b9b7 | 143 | emergency_restart(); |
1da177e4 | 144 | } |
1da177e4 LT |
145 | static struct sysrq_key_op sysrq_reboot_op = { |
146 | .handler = sysrq_handle_reboot, | |
147 | .help_msg = "reBoot", | |
148 | .action_msg = "Resetting", | |
149 | .enable_mask = SYSRQ_ENABLE_BOOT, | |
150 | }; | |
151 | ||
7d12e780 | 152 | static void sysrq_handle_sync(int key, struct tty_struct *tty) |
1da177e4 LT |
153 | { |
154 | emergency_sync(); | |
155 | } | |
1da177e4 LT |
156 | static struct sysrq_key_op sysrq_sync_op = { |
157 | .handler = sysrq_handle_sync, | |
158 | .help_msg = "Sync", | |
159 | .action_msg = "Emergency Sync", | |
160 | .enable_mask = SYSRQ_ENABLE_SYNC, | |
161 | }; | |
162 | ||
88ad0bf6 IM |
163 | static void sysrq_handle_show_timers(int key, struct tty_struct *tty) |
164 | { | |
165 | sysrq_timer_list_show(); | |
166 | } | |
167 | ||
168 | static struct sysrq_key_op sysrq_show_timers_op = { | |
169 | .handler = sysrq_handle_show_timers, | |
170 | .help_msg = "show-all-timers(Q)", | |
322acf65 | 171 | .action_msg = "Show clockevent devices & pending hrtimers (no others)", |
88ad0bf6 IM |
172 | }; |
173 | ||
7d12e780 | 174 | static void sysrq_handle_mountro(int key, struct tty_struct *tty) |
1da177e4 LT |
175 | { |
176 | emergency_remount(); | |
177 | } | |
1da177e4 LT |
178 | static struct sysrq_key_op sysrq_mountro_op = { |
179 | .handler = sysrq_handle_mountro, | |
180 | .help_msg = "Unmount", | |
181 | .action_msg = "Emergency Remount R/O", | |
182 | .enable_mask = SYSRQ_ENABLE_REMOUNT, | |
183 | }; | |
184 | ||
8c64580d | 185 | #ifdef CONFIG_LOCKDEP |
7d12e780 | 186 | static void sysrq_handle_showlocks(int key, struct tty_struct *tty) |
de5097c2 | 187 | { |
9a11b49a | 188 | debug_show_all_locks(); |
de5097c2 | 189 | } |
8c64580d | 190 | |
de5097c2 IM |
191 | static struct sysrq_key_op sysrq_showlocks_op = { |
192 | .handler = sysrq_handle_showlocks, | |
193 | .help_msg = "show-all-locks(D)", | |
194 | .action_msg = "Show Locks Held", | |
195 | }; | |
bf36b901 AM |
196 | #else |
197 | #define sysrq_showlocks_op (*(struct sysrq_key_op *)0) | |
de5097c2 | 198 | #endif |
1da177e4 | 199 | |
5045bcae RR |
200 | #ifdef CONFIG_SMP |
201 | static DEFINE_SPINLOCK(show_lock); | |
202 | ||
203 | static void showacpu(void *dummy) | |
204 | { | |
205 | unsigned long flags; | |
206 | ||
207 | /* Idle CPUs have no interesting backtrace. */ | |
208 | if (idle_cpu(smp_processor_id())) | |
209 | return; | |
210 | ||
211 | spin_lock_irqsave(&show_lock, flags); | |
212 | printk(KERN_INFO "CPU%d:\n", smp_processor_id()); | |
213 | show_stack(NULL, NULL); | |
214 | spin_unlock_irqrestore(&show_lock, flags); | |
215 | } | |
216 | ||
217 | static void sysrq_showregs_othercpus(struct work_struct *dummy) | |
218 | { | |
8b604d52 | 219 | smp_call_function(showacpu, NULL, 0); |
5045bcae RR |
220 | } |
221 | ||
222 | static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus); | |
223 | ||
224 | static void sysrq_handle_showallcpus(int key, struct tty_struct *tty) | |
225 | { | |
226 | struct pt_regs *regs = get_irq_regs(); | |
227 | if (regs) { | |
228 | printk(KERN_INFO "CPU%d:\n", smp_processor_id()); | |
229 | show_regs(regs); | |
230 | } | |
231 | schedule_work(&sysrq_showallcpus); | |
232 | } | |
233 | ||
234 | static struct sysrq_key_op sysrq_showallcpus_op = { | |
235 | .handler = sysrq_handle_showallcpus, | |
208b95ce | 236 | .help_msg = "show-backtrace-all-active-cpus(L)", |
5045bcae RR |
237 | .action_msg = "Show backtrace of all active CPUs", |
238 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
239 | }; | |
240 | #endif | |
241 | ||
7d12e780 | 242 | static void sysrq_handle_showregs(int key, struct tty_struct *tty) |
1da177e4 | 243 | { |
7d12e780 DH |
244 | struct pt_regs *regs = get_irq_regs(); |
245 | if (regs) | |
246 | show_regs(regs); | |
1da177e4 LT |
247 | } |
248 | static struct sysrq_key_op sysrq_showregs_op = { | |
249 | .handler = sysrq_handle_showregs, | |
208b95ce | 250 | .help_msg = "show-registers(P)", |
1da177e4 LT |
251 | .action_msg = "Show Regs", |
252 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
253 | }; | |
254 | ||
7d12e780 | 255 | static void sysrq_handle_showstate(int key, struct tty_struct *tty) |
1da177e4 LT |
256 | { |
257 | show_state(); | |
258 | } | |
259 | static struct sysrq_key_op sysrq_showstate_op = { | |
260 | .handler = sysrq_handle_showstate, | |
208b95ce | 261 | .help_msg = "show-task-states(T)", |
1da177e4 LT |
262 | .action_msg = "Show State", |
263 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
264 | }; | |
265 | ||
e59e2ae2 IM |
266 | static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty) |
267 | { | |
268 | show_state_filter(TASK_UNINTERRUPTIBLE); | |
269 | } | |
270 | static struct sysrq_key_op sysrq_showstate_blocked_op = { | |
271 | .handler = sysrq_handle_showstate_blocked, | |
208b95ce | 272 | .help_msg = "show-blocked-tasks(W)", |
e59e2ae2 IM |
273 | .action_msg = "Show Blocked State", |
274 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
275 | }; | |
276 | ||
69f698ad PZ |
277 | #ifdef CONFIG_TRACING |
278 | #include <linux/ftrace.h> | |
279 | ||
280 | static void sysrq_ftrace_dump(int key, struct tty_struct *tty) | |
281 | { | |
282 | ftrace_dump(); | |
283 | } | |
284 | static struct sysrq_key_op sysrq_ftrace_dump_op = { | |
285 | .handler = sysrq_ftrace_dump, | |
286 | .help_msg = "dumpZ-ftrace-buffer", | |
287 | .action_msg = "Dump ftrace buffer", | |
288 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
289 | }; | |
290 | #else | |
291 | #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)0) | |
292 | #endif | |
e59e2ae2 | 293 | |
7d12e780 | 294 | static void sysrq_handle_showmem(int key, struct tty_struct *tty) |
1da177e4 LT |
295 | { |
296 | show_mem(); | |
297 | } | |
298 | static struct sysrq_key_op sysrq_showmem_op = { | |
299 | .handler = sysrq_handle_showmem, | |
208b95ce | 300 | .help_msg = "show-memory-usage(M)", |
1da177e4 LT |
301 | .action_msg = "Show Memory", |
302 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
303 | }; | |
304 | ||
bf36b901 AM |
305 | /* |
306 | * Signal sysrq helper function. Sends a signal to all user processes. | |
307 | */ | |
1da177e4 LT |
308 | static void send_sig_all(int sig) |
309 | { | |
310 | struct task_struct *p; | |
311 | ||
312 | for_each_process(p) { | |
b460cbc5 | 313 | if (p->mm && !is_global_init(p)) |
1da177e4 LT |
314 | /* Not swapper, init nor kernel thread */ |
315 | force_sig(sig, p); | |
316 | } | |
317 | } | |
318 | ||
7d12e780 | 319 | static void sysrq_handle_term(int key, struct tty_struct *tty) |
1da177e4 LT |
320 | { |
321 | send_sig_all(SIGTERM); | |
322 | console_loglevel = 8; | |
323 | } | |
324 | static struct sysrq_key_op sysrq_term_op = { | |
325 | .handler = sysrq_handle_term, | |
208b95ce | 326 | .help_msg = "terminate-all-tasks(E)", |
1da177e4 LT |
327 | .action_msg = "Terminate All Tasks", |
328 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | |
329 | }; | |
330 | ||
65f27f38 | 331 | static void moom_callback(struct work_struct *ignored) |
1da177e4 | 332 | { |
0e88460d | 333 | out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0); |
1da177e4 LT |
334 | } |
335 | ||
65f27f38 | 336 | static DECLARE_WORK(moom_work, moom_callback); |
1da177e4 | 337 | |
7d12e780 | 338 | static void sysrq_handle_moom(int key, struct tty_struct *tty) |
1da177e4 LT |
339 | { |
340 | schedule_work(&moom_work); | |
341 | } | |
342 | static struct sysrq_key_op sysrq_moom_op = { | |
343 | .handler = sysrq_handle_moom, | |
208b95ce | 344 | .help_msg = "memory-full-oom-kill(F)", |
1da177e4 | 345 | .action_msg = "Manual OOM execution", |
b4236f81 | 346 | .enable_mask = SYSRQ_ENABLE_SIGNAL, |
1da177e4 LT |
347 | }; |
348 | ||
7d12e780 | 349 | static void sysrq_handle_kill(int key, struct tty_struct *tty) |
1da177e4 LT |
350 | { |
351 | send_sig_all(SIGKILL); | |
352 | console_loglevel = 8; | |
353 | } | |
354 | static struct sysrq_key_op sysrq_kill_op = { | |
355 | .handler = sysrq_handle_kill, | |
208b95ce | 356 | .help_msg = "kill-all-tasks(I)", |
1da177e4 LT |
357 | .action_msg = "Kill All Tasks", |
358 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | |
359 | }; | |
360 | ||
7d12e780 | 361 | static void sysrq_handle_unrt(int key, struct tty_struct *tty) |
1da177e4 LT |
362 | { |
363 | normalize_rt_tasks(); | |
364 | } | |
365 | static struct sysrq_key_op sysrq_unrt_op = { | |
366 | .handler = sysrq_handle_unrt, | |
208b95ce | 367 | .help_msg = "nice-all-RT-tasks(N)", |
1da177e4 LT |
368 | .action_msg = "Nice All RT Tasks", |
369 | .enable_mask = SYSRQ_ENABLE_RTNICE, | |
370 | }; | |
371 | ||
372 | /* Key Operations table and lock */ | |
373 | static DEFINE_SPINLOCK(sysrq_key_table_lock); | |
bf36b901 AM |
374 | |
375 | static struct sysrq_key_op *sysrq_key_table[36] = { | |
376 | &sysrq_loglevel_op, /* 0 */ | |
377 | &sysrq_loglevel_op, /* 1 */ | |
378 | &sysrq_loglevel_op, /* 2 */ | |
379 | &sysrq_loglevel_op, /* 3 */ | |
380 | &sysrq_loglevel_op, /* 4 */ | |
381 | &sysrq_loglevel_op, /* 5 */ | |
382 | &sysrq_loglevel_op, /* 6 */ | |
383 | &sysrq_loglevel_op, /* 7 */ | |
384 | &sysrq_loglevel_op, /* 8 */ | |
385 | &sysrq_loglevel_op, /* 9 */ | |
386 | ||
387 | /* | |
d346cce3 RD |
388 | * a: Don't use for system provided sysrqs, it is handled specially on |
389 | * sparc and will never arrive. | |
bf36b901 AM |
390 | */ |
391 | NULL, /* a */ | |
392 | &sysrq_reboot_op, /* b */ | |
d346cce3 | 393 | &sysrq_crashdump_op, /* c & ibm_emac driver debug */ |
bf36b901 AM |
394 | &sysrq_showlocks_op, /* d */ |
395 | &sysrq_term_op, /* e */ | |
396 | &sysrq_moom_op, /* f */ | |
d346cce3 | 397 | /* g: May be registered by ppc for kgdb */ |
bf36b901 AM |
398 | NULL, /* g */ |
399 | NULL, /* h */ | |
400 | &sysrq_kill_op, /* i */ | |
401 | NULL, /* j */ | |
402 | &sysrq_SAK_op, /* k */ | |
5045bcae RR |
403 | #ifdef CONFIG_SMP |
404 | &sysrq_showallcpus_op, /* l */ | |
405 | #else | |
bf36b901 | 406 | NULL, /* l */ |
5045bcae | 407 | #endif |
bf36b901 AM |
408 | &sysrq_showmem_op, /* m */ |
409 | &sysrq_unrt_op, /* n */ | |
d346cce3 | 410 | /* o: This will often be registered as 'Off' at init time */ |
bf36b901 AM |
411 | NULL, /* o */ |
412 | &sysrq_showregs_op, /* p */ | |
88ad0bf6 | 413 | &sysrq_show_timers_op, /* q */ |
d346cce3 | 414 | &sysrq_unraw_op, /* r */ |
bf36b901 AM |
415 | &sysrq_sync_op, /* s */ |
416 | &sysrq_showstate_op, /* t */ | |
417 | &sysrq_mountro_op, /* u */ | |
d346cce3 | 418 | /* v: May be registered at init time by SMP VOYAGER */ |
bf36b901 | 419 | NULL, /* v */ |
d346cce3 RD |
420 | &sysrq_showstate_blocked_op, /* w */ |
421 | /* x: May be registered on ppc/powerpc for xmon */ | |
422 | NULL, /* x */ | |
93dae5b7 | 423 | /* y: May be registered on sparc64 for global register dump */ |
bf36b901 | 424 | NULL, /* y */ |
69f698ad | 425 | &sysrq_ftrace_dump_op, /* z */ |
1da177e4 LT |
426 | }; |
427 | ||
428 | /* key2index calculation, -1 on invalid index */ | |
bf36b901 AM |
429 | static int sysrq_key_table_key2index(int key) |
430 | { | |
1da177e4 | 431 | int retval; |
bf36b901 AM |
432 | |
433 | if ((key >= '0') && (key <= '9')) | |
1da177e4 | 434 | retval = key - '0'; |
bf36b901 | 435 | else if ((key >= 'a') && (key <= 'z')) |
1da177e4 | 436 | retval = key + 10 - 'a'; |
bf36b901 | 437 | else |
1da177e4 | 438 | retval = -1; |
1da177e4 LT |
439 | return retval; |
440 | } | |
441 | ||
442 | /* | |
443 | * get and put functions for the table, exposed to modules. | |
444 | */ | |
bf36b901 AM |
445 | struct sysrq_key_op *__sysrq_get_key_op(int key) |
446 | { | |
447 | struct sysrq_key_op *op_p = NULL; | |
1da177e4 | 448 | int i; |
bf36b901 | 449 | |
1da177e4 | 450 | i = sysrq_key_table_key2index(key); |
bf36b901 AM |
451 | if (i != -1) |
452 | op_p = sysrq_key_table[i]; | |
1da177e4 LT |
453 | return op_p; |
454 | } | |
455 | ||
bf36b901 AM |
456 | static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p) |
457 | { | |
458 | int i = sysrq_key_table_key2index(key); | |
1da177e4 | 459 | |
1da177e4 LT |
460 | if (i != -1) |
461 | sysrq_key_table[i] = op_p; | |
462 | } | |
463 | ||
464 | /* | |
bf36b901 AM |
465 | * This is the non-locking version of handle_sysrq. It must/can only be called |
466 | * by sysrq key handlers, as they are inside of the lock | |
1da177e4 | 467 | */ |
7d12e780 | 468 | void __handle_sysrq(int key, struct tty_struct *tty, int check_mask) |
1da177e4 LT |
469 | { |
470 | struct sysrq_key_op *op_p; | |
471 | int orig_log_level; | |
bf36b901 | 472 | int i; |
1da177e4 LT |
473 | unsigned long flags; |
474 | ||
475 | spin_lock_irqsave(&sysrq_key_table_lock, flags); | |
476 | orig_log_level = console_loglevel; | |
477 | console_loglevel = 7; | |
478 | printk(KERN_INFO "SysRq : "); | |
479 | ||
480 | op_p = __sysrq_get_key_op(key); | |
481 | if (op_p) { | |
bf36b901 AM |
482 | /* |
483 | * Should we check for enabled operations (/proc/sysrq-trigger | |
484 | * should not) and is the invoked operation enabled? | |
485 | */ | |
5d6f647f | 486 | if (!check_mask || sysrq_on_mask(op_p->enable_mask)) { |
bf36b901 | 487 | printk("%s\n", op_p->action_msg); |
1da177e4 | 488 | console_loglevel = orig_log_level; |
7d12e780 | 489 | op_p->handler(key, tty); |
bf36b901 | 490 | } else { |
1da177e4 | 491 | printk("This sysrq operation is disabled.\n"); |
bf36b901 | 492 | } |
1da177e4 LT |
493 | } else { |
494 | printk("HELP : "); | |
495 | /* Only print the help msg once per handler */ | |
bf36b901 AM |
496 | for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) { |
497 | if (sysrq_key_table[i]) { | |
498 | int j; | |
499 | ||
500 | for (j = 0; sysrq_key_table[i] != | |
501 | sysrq_key_table[j]; j++) | |
502 | ; | |
503 | if (j != i) | |
504 | continue; | |
505 | printk("%s ", sysrq_key_table[i]->help_msg); | |
506 | } | |
1da177e4 | 507 | } |
bf36b901 | 508 | printk("\n"); |
1da177e4 LT |
509 | console_loglevel = orig_log_level; |
510 | } | |
511 | spin_unlock_irqrestore(&sysrq_key_table_lock, flags); | |
512 | } | |
513 | ||
514 | /* | |
515 | * This function is called by the keyboard handler when SysRq is pressed | |
516 | * and any other keycode arrives. | |
517 | */ | |
7d12e780 | 518 | void handle_sysrq(int key, struct tty_struct *tty) |
1da177e4 | 519 | { |
5d6f647f IM |
520 | if (sysrq_on()) |
521 | __handle_sysrq(key, tty, 1); | |
1da177e4 | 522 | } |
bf36b901 | 523 | EXPORT_SYMBOL(handle_sysrq); |
1da177e4 | 524 | |
cf62ddce | 525 | static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p, |
bf36b901 AM |
526 | struct sysrq_key_op *remove_op_p) |
527 | { | |
1da177e4 LT |
528 | |
529 | int retval; | |
530 | unsigned long flags; | |
531 | ||
532 | spin_lock_irqsave(&sysrq_key_table_lock, flags); | |
533 | if (__sysrq_get_key_op(key) == remove_op_p) { | |
534 | __sysrq_put_key_op(key, insert_op_p); | |
535 | retval = 0; | |
536 | } else { | |
537 | retval = -1; | |
538 | } | |
539 | spin_unlock_irqrestore(&sysrq_key_table_lock, flags); | |
1da177e4 LT |
540 | return retval; |
541 | } | |
542 | ||
543 | int register_sysrq_key(int key, struct sysrq_key_op *op_p) | |
544 | { | |
545 | return __sysrq_swap_key_ops(key, op_p, NULL); | |
546 | } | |
bf36b901 | 547 | EXPORT_SYMBOL(register_sysrq_key); |
1da177e4 LT |
548 | |
549 | int unregister_sysrq_key(int key, struct sysrq_key_op *op_p) | |
550 | { | |
551 | return __sysrq_swap_key_ops(key, NULL, op_p); | |
552 | } | |
1da177e4 | 553 | EXPORT_SYMBOL(unregister_sysrq_key); |
f40cbaa5 AD |
554 | |
555 | #ifdef CONFIG_PROC_FS | |
556 | /* | |
557 | * writing 'C' to /proc/sysrq-trigger is like sysrq-C | |
558 | */ | |
559 | static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, | |
560 | size_t count, loff_t *ppos) | |
561 | { | |
562 | if (count) { | |
563 | char c; | |
564 | ||
565 | if (get_user(c, buf)) | |
566 | return -EFAULT; | |
567 | __handle_sysrq(c, NULL, 0); | |
568 | } | |
569 | return count; | |
570 | } | |
571 | ||
572 | static const struct file_operations proc_sysrq_trigger_operations = { | |
573 | .write = write_sysrq_trigger, | |
574 | }; | |
575 | ||
576 | static int __init sysrq_init(void) | |
577 | { | |
578 | proc_create("sysrq-trigger", S_IWUSR, NULL, &proc_sysrq_trigger_operations); | |
579 | return 0; | |
580 | } | |
581 | module_init(sysrq_init); | |
582 | #endif |