aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/printk.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /kernel/printk.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'kernel/printk.c')
-rw-r--r--kernel/printk.c996
1 files changed, 996 insertions, 0 deletions
diff --git a/kernel/printk.c b/kernel/printk.c
new file mode 100644
index 000000000000..1498689548d1
--- /dev/null
+++ b/kernel/printk.c
@@ -0,0 +1,996 @@
1/*
2 * linux/kernel/printk.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Modified to make sys_syslog() more flexible: added commands to
7 * return the last 4k of kernel messages, regardless of whether
8 * they've been read or not. Added option to suppress kernel printk's
9 * to the console. Added hook for sending the console messages
10 * elsewhere, in preparation for a serial line console (someday).
11 * Ted Ts'o, 2/11/93.
12 * Modified for sysctl support, 1/8/97, Chris Horn.
13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14 * manfreds@colorfullife.com
15 * Rewrote bits to get rid of console_lock
16 * 01Mar01 Andrew Morton <andrewm@uow.edu.au>
17 */
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
23#include <linux/smp_lock.h>
24#include <linux/console.h>
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/interrupt.h> /* For in_interrupt() */
28#include <linux/config.h>
29#include <linux/delay.h>
30#include <linux/smp.h>
31#include <linux/security.h>
32#include <linux/bootmem.h>
33#include <linux/syscalls.h>
34
35#include <asm/uaccess.h>
36
37#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
38
39/* printk's without a loglevel use this.. */
40#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
41
42/* We show everything that is MORE important than this.. */
43#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
44#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
45
46DECLARE_WAIT_QUEUE_HEAD(log_wait);
47
48int console_printk[4] = {
49 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
50 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
51 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
52 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
53};
54
55EXPORT_SYMBOL(console_printk);
56
57/*
58 * Low lever drivers may need that to know if they can schedule in
59 * their unblank() callback or not. So let's export it.
60 */
61int oops_in_progress;
62EXPORT_SYMBOL(oops_in_progress);
63
64/*
65 * console_sem protects the console_drivers list, and also
66 * provides serialisation for access to the entire console
67 * driver system.
68 */
69static DECLARE_MUTEX(console_sem);
70struct console *console_drivers;
71/*
72 * This is used for debugging the mess that is the VT code by
73 * keeping track if we have the console semaphore held. It's
74 * definitely not the perfect debug tool (we don't know if _WE_
75 * hold it are racing, but it helps tracking those weird code
76 * path in the console code where we end up in places I want
77 * locked without the console sempahore held
78 */
79static int console_locked;
80
81/*
82 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
83 * It is also used in interesting ways to provide interlocking in
84 * release_console_sem().
85 */
86static DEFINE_SPINLOCK(logbuf_lock);
87
88static char __log_buf[__LOG_BUF_LEN];
89static char *log_buf = __log_buf;
90static int log_buf_len = __LOG_BUF_LEN;
91
92#define LOG_BUF_MASK (log_buf_len-1)
93#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
94
95/*
96 * The indices into log_buf are not constrained to log_buf_len - they
97 * must be masked before subscripting
98 */
99static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
100static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
101static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
102static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
103
104/*
105 * Array of consoles built from command line options (console=)
106 */
107struct console_cmdline
108{
109 char name[8]; /* Name of the driver */
110 int index; /* Minor dev. to use */
111 char *options; /* Options for the driver */
112};
113
114#define MAX_CMDLINECONSOLES 8
115
116static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
117static int selected_console = -1;
118static int preferred_console = -1;
119
120/* Flag: console code may call schedule() */
121static int console_may_schedule;
122
123/*
124 * Setup a list of consoles. Called from init/main.c
125 */
126static int __init console_setup(char *str)
127{
128 char name[sizeof(console_cmdline[0].name)];
129 char *s, *options;
130 int idx;
131
132 /*
133 * Decode str into name, index, options.
134 */
135 if (str[0] >= '0' && str[0] <= '9') {
136 strcpy(name, "ttyS");
137 strncpy(name + 4, str, sizeof(name) - 5);
138 } else
139 strncpy(name, str, sizeof(name) - 1);
140 name[sizeof(name) - 1] = 0;
141 if ((options = strchr(str, ',')) != NULL)
142 *(options++) = 0;
143#ifdef __sparc__
144 if (!strcmp(str, "ttya"))
145 strcpy(name, "ttyS0");
146 if (!strcmp(str, "ttyb"))
147 strcpy(name, "ttyS1");
148#endif
149 for(s = name; *s; s++)
150 if ((*s >= '0' && *s <= '9') || *s == ',')
151 break;
152 idx = simple_strtoul(s, NULL, 10);
153 *s = 0;
154
155 add_preferred_console(name, idx, options);
156 return 1;
157}
158
159__setup("console=", console_setup);
160
161/**
162 * add_preferred_console - add a device to the list of preferred consoles.
163 *
164 * The last preferred console added will be used for kernel messages
165 * and stdin/out/err for init. Normally this is used by console_setup
166 * above to handle user-supplied console arguments; however it can also
167 * be used by arch-specific code either to override the user or more
168 * commonly to provide a default console (ie from PROM variables) when
169 * the user has not supplied one.
170 */
171int __init add_preferred_console(char *name, int idx, char *options)
172{
173 struct console_cmdline *c;
174 int i;
175
176 /*
177 * See if this tty is not yet registered, and
178 * if we have a slot free.
179 */
180 for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
181 if (strcmp(console_cmdline[i].name, name) == 0 &&
182 console_cmdline[i].index == idx) {
183 selected_console = i;
184 return 0;
185 }
186 if (i == MAX_CMDLINECONSOLES)
187 return -E2BIG;
188 selected_console = i;
189 c = &console_cmdline[i];
190 memcpy(c->name, name, sizeof(c->name));
191 c->name[sizeof(c->name) - 1] = 0;
192 c->options = options;
193 c->index = idx;
194 return 0;
195}
196
197static int __init log_buf_len_setup(char *str)
198{
199 unsigned long size = memparse(str, &str);
200 unsigned long flags;
201
202 if (size)
203 size = roundup_pow_of_two(size);
204 if (size > log_buf_len) {
205 unsigned long start, dest_idx, offset;
206 char * new_log_buf;
207
208 new_log_buf = alloc_bootmem(size);
209 if (!new_log_buf) {
210 printk("log_buf_len: allocation failed\n");
211 goto out;
212 }
213
214 spin_lock_irqsave(&logbuf_lock, flags);
215 log_buf_len = size;
216 log_buf = new_log_buf;
217
218 offset = start = min(con_start, log_start);
219 dest_idx = 0;
220 while (start != log_end) {
221 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
222 start++;
223 dest_idx++;
224 }
225 log_start -= offset;
226 con_start -= offset;
227 log_end -= offset;
228 spin_unlock_irqrestore(&logbuf_lock, flags);
229
230 printk("log_buf_len: %d\n", log_buf_len);
231 }
232out:
233
234 return 1;
235}
236
237__setup("log_buf_len=", log_buf_len_setup);
238
239/*
240 * Commands to do_syslog:
241 *
242 * 0 -- Close the log. Currently a NOP.
243 * 1 -- Open the log. Currently a NOP.
244 * 2 -- Read from the log.
245 * 3 -- Read all messages remaining in the ring buffer.
246 * 4 -- Read and clear all messages remaining in the ring buffer
247 * 5 -- Clear ring buffer.
248 * 6 -- Disable printk's to console
249 * 7 -- Enable printk's to console
250 * 8 -- Set level of messages printed to console
251 * 9 -- Return number of unread characters in the log buffer
252 * 10 -- Return size of the log buffer
253 */
254int do_syslog(int type, char __user * buf, int len)
255{
256 unsigned long i, j, limit, count;
257 int do_clear = 0;
258 char c;
259 int error = 0;
260
261 error = security_syslog(type);
262 if (error)
263 return error;
264
265 switch (type) {
266 case 0: /* Close log */
267 break;
268 case 1: /* Open log */
269 break;
270 case 2: /* Read from log */
271 error = -EINVAL;
272 if (!buf || len < 0)
273 goto out;
274 error = 0;
275 if (!len)
276 goto out;
277 if (!access_ok(VERIFY_WRITE, buf, len)) {
278 error = -EFAULT;
279 goto out;
280 }
281 error = wait_event_interruptible(log_wait, (log_start - log_end));
282 if (error)
283 goto out;
284 i = 0;
285 spin_lock_irq(&logbuf_lock);
286 while (!error && (log_start != log_end) && i < len) {
287 c = LOG_BUF(log_start);
288 log_start++;
289 spin_unlock_irq(&logbuf_lock);
290 error = __put_user(c,buf);
291 buf++;
292 i++;
293 cond_resched();
294 spin_lock_irq(&logbuf_lock);
295 }
296 spin_unlock_irq(&logbuf_lock);
297 if (!error)
298 error = i;
299 break;
300 case 4: /* Read/clear last kernel messages */
301 do_clear = 1;
302 /* FALL THRU */
303 case 3: /* Read last kernel messages */
304 error = -EINVAL;
305 if (!buf || len < 0)
306 goto out;
307 error = 0;
308 if (!len)
309 goto out;
310 if (!access_ok(VERIFY_WRITE, buf, len)) {
311 error = -EFAULT;
312 goto out;
313 }
314 count = len;
315 if (count > log_buf_len)
316 count = log_buf_len;
317 spin_lock_irq(&logbuf_lock);
318 if (count > logged_chars)
319 count = logged_chars;
320 if (do_clear)
321 logged_chars = 0;
322 limit = log_end;
323 /*
324 * __put_user() could sleep, and while we sleep
325 * printk() could overwrite the messages
326 * we try to copy to user space. Therefore
327 * the messages are copied in reverse. <manfreds>
328 */
329 for(i = 0; i < count && !error; i++) {
330 j = limit-1-i;
331 if (j + log_buf_len < log_end)
332 break;
333 c = LOG_BUF(j);
334 spin_unlock_irq(&logbuf_lock);
335 error = __put_user(c,&buf[count-1-i]);
336 cond_resched();
337 spin_lock_irq(&logbuf_lock);
338 }
339 spin_unlock_irq(&logbuf_lock);
340 if (error)
341 break;
342 error = i;
343 if(i != count) {
344 int offset = count-error;
345 /* buffer overflow during copy, correct user buffer. */
346 for(i=0;i<error;i++) {
347 if (__get_user(c,&buf[i+offset]) ||
348 __put_user(c,&buf[i])) {
349 error = -EFAULT;
350 break;
351 }
352 cond_resched();
353 }
354 }
355 break;
356 case 5: /* Clear ring buffer */
357 logged_chars = 0;
358 break;
359 case 6: /* Disable logging to console */
360 console_loglevel = minimum_console_loglevel;
361 break;
362 case 7: /* Enable logging to console */
363 console_loglevel = default_console_loglevel;
364 break;
365 case 8: /* Set level of messages printed to console */
366 error = -EINVAL;
367 if (len < 1 || len > 8)
368 goto out;
369 if (len < minimum_console_loglevel)
370 len = minimum_console_loglevel;
371 console_loglevel = len;
372 error = 0;
373 break;
374 case 9: /* Number of chars in the log buffer */
375 error = log_end - log_start;
376 break;
377 case 10: /* Size of the log buffer */
378 error = log_buf_len;
379 break;
380 default:
381 error = -EINVAL;
382 break;
383 }
384out:
385 return error;
386}
387
388asmlinkage long sys_syslog(int type, char __user * buf, int len)
389{
390 return do_syslog(type, buf, len);
391}
392
393/*
394 * Call the console drivers on a range of log_buf
395 */
396static void __call_console_drivers(unsigned long start, unsigned long end)
397{
398 struct console *con;
399
400 for (con = console_drivers; con; con = con->next) {
401 if ((con->flags & CON_ENABLED) && con->write)
402 con->write(con, &LOG_BUF(start), end - start);
403 }
404}
405
406/*
407 * Write out chars from start to end - 1 inclusive
408 */
409static void _call_console_drivers(unsigned long start,
410 unsigned long end, int msg_log_level)
411{
412 if (msg_log_level < console_loglevel &&
413 console_drivers && start != end) {
414 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
415 /* wrapped write */
416 __call_console_drivers(start & LOG_BUF_MASK,
417 log_buf_len);
418 __call_console_drivers(0, end & LOG_BUF_MASK);
419 } else {
420 __call_console_drivers(start, end);
421 }
422 }
423}
424
425/*
426 * Call the console drivers, asking them to write out
427 * log_buf[start] to log_buf[end - 1].
428 * The console_sem must be held.
429 */
430static void call_console_drivers(unsigned long start, unsigned long end)
431{
432 unsigned long cur_index, start_print;
433 static int msg_level = -1;
434
435 if (((long)(start - end)) > 0)
436 BUG();
437
438 cur_index = start;
439 start_print = start;
440 while (cur_index != end) {
441 if ( msg_level < 0 &&
442 ((end - cur_index) > 2) &&
443 LOG_BUF(cur_index + 0) == '<' &&
444 LOG_BUF(cur_index + 1) >= '0' &&
445 LOG_BUF(cur_index + 1) <= '7' &&
446 LOG_BUF(cur_index + 2) == '>')
447 {
448 msg_level = LOG_BUF(cur_index + 1) - '0';
449 cur_index += 3;
450 start_print = cur_index;
451 }
452 while (cur_index != end) {
453 char c = LOG_BUF(cur_index);
454 cur_index++;
455
456 if (c == '\n') {
457 if (msg_level < 0) {
458 /*
459 * printk() has already given us loglevel tags in
460 * the buffer. This code is here in case the
461 * log buffer has wrapped right round and scribbled
462 * on those tags
463 */
464 msg_level = default_message_loglevel;
465 }
466 _call_console_drivers(start_print, cur_index, msg_level);
467 msg_level = -1;
468 start_print = cur_index;
469 break;
470 }
471 }
472 }
473 _call_console_drivers(start_print, end, msg_level);
474}
475
476static void emit_log_char(char c)
477{
478 LOG_BUF(log_end) = c;
479 log_end++;
480 if (log_end - log_start > log_buf_len)
481 log_start = log_end - log_buf_len;
482 if (log_end - con_start > log_buf_len)
483 con_start = log_end - log_buf_len;
484 if (logged_chars < log_buf_len)
485 logged_chars++;
486}
487
488/*
489 * Zap console related locks when oopsing. Only zap at most once
490 * every 10 seconds, to leave time for slow consoles to print a
491 * full oops.
492 */
493static void zap_locks(void)
494{
495 static unsigned long oops_timestamp;
496
497 if (time_after_eq(jiffies, oops_timestamp) &&
498 !time_after(jiffies, oops_timestamp + 30*HZ))
499 return;
500
501 oops_timestamp = jiffies;
502
503 /* If a crash is occurring, make sure we can't deadlock */
504 spin_lock_init(&logbuf_lock);
505 /* And make sure that we print immediately */
506 init_MUTEX(&console_sem);
507}
508
509#if defined(CONFIG_PRINTK_TIME)
510static int printk_time = 1;
511#else
512static int printk_time = 0;
513#endif
514
515static int __init printk_time_setup(char *str)
516{
517 if (*str)
518 return 0;
519 printk_time = 1;
520 return 1;
521}
522
523__setup("time", printk_time_setup);
524
525/*
526 * This is printk. It can be called from any context. We want it to work.
527 *
528 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
529 * call the console drivers. If we fail to get the semaphore we place the output
530 * into the log buffer and return. The current holder of the console_sem will
531 * notice the new output in release_console_sem() and will send it to the
532 * consoles before releasing the semaphore.
533 *
534 * One effect of this deferred printing is that code which calls printk() and
535 * then changes console_loglevel may break. This is because console_loglevel
536 * is inspected when the actual printing occurs.
537 */
538asmlinkage int printk(const char *fmt, ...)
539{
540 va_list args;
541 int r;
542
543 va_start(args, fmt);
544 r = vprintk(fmt, args);
545 va_end(args);
546
547 return r;
548}
549
550asmlinkage int vprintk(const char *fmt, va_list args)
551{
552 unsigned long flags;
553 int printed_len;
554 char *p;
555 static char printk_buf[1024];
556 static int log_level_unknown = 1;
557
558 if (unlikely(oops_in_progress))
559 zap_locks();
560
561 /* This stops the holder of console_sem just where we want him */
562 spin_lock_irqsave(&logbuf_lock, flags);
563
564 /* Emit the output into the temporary buffer */
565 printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
566
567 /*
568 * Copy the output into log_buf. If the caller didn't provide
569 * appropriate log level tags, we insert them here
570 */
571 for (p = printk_buf; *p; p++) {
572 if (log_level_unknown) {
573 /* log_level_unknown signals the start of a new line */
574 if (printk_time) {
575 int loglev_char;
576 char tbuf[50], *tp;
577 unsigned tlen;
578 unsigned long long t;
579 unsigned long nanosec_rem;
580
581 /*
582 * force the log level token to be
583 * before the time output.
584 */
585 if (p[0] == '<' && p[1] >='0' &&
586 p[1] <= '7' && p[2] == '>') {
587 loglev_char = p[1];
588 p += 3;
589 printed_len += 3;
590 } else {
591 loglev_char = default_message_loglevel
592 + '0';
593 }
594 t = sched_clock();
595 nanosec_rem = do_div(t, 1000000000);
596 tlen = sprintf(tbuf,
597 "<%c>[%5lu.%06lu] ",
598 loglev_char,
599 (unsigned long)t,
600 nanosec_rem/1000);
601
602 for (tp = tbuf; tp < tbuf + tlen; tp++)
603 emit_log_char(*tp);
604 printed_len += tlen - 3;
605 } else {
606 if (p[0] != '<' || p[1] < '0' ||
607 p[1] > '7' || p[2] != '>') {
608 emit_log_char('<');
609 emit_log_char(default_message_loglevel
610 + '0');
611 emit_log_char('>');
612 }
613 printed_len += 3;
614 }
615 log_level_unknown = 0;
616 if (!*p)
617 break;
618 }
619 emit_log_char(*p);
620 if (*p == '\n')
621 log_level_unknown = 1;
622 }
623
624 if (!cpu_online(smp_processor_id()) &&
625 system_state != SYSTEM_RUNNING) {
626 /*
627 * Some console drivers may assume that per-cpu resources have
628 * been allocated. So don't allow them to be called by this
629 * CPU until it is officially up. We shouldn't be calling into
630 * random console drivers on a CPU which doesn't exist yet..
631 */
632 spin_unlock_irqrestore(&logbuf_lock, flags);
633 goto out;
634 }
635 if (!down_trylock(&console_sem)) {
636 console_locked = 1;
637 /*
638 * We own the drivers. We can drop the spinlock and let
639 * release_console_sem() print the text
640 */
641 spin_unlock_irqrestore(&logbuf_lock, flags);
642 console_may_schedule = 0;
643 release_console_sem();
644 } else {
645 /*
646 * Someone else owns the drivers. We drop the spinlock, which
647 * allows the semaphore holder to proceed and to call the
648 * console drivers with the output which we just produced.
649 */
650 spin_unlock_irqrestore(&logbuf_lock, flags);
651 }
652out:
653 return printed_len;
654}
655EXPORT_SYMBOL(printk);
656EXPORT_SYMBOL(vprintk);
657
658/**
659 * acquire_console_sem - lock the console system for exclusive use.
660 *
661 * Acquires a semaphore which guarantees that the caller has
662 * exclusive access to the console system and the console_drivers list.
663 *
664 * Can sleep, returns nothing.
665 */
666void acquire_console_sem(void)
667{
668 if (in_interrupt())
669 BUG();
670 down(&console_sem);
671 console_locked = 1;
672 console_may_schedule = 1;
673}
674EXPORT_SYMBOL(acquire_console_sem);
675
676int try_acquire_console_sem(void)
677{
678 if (down_trylock(&console_sem))
679 return -1;
680 console_locked = 1;
681 console_may_schedule = 0;
682 return 0;
683}
684EXPORT_SYMBOL(try_acquire_console_sem);
685
686int is_console_locked(void)
687{
688 return console_locked;
689}
690EXPORT_SYMBOL(is_console_locked);
691
692/**
693 * release_console_sem - unlock the console system
694 *
695 * Releases the semaphore which the caller holds on the console system
696 * and the console driver list.
697 *
698 * While the semaphore was held, console output may have been buffered
699 * by printk(). If this is the case, release_console_sem() emits
700 * the output prior to releasing the semaphore.
701 *
702 * If there is output waiting for klogd, we wake it up.
703 *
704 * release_console_sem() may be called from any context.
705 */
706void release_console_sem(void)
707{
708 unsigned long flags;
709 unsigned long _con_start, _log_end;
710 unsigned long wake_klogd = 0;
711
712 for ( ; ; ) {
713 spin_lock_irqsave(&logbuf_lock, flags);
714 wake_klogd |= log_start - log_end;
715 if (con_start == log_end)
716 break; /* Nothing to print */
717 _con_start = con_start;
718 _log_end = log_end;
719 con_start = log_end; /* Flush */
720 spin_unlock(&logbuf_lock);
721 call_console_drivers(_con_start, _log_end);
722 local_irq_restore(flags);
723 }
724 console_locked = 0;
725 console_may_schedule = 0;
726 up(&console_sem);
727 spin_unlock_irqrestore(&logbuf_lock, flags);
728 if (wake_klogd && !oops_in_progress && waitqueue_active(&log_wait))
729 wake_up_interruptible(&log_wait);
730}
731EXPORT_SYMBOL(release_console_sem);
732
733/** console_conditional_schedule - yield the CPU if required
734 *
735 * If the console code is currently allowed to sleep, and
736 * if this CPU should yield the CPU to another task, do
737 * so here.
738 *
739 * Must be called within acquire_console_sem().
740 */
741void __sched console_conditional_schedule(void)
742{
743 if (console_may_schedule)
744 cond_resched();
745}
746EXPORT_SYMBOL(console_conditional_schedule);
747
748void console_print(const char *s)
749{
750 printk(KERN_EMERG "%s", s);
751}
752EXPORT_SYMBOL(console_print);
753
754void console_unblank(void)
755{
756 struct console *c;
757
758 /*
759 * console_unblank can no longer be called in interrupt context unless
760 * oops_in_progress is set to 1..
761 */
762 if (oops_in_progress) {
763 if (down_trylock(&console_sem) != 0)
764 return;
765 } else
766 acquire_console_sem();
767
768 console_locked = 1;
769 console_may_schedule = 0;
770 for (c = console_drivers; c != NULL; c = c->next)
771 if ((c->flags & CON_ENABLED) && c->unblank)
772 c->unblank();
773 release_console_sem();
774}
775EXPORT_SYMBOL(console_unblank);
776
777/*
778 * Return the console tty driver structure and its associated index
779 */
780struct tty_driver *console_device(int *index)
781{
782 struct console *c;
783 struct tty_driver *driver = NULL;
784
785 acquire_console_sem();
786 for (c = console_drivers; c != NULL; c = c->next) {
787 if (!c->device)
788 continue;
789 driver = c->device(c, index);
790 if (driver)
791 break;
792 }
793 release_console_sem();
794 return driver;
795}
796
797/*
798 * Prevent further output on the passed console device so that (for example)
799 * serial drivers can disable console output before suspending a port, and can
800 * re-enable output afterwards.
801 */
802void console_stop(struct console *console)
803{
804 acquire_console_sem();
805 console->flags &= ~CON_ENABLED;
806 release_console_sem();
807}
808EXPORT_SYMBOL(console_stop);
809
810void console_start(struct console *console)
811{
812 acquire_console_sem();
813 console->flags |= CON_ENABLED;
814 release_console_sem();
815}
816EXPORT_SYMBOL(console_start);
817
818/*
819 * The console driver calls this routine during kernel initialization
820 * to register the console printing procedure with printk() and to
821 * print any messages that were printed by the kernel before the
822 * console driver was initialized.
823 */
824void register_console(struct console * console)
825{
826 int i;
827 unsigned long flags;
828
829 if (preferred_console < 0)
830 preferred_console = selected_console;
831
832 /*
833 * See if we want to use this console driver. If we
834 * didn't select a console we take the first one
835 * that registers here.
836 */
837 if (preferred_console < 0) {
838 if (console->index < 0)
839 console->index = 0;
840 if (console->setup == NULL ||
841 console->setup(console, NULL) == 0) {
842 console->flags |= CON_ENABLED | CON_CONSDEV;
843 preferred_console = 0;
844 }
845 }
846
847 /*
848 * See if this console matches one we selected on
849 * the command line.
850 */
851 for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) {
852 if (strcmp(console_cmdline[i].name, console->name) != 0)
853 continue;
854 if (console->index >= 0 &&
855 console->index != console_cmdline[i].index)
856 continue;
857 if (console->index < 0)
858 console->index = console_cmdline[i].index;
859 if (console->setup &&
860 console->setup(console, console_cmdline[i].options) != 0)
861 break;
862 console->flags |= CON_ENABLED;
863 console->index = console_cmdline[i].index;
864 if (i == preferred_console)
865 console->flags |= CON_CONSDEV;
866 break;
867 }
868
869 if (!(console->flags & CON_ENABLED))
870 return;
871
872 if (console_drivers && (console_drivers->flags & CON_BOOT)) {
873 unregister_console(console_drivers);
874 console->flags &= ~CON_PRINTBUFFER;
875 }
876
877 /*
878 * Put this console in the list - keep the
879 * preferred driver at the head of the list.
880 */
881 acquire_console_sem();
882 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
883 console->next = console_drivers;
884 console_drivers = console;
885 } else {
886 console->next = console_drivers->next;
887 console_drivers->next = console;
888 }
889 if (console->flags & CON_PRINTBUFFER) {
890 /*
891 * release_console_sem() will print out the buffered messages
892 * for us.
893 */
894 spin_lock_irqsave(&logbuf_lock, flags);
895 con_start = log_start;
896 spin_unlock_irqrestore(&logbuf_lock, flags);
897 }
898 release_console_sem();
899}
900EXPORT_SYMBOL(register_console);
901
902int unregister_console(struct console * console)
903{
904 struct console *a,*b;
905 int res = 1;
906
907 acquire_console_sem();
908 if (console_drivers == console) {
909 console_drivers=console->next;
910 res = 0;
911 } else {
912 for (a=console_drivers->next, b=console_drivers ;
913 a; b=a, a=b->next) {
914 if (a == console) {
915 b->next = a->next;
916 res = 0;
917 break;
918 }
919 }
920 }
921
922 /* If last console is removed, we re-enable picking the first
923 * one that gets registered. Without that, pmac early boot console
924 * would prevent fbcon from taking over.
925 */
926 if (console_drivers == NULL)
927 preferred_console = selected_console;
928
929
930 release_console_sem();
931 return res;
932}
933EXPORT_SYMBOL(unregister_console);
934
935/**
936 * tty_write_message - write a message to a certain tty, not just the console.
937 *
938 * This is used for messages that need to be redirected to a specific tty.
939 * We don't put it into the syslog queue right now maybe in the future if
940 * really needed.
941 */
942void tty_write_message(struct tty_struct *tty, char *msg)
943{
944 if (tty && tty->driver->write)
945 tty->driver->write(tty, msg, strlen(msg));
946 return;
947}
948
949/*
950 * printk rate limiting, lifted from the networking subsystem.
951 *
952 * This enforces a rate limit: not more than one kernel message
953 * every printk_ratelimit_jiffies to make a denial-of-service
954 * attack impossible.
955 */
956int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
957{
958 static DEFINE_SPINLOCK(ratelimit_lock);
959 static unsigned long toks = 10*5*HZ;
960 static unsigned long last_msg;
961 static int missed;
962 unsigned long flags;
963 unsigned long now = jiffies;
964
965 spin_lock_irqsave(&ratelimit_lock, flags);
966 toks += now - last_msg;
967 last_msg = now;
968 if (toks > (ratelimit_burst * ratelimit_jiffies))
969 toks = ratelimit_burst * ratelimit_jiffies;
970 if (toks >= ratelimit_jiffies) {
971 int lost = missed;
972 missed = 0;
973 toks -= ratelimit_jiffies;
974 spin_unlock_irqrestore(&ratelimit_lock, flags);
975 if (lost)
976 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
977 return 1;
978 }
979 missed++;
980 spin_unlock_irqrestore(&ratelimit_lock, flags);
981 return 0;
982}
983EXPORT_SYMBOL(__printk_ratelimit);
984
985/* minimum time in jiffies between messages */
986int printk_ratelimit_jiffies = 5*HZ;
987
988/* number of messages we send before ratelimiting */
989int printk_ratelimit_burst = 10;
990
991int printk_ratelimit(void)
992{
993 return __printk_ratelimit(printk_ratelimit_jiffies,
994 printk_ratelimit_burst);
995}
996EXPORT_SYMBOL(printk_ratelimit);