Add a 00-INDEX file to Documentation/sysctl/
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / printk.c
blob52493474f0abafdfd7939ab57f3f2d6ad2006695
1 /*
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).
11 * Ted Ts'o, 2/11/93.
12 * Modified for sysctl support, 1/8/97, Chris Horn.
13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14 * 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>
20 #include <linux/mm.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.
61 int oops_in_progress;
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
67 * driver system.
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;
119 #ifdef CONFIG_PRINTK
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);
129 unsigned long flags;
131 if (size)
132 size = roundup_pow_of_two(size);
133 if (size > log_buf_len) {
134 unsigned long start, dest_idx, offset;
135 char *new_log_buf;
137 new_log_buf = alloc_bootmem(size);
138 if (!new_log_buf) {
139 printk(KERN_WARNING "log_buf_len: allocation failed\n");
140 goto out;
143 spin_lock_irqsave(&logbuf_lock, flags);
144 log_buf_len = size;
145 log_buf = new_log_buf;
147 offset = start = min(con_start, log_start);
148 dest_idx = 0;
149 while (start != log_end) {
150 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
151 start++;
152 dest_idx++;
154 log_start -= offset;
155 con_start -= offset;
156 log_end -= offset;
157 spin_unlock_irqrestore(&logbuf_lock, flags);
159 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
161 out:
162 return 1;
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)
174 unsigned long lpj;
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)
182 boot_delay = 0;
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);
188 return 1;
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)
198 return;
200 k = (unsigned long long)printk_delay_msec * boot_delay;
202 timeout = jiffies + msecs_to_jiffies(boot_delay);
203 while (k) {
204 k--;
205 cpu_relax();
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))
212 break;
213 touch_nmi_watchdog();
216 #else
217 static inline void boot_delay_msec(void)
220 #endif
223 * Return the number of unread characters in the log buffer.
225 int log_buf_get_len(void)
227 return logged_chars;
231 * Copy a range of characters from the log buffer.
233 int log_buf_copy(char *dest, int idx, int len)
235 int ret, max;
236 bool took_lock = false;
238 if (!oops_in_progress) {
239 spin_lock_irq(&logbuf_lock);
240 took_lock = true;
243 max = log_buf_get_len();
244 if (idx < 0 || idx >= max) {
245 ret = -1;
246 } else {
247 if (len > max)
248 len = max;
249 ret = len;
250 idx += (log_end - max);
251 while (len-- > 0)
252 dest[len] = LOG_BUF(idx + len);
255 if (took_lock)
256 spin_unlock_irq(&logbuf_lock);
258 return ret;
262 * Extract a single character from the log buffer.
264 int log_buf_read(int idx)
266 char ret;
268 if (log_buf_copy(&ret, idx, 1) == 1)
269 return ret;
270 else
271 return -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;
292 int do_clear = 0;
293 char c;
294 int error = 0;
296 error = security_syslog(type);
297 if (error)
298 return error;
300 switch (type) {
301 case 0: /* Close log */
302 break;
303 case 1: /* Open log */
304 break;
305 case 2: /* Read from log */
306 error = -EINVAL;
307 if (!buf || len < 0)
308 goto out;
309 error = 0;
310 if (!len)
311 goto out;
312 if (!access_ok(VERIFY_WRITE, buf, len)) {
313 error = -EFAULT;
314 goto out;
316 error = wait_event_interruptible(log_wait,
317 (log_start - log_end));
318 if (error)
319 goto out;
320 i = 0;
321 spin_lock_irq(&logbuf_lock);
322 while (!error && (log_start != log_end) && i < len) {
323 c = LOG_BUF(log_start);
324 log_start++;
325 spin_unlock_irq(&logbuf_lock);
326 error = __put_user(c,buf);
327 buf++;
328 i++;
329 cond_resched();
330 spin_lock_irq(&logbuf_lock);
332 spin_unlock_irq(&logbuf_lock);
333 if (!error)
334 error = i;
335 break;
336 case 4: /* Read/clear last kernel messages */
337 do_clear = 1;
338 /* FALL THRU */
339 case 3: /* Read last kernel messages */
340 error = -EINVAL;
341 if (!buf || len < 0)
342 goto out;
343 error = 0;
344 if (!len)
345 goto out;
346 if (!access_ok(VERIFY_WRITE, buf, len)) {
347 error = -EFAULT;
348 goto out;
350 count = len;
351 if (count > log_buf_len)
352 count = log_buf_len;
353 spin_lock_irq(&logbuf_lock);
354 if (count > logged_chars)
355 count = logged_chars;
356 if (do_clear)
357 logged_chars = 0;
358 limit = log_end;
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++) {
366 j = limit-1-i;
367 if (j + log_buf_len < log_end)
368 break;
369 c = LOG_BUF(j);
370 spin_unlock_irq(&logbuf_lock);
371 error = __put_user(c,&buf[count-1-i]);
372 cond_resched();
373 spin_lock_irq(&logbuf_lock);
375 spin_unlock_irq(&logbuf_lock);
376 if (error)
377 break;
378 error = i;
379 if (i != count) {
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])) {
385 error = -EFAULT;
386 break;
388 cond_resched();
391 break;
392 case 5: /* Clear ring buffer */
393 logged_chars = 0;
394 break;
395 case 6: /* Disable logging to console */
396 console_loglevel = minimum_console_loglevel;
397 break;
398 case 7: /* Enable logging to console */
399 console_loglevel = default_console_loglevel;
400 break;
401 case 8: /* Set level of messages printed to console */
402 error = -EINVAL;
403 if (len < 1 || len > 8)
404 goto out;
405 if (len < minimum_console_loglevel)
406 len = minimum_console_loglevel;
407 console_loglevel = len;
408 error = 0;
409 break;
410 case 9: /* Number of chars in the log buffer */
411 error = log_end - log_start;
412 break;
413 case 10: /* Size of the log buffer */
414 error = log_buf_len;
415 break;
416 default:
417 error = -EINVAL;
418 break;
420 out:
421 return error;
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)
434 struct console *con;
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)
448 ignore_loglevel = 1;
449 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
451 return 1;
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)) {
465 /* wrapped write */
466 __call_console_drivers(start & LOG_BUF_MASK,
467 log_buf_len);
468 __call_console_drivers(0, end & LOG_BUF_MASK);
469 } else {
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);
487 cur_index = start;
488 start_print = start;
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';
496 cur_index += 3;
497 start_print = cur_index;
499 while (cur_index != end) {
500 char c = LOG_BUF(cur_index);
502 cur_index++;
503 if (c == '\n') {
504 if (msg_level < 0) {
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
509 * on those tags
511 msg_level = default_message_loglevel;
513 _call_console_drivers(start_print, cur_index, msg_level);
514 msg_level = -1;
515 start_print = cur_index;
516 break;
520 _call_console_drivers(start_print, end, msg_level);
523 static void emit_log_char(char c)
525 LOG_BUF(log_end) = c;
526 log_end++;
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)
532 logged_chars++;
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
538 * full oops.
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))
546 return;
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;
558 #else
559 static int printk_time = 0;
560 #endif
561 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
563 static int __init printk_time_setup(char *str)
565 if (*str)
566 return 0;
567 printk_time = 1;
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");
571 return 1;
574 __setup("time", printk_time_setup);
576 __attribute__((weak)) unsigned long long printk_clock(void)
578 return sched_clock();
581 /* Check if we have any console registered that can be called early in boot. */
582 static int have_callable_console(void)
584 struct console *con;
586 for (con = console_drivers; con; con = con->next)
587 if (con->flags & CON_ANYTIME)
588 return 1;
590 return 0;
594 * printk - print a kernel message
595 * @fmt: format string
597 * This is printk(). It can be called from any context. We want it to work.
598 * Be aware of the fact that if oops_in_progress is not set, we might try to
599 * wake klogd up which could deadlock on runqueue lock if printk() is called
600 * from scheduler code.
602 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
603 * call the console drivers. If we fail to get the semaphore we place the output
604 * into the log buffer and return. The current holder of the console_sem will
605 * notice the new output in release_console_sem() and will send it to the
606 * consoles before releasing the semaphore.
608 * One effect of this deferred printing is that code which calls printk() and
609 * then changes console_loglevel may break. This is because console_loglevel
610 * is inspected when the actual printing occurs.
612 * See also:
613 * printf(3)
616 asmlinkage int printk(const char *fmt, ...)
618 va_list args;
619 int r;
621 va_start(args, fmt);
622 r = vprintk(fmt, args);
623 va_end(args);
625 return r;
628 /* cpu currently holding logbuf_lock */
629 static volatile unsigned int printk_cpu = UINT_MAX;
631 asmlinkage int vprintk(const char *fmt, va_list args)
633 unsigned long flags;
634 int printed_len;
635 char *p;
636 static char printk_buf[1024];
637 static int log_level_unknown = 1;
639 boot_delay_msec();
641 preempt_disable();
642 if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
643 /* If a crash is occurring during printk() on this CPU,
644 * make sure we can't deadlock */
645 zap_locks();
647 /* This stops the holder of console_sem just where we want him */
648 raw_local_irq_save(flags);
649 lockdep_off();
650 spin_lock(&logbuf_lock);
651 printk_cpu = smp_processor_id();
653 /* Emit the output into the temporary buffer */
654 printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
657 * Copy the output into log_buf. If the caller didn't provide
658 * appropriate log level tags, we insert them here
660 for (p = printk_buf; *p; p++) {
661 if (log_level_unknown) {
662 /* log_level_unknown signals the start of a new line */
663 if (printk_time) {
664 int loglev_char;
665 char tbuf[50], *tp;
666 unsigned tlen;
667 unsigned long long t;
668 unsigned long nanosec_rem;
671 * force the log level token to be
672 * before the time output.
674 if (p[0] == '<' && p[1] >='0' &&
675 p[1] <= '7' && p[2] == '>') {
676 loglev_char = p[1];
677 p += 3;
678 printed_len -= 3;
679 } else {
680 loglev_char = default_message_loglevel
681 + '0';
683 t = printk_clock();
684 nanosec_rem = do_div(t, 1000000000);
685 tlen = sprintf(tbuf,
686 "<%c>[%5lu.%06lu] ",
687 loglev_char,
688 (unsigned long)t,
689 nanosec_rem/1000);
691 for (tp = tbuf; tp < tbuf + tlen; tp++)
692 emit_log_char(*tp);
693 printed_len += tlen;
694 } else {
695 if (p[0] != '<' || p[1] < '0' ||
696 p[1] > '7' || p[2] != '>') {
697 emit_log_char('<');
698 emit_log_char(default_message_loglevel
699 + '0');
700 emit_log_char('>');
701 printed_len += 3;
704 log_level_unknown = 0;
705 if (!*p)
706 break;
708 emit_log_char(*p);
709 if (*p == '\n')
710 log_level_unknown = 1;
713 if (!down_trylock(&console_sem)) {
715 * We own the drivers. We can drop the spinlock and
716 * let release_console_sem() print the text, maybe ...
718 console_locked = 1;
719 printk_cpu = UINT_MAX;
720 spin_unlock(&logbuf_lock);
723 * Console drivers may assume that per-cpu resources have
724 * been allocated. So unless they're explicitly marked as
725 * being able to cope (CON_ANYTIME) don't call them until
726 * this CPU is officially up.
728 if (cpu_online(smp_processor_id()) || have_callable_console()) {
729 console_may_schedule = 0;
730 release_console_sem();
731 } else {
732 /* Release by hand to avoid flushing the buffer. */
733 console_locked = 0;
734 up(&console_sem);
736 lockdep_on();
737 raw_local_irq_restore(flags);
738 } else {
740 * Someone else owns the drivers. We drop the spinlock, which
741 * allows the semaphore holder to proceed and to call the
742 * console drivers with the output which we just produced.
744 printk_cpu = UINT_MAX;
745 spin_unlock(&logbuf_lock);
746 lockdep_on();
747 raw_local_irq_restore(flags);
750 preempt_enable();
751 return printed_len;
753 EXPORT_SYMBOL(printk);
754 EXPORT_SYMBOL(vprintk);
756 #else
758 asmlinkage long sys_syslog(int type, char __user *buf, int len)
760 return -ENOSYS;
763 static void call_console_drivers(unsigned long start, unsigned long end)
767 #endif
770 * Set up a list of consoles. Called from init/main.c
772 static int __init console_setup(char *str)
774 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
775 char *s, *options;
776 int idx;
779 * Decode str into name, index, options.
781 if (str[0] >= '0' && str[0] <= '9') {
782 strcpy(buf, "ttyS");
783 strncpy(buf + 4, str, sizeof(buf) - 5);
784 } else {
785 strncpy(buf, str, sizeof(buf) - 1);
787 buf[sizeof(buf) - 1] = 0;
788 if ((options = strchr(str, ',')) != NULL)
789 *(options++) = 0;
790 #ifdef __sparc__
791 if (!strcmp(str, "ttya"))
792 strcpy(buf, "ttyS0");
793 if (!strcmp(str, "ttyb"))
794 strcpy(buf, "ttyS1");
795 #endif
796 for (s = buf; *s; s++)
797 if ((*s >= '0' && *s <= '9') || *s == ',')
798 break;
799 idx = simple_strtoul(s, NULL, 10);
800 *s = 0;
802 add_preferred_console(buf, idx, options);
803 return 1;
805 __setup("console=", console_setup);
808 * add_preferred_console - add a device to the list of preferred consoles.
809 * @name: device name
810 * @idx: device index
811 * @options: options for this console
813 * The last preferred console added will be used for kernel messages
814 * and stdin/out/err for init. Normally this is used by console_setup
815 * above to handle user-supplied console arguments; however it can also
816 * be used by arch-specific code either to override the user or more
817 * commonly to provide a default console (ie from PROM variables) when
818 * the user has not supplied one.
820 int __init add_preferred_console(char *name, int idx, char *options)
822 struct console_cmdline *c;
823 int i;
826 * See if this tty is not yet registered, and
827 * if we have a slot free.
829 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
830 if (strcmp(console_cmdline[i].name, name) == 0 &&
831 console_cmdline[i].index == idx) {
832 selected_console = i;
833 return 0;
835 if (i == MAX_CMDLINECONSOLES)
836 return -E2BIG;
837 selected_console = i;
838 c = &console_cmdline[i];
839 memcpy(c->name, name, sizeof(c->name));
840 c->name[sizeof(c->name) - 1] = 0;
841 c->options = options;
842 c->index = idx;
843 return 0;
846 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
848 struct console_cmdline *c;
849 int i;
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 c = &console_cmdline[i];
855 memcpy(c->name, name_new, sizeof(c->name));
856 c->name[sizeof(c->name) - 1] = 0;
857 c->options = options;
858 c->index = idx_new;
859 return i;
861 /* not found */
862 return -1;
865 #ifndef CONFIG_DISABLE_CONSOLE_SUSPEND
867 * suspend_console - suspend the console subsystem
869 * This disables printk() while we go into suspend states
871 void suspend_console(void)
873 printk("Suspending console(s)\n");
874 acquire_console_sem();
875 console_suspended = 1;
878 void resume_console(void)
880 console_suspended = 0;
881 release_console_sem();
883 #endif /* CONFIG_DISABLE_CONSOLE_SUSPEND */
886 * acquire_console_sem - lock the console system for exclusive use.
888 * Acquires a semaphore which guarantees that the caller has
889 * exclusive access to the console system and the console_drivers list.
891 * Can sleep, returns nothing.
893 void acquire_console_sem(void)
895 BUG_ON(in_interrupt());
896 if (console_suspended) {
897 down(&secondary_console_sem);
898 return;
900 down(&console_sem);
901 console_locked = 1;
902 console_may_schedule = 1;
904 EXPORT_SYMBOL(acquire_console_sem);
906 int try_acquire_console_sem(void)
908 if (down_trylock(&console_sem))
909 return -1;
910 console_locked = 1;
911 console_may_schedule = 0;
912 return 0;
914 EXPORT_SYMBOL(try_acquire_console_sem);
916 int is_console_locked(void)
918 return console_locked;
921 void wake_up_klogd(void)
923 if (!oops_in_progress && waitqueue_active(&log_wait))
924 wake_up_interruptible(&log_wait);
928 * release_console_sem - unlock the console system
930 * Releases the semaphore which the caller holds on the console system
931 * and the console driver list.
933 * While the semaphore was held, console output may have been buffered
934 * by printk(). If this is the case, release_console_sem() emits
935 * the output prior to releasing the semaphore.
937 * If there is output waiting for klogd, we wake it up.
939 * release_console_sem() may be called from any context.
941 void release_console_sem(void)
943 unsigned long flags;
944 unsigned long _con_start, _log_end;
945 unsigned long wake_klogd = 0;
947 if (console_suspended) {
948 up(&secondary_console_sem);
949 return;
952 console_may_schedule = 0;
954 for ( ; ; ) {
955 spin_lock_irqsave(&logbuf_lock, flags);
956 wake_klogd |= log_start - log_end;
957 if (con_start == log_end)
958 break; /* Nothing to print */
959 _con_start = con_start;
960 _log_end = log_end;
961 con_start = log_end; /* Flush */
962 spin_unlock(&logbuf_lock);
963 call_console_drivers(_con_start, _log_end);
964 local_irq_restore(flags);
966 console_locked = 0;
967 up(&console_sem);
968 spin_unlock_irqrestore(&logbuf_lock, flags);
969 if (wake_klogd)
970 wake_up_klogd();
972 EXPORT_SYMBOL(release_console_sem);
975 * console_conditional_schedule - yield the CPU if required
977 * If the console code is currently allowed to sleep, and
978 * if this CPU should yield the CPU to another task, do
979 * so here.
981 * Must be called within acquire_console_sem().
983 void __sched console_conditional_schedule(void)
985 if (console_may_schedule)
986 cond_resched();
988 EXPORT_SYMBOL(console_conditional_schedule);
990 void console_print(const char *s)
992 printk(KERN_EMERG "%s", s);
994 EXPORT_SYMBOL(console_print);
996 void console_unblank(void)
998 struct console *c;
1001 * console_unblank can no longer be called in interrupt context unless
1002 * oops_in_progress is set to 1..
1004 if (oops_in_progress) {
1005 if (down_trylock(&console_sem) != 0)
1006 return;
1007 } else
1008 acquire_console_sem();
1010 console_locked = 1;
1011 console_may_schedule = 0;
1012 for (c = console_drivers; c != NULL; c = c->next)
1013 if ((c->flags & CON_ENABLED) && c->unblank)
1014 c->unblank();
1015 release_console_sem();
1019 * Return the console tty driver structure and its associated index
1021 struct tty_driver *console_device(int *index)
1023 struct console *c;
1024 struct tty_driver *driver = NULL;
1026 acquire_console_sem();
1027 for (c = console_drivers; c != NULL; c = c->next) {
1028 if (!c->device)
1029 continue;
1030 driver = c->device(c, index);
1031 if (driver)
1032 break;
1034 release_console_sem();
1035 return driver;
1039 * Prevent further output on the passed console device so that (for example)
1040 * serial drivers can disable console output before suspending a port, and can
1041 * re-enable output afterwards.
1043 void console_stop(struct console *console)
1045 acquire_console_sem();
1046 console->flags &= ~CON_ENABLED;
1047 release_console_sem();
1049 EXPORT_SYMBOL(console_stop);
1051 void console_start(struct console *console)
1053 acquire_console_sem();
1054 console->flags |= CON_ENABLED;
1055 release_console_sem();
1057 EXPORT_SYMBOL(console_start);
1060 * The console driver calls this routine during kernel initialization
1061 * to register the console printing procedure with printk() and to
1062 * print any messages that were printed by the kernel before the
1063 * console driver was initialized.
1065 void register_console(struct console *console)
1067 int i;
1068 unsigned long flags;
1069 struct console *bootconsole = NULL;
1071 if (console_drivers) {
1072 if (console->flags & CON_BOOT)
1073 return;
1074 if (console_drivers->flags & CON_BOOT)
1075 bootconsole = console_drivers;
1078 if (preferred_console < 0 || bootconsole || !console_drivers)
1079 preferred_console = selected_console;
1081 if (console->early_setup)
1082 console->early_setup();
1085 * See if we want to use this console driver. If we
1086 * didn't select a console we take the first one
1087 * that registers here.
1089 if (preferred_console < 0) {
1090 if (console->index < 0)
1091 console->index = 0;
1092 if (console->setup == NULL ||
1093 console->setup(console, NULL) == 0) {
1094 console->flags |= CON_ENABLED | CON_CONSDEV;
1095 preferred_console = 0;
1100 * See if this console matches one we selected on
1101 * the command line.
1103 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1104 i++) {
1105 if (strcmp(console_cmdline[i].name, console->name) != 0)
1106 continue;
1107 if (console->index >= 0 &&
1108 console->index != console_cmdline[i].index)
1109 continue;
1110 if (console->index < 0)
1111 console->index = console_cmdline[i].index;
1112 if (console->setup &&
1113 console->setup(console, console_cmdline[i].options) != 0)
1114 break;
1115 console->flags |= CON_ENABLED;
1116 console->index = console_cmdline[i].index;
1117 if (i == selected_console) {
1118 console->flags |= CON_CONSDEV;
1119 preferred_console = selected_console;
1121 break;
1124 if (!(console->flags & CON_ENABLED))
1125 return;
1127 if (bootconsole && (console->flags & CON_CONSDEV)) {
1128 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1129 bootconsole->name, bootconsole->index,
1130 console->name, console->index);
1131 unregister_console(bootconsole);
1132 console->flags &= ~CON_PRINTBUFFER;
1133 } else {
1134 printk(KERN_INFO "console [%s%d] enabled\n",
1135 console->name, console->index);
1139 * Put this console in the list - keep the
1140 * preferred driver at the head of the list.
1142 acquire_console_sem();
1143 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1144 console->next = console_drivers;
1145 console_drivers = console;
1146 if (console->next)
1147 console->next->flags &= ~CON_CONSDEV;
1148 } else {
1149 console->next = console_drivers->next;
1150 console_drivers->next = console;
1152 if (console->flags & CON_PRINTBUFFER) {
1154 * release_console_sem() will print out the buffered messages
1155 * for us.
1157 spin_lock_irqsave(&logbuf_lock, flags);
1158 con_start = log_start;
1159 spin_unlock_irqrestore(&logbuf_lock, flags);
1161 release_console_sem();
1163 EXPORT_SYMBOL(register_console);
1165 int unregister_console(struct console *console)
1167 struct console *a, *b;
1168 int res = 1;
1170 acquire_console_sem();
1171 if (console_drivers == console) {
1172 console_drivers=console->next;
1173 res = 0;
1174 } else if (console_drivers) {
1175 for (a=console_drivers->next, b=console_drivers ;
1176 a; b=a, a=b->next) {
1177 if (a == console) {
1178 b->next = a->next;
1179 res = 0;
1180 break;
1186 * If this isn't the last console and it has CON_CONSDEV set, we
1187 * need to set it on the next preferred console.
1189 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1190 console_drivers->flags |= CON_CONSDEV;
1192 release_console_sem();
1193 return res;
1195 EXPORT_SYMBOL(unregister_console);
1197 static int __init disable_boot_consoles(void)
1199 if (console_drivers != NULL) {
1200 if (console_drivers->flags & CON_BOOT) {
1201 printk(KERN_INFO "turn off boot console %s%d\n",
1202 console_drivers->name, console_drivers->index);
1203 return unregister_console(console_drivers);
1206 return 0;
1208 late_initcall(disable_boot_consoles);
1211 * tty_write_message - write a message to a certain tty, not just the console.
1212 * @tty: the destination tty_struct
1213 * @msg: the message to write
1215 * This is used for messages that need to be redirected to a specific tty.
1216 * We don't put it into the syslog queue right now maybe in the future if
1217 * really needed.
1219 void tty_write_message(struct tty_struct *tty, char *msg)
1221 if (tty && tty->driver->write)
1222 tty->driver->write(tty, msg, strlen(msg));
1223 return;
1227 * printk rate limiting, lifted from the networking subsystem.
1229 * This enforces a rate limit: not more than one kernel message
1230 * every printk_ratelimit_jiffies to make a denial-of-service
1231 * attack impossible.
1233 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1235 static DEFINE_SPINLOCK(ratelimit_lock);
1236 static unsigned long toks = 10 * 5 * HZ;
1237 static unsigned long last_msg;
1238 static int missed;
1239 unsigned long flags;
1240 unsigned long now = jiffies;
1242 spin_lock_irqsave(&ratelimit_lock, flags);
1243 toks += now - last_msg;
1244 last_msg = now;
1245 if (toks > (ratelimit_burst * ratelimit_jiffies))
1246 toks = ratelimit_burst * ratelimit_jiffies;
1247 if (toks >= ratelimit_jiffies) {
1248 int lost = missed;
1250 missed = 0;
1251 toks -= ratelimit_jiffies;
1252 spin_unlock_irqrestore(&ratelimit_lock, flags);
1253 if (lost)
1254 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1255 return 1;
1257 missed++;
1258 spin_unlock_irqrestore(&ratelimit_lock, flags);
1259 return 0;
1261 EXPORT_SYMBOL(__printk_ratelimit);
1263 /* minimum time in jiffies between messages */
1264 int printk_ratelimit_jiffies = 5 * HZ;
1266 /* number of messages we send before ratelimiting */
1267 int printk_ratelimit_burst = 10;
1269 int printk_ratelimit(void)
1271 return __printk_ratelimit(printk_ratelimit_jiffies,
1272 printk_ratelimit_burst);
1274 EXPORT_SYMBOL(printk_ratelimit);
1277 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1278 * @caller_jiffies: pointer to caller's state
1279 * @interval_msecs: minimum interval between prints
1281 * printk_timed_ratelimit() returns true if more than @interval_msecs
1282 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1283 * returned true.
1285 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1286 unsigned int interval_msecs)
1288 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1289 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1290 return true;
1292 return false;
1294 EXPORT_SYMBOL(printk_timed_ratelimit);