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/smp_lock.h>
24 #include <linux/console.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/interrupt.h> /* For in_interrupt() */
29 #include <linux/config.h>
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>
36 #include <asm/uaccess.h>
38 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
40 /* printk's without a loglevel use this.. */
41 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
43 /* We show everything that is MORE important than this.. */
44 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
45 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
47 DECLARE_WAIT_QUEUE_HEAD(log_wait
);
49 int console_printk
[4] = {
50 DEFAULT_CONSOLE_LOGLEVEL
, /* console_loglevel */
51 DEFAULT_MESSAGE_LOGLEVEL
, /* default_message_loglevel */
52 MINIMUM_CONSOLE_LOGLEVEL
, /* minimum_console_loglevel */
53 DEFAULT_CONSOLE_LOGLEVEL
, /* default_console_loglevel */
56 EXPORT_SYMBOL(console_printk
);
59 * Low lever drivers may need that to know if they can schedule in
60 * their unblank() callback or not. So let's export it.
63 EXPORT_SYMBOL(oops_in_progress
);
66 * console_sem protects the console_drivers list, and also
67 * provides serialisation for access to the entire console
70 static DECLARE_MUTEX(console_sem
);
71 static DECLARE_MUTEX(secondary_console_sem
);
72 struct console
*console_drivers
;
74 * This is used for debugging the mess that is the VT code by
75 * keeping track if we have the console semaphore held. It's
76 * definitely not the perfect debug tool (we don't know if _WE_
77 * hold it are racing, but it helps tracking those weird code
78 * path in the console code where we end up in places I want
79 * locked without the console sempahore held
81 static int console_locked
, console_suspended
;
84 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
85 * It is also used in interesting ways to provide interlocking in
86 * release_console_sem().
88 static DEFINE_SPINLOCK(logbuf_lock
);
90 #define LOG_BUF_MASK (log_buf_len-1)
91 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
94 * The indices into log_buf are not constrained to log_buf_len - they
95 * must be masked before subscripting
97 static unsigned long log_start
; /* Index into log_buf: next char to be read by syslog() */
98 static unsigned long con_start
; /* Index into log_buf: next char to be sent to consoles */
99 static unsigned long log_end
; /* Index into log_buf: most-recently-written-char + 1 */
102 * Array of consoles built from command line options (console=)
104 struct console_cmdline
106 char name
[8]; /* Name of the driver */
107 int index
; /* Minor dev. to use */
108 char *options
; /* Options for the driver */
111 #define MAX_CMDLINECONSOLES 8
113 static struct console_cmdline console_cmdline
[MAX_CMDLINECONSOLES
];
114 static int selected_console
= -1;
115 static int preferred_console
= -1;
117 /* Flag: console code may call schedule() */
118 static int console_may_schedule
;
122 static char __log_buf
[__LOG_BUF_LEN
];
123 static char *log_buf
= __log_buf
;
124 static int log_buf_len
= __LOG_BUF_LEN
;
125 static unsigned long logged_chars
; /* Number of chars produced since last read+clear operation */
127 static int __init
log_buf_len_setup(char *str
)
129 unsigned long size
= memparse(str
, &str
);
133 size
= roundup_pow_of_two(size
);
134 if (size
> log_buf_len
) {
135 unsigned long start
, dest_idx
, offset
;
138 new_log_buf
= alloc_bootmem(size
);
140 printk(KERN_WARNING
"log_buf_len: allocation failed\n");
144 spin_lock_irqsave(&logbuf_lock
, flags
);
146 log_buf
= new_log_buf
;
148 offset
= start
= min(con_start
, log_start
);
150 while (start
!= log_end
) {
151 log_buf
[dest_idx
] = __log_buf
[start
& (__LOG_BUF_LEN
- 1)];
158 spin_unlock_irqrestore(&logbuf_lock
, flags
);
160 printk(KERN_NOTICE
"log_buf_len: %d\n", log_buf_len
);
166 __setup("log_buf_len=", log_buf_len_setup
);
169 * Commands to do_syslog:
171 * 0 -- Close the log. Currently a NOP.
172 * 1 -- Open the log. Currently a NOP.
173 * 2 -- Read from the log.
174 * 3 -- Read all messages remaining in the ring buffer.
175 * 4 -- Read and clear all messages remaining in the ring buffer
176 * 5 -- Clear ring buffer.
177 * 6 -- Disable printk's to console
178 * 7 -- Enable printk's to console
179 * 8 -- Set level of messages printed to console
180 * 9 -- Return number of unread characters in the log buffer
181 * 10 -- Return size of the log buffer
183 int do_syslog(int type
, char __user
*buf
, int len
)
185 unsigned long i
, j
, limit
, count
;
190 error
= security_syslog(type
);
195 case 0: /* Close log */
197 case 1: /* Open log */
199 case 2: /* Read from log */
206 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
210 error
= wait_event_interruptible(log_wait
,
211 (log_start
- log_end
));
215 spin_lock_irq(&logbuf_lock
);
216 while (!error
&& (log_start
!= log_end
) && i
< len
) {
217 c
= LOG_BUF(log_start
);
219 spin_unlock_irq(&logbuf_lock
);
220 error
= __put_user(c
,buf
);
224 spin_lock_irq(&logbuf_lock
);
226 spin_unlock_irq(&logbuf_lock
);
230 case 4: /* Read/clear last kernel messages */
233 case 3: /* Read last kernel messages */
240 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
245 if (count
> log_buf_len
)
247 spin_lock_irq(&logbuf_lock
);
248 if (count
> logged_chars
)
249 count
= logged_chars
;
254 * __put_user() could sleep, and while we sleep
255 * printk() could overwrite the messages
256 * we try to copy to user space. Therefore
257 * the messages are copied in reverse. <manfreds>
259 for (i
= 0; i
< count
&& !error
; i
++) {
261 if (j
+ log_buf_len
< log_end
)
264 spin_unlock_irq(&logbuf_lock
);
265 error
= __put_user(c
,&buf
[count
-1-i
]);
267 spin_lock_irq(&logbuf_lock
);
269 spin_unlock_irq(&logbuf_lock
);
274 int offset
= count
-error
;
275 /* buffer overflow during copy, correct user buffer. */
276 for (i
= 0; i
< error
; i
++) {
277 if (__get_user(c
,&buf
[i
+offset
]) ||
278 __put_user(c
,&buf
[i
])) {
286 case 5: /* Clear ring buffer */
289 case 6: /* Disable logging to console */
290 console_loglevel
= minimum_console_loglevel
;
292 case 7: /* Enable logging to console */
293 console_loglevel
= default_console_loglevel
;
295 case 8: /* Set level of messages printed to console */
297 if (len
< 1 || len
> 8)
299 if (len
< minimum_console_loglevel
)
300 len
= minimum_console_loglevel
;
301 console_loglevel
= len
;
304 case 9: /* Number of chars in the log buffer */
305 error
= log_end
- log_start
;
307 case 10: /* Size of the log buffer */
318 asmlinkage
long sys_syslog(int type
, char __user
*buf
, int len
)
320 return do_syslog(type
, buf
, len
);
324 * Call the console drivers on a range of log_buf
326 static void __call_console_drivers(unsigned long start
, unsigned long end
)
330 for (con
= console_drivers
; con
; con
= con
->next
) {
331 if ((con
->flags
& CON_ENABLED
) && con
->write
&&
332 (cpu_online(smp_processor_id()) ||
333 (con
->flags
& CON_ANYTIME
)))
334 con
->write(con
, &LOG_BUF(start
), end
- start
);
339 * Write out chars from start to end - 1 inclusive
341 static void _call_console_drivers(unsigned long start
,
342 unsigned long end
, int msg_log_level
)
344 if (msg_log_level
< console_loglevel
&&
345 console_drivers
&& start
!= end
) {
346 if ((start
& LOG_BUF_MASK
) > (end
& LOG_BUF_MASK
)) {
348 __call_console_drivers(start
& LOG_BUF_MASK
,
350 __call_console_drivers(0, end
& LOG_BUF_MASK
);
352 __call_console_drivers(start
, end
);
358 * Call the console drivers, asking them to write out
359 * log_buf[start] to log_buf[end - 1].
360 * The console_sem must be held.
362 static void call_console_drivers(unsigned long start
, unsigned long end
)
364 unsigned long cur_index
, start_print
;
365 static int msg_level
= -1;
367 BUG_ON(((long)(start
- end
)) > 0);
371 while (cur_index
!= end
) {
372 if (msg_level
< 0 && ((end
- cur_index
) > 2) &&
373 LOG_BUF(cur_index
+ 0) == '<' &&
374 LOG_BUF(cur_index
+ 1) >= '0' &&
375 LOG_BUF(cur_index
+ 1) <= '7' &&
376 LOG_BUF(cur_index
+ 2) == '>') {
377 msg_level
= LOG_BUF(cur_index
+ 1) - '0';
379 start_print
= cur_index
;
381 while (cur_index
!= end
) {
382 char c
= LOG_BUF(cur_index
);
388 * printk() has already given us loglevel tags in
389 * the buffer. This code is here in case the
390 * log buffer has wrapped right round and scribbled
393 msg_level
= default_message_loglevel
;
395 _call_console_drivers(start_print
, cur_index
, msg_level
);
397 start_print
= cur_index
;
402 _call_console_drivers(start_print
, end
, msg_level
);
405 static void emit_log_char(char c
)
407 LOG_BUF(log_end
) = c
;
409 if (log_end
- log_start
> log_buf_len
)
410 log_start
= log_end
- log_buf_len
;
411 if (log_end
- con_start
> log_buf_len
)
412 con_start
= log_end
- log_buf_len
;
413 if (logged_chars
< log_buf_len
)
418 * Zap console related locks when oopsing. Only zap at most once
419 * every 10 seconds, to leave time for slow consoles to print a
422 static void zap_locks(void)
424 static unsigned long oops_timestamp
;
426 if (time_after_eq(jiffies
, oops_timestamp
) &&
427 !time_after(jiffies
, oops_timestamp
+ 30 * HZ
))
430 oops_timestamp
= jiffies
;
432 /* If a crash is occurring, make sure we can't deadlock */
433 spin_lock_init(&logbuf_lock
);
434 /* And make sure that we print immediately */
435 init_MUTEX(&console_sem
);
438 #if defined(CONFIG_PRINTK_TIME)
439 static int printk_time
= 1;
441 static int printk_time
= 0;
443 module_param(printk_time
, int, S_IRUGO
| S_IWUSR
);
445 static int __init
printk_time_setup(char *str
)
453 __setup("time", printk_time_setup
);
455 __attribute__((weak
)) unsigned long long printk_clock(void)
457 return sched_clock();
460 /* Check if we have any console registered that can be called early in boot. */
461 static int have_callable_console(void)
465 for (con
= console_drivers
; con
; con
= con
->next
)
466 if (con
->flags
& CON_ANYTIME
)
473 * printk - print a kernel message
474 * @fmt: format string
476 * This is printk. It can be called from any context. We want it to work.
478 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
479 * call the console drivers. If we fail to get the semaphore we place the output
480 * into the log buffer and return. The current holder of the console_sem will
481 * notice the new output in release_console_sem() and will send it to the
482 * consoles before releasing the semaphore.
484 * One effect of this deferred printing is that code which calls printk() and
485 * then changes console_loglevel may break. This is because console_loglevel
486 * is inspected when the actual printing occurs.
492 asmlinkage
int printk(const char *fmt
, ...)
498 r
= vprintk(fmt
, args
);
504 /* cpu currently holding logbuf_lock */
505 static volatile unsigned int printk_cpu
= UINT_MAX
;
507 asmlinkage
int vprintk(const char *fmt
, va_list args
)
512 static char printk_buf
[1024];
513 static int log_level_unknown
= 1;
516 if (unlikely(oops_in_progress
) && printk_cpu
== smp_processor_id())
517 /* If a crash is occurring during printk() on this CPU,
518 * make sure we can't deadlock */
521 /* This stops the holder of console_sem just where we want him */
522 spin_lock_irqsave(&logbuf_lock
, flags
);
523 printk_cpu
= smp_processor_id();
525 /* Emit the output into the temporary buffer */
526 printed_len
= vscnprintf(printk_buf
, sizeof(printk_buf
), fmt
, args
);
529 * Copy the output into log_buf. If the caller didn't provide
530 * appropriate log level tags, we insert them here
532 for (p
= printk_buf
; *p
; p
++) {
533 if (log_level_unknown
) {
534 /* log_level_unknown signals the start of a new line */
539 unsigned long long t
;
540 unsigned long nanosec_rem
;
543 * force the log level token to be
544 * before the time output.
546 if (p
[0] == '<' && p
[1] >='0' &&
547 p
[1] <= '7' && p
[2] == '>') {
552 loglev_char
= default_message_loglevel
556 nanosec_rem
= do_div(t
, 1000000000);
563 for (tp
= tbuf
; tp
< tbuf
+ tlen
; tp
++)
567 if (p
[0] != '<' || p
[1] < '0' ||
568 p
[1] > '7' || p
[2] != '>') {
570 emit_log_char(default_message_loglevel
576 log_level_unknown
= 0;
582 log_level_unknown
= 1;
585 if (!down_trylock(&console_sem
)) {
587 * We own the drivers. We can drop the spinlock and
588 * let release_console_sem() print the text, maybe ...
591 printk_cpu
= UINT_MAX
;
592 spin_unlock_irqrestore(&logbuf_lock
, flags
);
595 * Console drivers may assume that per-cpu resources have
596 * been allocated. So unless they're explicitly marked as
597 * being able to cope (CON_ANYTIME) don't call them until
598 * this CPU is officially up.
600 if (cpu_online(smp_processor_id()) || have_callable_console()) {
601 console_may_schedule
= 0;
602 release_console_sem();
604 /* Release by hand to avoid flushing the buffer. */
610 * Someone else owns the drivers. We drop the spinlock, which
611 * allows the semaphore holder to proceed and to call the
612 * console drivers with the output which we just produced.
614 printk_cpu
= UINT_MAX
;
615 spin_unlock_irqrestore(&logbuf_lock
, flags
);
621 EXPORT_SYMBOL(printk
);
622 EXPORT_SYMBOL(vprintk
);
626 asmlinkage
long sys_syslog(int type
, char __user
*buf
, int len
)
631 int do_syslog(int type
, char __user
*buf
, int len
)
636 static void call_console_drivers(unsigned long start
, unsigned long end
)
643 * Set up a list of consoles. Called from init/main.c
645 static int __init
console_setup(char *str
)
647 char name
[sizeof(console_cmdline
[0].name
)];
652 * Decode str into name, index, options.
654 if (str
[0] >= '0' && str
[0] <= '9') {
655 strcpy(name
, "ttyS");
656 strncpy(name
+ 4, str
, sizeof(name
) - 5);
658 strncpy(name
, str
, sizeof(name
) - 1);
660 name
[sizeof(name
) - 1] = 0;
661 if ((options
= strchr(str
, ',')) != NULL
)
664 if (!strcmp(str
, "ttya"))
665 strcpy(name
, "ttyS0");
666 if (!strcmp(str
, "ttyb"))
667 strcpy(name
, "ttyS1");
669 for (s
= name
; *s
; s
++)
670 if ((*s
>= '0' && *s
<= '9') || *s
== ',')
672 idx
= simple_strtoul(s
, NULL
, 10);
675 add_preferred_console(name
, idx
, options
);
678 __setup("console=", console_setup
);
681 * add_preferred_console - add a device to the list of preferred consoles.
684 * @options: options for this console
686 * The last preferred console added will be used for kernel messages
687 * and stdin/out/err for init. Normally this is used by console_setup
688 * above to handle user-supplied console arguments; however it can also
689 * be used by arch-specific code either to override the user or more
690 * commonly to provide a default console (ie from PROM variables) when
691 * the user has not supplied one.
693 int __init
add_preferred_console(char *name
, int idx
, char *options
)
695 struct console_cmdline
*c
;
699 * See if this tty is not yet registered, and
700 * if we have a slot free.
702 for(i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0]; i
++)
703 if (strcmp(console_cmdline
[i
].name
, name
) == 0 &&
704 console_cmdline
[i
].index
== idx
) {
705 selected_console
= i
;
708 if (i
== MAX_CMDLINECONSOLES
)
710 selected_console
= i
;
711 c
= &console_cmdline
[i
];
712 memcpy(c
->name
, name
, sizeof(c
->name
));
713 c
->name
[sizeof(c
->name
) - 1] = 0;
714 c
->options
= options
;
720 * suspend_console - suspend the console subsystem
722 * This disables printk() while we go into suspend states
724 void suspend_console(void)
726 acquire_console_sem();
727 console_suspended
= 1;
730 void resume_console(void)
732 console_suspended
= 0;
733 release_console_sem();
737 * acquire_console_sem - lock the console system for exclusive use.
739 * Acquires a semaphore which guarantees that the caller has
740 * exclusive access to the console system and the console_drivers list.
742 * Can sleep, returns nothing.
744 void acquire_console_sem(void)
746 BUG_ON(in_interrupt());
747 if (console_suspended
) {
748 down(&secondary_console_sem
);
753 console_may_schedule
= 1;
755 EXPORT_SYMBOL(acquire_console_sem
);
757 int try_acquire_console_sem(void)
759 if (down_trylock(&console_sem
))
762 console_may_schedule
= 0;
765 EXPORT_SYMBOL(try_acquire_console_sem
);
767 int is_console_locked(void)
769 return console_locked
;
771 EXPORT_SYMBOL(is_console_locked
);
774 * release_console_sem - unlock the console system
776 * Releases the semaphore which the caller holds on the console system
777 * and the console driver list.
779 * While the semaphore was held, console output may have been buffered
780 * by printk(). If this is the case, release_console_sem() emits
781 * the output prior to releasing the semaphore.
783 * If there is output waiting for klogd, we wake it up.
785 * release_console_sem() may be called from any context.
787 void release_console_sem(void)
790 unsigned long _con_start
, _log_end
;
791 unsigned long wake_klogd
= 0;
793 if (console_suspended
) {
794 up(&secondary_console_sem
);
798 spin_lock_irqsave(&logbuf_lock
, flags
);
799 wake_klogd
|= log_start
- log_end
;
800 if (con_start
== log_end
)
801 break; /* Nothing to print */
802 _con_start
= con_start
;
804 con_start
= log_end
; /* Flush */
805 spin_unlock(&logbuf_lock
);
806 call_console_drivers(_con_start
, _log_end
);
807 local_irq_restore(flags
);
810 console_may_schedule
= 0;
812 spin_unlock_irqrestore(&logbuf_lock
, flags
);
813 if (wake_klogd
&& !oops_in_progress
&& waitqueue_active(&log_wait
))
814 wake_up_interruptible(&log_wait
);
816 EXPORT_SYMBOL(release_console_sem
);
819 * console_conditional_schedule - yield the CPU if required
821 * If the console code is currently allowed to sleep, and
822 * if this CPU should yield the CPU to another task, do
825 * Must be called within acquire_console_sem().
827 void __sched
console_conditional_schedule(void)
829 if (console_may_schedule
)
832 EXPORT_SYMBOL(console_conditional_schedule
);
834 void console_print(const char *s
)
836 printk(KERN_EMERG
"%s", s
);
838 EXPORT_SYMBOL(console_print
);
840 void console_unblank(void)
845 * console_unblank can no longer be called in interrupt context unless
846 * oops_in_progress is set to 1..
848 if (oops_in_progress
) {
849 if (down_trylock(&console_sem
) != 0)
852 acquire_console_sem();
855 console_may_schedule
= 0;
856 for (c
= console_drivers
; c
!= NULL
; c
= c
->next
)
857 if ((c
->flags
& CON_ENABLED
) && c
->unblank
)
859 release_console_sem();
863 * Return the console tty driver structure and its associated index
865 struct tty_driver
*console_device(int *index
)
868 struct tty_driver
*driver
= NULL
;
870 acquire_console_sem();
871 for (c
= console_drivers
; c
!= NULL
; c
= c
->next
) {
874 driver
= c
->device(c
, index
);
878 release_console_sem();
883 * Prevent further output on the passed console device so that (for example)
884 * serial drivers can disable console output before suspending a port, and can
885 * re-enable output afterwards.
887 void console_stop(struct console
*console
)
889 acquire_console_sem();
890 console
->flags
&= ~CON_ENABLED
;
891 release_console_sem();
893 EXPORT_SYMBOL(console_stop
);
895 void console_start(struct console
*console
)
897 acquire_console_sem();
898 console
->flags
|= CON_ENABLED
;
899 release_console_sem();
901 EXPORT_SYMBOL(console_start
);
904 * The console driver calls this routine during kernel initialization
905 * to register the console printing procedure with printk() and to
906 * print any messages that were printed by the kernel before the
907 * console driver was initialized.
909 void register_console(struct console
*console
)
914 if (preferred_console
< 0)
915 preferred_console
= selected_console
;
918 * See if we want to use this console driver. If we
919 * didn't select a console we take the first one
920 * that registers here.
922 if (preferred_console
< 0) {
923 if (console
->index
< 0)
925 if (console
->setup
== NULL
||
926 console
->setup(console
, NULL
) == 0) {
927 console
->flags
|= CON_ENABLED
| CON_CONSDEV
;
928 preferred_console
= 0;
933 * See if this console matches one we selected on
936 for (i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0];
938 if (strcmp(console_cmdline
[i
].name
, console
->name
) != 0)
940 if (console
->index
>= 0 &&
941 console
->index
!= console_cmdline
[i
].index
)
943 if (console
->index
< 0)
944 console
->index
= console_cmdline
[i
].index
;
945 if (console
->setup
&&
946 console
->setup(console
, console_cmdline
[i
].options
) != 0)
948 console
->flags
|= CON_ENABLED
;
949 console
->index
= console_cmdline
[i
].index
;
950 if (i
== selected_console
) {
951 console
->flags
|= CON_CONSDEV
;
952 preferred_console
= selected_console
;
957 if (!(console
->flags
& CON_ENABLED
))
960 if (console_drivers
&& (console_drivers
->flags
& CON_BOOT
)) {
961 unregister_console(console_drivers
);
962 console
->flags
&= ~CON_PRINTBUFFER
;
966 * Put this console in the list - keep the
967 * preferred driver at the head of the list.
969 acquire_console_sem();
970 if ((console
->flags
& CON_CONSDEV
) || console_drivers
== NULL
) {
971 console
->next
= console_drivers
;
972 console_drivers
= console
;
974 console
->next
->flags
&= ~CON_CONSDEV
;
976 console
->next
= console_drivers
->next
;
977 console_drivers
->next
= console
;
979 if (console
->flags
& CON_PRINTBUFFER
) {
981 * release_console_sem() will print out the buffered messages
984 spin_lock_irqsave(&logbuf_lock
, flags
);
985 con_start
= log_start
;
986 spin_unlock_irqrestore(&logbuf_lock
, flags
);
988 release_console_sem();
990 EXPORT_SYMBOL(register_console
);
992 int unregister_console(struct console
*console
)
994 struct console
*a
, *b
;
997 acquire_console_sem();
998 if (console_drivers
== console
) {
999 console_drivers
=console
->next
;
1001 } else if (console_drivers
) {
1002 for (a
=console_drivers
->next
, b
=console_drivers
;
1003 a
; b
=a
, a
=b
->next
) {
1012 /* If last console is removed, we re-enable picking the first
1013 * one that gets registered. Without that, pmac early boot console
1014 * would prevent fbcon from taking over.
1016 * If this isn't the last console and it has CON_CONSDEV set, we
1017 * need to set it on the next preferred console.
1019 if (console_drivers
== NULL
)
1020 preferred_console
= selected_console
;
1021 else if (console
->flags
& CON_CONSDEV
)
1022 console_drivers
->flags
|= CON_CONSDEV
;
1024 release_console_sem();
1027 EXPORT_SYMBOL(unregister_console
);
1030 * tty_write_message - write a message to a certain tty, not just the console.
1031 * @tty: the destination tty_struct
1032 * @msg: the message to write
1034 * This is used for messages that need to be redirected to a specific tty.
1035 * We don't put it into the syslog queue right now maybe in the future if
1038 void tty_write_message(struct tty_struct
*tty
, char *msg
)
1040 if (tty
&& tty
->driver
->write
)
1041 tty
->driver
->write(tty
, msg
, strlen(msg
));
1046 * printk rate limiting, lifted from the networking subsystem.
1048 * This enforces a rate limit: not more than one kernel message
1049 * every printk_ratelimit_jiffies to make a denial-of-service
1050 * attack impossible.
1052 int __printk_ratelimit(int ratelimit_jiffies
, int ratelimit_burst
)
1054 static DEFINE_SPINLOCK(ratelimit_lock
);
1055 static unsigned long toks
= 10 * 5 * HZ
;
1056 static unsigned long last_msg
;
1058 unsigned long flags
;
1059 unsigned long now
= jiffies
;
1061 spin_lock_irqsave(&ratelimit_lock
, flags
);
1062 toks
+= now
- last_msg
;
1064 if (toks
> (ratelimit_burst
* ratelimit_jiffies
))
1065 toks
= ratelimit_burst
* ratelimit_jiffies
;
1066 if (toks
>= ratelimit_jiffies
) {
1070 toks
-= ratelimit_jiffies
;
1071 spin_unlock_irqrestore(&ratelimit_lock
, flags
);
1073 printk(KERN_WARNING
"printk: %d messages suppressed.\n", lost
);
1077 spin_unlock_irqrestore(&ratelimit_lock
, flags
);
1080 EXPORT_SYMBOL(__printk_ratelimit
);
1082 /* minimum time in jiffies between messages */
1083 int printk_ratelimit_jiffies
= 5 * HZ
;
1085 /* number of messages we send before ratelimiting */
1086 int printk_ratelimit_burst
= 10;
1088 int printk_ratelimit(void)
1090 return __printk_ratelimit(printk_ratelimit_jiffies
,
1091 printk_ratelimit_burst
);
1093 EXPORT_SYMBOL(printk_ratelimit
);