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/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/interrupt.h> /* For in_interrupt() */
28 #include <linux/delay.h>
29 #include <linux/smp.h>
30 #include <linux/security.h>
31 #include <linux/bootmem.h>
32 #include <linux/syscalls.h>
33 #include <linux/jiffies.h>
35 #include <asm/uaccess.h>
37 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
39 /* printk's without a loglevel use this.. */
40 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
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 */
46 DECLARE_WAIT_QUEUE_HEAD(log_wait
);
48 int 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 */
56 * Low level drivers may need that to know if they can schedule in
57 * their unblank() callback or not. So let's export it.
60 EXPORT_SYMBOL(oops_in_progress
);
63 * console_sem protects the console_drivers list, and also
64 * provides serialisation for access to the entire console
67 static DECLARE_MUTEX(console_sem
);
68 static DECLARE_MUTEX(secondary_console_sem
);
69 struct console
*console_drivers
;
71 * This is used for debugging the mess that is the VT code by
72 * keeping track if we have the console semaphore held. It's
73 * definitely not the perfect debug tool (we don't know if _WE_
74 * hold it are racing, but it helps tracking those weird code
75 * path in the console code where we end up in places I want
76 * locked without the console sempahore held
78 static int console_locked
, console_suspended
;
81 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
82 * It is also used in interesting ways to provide interlocking in
83 * release_console_sem().
85 static DEFINE_SPINLOCK(logbuf_lock
);
87 #define LOG_BUF_MASK (log_buf_len-1)
88 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
91 * The indices into log_buf are not constrained to log_buf_len - they
92 * must be masked before subscripting
94 static unsigned long log_start
; /* Index into log_buf: next char to be read by syslog() */
95 static unsigned long con_start
; /* Index into log_buf: next char to be sent to consoles */
96 static unsigned long log_end
; /* Index into log_buf: most-recently-written-char + 1 */
99 * Array of consoles built from command line options (console=)
101 struct console_cmdline
103 char name
[8]; /* Name of the driver */
104 int index
; /* Minor dev. to use */
105 char *options
; /* Options for the driver */
108 #define MAX_CMDLINECONSOLES 8
110 static struct console_cmdline console_cmdline
[MAX_CMDLINECONSOLES
];
111 static int selected_console
= -1;
112 static int preferred_console
= -1;
114 /* Flag: console code may call schedule() */
115 static int console_may_schedule
;
119 static char __log_buf
[__LOG_BUF_LEN
];
120 static char *log_buf
= __log_buf
;
121 static int log_buf_len
= __LOG_BUF_LEN
;
122 static unsigned long logged_chars
; /* Number of chars produced since last read+clear operation */
124 static int __init
log_buf_len_setup(char *str
)
126 unsigned long size
= memparse(str
, &str
);
130 size
= roundup_pow_of_two(size
);
131 if (size
> log_buf_len
) {
132 unsigned long start
, dest_idx
, offset
;
135 new_log_buf
= alloc_bootmem(size
);
137 printk(KERN_WARNING
"log_buf_len: allocation failed\n");
141 spin_lock_irqsave(&logbuf_lock
, flags
);
143 log_buf
= new_log_buf
;
145 offset
= start
= min(con_start
, log_start
);
147 while (start
!= log_end
) {
148 log_buf
[dest_idx
] = __log_buf
[start
& (__LOG_BUF_LEN
- 1)];
155 spin_unlock_irqrestore(&logbuf_lock
, flags
);
157 printk(KERN_NOTICE
"log_buf_len: %d\n", log_buf_len
);
163 __setup("log_buf_len=", log_buf_len_setup
);
166 * Commands to do_syslog:
168 * 0 -- Close the log. Currently a NOP.
169 * 1 -- Open the log. Currently a NOP.
170 * 2 -- Read from the log.
171 * 3 -- Read all messages remaining in the ring buffer.
172 * 4 -- Read and clear all messages remaining in the ring buffer
173 * 5 -- Clear ring buffer.
174 * 6 -- Disable printk's to console
175 * 7 -- Enable printk's to console
176 * 8 -- Set level of messages printed to console
177 * 9 -- Return number of unread characters in the log buffer
178 * 10 -- Return size of the log buffer
180 int do_syslog(int type
, char __user
*buf
, int len
)
182 unsigned long i
, j
, limit
, count
;
187 error
= security_syslog(type
);
192 case 0: /* Close log */
194 case 1: /* Open log */
196 case 2: /* Read from log */
203 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
207 error
= wait_event_interruptible(log_wait
,
208 (log_start
- log_end
));
212 spin_lock_irq(&logbuf_lock
);
213 while (!error
&& (log_start
!= log_end
) && i
< len
) {
214 c
= LOG_BUF(log_start
);
216 spin_unlock_irq(&logbuf_lock
);
217 error
= __put_user(c
,buf
);
221 spin_lock_irq(&logbuf_lock
);
223 spin_unlock_irq(&logbuf_lock
);
227 case 4: /* Read/clear last kernel messages */
230 case 3: /* Read last kernel messages */
237 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
242 if (count
> log_buf_len
)
244 spin_lock_irq(&logbuf_lock
);
245 if (count
> logged_chars
)
246 count
= logged_chars
;
251 * __put_user() could sleep, and while we sleep
252 * printk() could overwrite the messages
253 * we try to copy to user space. Therefore
254 * the messages are copied in reverse. <manfreds>
256 for (i
= 0; i
< count
&& !error
; i
++) {
258 if (j
+ log_buf_len
< log_end
)
261 spin_unlock_irq(&logbuf_lock
);
262 error
= __put_user(c
,&buf
[count
-1-i
]);
264 spin_lock_irq(&logbuf_lock
);
266 spin_unlock_irq(&logbuf_lock
);
271 int offset
= count
-error
;
272 /* buffer overflow during copy, correct user buffer. */
273 for (i
= 0; i
< error
; i
++) {
274 if (__get_user(c
,&buf
[i
+offset
]) ||
275 __put_user(c
,&buf
[i
])) {
283 case 5: /* Clear ring buffer */
286 case 6: /* Disable logging to console */
287 console_loglevel
= minimum_console_loglevel
;
289 case 7: /* Enable logging to console */
290 console_loglevel
= default_console_loglevel
;
292 case 8: /* Set level of messages printed to console */
294 if (len
< 1 || len
> 8)
296 if (len
< minimum_console_loglevel
)
297 len
= minimum_console_loglevel
;
298 console_loglevel
= len
;
301 case 9: /* Number of chars in the log buffer */
302 error
= log_end
- log_start
;
304 case 10: /* Size of the log buffer */
315 asmlinkage
long sys_syslog(int type
, char __user
*buf
, int len
)
317 return do_syslog(type
, buf
, len
);
321 * Call the console drivers on a range of log_buf
323 static void __call_console_drivers(unsigned long start
, unsigned long end
)
327 for (con
= console_drivers
; con
; con
= con
->next
) {
328 if ((con
->flags
& CON_ENABLED
) && con
->write
&&
329 (cpu_online(smp_processor_id()) ||
330 (con
->flags
& CON_ANYTIME
)))
331 con
->write(con
, &LOG_BUF(start
), end
- start
);
335 static int __read_mostly ignore_loglevel
;
337 static int __init
ignore_loglevel_setup(char *str
)
340 printk(KERN_INFO
"debug: ignoring loglevel setting.\n");
345 __setup("ignore_loglevel", ignore_loglevel_setup
);
348 * Write out chars from start to end - 1 inclusive
350 static void _call_console_drivers(unsigned long start
,
351 unsigned long end
, int msg_log_level
)
353 if ((msg_log_level
< console_loglevel
|| ignore_loglevel
) &&
354 console_drivers
&& start
!= end
) {
355 if ((start
& LOG_BUF_MASK
) > (end
& LOG_BUF_MASK
)) {
357 __call_console_drivers(start
& LOG_BUF_MASK
,
359 __call_console_drivers(0, end
& LOG_BUF_MASK
);
361 __call_console_drivers(start
, end
);
367 * Call the console drivers, asking them to write out
368 * log_buf[start] to log_buf[end - 1].
369 * The console_sem must be held.
371 static void call_console_drivers(unsigned long start
, unsigned long end
)
373 unsigned long cur_index
, start_print
;
374 static int msg_level
= -1;
376 BUG_ON(((long)(start
- end
)) > 0);
380 while (cur_index
!= end
) {
381 if (msg_level
< 0 && ((end
- cur_index
) > 2) &&
382 LOG_BUF(cur_index
+ 0) == '<' &&
383 LOG_BUF(cur_index
+ 1) >= '0' &&
384 LOG_BUF(cur_index
+ 1) <= '7' &&
385 LOG_BUF(cur_index
+ 2) == '>') {
386 msg_level
= LOG_BUF(cur_index
+ 1) - '0';
388 start_print
= cur_index
;
390 while (cur_index
!= end
) {
391 char c
= LOG_BUF(cur_index
);
397 * printk() has already given us loglevel tags in
398 * the buffer. This code is here in case the
399 * log buffer has wrapped right round and scribbled
402 msg_level
= default_message_loglevel
;
404 _call_console_drivers(start_print
, cur_index
, msg_level
);
406 start_print
= cur_index
;
411 _call_console_drivers(start_print
, end
, msg_level
);
414 static void emit_log_char(char c
)
416 LOG_BUF(log_end
) = c
;
418 if (log_end
- log_start
> log_buf_len
)
419 log_start
= log_end
- log_buf_len
;
420 if (log_end
- con_start
> log_buf_len
)
421 con_start
= log_end
- log_buf_len
;
422 if (logged_chars
< log_buf_len
)
427 * Zap console related locks when oopsing. Only zap at most once
428 * every 10 seconds, to leave time for slow consoles to print a
431 static void zap_locks(void)
433 static unsigned long oops_timestamp
;
435 if (time_after_eq(jiffies
, oops_timestamp
) &&
436 !time_after(jiffies
, oops_timestamp
+ 30 * HZ
))
439 oops_timestamp
= jiffies
;
441 /* If a crash is occurring, make sure we can't deadlock */
442 spin_lock_init(&logbuf_lock
);
443 /* And make sure that we print immediately */
444 init_MUTEX(&console_sem
);
447 #if defined(CONFIG_PRINTK_TIME)
448 static int printk_time
= 1;
450 static int printk_time
= 0;
452 module_param(printk_time
, int, S_IRUGO
| S_IWUSR
);
454 static int __init
printk_time_setup(char *str
)
462 __setup("time", printk_time_setup
);
464 __attribute__((weak
)) unsigned long long printk_clock(void)
466 return sched_clock();
469 /* Check if we have any console registered that can be called early in boot. */
470 static int have_callable_console(void)
472 #if !defined(CONFIG_HWSIM)
475 for (con
= console_drivers
; con
; con
= con
->next
)
476 if (con
->flags
& CON_ANYTIME
)
484 * printk - print a kernel message
485 * @fmt: format string
487 * This is printk(). It can be called from any context. We want it to work.
489 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
490 * call the console drivers. If we fail to get the semaphore we place the output
491 * into the log buffer and return. The current holder of the console_sem will
492 * notice the new output in release_console_sem() and will send it to the
493 * consoles before releasing the semaphore.
495 * One effect of this deferred printing is that code which calls printk() and
496 * then changes console_loglevel may break. This is because console_loglevel
497 * is inspected when the actual printing occurs.
503 asmlinkage
int printk(const char *fmt
, ...)
509 r
= vprintk(fmt
, args
);
515 /* cpu currently holding logbuf_lock */
516 static volatile unsigned int printk_cpu
= UINT_MAX
;
518 asmlinkage
int vprintk(const char *fmt
, va_list args
)
523 static char printk_buf
[1024];
524 static int log_level_unknown
= 1;
526 #if defined(CONFIG_HWSIM) && defined(mips)
527 if (log_buf
== __log_buf
)
528 log_buf
= KSEG1ADDR((char *) __log_buf
);
532 if (unlikely(oops_in_progress
) && printk_cpu
== smp_processor_id())
533 /* If a crash is occurring during printk() on this CPU,
534 * make sure we can't deadlock */
537 /* This stops the holder of console_sem just where we want him */
538 raw_local_irq_save(flags
);
540 spin_lock(&logbuf_lock
);
541 printk_cpu
= smp_processor_id();
543 /* Emit the output into the temporary buffer */
544 printed_len
= vscnprintf(printk_buf
, sizeof(printk_buf
), fmt
, args
);
547 * Copy the output into log_buf. If the caller didn't provide
548 * appropriate log level tags, we insert them here
550 for (p
= printk_buf
; *p
; p
++) {
551 if (log_level_unknown
) {
552 /* log_level_unknown signals the start of a new line */
557 unsigned long long t
;
558 unsigned long nanosec_rem
;
561 * force the log level token to be
562 * before the time output.
564 if (p
[0] == '<' && p
[1] >='0' &&
565 p
[1] <= '7' && p
[2] == '>') {
570 loglev_char
= default_message_loglevel
574 nanosec_rem
= do_div(t
, 1000000000);
581 for (tp
= tbuf
; tp
< tbuf
+ tlen
; tp
++)
585 if (p
[0] != '<' || p
[1] < '0' ||
586 p
[1] > '7' || p
[2] != '>') {
588 emit_log_char(default_message_loglevel
594 log_level_unknown
= 0;
600 log_level_unknown
= 1;
603 if (!down_trylock(&console_sem
)) {
605 * We own the drivers. We can drop the spinlock and
606 * let release_console_sem() print the text, maybe ...
609 printk_cpu
= UINT_MAX
;
610 spin_unlock(&logbuf_lock
);
613 * Console drivers may assume that per-cpu resources have
614 * been allocated. So unless they're explicitly marked as
615 * being able to cope (CON_ANYTIME) don't call them until
616 * this CPU is officially up.
618 if (cpu_online(smp_processor_id()) || have_callable_console()) {
619 console_may_schedule
= 0;
620 release_console_sem();
622 /* Release by hand to avoid flushing the buffer. */
627 raw_local_irq_restore(flags
);
630 * Someone else owns the drivers. We drop the spinlock, which
631 * allows the semaphore holder to proceed and to call the
632 * console drivers with the output which we just produced.
634 printk_cpu
= UINT_MAX
;
635 spin_unlock(&logbuf_lock
);
637 raw_local_irq_restore(flags
);
643 EXPORT_SYMBOL(printk
);
644 EXPORT_SYMBOL(vprintk
);
648 asmlinkage
long sys_syslog(int type
, char __user
*buf
, int len
)
653 static void call_console_drivers(unsigned long start
, unsigned long end
)
660 * Set up a list of consoles. Called from init/main.c
662 static int __init
console_setup(char *str
)
664 char name
[sizeof(console_cmdline
[0].name
)];
669 * Decode str into name, index, options.
671 if (str
[0] >= '0' && str
[0] <= '9') {
672 strcpy(name
, "ttyS");
673 strncpy(name
+ 4, str
, sizeof(name
) - 5);
675 strncpy(name
, str
, sizeof(name
) - 1);
677 name
[sizeof(name
) - 1] = 0;
678 if ((options
= strchr(str
, ',')) != NULL
)
681 if (!strcmp(str
, "ttya"))
682 strcpy(name
, "ttyS0");
683 if (!strcmp(str
, "ttyb"))
684 strcpy(name
, "ttyS1");
686 for (s
= name
; *s
; s
++)
687 if ((*s
>= '0' && *s
<= '9') || *s
== ',')
689 idx
= simple_strtoul(s
, NULL
, 10);
692 add_preferred_console(name
, idx
, options
);
695 __setup("console=", console_setup
);
698 * add_preferred_console - add a device to the list of preferred consoles.
701 * @options: options for this console
703 * The last preferred console added will be used for kernel messages
704 * and stdin/out/err for init. Normally this is used by console_setup
705 * above to handle user-supplied console arguments; however it can also
706 * be used by arch-specific code either to override the user or more
707 * commonly to provide a default console (ie from PROM variables) when
708 * the user has not supplied one.
710 int __init
add_preferred_console(char *name
, int idx
, char *options
)
712 struct console_cmdline
*c
;
716 * See if this tty is not yet registered, and
717 * if we have a slot free.
719 for(i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0]; i
++)
720 if (strcmp(console_cmdline
[i
].name
, name
) == 0 &&
721 console_cmdline
[i
].index
== idx
) {
722 selected_console
= i
;
725 if (i
== MAX_CMDLINECONSOLES
)
727 selected_console
= i
;
728 c
= &console_cmdline
[i
];
729 memcpy(c
->name
, name
, sizeof(c
->name
));
730 c
->name
[sizeof(c
->name
) - 1] = 0;
731 c
->options
= options
;
736 #ifndef CONFIG_DISABLE_CONSOLE_SUSPEND
738 * suspend_console - suspend the console subsystem
740 * This disables printk() while we go into suspend states
742 void suspend_console(void)
744 printk("Suspending console(s)\n");
745 acquire_console_sem();
746 console_suspended
= 1;
749 void resume_console(void)
751 console_suspended
= 0;
752 release_console_sem();
754 #endif /* CONFIG_DISABLE_CONSOLE_SUSPEND */
757 * acquire_console_sem - lock the console system for exclusive use.
759 * Acquires a semaphore which guarantees that the caller has
760 * exclusive access to the console system and the console_drivers list.
762 * Can sleep, returns nothing.
764 void acquire_console_sem(void)
766 BUG_ON(in_interrupt());
767 if (console_suspended
) {
768 down(&secondary_console_sem
);
773 console_may_schedule
= 1;
775 EXPORT_SYMBOL(acquire_console_sem
);
777 int try_acquire_console_sem(void)
779 if (down_trylock(&console_sem
))
782 console_may_schedule
= 0;
785 EXPORT_SYMBOL(try_acquire_console_sem
);
787 int is_console_locked(void)
789 return console_locked
;
792 void wake_up_klogd(void)
794 if (!oops_in_progress
&& waitqueue_active(&log_wait
))
795 wake_up_interruptible(&log_wait
);
799 * release_console_sem - unlock the console system
801 * Releases the semaphore which the caller holds on the console system
802 * and the console driver list.
804 * While the semaphore was held, console output may have been buffered
805 * by printk(). If this is the case, release_console_sem() emits
806 * the output prior to releasing the semaphore.
808 * If there is output waiting for klogd, we wake it up.
810 * release_console_sem() may be called from any context.
812 void release_console_sem(void)
815 unsigned long _con_start
, _log_end
;
816 unsigned long wake_klogd
= 0;
818 if (console_suspended
) {
819 up(&secondary_console_sem
);
823 console_may_schedule
= 0;
826 spin_lock_irqsave(&logbuf_lock
, flags
);
827 wake_klogd
|= log_start
- log_end
;
828 if (con_start
== log_end
)
829 break; /* Nothing to print */
830 _con_start
= con_start
;
832 con_start
= log_end
; /* Flush */
833 spin_unlock(&logbuf_lock
);
834 call_console_drivers(_con_start
, _log_end
);
835 local_irq_restore(flags
);
839 spin_unlock_irqrestore(&logbuf_lock
, flags
);
843 EXPORT_SYMBOL(release_console_sem
);
846 * console_conditional_schedule - yield the CPU if required
848 * If the console code is currently allowed to sleep, and
849 * if this CPU should yield the CPU to another task, do
852 * Must be called within acquire_console_sem().
854 void __sched
console_conditional_schedule(void)
856 if (console_may_schedule
)
859 EXPORT_SYMBOL(console_conditional_schedule
);
861 void console_print(const char *s
)
863 printk(KERN_EMERG
"%s", s
);
865 EXPORT_SYMBOL(console_print
);
867 void console_unblank(void)
872 * console_unblank can no longer be called in interrupt context unless
873 * oops_in_progress is set to 1..
875 if (oops_in_progress
) {
876 if (down_trylock(&console_sem
) != 0)
879 acquire_console_sem();
882 console_may_schedule
= 0;
883 for (c
= console_drivers
; c
!= NULL
; c
= c
->next
)
884 if ((c
->flags
& CON_ENABLED
) && c
->unblank
)
886 release_console_sem();
890 * Return the console tty driver structure and its associated index
892 struct tty_driver
*console_device(int *index
)
895 struct tty_driver
*driver
= NULL
;
897 acquire_console_sem();
898 for (c
= console_drivers
; c
!= NULL
; c
= c
->next
) {
901 driver
= c
->device(c
, index
);
905 release_console_sem();
910 * Prevent further output on the passed console device so that (for example)
911 * serial drivers can disable console output before suspending a port, and can
912 * re-enable output afterwards.
914 void console_stop(struct console
*console
)
916 acquire_console_sem();
917 console
->flags
&= ~CON_ENABLED
;
918 release_console_sem();
920 EXPORT_SYMBOL(console_stop
);
922 void console_start(struct console
*console
)
924 acquire_console_sem();
925 console
->flags
|= CON_ENABLED
;
926 release_console_sem();
928 EXPORT_SYMBOL(console_start
);
931 * The console driver calls this routine during kernel initialization
932 * to register the console printing procedure with printk() and to
933 * print any messages that were printed by the kernel before the
934 * console driver was initialized.
936 void register_console(struct console
*console
)
940 struct console
*bootconsole
= NULL
;
942 if (console_drivers
) {
943 if (console
->flags
& CON_BOOT
)
945 if (console_drivers
->flags
& CON_BOOT
)
946 bootconsole
= console_drivers
;
949 if (preferred_console
< 0 || bootconsole
|| !console_drivers
)
950 preferred_console
= selected_console
;
953 * See if we want to use this console driver. If we
954 * didn't select a console we take the first one
955 * that registers here.
957 if (preferred_console
< 0) {
958 if (console
->index
< 0)
960 if (console
->setup
== NULL
||
961 console
->setup(console
, NULL
) == 0) {
962 console
->flags
|= CON_ENABLED
| CON_CONSDEV
;
963 preferred_console
= 0;
968 * See if this console matches one we selected on
971 for (i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0];
973 if (strcmp(console_cmdline
[i
].name
, console
->name
) != 0)
975 if (console
->index
>= 0 &&
976 console
->index
!= console_cmdline
[i
].index
)
978 if (console
->index
< 0)
979 console
->index
= console_cmdline
[i
].index
;
980 if (console
->setup
&&
981 console
->setup(console
, console_cmdline
[i
].options
) != 0)
983 console
->flags
|= CON_ENABLED
;
984 console
->index
= console_cmdline
[i
].index
;
985 if (i
== selected_console
) {
986 console
->flags
|= CON_CONSDEV
;
987 preferred_console
= selected_console
;
992 if (!(console
->flags
& CON_ENABLED
))
995 if (bootconsole
&& (console
->flags
& CON_CONSDEV
)) {
996 printk(KERN_INFO
"console handover: boot [%s%d] -> real [%s%d]\n",
997 bootconsole
->name
, bootconsole
->index
,
998 console
->name
, console
->index
);
999 unregister_console(bootconsole
);
1000 console
->flags
&= ~CON_PRINTBUFFER
;
1002 printk(KERN_INFO
"console [%s%d] enabled\n",
1003 console
->name
, console
->index
);
1007 * Put this console in the list - keep the
1008 * preferred driver at the head of the list.
1010 acquire_console_sem();
1011 if ((console
->flags
& CON_CONSDEV
) || console_drivers
== NULL
) {
1012 console
->next
= console_drivers
;
1013 console_drivers
= console
;
1015 console
->next
->flags
&= ~CON_CONSDEV
;
1017 console
->next
= console_drivers
->next
;
1018 console_drivers
->next
= console
;
1020 if (console
->flags
& CON_PRINTBUFFER
) {
1022 * release_console_sem() will print out the buffered messages
1025 spin_lock_irqsave(&logbuf_lock
, flags
);
1026 con_start
= log_start
;
1027 spin_unlock_irqrestore(&logbuf_lock
, flags
);
1029 release_console_sem();
1031 EXPORT_SYMBOL(register_console
);
1033 int unregister_console(struct console
*console
)
1035 struct console
*a
, *b
;
1038 acquire_console_sem();
1039 if (console_drivers
== console
) {
1040 console_drivers
=console
->next
;
1042 } else if (console_drivers
) {
1043 for (a
=console_drivers
->next
, b
=console_drivers
;
1044 a
; b
=a
, a
=b
->next
) {
1054 * If this isn't the last console and it has CON_CONSDEV set, we
1055 * need to set it on the next preferred console.
1057 if (console_drivers
!= NULL
&& console
->flags
& CON_CONSDEV
)
1058 console_drivers
->flags
|= CON_CONSDEV
;
1060 release_console_sem();
1063 EXPORT_SYMBOL(unregister_console
);
1066 * tty_write_message - write a message to a certain tty, not just the console.
1067 * @tty: the destination tty_struct
1068 * @msg: the message to write
1070 * This is used for messages that need to be redirected to a specific tty.
1071 * We don't put it into the syslog queue right now maybe in the future if
1074 void tty_write_message(struct tty_struct
*tty
, char *msg
)
1076 if (tty
&& tty
->driver
->write
)
1077 tty
->driver
->write(tty
, msg
, strlen(msg
));
1082 * printk rate limiting, lifted from the networking subsystem.
1084 * This enforces a rate limit: not more than one kernel message
1085 * every printk_ratelimit_jiffies to make a denial-of-service
1086 * attack impossible.
1088 int __printk_ratelimit(int ratelimit_jiffies
, int ratelimit_burst
)
1090 static DEFINE_SPINLOCK(ratelimit_lock
);
1091 static unsigned long toks
= 10 * 5 * HZ
;
1092 static unsigned long last_msg
;
1094 unsigned long flags
;
1095 unsigned long now
= jiffies
;
1097 spin_lock_irqsave(&ratelimit_lock
, flags
);
1098 toks
+= now
- last_msg
;
1100 if (toks
> (ratelimit_burst
* ratelimit_jiffies
))
1101 toks
= ratelimit_burst
* ratelimit_jiffies
;
1102 if (toks
>= ratelimit_jiffies
) {
1106 toks
-= ratelimit_jiffies
;
1107 spin_unlock_irqrestore(&ratelimit_lock
, flags
);
1109 printk(KERN_WARNING
"printk: %d messages suppressed.\n", lost
);
1113 spin_unlock_irqrestore(&ratelimit_lock
, flags
);
1116 EXPORT_SYMBOL(__printk_ratelimit
);
1118 /* minimum time in jiffies between messages */
1119 int printk_ratelimit_jiffies
= 5 * HZ
;
1121 /* number of messages we send before ratelimiting */
1122 int printk_ratelimit_burst
= 10;
1124 int printk_ratelimit(void)
1126 return __printk_ratelimit(printk_ratelimit_jiffies
,
1127 printk_ratelimit_burst
);
1129 EXPORT_SYMBOL(printk_ratelimit
);
1132 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1133 * @caller_jiffies: pointer to caller's state
1134 * @interval_msecs: minimum interval between prints
1136 * printk_timed_ratelimit() returns true if more than @interval_msecs
1137 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1140 bool printk_timed_ratelimit(unsigned long *caller_jiffies
,
1141 unsigned int interval_msecs
)
1143 if (*caller_jiffies
== 0 || time_after(jiffies
, *caller_jiffies
)) {
1144 *caller_jiffies
= jiffies
+ msecs_to_jiffies(interval_msecs
);
1149 EXPORT_SYMBOL(printk_timed_ratelimit
);