2 * linux/kernel/printk.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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).
12 * Modified for sysctl support, 1/8/97, Chris Horn.
13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14 * manfred@colorfullife.com
15 * Rewrote bits to get rid of console_lock
16 * 01Mar01 Andrew Morton <andrewm@uow.edu.au>
19 #include <linux/kernel.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/console.h>
24 #include <linux/init.h>
25 #include <linux/jiffies.h>
26 #include <linux/nmi.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/interrupt.h> /* For in_interrupt() */
30 #include <linux/delay.h>
31 #include <linux/smp.h>
32 #include <linux/security.h>
33 #include <linux/bootmem.h>
34 #include <linux/syscalls.h>
35 #include <linux/jiffies.h>
37 #include <asm/uaccess.h>
39 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
41 /* printk's without a loglevel use this.. */
42 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
44 /* We show everything that is MORE important than this.. */
45 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
46 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
48 DECLARE_WAIT_QUEUE_HEAD(log_wait);
50 int console_printk[4] = {
51 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
52 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
53 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
54 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
58 * Low level drivers may need that to know if they can schedule in
59 * their unblank() callback or not. So let's export it.
62 EXPORT_SYMBOL(oops_in_progress);
65 * console_sem protects the console_drivers list, and also
66 * provides serialisation for access to the entire console
69 static DECLARE_MUTEX(console_sem);
70 static DECLARE_MUTEX(secondary_console_sem);
71 struct console *console_drivers;
73 * This is used for debugging the mess that is the VT code by
74 * keeping track if we have the console semaphore held. It's
75 * definitely not the perfect debug tool (we don't know if _WE_
76 * hold it are racing, but it helps tracking those weird code
77 * path in the console code where we end up in places I want
78 * locked without the console sempahore held
80 static int console_locked, console_suspended;
83 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
84 * It is also used in interesting ways to provide interlocking in
85 * release_console_sem().
87 static DEFINE_SPINLOCK(logbuf_lock);
89 #define LOG_BUF_MASK (log_buf_len-1)
90 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
93 * The indices into log_buf are not constrained to log_buf_len - they
94 * must be masked before subscripting
96 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
97 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
98 static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
101 * Array of consoles built from command line options (console=)
103 struct console_cmdline
105 char name[8]; /* Name of the driver */
106 int index; /* Minor dev. to use */
107 char *options; /* Options for the driver */
110 #define MAX_CMDLINECONSOLES 8
112 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
113 static int selected_console = -1;
114 static int preferred_console = -1;
116 /* Flag: console code may call schedule() */
117 static int console_may_schedule;
121 static char __log_buf[__LOG_BUF_LEN];
122 static char *log_buf = __log_buf;
123 static int log_buf_len = __LOG_BUF_LEN;
124 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
126 static int __init log_buf_len_setup(char *str)
128 unsigned long size = memparse(str, &str);
132 size = roundup_pow_of_two(size);
133 if (size > log_buf_len) {
134 unsigned long start, dest_idx, offset;
137 new_log_buf = alloc_bootmem(size);
139 printk(KERN_WARNING "log_buf_len: allocation failed\n");
143 spin_lock_irqsave(&logbuf_lock, flags);
145 log_buf = new_log_buf;
147 offset = start = min(con_start, log_start);
149 while (start != log_end) {
150 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
157 spin_unlock_irqrestore(&logbuf_lock, flags);
159 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
165 __setup("log_buf_len=", log_buf_len_setup);
167 #ifdef CONFIG_BOOT_PRINTK_DELAY
169 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
170 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
172 static int __init boot_delay_setup(char *str)
175 unsigned long long loops_per_msec;
177 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
178 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
180 get_option(&str, &boot_delay);
181 if (boot_delay > 10 * 1000)
184 printk_delay_msec = loops_per_msec;
185 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
186 "HZ: %d, printk_delay_msec: %llu\n",
187 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
190 __setup("boot_delay=", boot_delay_setup);
192 static void boot_delay_msec(void)
194 unsigned long long k;
195 unsigned long timeout;
197 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
200 k = (unsigned long long)printk_delay_msec * boot_delay;
202 timeout = jiffies + msecs_to_jiffies(boot_delay);
207 * use (volatile) jiffies to prevent
208 * compiler reduction; loop termination via jiffies
209 * is secondary and may or may not happen.
211 if (time_after(jiffies, timeout))
213 touch_nmi_watchdog();
217 static inline void boot_delay_msec(void)
223 * Return the number of unread characters in the log buffer.
225 int log_buf_get_len(void)
231 * Copy a range of characters from the log buffer.
233 int log_buf_copy(char *dest, int idx, int len)
236 bool took_lock = false;
238 if (!oops_in_progress) {
239 spin_lock_irq(&logbuf_lock);
243 max = log_buf_get_len();
244 if (idx < 0 || idx >= max) {
250 idx += (log_end - max);
252 dest[len] = LOG_BUF(idx + len);
256 spin_unlock_irq(&logbuf_lock);
262 * Extract a single character from the log buffer.
264 int log_buf_read(int idx)
268 if (log_buf_copy(&ret, idx, 1) == 1)
275 * Commands to do_syslog:
277 * 0 -- Close the log. Currently a NOP.
278 * 1 -- Open the log. Currently a NOP.
279 * 2 -- Read from the log.
280 * 3 -- Read all messages remaining in the ring buffer.
281 * 4 -- Read and clear all messages remaining in the ring buffer
282 * 5 -- Clear ring buffer.
283 * 6 -- Disable printk's to console
284 * 7 -- Enable printk's to console
285 * 8 -- Set level of messages printed to console
286 * 9 -- Return number of unread characters in the log buffer
287 * 10 -- Return size of the log buffer
289 int do_syslog(int type, char __user *buf, int len)
291 unsigned long i, j, limit, count;
296 error = security_syslog(type);
301 case 0: /* Close log */
303 case 1: /* Open log */
305 case 2: /* Read from log */
312 if (!access_ok(VERIFY_WRITE, buf, len)) {
316 error = wait_event_interruptible(log_wait,
317 (log_start - log_end));
321 spin_lock_irq(&logbuf_lock);
322 while (!error && (log_start != log_end) && i < len) {
323 c = LOG_BUF(log_start);
325 spin_unlock_irq(&logbuf_lock);
326 error = __put_user(c,buf);
330 spin_lock_irq(&logbuf_lock);
332 spin_unlock_irq(&logbuf_lock);
336 case 4: /* Read/clear last kernel messages */
339 case 3: /* Read last kernel messages */
346 if (!access_ok(VERIFY_WRITE, buf, len)) {
351 if (count > log_buf_len)
353 spin_lock_irq(&logbuf_lock);
354 if (count > logged_chars)
355 count = logged_chars;
360 * __put_user() could sleep, and while we sleep
361 * printk() could overwrite the messages
362 * we try to copy to user space. Therefore
363 * the messages are copied in reverse. <manfreds>
365 for (i = 0; i < count && !error; i++) {
367 if (j + log_buf_len < log_end)
370 spin_unlock_irq(&logbuf_lock);
371 error = __put_user(c,&buf[count-1-i]);
373 spin_lock_irq(&logbuf_lock);
375 spin_unlock_irq(&logbuf_lock);
380 int offset = count-error;
381 /* buffer overflow during copy, correct user buffer. */
382 for (i = 0; i < error; i++) {
383 if (__get_user(c,&buf[i+offset]) ||
384 __put_user(c,&buf[i])) {
392 case 5: /* Clear ring buffer */
395 case 6: /* Disable logging to console */
396 console_loglevel = minimum_console_loglevel;
398 case 7: /* Enable logging to console */
399 console_loglevel = default_console_loglevel;
401 case 8: /* Set level of messages printed to console */
403 if (len < 1 || len > 8)
405 if (len < minimum_console_loglevel)
406 len = minimum_console_loglevel;
407 console_loglevel = len;
410 case 9: /* Number of chars in the log buffer */
411 error = log_end - log_start;
413 case 10: /* Size of the log buffer */
424 asmlinkage long sys_syslog(int type, char __user *buf, int len)
426 return do_syslog(type, buf, len);
430 * Call the console drivers on a range of log_buf
432 static void __call_console_drivers(unsigned long start, unsigned long end)
436 for (con = console_drivers; con; con = con->next) {
437 if ((con->flags & CON_ENABLED) && con->write &&
438 (cpu_online(smp_processor_id()) ||
439 (con->flags & CON_ANYTIME)))
440 con->write(con, &LOG_BUF(start), end - start);
444 static int __read_mostly ignore_loglevel;
446 static int __init ignore_loglevel_setup(char *str)
449 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
454 __setup("ignore_loglevel", ignore_loglevel_setup);
457 * Write out chars from start to end - 1 inclusive
459 static void _call_console_drivers(unsigned long start,
460 unsigned long end, int msg_log_level)
462 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
463 console_drivers && start != end) {
464 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
466 __call_console_drivers(start & LOG_BUF_MASK,
468 __call_console_drivers(0, end & LOG_BUF_MASK);
470 __call_console_drivers(start, end);
476 * Call the console drivers, asking them to write out
477 * log_buf[start] to log_buf[end - 1].
478 * The console_sem must be held.
480 static void call_console_drivers(unsigned long start, unsigned long end)
482 unsigned long cur_index, start_print;
483 static int msg_level = -1;
485 BUG_ON(((long)(start - end)) > 0);
489 while (cur_index != end) {
490 if (msg_level < 0 && ((end - cur_index) > 2) &&
491 LOG_BUF(cur_index + 0) == '<' &&
492 LOG_BUF(cur_index + 1) >= '0' &&
493 LOG_BUF(cur_index + 1) <= '7' &&
494 LOG_BUF(cur_index + 2) == '>') {
495 msg_level = LOG_BUF(cur_index + 1) - '0';
497 start_print = cur_index;
499 while (cur_index != end) {
500 char c = LOG_BUF(cur_index);
506 * printk() has already given us loglevel tags in
507 * the buffer. This code is here in case the
508 * log buffer has wrapped right round and scribbled
511 msg_level = default_message_loglevel;
513 _call_console_drivers(start_print, cur_index, msg_level);
515 start_print = cur_index;
520 _call_console_drivers(start_print, end, msg_level);
523 static void emit_log_char(char c)
525 LOG_BUF(log_end) = c;
527 if (log_end - log_start > log_buf_len)
528 log_start = log_end - log_buf_len;
529 if (log_end - con_start > log_buf_len)
530 con_start = log_end - log_buf_len;
531 if (logged_chars < log_buf_len)
536 * Zap console related locks when oopsing. Only zap at most once
537 * every 10 seconds, to leave time for slow consoles to print a
540 static void zap_locks(void)
542 static unsigned long oops_timestamp;
544 if (time_after_eq(jiffies, oops_timestamp) &&
545 !time_after(jiffies, oops_timestamp + 30 * HZ))
548 oops_timestamp = jiffies;
550 /* If a crash is occurring, make sure we can't deadlock */
551 spin_lock_init(&logbuf_lock);
552 /* And make sure that we print immediately */
553 init_MUTEX(&console_sem);
556 #if defined(CONFIG_PRINTK_TIME)
557 static int printk_time = 1;
559 static int printk_time = 0;
561 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
563 static int __init printk_time_setup(char *str)
568 printk(KERN_NOTICE "The 'time' option is deprecated and "
569 "is scheduled for removal in early 2008\n");
570 printk(KERN_NOTICE "Use 'printk.time=<value>' instead\n");
574 __setup("time", printk_time_setup);
576 /* Check if we have any console registered that can be called early in boot. */
577 static int have_callable_console(void)
581 for (con = console_drivers; con; con = con->next)
582 if (con->flags & CON_ANYTIME)
589 * printk - print a kernel message
590 * @fmt: format string
592 * This is printk(). It can be called from any context. We want it to work.
593 * Be aware of the fact that if oops_in_progress is not set, we might try to
594 * wake klogd up which could deadlock on runqueue lock if printk() is called
595 * from scheduler code.
597 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
598 * call the console drivers. If we fail to get the semaphore we place the output
599 * into the log buffer and return. The current holder of the console_sem will
600 * notice the new output in release_console_sem() and will send it to the
601 * consoles before releasing the semaphore.
603 * One effect of this deferred printing is that code which calls printk() and
604 * then changes console_loglevel may break. This is because console_loglevel
605 * is inspected when the actual printing occurs.
611 asmlinkage int printk(const char *fmt, ...)
617 r = vprintk(fmt, args);
623 /* cpu currently holding logbuf_lock */
624 static volatile unsigned int printk_cpu = UINT_MAX;
626 const char printk_recursion_bug_msg [] =
627 KERN_CRIT "BUG: recent printk recursion!\n";
628 static int printk_recursion_bug;
630 asmlinkage int vprintk(const char *fmt, va_list args)
632 static int log_level_unknown = 1;
633 static char printk_buf[1024];
643 /* This stops the holder of console_sem just where we want him */
644 raw_local_irq_save(flags);
645 this_cpu = smp_processor_id();
648 * Ouch, printk recursed into itself!
650 if (unlikely(printk_cpu == this_cpu)) {
652 * If a crash is occurring during printk() on this CPU,
653 * then try to get the crash message out but make sure
654 * we can't deadlock. Otherwise just return to avoid the
655 * recursion and return - but flag the recursion so that
656 * it can be printed at the next appropriate moment:
658 if (!oops_in_progress) {
659 printk_recursion_bug = 1;
660 goto out_restore_irqs;
666 spin_lock(&logbuf_lock);
667 printk_cpu = this_cpu;
669 if (printk_recursion_bug) {
670 printk_recursion_bug = 0;
671 strcpy(printk_buf, printk_recursion_bug_msg);
672 printed_len = sizeof(printk_recursion_bug_msg);
674 /* Emit the output into the temporary buffer */
675 printed_len += vscnprintf(printk_buf + printed_len,
676 sizeof(printk_buf), fmt, args);
679 * Copy the output into log_buf. If the caller didn't provide
680 * appropriate log level tags, we insert them here
682 for (p = printk_buf; *p; p++) {
683 if (log_level_unknown) {
684 /* log_level_unknown signals the start of a new line */
689 unsigned long long t;
690 unsigned long nanosec_rem;
693 * force the log level token to be
694 * before the time output.
696 if (p[0] == '<' && p[1] >='0' &&
697 p[1] <= '7' && p[2] == '>') {
702 loglev_char = default_message_loglevel
706 if (system_state != SYSTEM_BOOTING)
707 t = ktime_to_ns(ktime_get());
708 nanosec_rem = do_div(t, 1000000000);
715 for (tp = tbuf; tp < tbuf + tlen; tp++)
719 if (p[0] != '<' || p[1] < '0' ||
720 p[1] > '7' || p[2] != '>') {
722 emit_log_char(default_message_loglevel
728 log_level_unknown = 0;
734 log_level_unknown = 1;
737 if (!down_trylock(&console_sem)) {
739 * We own the drivers. We can drop the spinlock and
740 * let release_console_sem() print the text, maybe ...
743 printk_cpu = UINT_MAX;
744 spin_unlock(&logbuf_lock);
747 * Console drivers may assume that per-cpu resources have
748 * been allocated. So unless they're explicitly marked as
749 * being able to cope (CON_ANYTIME) don't call them until
750 * this CPU is officially up.
752 if (cpu_online(smp_processor_id()) || have_callable_console()) {
753 console_may_schedule = 0;
754 release_console_sem();
756 /* Release by hand to avoid flushing the buffer. */
761 raw_local_irq_restore(flags);
764 * Someone else owns the drivers. We drop the spinlock, which
765 * allows the semaphore holder to proceed and to call the
766 * console drivers with the output which we just produced.
768 printk_cpu = UINT_MAX;
769 spin_unlock(&logbuf_lock);
772 raw_local_irq_restore(flags);
778 EXPORT_SYMBOL(printk);
779 EXPORT_SYMBOL(vprintk);
783 asmlinkage long sys_syslog(int type, char __user *buf, int len)
788 static void call_console_drivers(unsigned long start, unsigned long end)
795 * Set up a list of consoles. Called from init/main.c
797 static int __init console_setup(char *str)
799 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
804 * Decode str into name, index, options.
806 if (str[0] >= '0' && str[0] <= '9') {
808 strncpy(buf + 4, str, sizeof(buf) - 5);
810 strncpy(buf, str, sizeof(buf) - 1);
812 buf[sizeof(buf) - 1] = 0;
813 if ((options = strchr(str, ',')) != NULL)
816 if (!strcmp(str, "ttya"))
817 strcpy(buf, "ttyS0");
818 if (!strcmp(str, "ttyb"))
819 strcpy(buf, "ttyS1");
821 for (s = buf; *s; s++)
822 if ((*s >= '0' && *s <= '9') || *s == ',')
824 idx = simple_strtoul(s, NULL, 10);
827 add_preferred_console(buf, idx, options);
830 __setup("console=", console_setup);
833 * add_preferred_console - add a device to the list of preferred consoles.
836 * @options: options for this console
838 * The last preferred console added will be used for kernel messages
839 * and stdin/out/err for init. Normally this is used by console_setup
840 * above to handle user-supplied console arguments; however it can also
841 * be used by arch-specific code either to override the user or more
842 * commonly to provide a default console (ie from PROM variables) when
843 * the user has not supplied one.
845 int add_preferred_console(char *name, int idx, char *options)
847 struct console_cmdline *c;
851 * See if this tty is not yet registered, and
852 * if we have a slot free.
854 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
855 if (strcmp(console_cmdline[i].name, name) == 0 &&
856 console_cmdline[i].index == idx) {
857 selected_console = i;
860 if (i == MAX_CMDLINECONSOLES)
862 selected_console = i;
863 c = &console_cmdline[i];
864 memcpy(c->name, name, sizeof(c->name));
865 c->name[sizeof(c->name) - 1] = 0;
866 c->options = options;
871 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
873 struct console_cmdline *c;
876 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
877 if (strcmp(console_cmdline[i].name, name) == 0 &&
878 console_cmdline[i].index == idx) {
879 c = &console_cmdline[i];
880 memcpy(c->name, name_new, sizeof(c->name));
881 c->name[sizeof(c->name) - 1] = 0;
882 c->options = options;
890 int console_suspend_enabled = 1;
891 EXPORT_SYMBOL(console_suspend_enabled);
893 static int __init console_suspend_disable(char *str)
895 console_suspend_enabled = 0;
898 __setup("no_console_suspend", console_suspend_disable);
901 * suspend_console - suspend the console subsystem
903 * This disables printk() while we go into suspend states
905 void suspend_console(void)
907 if (!console_suspend_enabled)
909 printk("Suspending console(s)\n");
910 acquire_console_sem();
911 console_suspended = 1;
914 void resume_console(void)
916 if (!console_suspend_enabled)
918 console_suspended = 0;
919 release_console_sem();
923 * acquire_console_sem - lock the console system for exclusive use.
925 * Acquires a semaphore which guarantees that the caller has
926 * exclusive access to the console system and the console_drivers list.
928 * Can sleep, returns nothing.
930 void acquire_console_sem(void)
932 BUG_ON(in_interrupt());
933 if (console_suspended) {
934 down(&secondary_console_sem);
939 console_may_schedule = 1;
941 EXPORT_SYMBOL(acquire_console_sem);
943 int try_acquire_console_sem(void)
945 if (down_trylock(&console_sem))
948 console_may_schedule = 0;
951 EXPORT_SYMBOL(try_acquire_console_sem);
953 int is_console_locked(void)
955 return console_locked;
958 void wake_up_klogd(void)
960 if (!oops_in_progress && waitqueue_active(&log_wait))
961 wake_up_interruptible(&log_wait);
965 * release_console_sem - unlock the console system
967 * Releases the semaphore which the caller holds on the console system
968 * and the console driver list.
970 * While the semaphore was held, console output may have been buffered
971 * by printk(). If this is the case, release_console_sem() emits
972 * the output prior to releasing the semaphore.
974 * If there is output waiting for klogd, we wake it up.
976 * release_console_sem() may be called from any context.
978 void release_console_sem(void)
981 unsigned long _con_start, _log_end;
982 unsigned long wake_klogd = 0;
984 if (console_suspended) {
985 up(&secondary_console_sem);
989 console_may_schedule = 0;
992 spin_lock_irqsave(&logbuf_lock, flags);
993 wake_klogd |= log_start - log_end;
994 if (con_start == log_end)
995 break; /* Nothing to print */
996 _con_start = con_start;
998 con_start = log_end; /* Flush */
999 spin_unlock(&logbuf_lock);
1000 call_console_drivers(_con_start, _log_end);
1001 local_irq_restore(flags);
1005 spin_unlock_irqrestore(&logbuf_lock, flags);
1009 EXPORT_SYMBOL(release_console_sem);
1012 * console_conditional_schedule - yield the CPU if required
1014 * If the console code is currently allowed to sleep, and
1015 * if this CPU should yield the CPU to another task, do
1018 * Must be called within acquire_console_sem().
1020 void __sched console_conditional_schedule(void)
1022 if (console_may_schedule)
1025 EXPORT_SYMBOL(console_conditional_schedule);
1027 void console_print(const char *s)
1029 printk(KERN_EMERG "%s", s);
1031 EXPORT_SYMBOL(console_print);
1033 void console_unblank(void)
1038 * console_unblank can no longer be called in interrupt context unless
1039 * oops_in_progress is set to 1..
1041 if (oops_in_progress) {
1042 if (down_trylock(&console_sem) != 0)
1045 acquire_console_sem();
1048 console_may_schedule = 0;
1049 for (c = console_drivers; c != NULL; c = c->next)
1050 if ((c->flags & CON_ENABLED) && c->unblank)
1052 release_console_sem();
1056 * Return the console tty driver structure and its associated index
1058 struct tty_driver *console_device(int *index)
1061 struct tty_driver *driver = NULL;
1063 acquire_console_sem();
1064 for (c = console_drivers; c != NULL; c = c->next) {
1067 driver = c->device(c, index);
1071 release_console_sem();
1076 * Prevent further output on the passed console device so that (for example)
1077 * serial drivers can disable console output before suspending a port, and can
1078 * re-enable output afterwards.
1080 void console_stop(struct console *console)
1082 acquire_console_sem();
1083 console->flags &= ~CON_ENABLED;
1084 release_console_sem();
1086 EXPORT_SYMBOL(console_stop);
1088 void console_start(struct console *console)
1090 acquire_console_sem();
1091 console->flags |= CON_ENABLED;
1092 release_console_sem();
1094 EXPORT_SYMBOL(console_start);
1097 * The console driver calls this routine during kernel initialization
1098 * to register the console printing procedure with printk() and to
1099 * print any messages that were printed by the kernel before the
1100 * console driver was initialized.
1102 void register_console(struct console *console)
1105 unsigned long flags;
1106 struct console *bootconsole = NULL;
1108 if (console_drivers) {
1109 if (console->flags & CON_BOOT)
1111 if (console_drivers->flags & CON_BOOT)
1112 bootconsole = console_drivers;
1115 if (preferred_console < 0 || bootconsole || !console_drivers)
1116 preferred_console = selected_console;
1118 if (console->early_setup)
1119 console->early_setup();
1122 * See if we want to use this console driver. If we
1123 * didn't select a console we take the first one
1124 * that registers here.
1126 if (preferred_console < 0) {
1127 if (console->index < 0)
1129 if (console->setup == NULL ||
1130 console->setup(console, NULL) == 0) {
1131 console->flags |= CON_ENABLED | CON_CONSDEV;
1132 preferred_console = 0;
1137 * See if this console matches one we selected on
1140 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1142 if (strcmp(console_cmdline[i].name, console->name) != 0)
1144 if (console->index >= 0 &&
1145 console->index != console_cmdline[i].index)
1147 if (console->index < 0)
1148 console->index = console_cmdline[i].index;
1149 if (console->setup &&
1150 console->setup(console, console_cmdline[i].options) != 0)
1152 console->flags |= CON_ENABLED;
1153 console->index = console_cmdline[i].index;
1154 if (i == selected_console) {
1155 console->flags |= CON_CONSDEV;
1156 preferred_console = selected_console;
1161 if (!(console->flags & CON_ENABLED))
1164 if (bootconsole && (console->flags & CON_CONSDEV)) {
1165 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1166 bootconsole->name, bootconsole->index,
1167 console->name, console->index);
1168 unregister_console(bootconsole);
1169 console->flags &= ~CON_PRINTBUFFER;
1171 printk(KERN_INFO "console [%s%d] enabled\n",
1172 console->name, console->index);
1176 * Put this console in the list - keep the
1177 * preferred driver at the head of the list.
1179 acquire_console_sem();
1180 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1181 console->next = console_drivers;
1182 console_drivers = console;
1184 console->next->flags &= ~CON_CONSDEV;
1186 console->next = console_drivers->next;
1187 console_drivers->next = console;
1189 if (console->flags & CON_PRINTBUFFER) {
1191 * release_console_sem() will print out the buffered messages
1194 spin_lock_irqsave(&logbuf_lock, flags);
1195 con_start = log_start;
1196 spin_unlock_irqrestore(&logbuf_lock, flags);
1198 release_console_sem();
1200 EXPORT_SYMBOL(register_console);
1202 int unregister_console(struct console *console)
1204 struct console *a, *b;
1207 acquire_console_sem();
1208 if (console_drivers == console) {
1209 console_drivers=console->next;
1211 } else if (console_drivers) {
1212 for (a=console_drivers->next, b=console_drivers ;
1213 a; b=a, a=b->next) {
1223 * If this isn't the last console and it has CON_CONSDEV set, we
1224 * need to set it on the next preferred console.
1226 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1227 console_drivers->flags |= CON_CONSDEV;
1229 release_console_sem();
1232 EXPORT_SYMBOL(unregister_console);
1234 static int __init disable_boot_consoles(void)
1236 if (console_drivers != NULL) {
1237 if (console_drivers->flags & CON_BOOT) {
1238 printk(KERN_INFO "turn off boot console %s%d\n",
1239 console_drivers->name, console_drivers->index);
1240 return unregister_console(console_drivers);
1245 late_initcall(disable_boot_consoles);
1248 * tty_write_message - write a message to a certain tty, not just the console.
1249 * @tty: the destination tty_struct
1250 * @msg: the message to write
1252 * This is used for messages that need to be redirected to a specific tty.
1253 * We don't put it into the syslog queue right now maybe in the future if
1256 void tty_write_message(struct tty_struct *tty, char *msg)
1258 if (tty && tty->driver->write)
1259 tty->driver->write(tty, msg, strlen(msg));
1264 * printk rate limiting, lifted from the networking subsystem.
1266 * This enforces a rate limit: not more than one kernel message
1267 * every printk_ratelimit_jiffies to make a denial-of-service
1268 * attack impossible.
1270 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1272 static DEFINE_SPINLOCK(ratelimit_lock);
1273 static unsigned long toks = 10 * 5 * HZ;
1274 static unsigned long last_msg;
1276 unsigned long flags;
1277 unsigned long now = jiffies;
1279 spin_lock_irqsave(&ratelimit_lock, flags);
1280 toks += now - last_msg;
1282 if (toks > (ratelimit_burst * ratelimit_jiffies))
1283 toks = ratelimit_burst * ratelimit_jiffies;
1284 if (toks >= ratelimit_jiffies) {
1288 toks -= ratelimit_jiffies;
1289 spin_unlock_irqrestore(&ratelimit_lock, flags);
1291 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1295 spin_unlock_irqrestore(&ratelimit_lock, flags);
1298 EXPORT_SYMBOL(__printk_ratelimit);
1300 /* minimum time in jiffies between messages */
1301 int printk_ratelimit_jiffies = 5 * HZ;
1303 /* number of messages we send before ratelimiting */
1304 int printk_ratelimit_burst = 10;
1306 int printk_ratelimit(void)
1308 return __printk_ratelimit(printk_ratelimit_jiffies,
1309 printk_ratelimit_burst);
1311 EXPORT_SYMBOL(printk_ratelimit);
1314 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1315 * @caller_jiffies: pointer to caller's state
1316 * @interval_msecs: minimum interval between prints
1318 * printk_timed_ratelimit() returns true if more than @interval_msecs
1319 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1322 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1323 unsigned int interval_msecs)
1325 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1326 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1331 EXPORT_SYMBOL(printk_timed_ratelimit);