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 #ifdef CONFIG_DEBUG_LL
42 extern void printascii(char *);
45 /* printk's without a loglevel use this.. */
46 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
48 /* We show everything that is MORE important than this.. */
49 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
50 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
52 DECLARE_WAIT_QUEUE_HEAD(log_wait);
54 int console_printk[4] = {
55 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
56 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
57 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
58 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
62 * Low level drivers may need that to know if they can schedule in
63 * their unblank() callback or not. So let's export it.
66 EXPORT_SYMBOL(oops_in_progress);
69 * console_sem protects the console_drivers list, and also
70 * provides serialisation for access to the entire console
73 static DECLARE_MUTEX(console_sem);
74 static DECLARE_MUTEX(secondary_console_sem);
75 struct console *console_drivers;
77 * This is used for debugging the mess that is the VT code by
78 * keeping track if we have the console semaphore held. It's
79 * definitely not the perfect debug tool (we don't know if _WE_
80 * hold it are racing, but it helps tracking those weird code
81 * path in the console code where we end up in places I want
82 * locked without the console sempahore held
84 static int console_locked, console_suspended;
87 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
88 * It is also used in interesting ways to provide interlocking in
89 * release_console_sem().
91 static DEFINE_SPINLOCK(logbuf_lock);
93 #define LOG_BUF_MASK (log_buf_len-1)
94 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
97 * The indices into log_buf are not constrained to log_buf_len - they
98 * must be masked before subscripting
100 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
101 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
102 static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
105 * Array of consoles built from command line options (console=)
107 struct console_cmdline
109 char name[8]; /* Name of the driver */
110 int index; /* Minor dev. to use */
111 char *options; /* Options for the driver */
114 #define MAX_CMDLINECONSOLES 8
116 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
117 static int selected_console = -1;
118 static int preferred_console = -1;
120 /* Flag: console code may call schedule() */
121 static int console_may_schedule;
125 static char __log_buf[__LOG_BUF_LEN];
126 static char *log_buf = __log_buf;
127 static int log_buf_len = __LOG_BUF_LEN;
128 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
130 static int __init log_buf_len_setup(char *str)
132 unsigned long size = memparse(str, &str);
136 size = roundup_pow_of_two(size);
137 if (size > log_buf_len) {
138 unsigned long start, dest_idx, offset;
141 new_log_buf = alloc_bootmem(size);
143 printk(KERN_WARNING "log_buf_len: allocation failed\n");
147 spin_lock_irqsave(&logbuf_lock, flags);
149 log_buf = new_log_buf;
151 offset = start = min(con_start, log_start);
153 while (start != log_end) {
154 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
161 spin_unlock_irqrestore(&logbuf_lock, flags);
163 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
169 __setup("log_buf_len=", log_buf_len_setup);
171 #ifdef CONFIG_BOOT_PRINTK_DELAY
173 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
174 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
176 static int __init boot_delay_setup(char *str)
179 unsigned long long loops_per_msec;
181 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
182 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
184 get_option(&str, &boot_delay);
185 if (boot_delay > 10 * 1000)
188 printk_delay_msec = loops_per_msec;
189 printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
190 "HZ: %d, printk_delay_msec: %llu\n",
191 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
194 __setup("boot_delay=", boot_delay_setup);
196 static void boot_delay_msec(void)
198 unsigned long long k;
199 unsigned long timeout;
201 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
204 k = (unsigned long long)printk_delay_msec * boot_delay;
206 timeout = jiffies + msecs_to_jiffies(boot_delay);
211 * use (volatile) jiffies to prevent
212 * compiler reduction; loop termination via jiffies
213 * is secondary and may or may not happen.
215 if (time_after(jiffies, timeout))
217 touch_nmi_watchdog();
221 static inline void boot_delay_msec(void)
227 * Return the number of unread characters in the log buffer.
229 int log_buf_get_len(void)
235 * Copy a range of characters from the log buffer.
237 int log_buf_copy(char *dest, int idx, int len)
240 bool took_lock = false;
242 if (!oops_in_progress) {
243 spin_lock_irq(&logbuf_lock);
247 max = log_buf_get_len();
248 if (idx < 0 || idx >= max) {
254 idx += (log_end - max);
256 dest[len] = LOG_BUF(idx + len);
260 spin_unlock_irq(&logbuf_lock);
266 * Extract a single character from the log buffer.
268 int log_buf_read(int idx)
272 if (log_buf_copy(&ret, idx, 1) == 1)
279 * Commands to do_syslog:
281 * 0 -- Close the log. Currently a NOP.
282 * 1 -- Open the log. Currently a NOP.
283 * 2 -- Read from the log.
284 * 3 -- Read all messages remaining in the ring buffer.
285 * 4 -- Read and clear all messages remaining in the ring buffer
286 * 5 -- Clear ring buffer.
287 * 6 -- Disable printk's to console
288 * 7 -- Enable printk's to console
289 * 8 -- Set level of messages printed to console
290 * 9 -- Return number of unread characters in the log buffer
291 * 10 -- Return size of the log buffer
293 int do_syslog(int type, char __user *buf, int len)
295 unsigned long i, j, limit, count;
300 error = security_syslog(type);
305 case 0: /* Close log */
307 case 1: /* Open log */
309 case 2: /* Read from log */
316 if (!access_ok(VERIFY_WRITE, buf, len)) {
320 error = wait_event_interruptible(log_wait,
321 (log_start - log_end));
325 spin_lock_irq(&logbuf_lock);
326 while (!error && (log_start != log_end) && i < len) {
327 c = LOG_BUF(log_start);
329 spin_unlock_irq(&logbuf_lock);
330 error = __put_user(c,buf);
334 spin_lock_irq(&logbuf_lock);
336 spin_unlock_irq(&logbuf_lock);
340 case 4: /* Read/clear last kernel messages */
343 case 3: /* Read last kernel messages */
350 if (!access_ok(VERIFY_WRITE, buf, len)) {
355 if (count > log_buf_len)
357 spin_lock_irq(&logbuf_lock);
358 if (count > logged_chars)
359 count = logged_chars;
364 * __put_user() could sleep, and while we sleep
365 * printk() could overwrite the messages
366 * we try to copy to user space. Therefore
367 * the messages are copied in reverse. <manfreds>
369 for (i = 0; i < count && !error; i++) {
371 if (j + log_buf_len < log_end)
374 spin_unlock_irq(&logbuf_lock);
375 error = __put_user(c,&buf[count-1-i]);
377 spin_lock_irq(&logbuf_lock);
379 spin_unlock_irq(&logbuf_lock);
384 int offset = count-error;
385 /* buffer overflow during copy, correct user buffer. */
386 for (i = 0; i < error; i++) {
387 if (__get_user(c,&buf[i+offset]) ||
388 __put_user(c,&buf[i])) {
396 case 5: /* Clear ring buffer */
399 case 6: /* Disable logging to console */
400 console_loglevel = minimum_console_loglevel;
402 case 7: /* Enable logging to console */
403 console_loglevel = default_console_loglevel;
405 case 8: /* Set level of messages printed to console */
407 if (len < 1 || len > 8)
409 if (len < minimum_console_loglevel)
410 len = minimum_console_loglevel;
411 console_loglevel = len;
414 case 9: /* Number of chars in the log buffer */
415 error = log_end - log_start;
417 case 10: /* Size of the log buffer */
428 asmlinkage long sys_syslog(int type, char __user *buf, int len)
430 return do_syslog(type, buf, len);
434 * Call the console drivers on a range of log_buf
436 static void __call_console_drivers(unsigned long start, unsigned long end)
440 for (con = console_drivers; con; con = con->next) {
441 if ((con->flags & CON_ENABLED) && con->write &&
442 (cpu_online(smp_processor_id()) ||
443 (con->flags & CON_ANYTIME)))
444 con->write(con, &LOG_BUF(start), end - start);
448 static int __read_mostly ignore_loglevel;
450 static int __init ignore_loglevel_setup(char *str)
453 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
458 __setup("ignore_loglevel", ignore_loglevel_setup);
461 * Write out chars from start to end - 1 inclusive
463 static void _call_console_drivers(unsigned long start,
464 unsigned long end, int msg_log_level)
466 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
467 console_drivers && start != end) {
468 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
470 __call_console_drivers(start & LOG_BUF_MASK,
472 __call_console_drivers(0, end & LOG_BUF_MASK);
474 __call_console_drivers(start, end);
480 * Call the console drivers, asking them to write out
481 * log_buf[start] to log_buf[end - 1].
482 * The console_sem must be held.
484 static void call_console_drivers(unsigned long start, unsigned long end)
486 unsigned long cur_index, start_print;
487 static int msg_level = -1;
489 BUG_ON(((long)(start - end)) > 0);
493 while (cur_index != end) {
494 if (msg_level < 0 && ((end - cur_index) > 2) &&
495 LOG_BUF(cur_index + 0) == '<' &&
496 LOG_BUF(cur_index + 1) >= '0' &&
497 LOG_BUF(cur_index + 1) <= '7' &&
498 LOG_BUF(cur_index + 2) == '>') {
499 msg_level = LOG_BUF(cur_index + 1) - '0';
501 start_print = cur_index;
503 while (cur_index != end) {
504 char c = LOG_BUF(cur_index);
510 * printk() has already given us loglevel tags in
511 * the buffer. This code is here in case the
512 * log buffer has wrapped right round and scribbled
515 msg_level = default_message_loglevel;
517 _call_console_drivers(start_print, cur_index, msg_level);
519 start_print = cur_index;
524 _call_console_drivers(start_print, end, msg_level);
527 static void emit_log_char(char c)
529 LOG_BUF(log_end) = c;
531 if (log_end - log_start > log_buf_len)
532 log_start = log_end - log_buf_len;
533 if (log_end - con_start > log_buf_len)
534 con_start = log_end - log_buf_len;
535 if (logged_chars < log_buf_len)
540 * Zap console related locks when oopsing. Only zap at most once
541 * every 10 seconds, to leave time for slow consoles to print a
544 static void zap_locks(void)
546 static unsigned long oops_timestamp;
548 if (time_after_eq(jiffies, oops_timestamp) &&
549 !time_after(jiffies, oops_timestamp + 30 * HZ))
552 oops_timestamp = jiffies;
554 /* If a crash is occurring, make sure we can't deadlock */
555 spin_lock_init(&logbuf_lock);
556 /* And make sure that we print immediately */
557 init_MUTEX(&console_sem);
560 static int printk_time = 0;
562 #ifdef CONFIG_PRINTK_TIME
565 * Initialize printk time. Note that on some systems sched_clock()
566 * does not work until timer is initialized.
568 static int __init printk_time_init(void)
574 subsys_initcall(printk_time_init);
578 static int __init printk_time_setup(char *str)
583 printk(KERN_NOTICE "The 'time' option is deprecated and "
584 "is scheduled for removal in early 2008\n");
585 printk(KERN_NOTICE "Use 'printk.time=<value>' instead\n");
589 __setup("time", printk_time_setup);
592 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
594 __attribute__((weak)) unsigned long long printk_clock(void)
596 return sched_clock();
599 /* Check if we have any console registered that can be called early in boot. */
600 static int have_callable_console(void)
604 for (con = console_drivers; con; con = con->next)
605 if (con->flags & CON_ANYTIME)
612 * printk - print a kernel message
613 * @fmt: format string
615 * This is printk(). It can be called from any context. We want it to work.
616 * Be aware of the fact that if oops_in_progress is not set, we might try to
617 * wake klogd up which could deadlock on runqueue lock if printk() is called
618 * from scheduler code.
620 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
621 * call the console drivers. If we fail to get the semaphore we place the output
622 * into the log buffer and return. The current holder of the console_sem will
623 * notice the new output in release_console_sem() and will send it to the
624 * consoles before releasing the semaphore.
626 * One effect of this deferred printing is that code which calls printk() and
627 * then changes console_loglevel may break. This is because console_loglevel
628 * is inspected when the actual printing occurs.
634 asmlinkage int printk(const char *fmt, ...)
640 r = vprintk(fmt, args);
646 /* cpu currently holding logbuf_lock */
647 static volatile unsigned int printk_cpu = UINT_MAX;
649 asmlinkage int vprintk(const char *fmt, va_list args)
654 static char printk_buf[1024];
655 static int log_level_unknown = 1;
660 if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
661 /* If a crash is occurring during printk() on this CPU,
662 * make sure we can't deadlock */
665 /* This stops the holder of console_sem just where we want him */
666 raw_local_irq_save(flags);
668 spin_lock(&logbuf_lock);
669 printk_cpu = smp_processor_id();
671 /* Emit the output into the temporary buffer */
672 printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
674 #ifdef CONFIG_DEBUG_LL
675 printascii(printk_buf);
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 nanosec_rem = do_div(t, 1000000000);
713 for (tp = tbuf; tp < tbuf + tlen; tp++)
717 if (p[0] != '<' || p[1] < '0' ||
718 p[1] > '7' || p[2] != '>') {
720 emit_log_char(default_message_loglevel
726 log_level_unknown = 0;
732 log_level_unknown = 1;
735 if (!down_trylock(&console_sem)) {
737 * We own the drivers. We can drop the spinlock and
738 * let release_console_sem() print the text, maybe ...
741 printk_cpu = UINT_MAX;
742 spin_unlock(&logbuf_lock);
745 * Console drivers may assume that per-cpu resources have
746 * been allocated. So unless they're explicitly marked as
747 * being able to cope (CON_ANYTIME) don't call them until
748 * this CPU is officially up.
750 if (cpu_online(smp_processor_id()) || have_callable_console()) {
751 console_may_schedule = 0;
752 release_console_sem();
754 /* Release by hand to avoid flushing the buffer. */
759 raw_local_irq_restore(flags);
762 * Someone else owns the drivers. We drop the spinlock, which
763 * allows the semaphore holder to proceed and to call the
764 * console drivers with the output which we just produced.
766 printk_cpu = UINT_MAX;
767 spin_unlock(&logbuf_lock);
769 raw_local_irq_restore(flags);
775 EXPORT_SYMBOL(printk);
776 EXPORT_SYMBOL(vprintk);
780 asmlinkage long sys_syslog(int type, char __user *buf, int len)
785 static void call_console_drivers(unsigned long start, unsigned long end)
792 * Set up a list of consoles. Called from init/main.c
794 static int __init console_setup(char *str)
796 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
801 * Decode str into name, index, options.
803 if (str[0] >= '0' && str[0] <= '9') {
805 strncpy(buf + 4, str, sizeof(buf) - 5);
807 strncpy(buf, str, sizeof(buf) - 1);
809 buf[sizeof(buf) - 1] = 0;
810 if ((options = strchr(str, ',')) != NULL)
813 if (!strcmp(str, "ttya"))
814 strcpy(buf, "ttyS0");
815 if (!strcmp(str, "ttyb"))
816 strcpy(buf, "ttyS1");
818 for (s = buf; *s; s++)
819 if ((*s >= '0' && *s <= '9') || *s == ',')
821 idx = simple_strtoul(s, NULL, 10);
824 add_preferred_console(buf, idx, options);
827 __setup("console=", console_setup);
830 * add_preferred_console - add a device to the list of preferred consoles.
833 * @options: options for this console
835 * The last preferred console added will be used for kernel messages
836 * and stdin/out/err for init. Normally this is used by console_setup
837 * above to handle user-supplied console arguments; however it can also
838 * be used by arch-specific code either to override the user or more
839 * commonly to provide a default console (ie from PROM variables) when
840 * the user has not supplied one.
842 int add_preferred_console(char *name, int idx, char *options)
844 struct console_cmdline *c;
848 * See if this tty is not yet registered, and
849 * if we have a slot free.
851 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
852 if (strcmp(console_cmdline[i].name, name) == 0 &&
853 console_cmdline[i].index == idx) {
854 selected_console = i;
857 if (i == MAX_CMDLINECONSOLES)
859 selected_console = i;
860 c = &console_cmdline[i];
861 memcpy(c->name, name, sizeof(c->name));
862 c->name[sizeof(c->name) - 1] = 0;
863 c->options = options;
868 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
870 struct console_cmdline *c;
873 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
874 if (strcmp(console_cmdline[i].name, name) == 0 &&
875 console_cmdline[i].index == idx) {
876 c = &console_cmdline[i];
877 memcpy(c->name, name_new, sizeof(c->name));
878 c->name[sizeof(c->name) - 1] = 0;
879 c->options = options;
887 int console_suspend_enabled = 1;
888 EXPORT_SYMBOL(console_suspend_enabled);
890 static int __init console_suspend_disable(char *str)
892 console_suspend_enabled = 0;
895 __setup("no_console_suspend", console_suspend_disable);
898 * suspend_console - suspend the console subsystem
900 * This disables printk() while we go into suspend states
902 void suspend_console(void)
904 if (!console_suspend_enabled)
906 printk("Suspending console(s)\n");
907 acquire_console_sem();
908 console_suspended = 1;
911 void resume_console(void)
913 if (!console_suspend_enabled)
915 console_suspended = 0;
916 release_console_sem();
920 * acquire_console_sem - lock the console system for exclusive use.
922 * Acquires a semaphore which guarantees that the caller has
923 * exclusive access to the console system and the console_drivers list.
925 * Can sleep, returns nothing.
927 void acquire_console_sem(void)
929 BUG_ON(in_interrupt());
930 if (console_suspended) {
931 down(&secondary_console_sem);
936 console_may_schedule = 1;
938 EXPORT_SYMBOL(acquire_console_sem);
940 int try_acquire_console_sem(void)
942 if (down_trylock(&console_sem))
945 console_may_schedule = 0;
948 EXPORT_SYMBOL(try_acquire_console_sem);
950 int is_console_locked(void)
952 return console_locked;
955 void wake_up_klogd(void)
957 if (!oops_in_progress && waitqueue_active(&log_wait))
958 wake_up_interruptible(&log_wait);
962 * release_console_sem - unlock the console system
964 * Releases the semaphore which the caller holds on the console system
965 * and the console driver list.
967 * While the semaphore was held, console output may have been buffered
968 * by printk(). If this is the case, release_console_sem() emits
969 * the output prior to releasing the semaphore.
971 * If there is output waiting for klogd, we wake it up.
973 * release_console_sem() may be called from any context.
975 void release_console_sem(void)
978 unsigned long _con_start, _log_end;
979 unsigned long wake_klogd = 0;
981 if (console_suspended) {
982 up(&secondary_console_sem);
986 console_may_schedule = 0;
989 spin_lock_irqsave(&logbuf_lock, flags);
990 wake_klogd |= log_start - log_end;
991 if (con_start == log_end)
992 break; /* Nothing to print */
993 _con_start = con_start;
995 con_start = log_end; /* Flush */
996 spin_unlock(&logbuf_lock);
997 call_console_drivers(_con_start, _log_end);
998 local_irq_restore(flags);
1002 spin_unlock_irqrestore(&logbuf_lock, flags);
1006 EXPORT_SYMBOL(release_console_sem);
1009 * console_conditional_schedule - yield the CPU if required
1011 * If the console code is currently allowed to sleep, and
1012 * if this CPU should yield the CPU to another task, do
1015 * Must be called within acquire_console_sem().
1017 void __sched console_conditional_schedule(void)
1019 if (console_may_schedule)
1022 EXPORT_SYMBOL(console_conditional_schedule);
1024 void console_print(const char *s)
1026 printk(KERN_EMERG "%s", s);
1028 EXPORT_SYMBOL(console_print);
1030 void console_unblank(void)
1035 * console_unblank can no longer be called in interrupt context unless
1036 * oops_in_progress is set to 1..
1038 if (oops_in_progress) {
1039 if (down_trylock(&console_sem) != 0)
1042 acquire_console_sem();
1045 console_may_schedule = 0;
1046 for (c = console_drivers; c != NULL; c = c->next)
1047 if ((c->flags & CON_ENABLED) && c->unblank)
1049 release_console_sem();
1053 * Return the console tty driver structure and its associated index
1055 struct tty_driver *console_device(int *index)
1058 struct tty_driver *driver = NULL;
1060 acquire_console_sem();
1061 for (c = console_drivers; c != NULL; c = c->next) {
1064 driver = c->device(c, index);
1068 release_console_sem();
1073 * Prevent further output on the passed console device so that (for example)
1074 * serial drivers can disable console output before suspending a port, and can
1075 * re-enable output afterwards.
1077 void console_stop(struct console *console)
1079 acquire_console_sem();
1080 console->flags &= ~CON_ENABLED;
1081 release_console_sem();
1083 EXPORT_SYMBOL(console_stop);
1085 void console_start(struct console *console)
1087 acquire_console_sem();
1088 console->flags |= CON_ENABLED;
1089 release_console_sem();
1091 EXPORT_SYMBOL(console_start);
1094 * The console driver calls this routine during kernel initialization
1095 * to register the console printing procedure with printk() and to
1096 * print any messages that were printed by the kernel before the
1097 * console driver was initialized.
1099 void register_console(struct console *console)
1102 unsigned long flags;
1103 struct console *bootconsole = NULL;
1105 if (console_drivers) {
1106 if (console->flags & CON_BOOT)
1108 if (console_drivers->flags & CON_BOOT)
1109 bootconsole = console_drivers;
1112 if (preferred_console < 0 || bootconsole || !console_drivers)
1113 preferred_console = selected_console;
1115 if (console->early_setup)
1116 console->early_setup();
1119 * See if we want to use this console driver. If we
1120 * didn't select a console we take the first one
1121 * that registers here.
1123 if (preferred_console < 0) {
1124 if (console->index < 0)
1126 if (console->setup == NULL ||
1127 console->setup(console, NULL) == 0) {
1128 console->flags |= CON_ENABLED | CON_CONSDEV;
1129 preferred_console = 0;
1134 * See if this console matches one we selected on
1137 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1139 if (strcmp(console_cmdline[i].name, console->name) != 0)
1141 if (console->index >= 0 &&
1142 console->index != console_cmdline[i].index)
1144 if (console->index < 0)
1145 console->index = console_cmdline[i].index;
1146 if (console->setup &&
1147 console->setup(console, console_cmdline[i].options) != 0)
1149 console->flags |= CON_ENABLED;
1150 console->index = console_cmdline[i].index;
1151 if (i == selected_console) {
1152 console->flags |= CON_CONSDEV;
1153 preferred_console = selected_console;
1158 if (!(console->flags & CON_ENABLED))
1161 if (bootconsole && (console->flags & CON_CONSDEV)) {
1162 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1163 bootconsole->name, bootconsole->index,
1164 console->name, console->index);
1165 unregister_console(bootconsole);
1166 console->flags &= ~CON_PRINTBUFFER;
1168 printk(KERN_INFO "console [%s%d] enabled\n",
1169 console->name, console->index);
1173 * Put this console in the list - keep the
1174 * preferred driver at the head of the list.
1176 acquire_console_sem();
1177 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1178 console->next = console_drivers;
1179 console_drivers = console;
1181 console->next->flags &= ~CON_CONSDEV;
1183 console->next = console_drivers->next;
1184 console_drivers->next = console;
1186 if (console->flags & CON_PRINTBUFFER) {
1188 * release_console_sem() will print out the buffered messages
1191 spin_lock_irqsave(&logbuf_lock, flags);
1192 con_start = log_start;
1193 spin_unlock_irqrestore(&logbuf_lock, flags);
1195 release_console_sem();
1197 EXPORT_SYMBOL(register_console);
1199 int unregister_console(struct console *console)
1201 struct console *a, *b;
1204 acquire_console_sem();
1205 if (console_drivers == console) {
1206 console_drivers=console->next;
1208 } else if (console_drivers) {
1209 for (a=console_drivers->next, b=console_drivers ;
1210 a; b=a, a=b->next) {
1220 * If this isn't the last console and it has CON_CONSDEV set, we
1221 * need to set it on the next preferred console.
1223 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1224 console_drivers->flags |= CON_CONSDEV;
1226 release_console_sem();
1229 EXPORT_SYMBOL(unregister_console);
1231 static int __init disable_boot_consoles(void)
1233 if (console_drivers != NULL) {
1234 if (console_drivers->flags & CON_BOOT) {
1235 printk(KERN_INFO "turn off boot console %s%d\n",
1236 console_drivers->name, console_drivers->index);
1237 return unregister_console(console_drivers);
1242 late_initcall(disable_boot_consoles);
1245 * tty_write_message - write a message to a certain tty, not just the console.
1246 * @tty: the destination tty_struct
1247 * @msg: the message to write
1249 * This is used for messages that need to be redirected to a specific tty.
1250 * We don't put it into the syslog queue right now maybe in the future if
1253 void tty_write_message(struct tty_struct *tty, char *msg)
1255 if (tty && tty->driver->write)
1256 tty->driver->write(tty, msg, strlen(msg));
1261 * printk rate limiting, lifted from the networking subsystem.
1263 * This enforces a rate limit: not more than one kernel message
1264 * every printk_ratelimit_jiffies to make a denial-of-service
1265 * attack impossible.
1267 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1269 static DEFINE_SPINLOCK(ratelimit_lock);
1270 static unsigned long toks = 10 * 5 * HZ;
1271 static unsigned long last_msg;
1273 unsigned long flags;
1274 unsigned long now = jiffies;
1276 spin_lock_irqsave(&ratelimit_lock, flags);
1277 toks += now - last_msg;
1279 if (toks > (ratelimit_burst * ratelimit_jiffies))
1280 toks = ratelimit_burst * ratelimit_jiffies;
1281 if (toks >= ratelimit_jiffies) {
1285 toks -= ratelimit_jiffies;
1286 spin_unlock_irqrestore(&ratelimit_lock, flags);
1288 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1292 spin_unlock_irqrestore(&ratelimit_lock, flags);
1295 EXPORT_SYMBOL(__printk_ratelimit);
1297 /* minimum time in jiffies between messages */
1298 int printk_ratelimit_jiffies = 5 * HZ;
1300 /* number of messages we send before ratelimiting */
1301 int printk_ratelimit_burst = 10;
1303 int printk_ratelimit(void)
1305 return __printk_ratelimit(printk_ratelimit_jiffies,
1306 printk_ratelimit_burst);
1308 EXPORT_SYMBOL(printk_ratelimit);
1311 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1312 * @caller_jiffies: pointer to caller's state
1313 * @interval_msecs: minimum interval between prints
1315 * printk_timed_ratelimit() returns true if more than @interval_msecs
1316 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1319 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1320 unsigned int interval_msecs)
1322 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1323 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1328 EXPORT_SYMBOL(printk_timed_ratelimit);