add printk.time option, deprecate 'time'
[deliverable/linux.git] / kernel / printk.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/printk.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
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.
40dc5651 13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
624dffcb 14 * manfred@colorfullife.com
1da177e4
LT
15 * Rewrote bits to get rid of console_lock
16 * 01Mar01 Andrew Morton <andrewm@uow.edu.au>
17 */
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
1da177e4
LT
23#include <linux/console.h>
24#include <linux/init.h>
25#include <linux/module.h>
3b9c0410 26#include <linux/moduleparam.h>
1da177e4 27#include <linux/interrupt.h> /* For in_interrupt() */
1da177e4
LT
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>
f46c4833 33#include <linux/jiffies.h>
1da177e4
LT
34
35#include <asm/uaccess.h>
36
37#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
38
39/* printk's without a loglevel use this.. */
40#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
41
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 */
45
46DECLARE_WAIT_QUEUE_HEAD(log_wait);
47
48int 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 */
53};
54
1da177e4 55/*
0bbfb7c2 56 * Low level drivers may need that to know if they can schedule in
1da177e4
LT
57 * their unblank() callback or not. So let's export it.
58 */
59int oops_in_progress;
60EXPORT_SYMBOL(oops_in_progress);
61
62/*
63 * console_sem protects the console_drivers list, and also
64 * provides serialisation for access to the entire console
65 * driver system.
66 */
67static DECLARE_MUTEX(console_sem);
557240b4 68static DECLARE_MUTEX(secondary_console_sem);
1da177e4
LT
69struct console *console_drivers;
70/*
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
77 */
557240b4 78static int console_locked, console_suspended;
1da177e4
LT
79
80/*
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().
84 */
85static DEFINE_SPINLOCK(logbuf_lock);
86
1da177e4
LT
87#define LOG_BUF_MASK (log_buf_len-1)
88#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
89
90/*
91 * The indices into log_buf are not constrained to log_buf_len - they
92 * must be masked before subscripting
93 */
94static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
95static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
96static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
1da177e4
LT
97
98/*
99 * Array of consoles built from command line options (console=)
100 */
101struct console_cmdline
102{
103 char name[8]; /* Name of the driver */
104 int index; /* Minor dev. to use */
105 char *options; /* Options for the driver */
106};
107
108#define MAX_CMDLINECONSOLES 8
109
110static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
111static int selected_console = -1;
112static int preferred_console = -1;
113
114/* Flag: console code may call schedule() */
115static int console_may_schedule;
116
d59745ce
MM
117#ifdef CONFIG_PRINTK
118
119static char __log_buf[__LOG_BUF_LEN];
120static char *log_buf = __log_buf;
121static int log_buf_len = __LOG_BUF_LEN;
122static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
123
1da177e4
LT
124static int __init log_buf_len_setup(char *str)
125{
126 unsigned long size = memparse(str, &str);
127 unsigned long flags;
128
129 if (size)
130 size = roundup_pow_of_two(size);
131 if (size > log_buf_len) {
132 unsigned long start, dest_idx, offset;
40dc5651 133 char *new_log_buf;
1da177e4
LT
134
135 new_log_buf = alloc_bootmem(size);
136 if (!new_log_buf) {
40dc5651 137 printk(KERN_WARNING "log_buf_len: allocation failed\n");
1da177e4
LT
138 goto out;
139 }
140
141 spin_lock_irqsave(&logbuf_lock, flags);
142 log_buf_len = size;
143 log_buf = new_log_buf;
144
145 offset = start = min(con_start, log_start);
146 dest_idx = 0;
147 while (start != log_end) {
148 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
149 start++;
150 dest_idx++;
151 }
152 log_start -= offset;
153 con_start -= offset;
154 log_end -= offset;
155 spin_unlock_irqrestore(&logbuf_lock, flags);
156
40dc5651 157 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
1da177e4
LT
158 }
159out:
1da177e4
LT
160 return 1;
161}
162
163__setup("log_buf_len=", log_buf_len_setup);
164
165/*
166 * Commands to do_syslog:
167 *
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
179 */
40dc5651 180int do_syslog(int type, char __user *buf, int len)
1da177e4
LT
181{
182 unsigned long i, j, limit, count;
183 int do_clear = 0;
184 char c;
185 int error = 0;
186
187 error = security_syslog(type);
188 if (error)
189 return error;
190
191 switch (type) {
192 case 0: /* Close log */
193 break;
194 case 1: /* Open log */
195 break;
196 case 2: /* Read from log */
197 error = -EINVAL;
198 if (!buf || len < 0)
199 goto out;
200 error = 0;
201 if (!len)
202 goto out;
203 if (!access_ok(VERIFY_WRITE, buf, len)) {
204 error = -EFAULT;
205 goto out;
206 }
40dc5651
JJ
207 error = wait_event_interruptible(log_wait,
208 (log_start - log_end));
1da177e4
LT
209 if (error)
210 goto out;
211 i = 0;
212 spin_lock_irq(&logbuf_lock);
213 while (!error && (log_start != log_end) && i < len) {
214 c = LOG_BUF(log_start);
215 log_start++;
216 spin_unlock_irq(&logbuf_lock);
217 error = __put_user(c,buf);
218 buf++;
219 i++;
220 cond_resched();
221 spin_lock_irq(&logbuf_lock);
222 }
223 spin_unlock_irq(&logbuf_lock);
224 if (!error)
225 error = i;
226 break;
227 case 4: /* Read/clear last kernel messages */
40dc5651 228 do_clear = 1;
1da177e4
LT
229 /* FALL THRU */
230 case 3: /* Read last kernel messages */
231 error = -EINVAL;
232 if (!buf || len < 0)
233 goto out;
234 error = 0;
235 if (!len)
236 goto out;
237 if (!access_ok(VERIFY_WRITE, buf, len)) {
238 error = -EFAULT;
239 goto out;
240 }
241 count = len;
242 if (count > log_buf_len)
243 count = log_buf_len;
244 spin_lock_irq(&logbuf_lock);
245 if (count > logged_chars)
246 count = logged_chars;
247 if (do_clear)
248 logged_chars = 0;
249 limit = log_end;
250 /*
251 * __put_user() could sleep, and while we sleep
40dc5651 252 * printk() could overwrite the messages
1da177e4
LT
253 * we try to copy to user space. Therefore
254 * the messages are copied in reverse. <manfreds>
255 */
40dc5651 256 for (i = 0; i < count && !error; i++) {
1da177e4
LT
257 j = limit-1-i;
258 if (j + log_buf_len < log_end)
259 break;
260 c = LOG_BUF(j);
261 spin_unlock_irq(&logbuf_lock);
262 error = __put_user(c,&buf[count-1-i]);
263 cond_resched();
264 spin_lock_irq(&logbuf_lock);
265 }
266 spin_unlock_irq(&logbuf_lock);
267 if (error)
268 break;
269 error = i;
40dc5651 270 if (i != count) {
1da177e4
LT
271 int offset = count-error;
272 /* buffer overflow during copy, correct user buffer. */
40dc5651 273 for (i = 0; i < error; i++) {
1da177e4
LT
274 if (__get_user(c,&buf[i+offset]) ||
275 __put_user(c,&buf[i])) {
276 error = -EFAULT;
277 break;
278 }
279 cond_resched();
280 }
281 }
282 break;
283 case 5: /* Clear ring buffer */
284 logged_chars = 0;
285 break;
286 case 6: /* Disable logging to console */
287 console_loglevel = minimum_console_loglevel;
288 break;
289 case 7: /* Enable logging to console */
290 console_loglevel = default_console_loglevel;
291 break;
292 case 8: /* Set level of messages printed to console */
293 error = -EINVAL;
294 if (len < 1 || len > 8)
295 goto out;
296 if (len < minimum_console_loglevel)
297 len = minimum_console_loglevel;
298 console_loglevel = len;
299 error = 0;
300 break;
301 case 9: /* Number of chars in the log buffer */
302 error = log_end - log_start;
303 break;
304 case 10: /* Size of the log buffer */
305 error = log_buf_len;
306 break;
307 default:
308 error = -EINVAL;
309 break;
310 }
311out:
312 return error;
313}
314
40dc5651 315asmlinkage long sys_syslog(int type, char __user *buf, int len)
1da177e4
LT
316{
317 return do_syslog(type, buf, len);
318}
319
320/*
321 * Call the console drivers on a range of log_buf
322 */
323static void __call_console_drivers(unsigned long start, unsigned long end)
324{
325 struct console *con;
326
327 for (con = console_drivers; con; con = con->next) {
76a8ad29
ME
328 if ((con->flags & CON_ENABLED) && con->write &&
329 (cpu_online(smp_processor_id()) ||
330 (con->flags & CON_ANYTIME)))
1da177e4
LT
331 con->write(con, &LOG_BUF(start), end - start);
332 }
333}
334
79290822
IM
335static int __read_mostly ignore_loglevel;
336
99eea6a1 337static int __init ignore_loglevel_setup(char *str)
79290822
IM
338{
339 ignore_loglevel = 1;
340 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
341
342 return 1;
343}
344
345__setup("ignore_loglevel", ignore_loglevel_setup);
346
1da177e4
LT
347/*
348 * Write out chars from start to end - 1 inclusive
349 */
350static void _call_console_drivers(unsigned long start,
351 unsigned long end, int msg_log_level)
352{
79290822 353 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
1da177e4
LT
354 console_drivers && start != end) {
355 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
356 /* wrapped write */
357 __call_console_drivers(start & LOG_BUF_MASK,
358 log_buf_len);
359 __call_console_drivers(0, end & LOG_BUF_MASK);
360 } else {
361 __call_console_drivers(start, end);
362 }
363 }
364}
365
366/*
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.
370 */
371static void call_console_drivers(unsigned long start, unsigned long end)
372{
373 unsigned long cur_index, start_print;
374 static int msg_level = -1;
375
8abd8e29 376 BUG_ON(((long)(start - end)) > 0);
1da177e4
LT
377
378 cur_index = start;
379 start_print = start;
380 while (cur_index != end) {
40dc5651
JJ
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) == '>') {
1da177e4
LT
386 msg_level = LOG_BUF(cur_index + 1) - '0';
387 cur_index += 3;
388 start_print = cur_index;
389 }
390 while (cur_index != end) {
391 char c = LOG_BUF(cur_index);
1da177e4 392
40dc5651 393 cur_index++;
1da177e4
LT
394 if (c == '\n') {
395 if (msg_level < 0) {
396 /*
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
400 * on those tags
401 */
402 msg_level = default_message_loglevel;
403 }
404 _call_console_drivers(start_print, cur_index, msg_level);
405 msg_level = -1;
406 start_print = cur_index;
407 break;
408 }
409 }
410 }
411 _call_console_drivers(start_print, end, msg_level);
412}
413
414static void emit_log_char(char c)
415{
416 LOG_BUF(log_end) = c;
417 log_end++;
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)
423 logged_chars++;
424}
425
426/*
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
429 * full oops.
430 */
431static void zap_locks(void)
432{
433 static unsigned long oops_timestamp;
434
435 if (time_after_eq(jiffies, oops_timestamp) &&
40dc5651 436 !time_after(jiffies, oops_timestamp + 30 * HZ))
1da177e4
LT
437 return;
438
439 oops_timestamp = jiffies;
440
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);
445}
446
447#if defined(CONFIG_PRINTK_TIME)
448static int printk_time = 1;
449#else
450static int printk_time = 0;
451#endif
e84845c4 452module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
1da177e4
LT
453
454static int __init printk_time_setup(char *str)
455{
456 if (*str)
457 return 0;
458 printk_time = 1;
e84845c4
RD
459 printk(KERN_NOTICE "The 'time' option is deprecated and "
460 "is scheduled for removal in early 2008\n");
461 printk(KERN_NOTICE "Use 'printk.time=<value>' instead\n");
1da177e4
LT
462 return 1;
463}
464
465__setup("time", printk_time_setup);
466
31f6d9d6
AM
467__attribute__((weak)) unsigned long long printk_clock(void)
468{
469 return sched_clock();
470}
471
76a8ad29
ME
472/* Check if we have any console registered that can be called early in boot. */
473static int have_callable_console(void)
474{
475 struct console *con;
476
477 for (con = console_drivers; con; con = con->next)
478 if (con->flags & CON_ANYTIME)
479 return 1;
480
481 return 0;
482}
483
ddad86c2
MW
484/**
485 * printk - print a kernel message
486 * @fmt: format string
487 *
72fd4a35 488 * This is printk(). It can be called from any context. We want it to work.
40dc5651 489 *
1da177e4
LT
490 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
491 * call the console drivers. If we fail to get the semaphore we place the output
492 * into the log buffer and return. The current holder of the console_sem will
493 * notice the new output in release_console_sem() and will send it to the
494 * consoles before releasing the semaphore.
495 *
496 * One effect of this deferred printing is that code which calls printk() and
497 * then changes console_loglevel may break. This is because console_loglevel
498 * is inspected when the actual printing occurs.
ddad86c2
MW
499 *
500 * See also:
501 * printf(3)
1da177e4 502 */
d59745ce 503
1da177e4
LT
504asmlinkage int printk(const char *fmt, ...)
505{
506 va_list args;
507 int r;
508
509 va_start(args, fmt);
510 r = vprintk(fmt, args);
511 va_end(args);
512
513 return r;
514}
515
fe21773d
DH
516/* cpu currently holding logbuf_lock */
517static volatile unsigned int printk_cpu = UINT_MAX;
518
1da177e4
LT
519asmlinkage int vprintk(const char *fmt, va_list args)
520{
521 unsigned long flags;
522 int printed_len;
523 char *p;
524 static char printk_buf[1024];
525 static int log_level_unknown = 1;
526
fe21773d
DH
527 preempt_disable();
528 if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id())
529 /* If a crash is occurring during printk() on this CPU,
530 * make sure we can't deadlock */
1da177e4
LT
531 zap_locks();
532
533 /* This stops the holder of console_sem just where we want him */
1efc5da3 534 raw_local_irq_save(flags);
a0f1ccfd
IM
535 lockdep_off();
536 spin_lock(&logbuf_lock);
fe21773d 537 printk_cpu = smp_processor_id();
1da177e4
LT
538
539 /* Emit the output into the temporary buffer */
540 printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
541
542 /*
543 * Copy the output into log_buf. If the caller didn't provide
544 * appropriate log level tags, we insert them here
545 */
546 for (p = printk_buf; *p; p++) {
547 if (log_level_unknown) {
548 /* log_level_unknown signals the start of a new line */
549 if (printk_time) {
550 int loglev_char;
551 char tbuf[50], *tp;
552 unsigned tlen;
553 unsigned long long t;
554 unsigned long nanosec_rem;
555
556 /*
557 * force the log level token to be
558 * before the time output.
559 */
560 if (p[0] == '<' && p[1] >='0' &&
561 p[1] <= '7' && p[2] == '>') {
562 loglev_char = p[1];
563 p += 3;
025510cd 564 printed_len -= 3;
1da177e4
LT
565 } else {
566 loglev_char = default_message_loglevel
567 + '0';
568 }
31f6d9d6 569 t = printk_clock();
1da177e4
LT
570 nanosec_rem = do_div(t, 1000000000);
571 tlen = sprintf(tbuf,
572 "<%c>[%5lu.%06lu] ",
573 loglev_char,
574 (unsigned long)t,
575 nanosec_rem/1000);
576
577 for (tp = tbuf; tp < tbuf + tlen; tp++)
578 emit_log_char(*tp);
025510cd 579 printed_len += tlen;
1da177e4
LT
580 } else {
581 if (p[0] != '<' || p[1] < '0' ||
582 p[1] > '7' || p[2] != '>') {
583 emit_log_char('<');
584 emit_log_char(default_message_loglevel
585 + '0');
586 emit_log_char('>');
025510cd 587 printed_len += 3;
1da177e4 588 }
1da177e4
LT
589 }
590 log_level_unknown = 0;
591 if (!*p)
592 break;
593 }
594 emit_log_char(*p);
595 if (*p == '\n')
596 log_level_unknown = 1;
597 }
598
76a8ad29 599 if (!down_trylock(&console_sem)) {
1da177e4 600 /*
76a8ad29
ME
601 * We own the drivers. We can drop the spinlock and
602 * let release_console_sem() print the text, maybe ...
1da177e4 603 */
76a8ad29 604 console_locked = 1;
fe21773d 605 printk_cpu = UINT_MAX;
a0f1ccfd 606 spin_unlock(&logbuf_lock);
76a8ad29 607
1da177e4 608 /*
76a8ad29
ME
609 * Console drivers may assume that per-cpu resources have
610 * been allocated. So unless they're explicitly marked as
611 * being able to cope (CON_ANYTIME) don't call them until
612 * this CPU is officially up.
1da177e4 613 */
76a8ad29
ME
614 if (cpu_online(smp_processor_id()) || have_callable_console()) {
615 console_may_schedule = 0;
616 release_console_sem();
617 } else {
618 /* Release by hand to avoid flushing the buffer. */
619 console_locked = 0;
620 up(&console_sem);
621 }
a0f1ccfd 622 lockdep_on();
1efc5da3 623 raw_local_irq_restore(flags);
1da177e4
LT
624 } else {
625 /*
626 * Someone else owns the drivers. We drop the spinlock, which
627 * allows the semaphore holder to proceed and to call the
628 * console drivers with the output which we just produced.
629 */
fe21773d 630 printk_cpu = UINT_MAX;
a0f1ccfd
IM
631 spin_unlock(&logbuf_lock);
632 lockdep_on();
1efc5da3 633 raw_local_irq_restore(flags);
1da177e4 634 }
76a8ad29 635
fe21773d 636 preempt_enable();
1da177e4
LT
637 return printed_len;
638}
639EXPORT_SYMBOL(printk);
640EXPORT_SYMBOL(vprintk);
641
d59745ce
MM
642#else
643
40dc5651 644asmlinkage long sys_syslog(int type, char __user *buf, int len)
d59745ce 645{
c36264df 646 return -ENOSYS;
40dc5651
JJ
647}
648
649static void call_console_drivers(unsigned long start, unsigned long end)
650{
651}
d59745ce
MM
652
653#endif
654
2ea1c539
JB
655/*
656 * Set up a list of consoles. Called from init/main.c
657 */
658static int __init console_setup(char *str)
659{
eaa944af 660 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
2ea1c539
JB
661 char *s, *options;
662 int idx;
663
664 /*
665 * Decode str into name, index, options.
666 */
667 if (str[0] >= '0' && str[0] <= '9') {
eaa944af
YL
668 strcpy(buf, "ttyS");
669 strncpy(buf + 4, str, sizeof(buf) - 5);
2ea1c539 670 } else {
eaa944af 671 strncpy(buf, str, sizeof(buf) - 1);
2ea1c539 672 }
eaa944af 673 buf[sizeof(buf) - 1] = 0;
2ea1c539
JB
674 if ((options = strchr(str, ',')) != NULL)
675 *(options++) = 0;
676#ifdef __sparc__
677 if (!strcmp(str, "ttya"))
eaa944af 678 strcpy(buf, "ttyS0");
2ea1c539 679 if (!strcmp(str, "ttyb"))
eaa944af 680 strcpy(buf, "ttyS1");
2ea1c539 681#endif
eaa944af 682 for (s = buf; *s; s++)
2ea1c539
JB
683 if ((*s >= '0' && *s <= '9') || *s == ',')
684 break;
685 idx = simple_strtoul(s, NULL, 10);
686 *s = 0;
687
eaa944af 688 add_preferred_console(buf, idx, options);
2ea1c539
JB
689 return 1;
690}
691__setup("console=", console_setup);
692
3c0547ba
MM
693/**
694 * add_preferred_console - add a device to the list of preferred consoles.
ddad86c2
MW
695 * @name: device name
696 * @idx: device index
697 * @options: options for this console
3c0547ba
MM
698 *
699 * The last preferred console added will be used for kernel messages
700 * and stdin/out/err for init. Normally this is used by console_setup
701 * above to handle user-supplied console arguments; however it can also
702 * be used by arch-specific code either to override the user or more
703 * commonly to provide a default console (ie from PROM variables) when
704 * the user has not supplied one.
705 */
706int __init add_preferred_console(char *name, int idx, char *options)
707{
708 struct console_cmdline *c;
709 int i;
710
711 /*
712 * See if this tty is not yet registered, and
713 * if we have a slot free.
714 */
eaa944af 715 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
3c0547ba
MM
716 if (strcmp(console_cmdline[i].name, name) == 0 &&
717 console_cmdline[i].index == idx) {
718 selected_console = i;
719 return 0;
720 }
721 if (i == MAX_CMDLINECONSOLES)
722 return -E2BIG;
723 selected_console = i;
724 c = &console_cmdline[i];
725 memcpy(c->name, name, sizeof(c->name));
726 c->name[sizeof(c->name) - 1] = 0;
727 c->options = options;
728 c->index = idx;
729 return 0;
730}
731
18a8bd94
YL
732int __init update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
733{
734 struct console_cmdline *c;
735 int i;
736
737 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
738 if (strcmp(console_cmdline[i].name, name) == 0 &&
739 console_cmdline[i].index == idx) {
740 c = &console_cmdline[i];
741 memcpy(c->name, name_new, sizeof(c->name));
742 c->name[sizeof(c->name) - 1] = 0;
743 c->options = options;
744 c->index = idx_new;
745 return i;
746 }
747 /* not found */
748 return -1;
749}
750
c8eb8b40 751#ifndef CONFIG_DISABLE_CONSOLE_SUSPEND
557240b4
LT
752/**
753 * suspend_console - suspend the console subsystem
754 *
755 * This disables printk() while we go into suspend states
756 */
757void suspend_console(void)
758{
c8eb8b40 759 printk("Suspending console(s)\n");
557240b4
LT
760 acquire_console_sem();
761 console_suspended = 1;
762}
763
764void resume_console(void)
765{
766 console_suspended = 0;
767 release_console_sem();
768}
c8eb8b40 769#endif /* CONFIG_DISABLE_CONSOLE_SUSPEND */
557240b4 770
1da177e4
LT
771/**
772 * acquire_console_sem - lock the console system for exclusive use.
773 *
774 * Acquires a semaphore which guarantees that the caller has
775 * exclusive access to the console system and the console_drivers list.
776 *
777 * Can sleep, returns nothing.
778 */
779void acquire_console_sem(void)
780{
8abd8e29 781 BUG_ON(in_interrupt());
557240b4
LT
782 if (console_suspended) {
783 down(&secondary_console_sem);
784 return;
785 }
1da177e4
LT
786 down(&console_sem);
787 console_locked = 1;
788 console_may_schedule = 1;
789}
790EXPORT_SYMBOL(acquire_console_sem);
791
792int try_acquire_console_sem(void)
793{
794 if (down_trylock(&console_sem))
795 return -1;
796 console_locked = 1;
797 console_may_schedule = 0;
798 return 0;
799}
800EXPORT_SYMBOL(try_acquire_console_sem);
801
802int is_console_locked(void)
803{
804 return console_locked;
805}
1da177e4 806
e3e8a75d
KK
807void wake_up_klogd(void)
808{
809 if (!oops_in_progress && waitqueue_active(&log_wait))
810 wake_up_interruptible(&log_wait);
811}
812
1da177e4
LT
813/**
814 * release_console_sem - unlock the console system
815 *
816 * Releases the semaphore which the caller holds on the console system
817 * and the console driver list.
818 *
819 * While the semaphore was held, console output may have been buffered
820 * by printk(). If this is the case, release_console_sem() emits
821 * the output prior to releasing the semaphore.
822 *
823 * If there is output waiting for klogd, we wake it up.
824 *
825 * release_console_sem() may be called from any context.
826 */
827void release_console_sem(void)
828{
829 unsigned long flags;
830 unsigned long _con_start, _log_end;
831 unsigned long wake_klogd = 0;
832
557240b4
LT
833 if (console_suspended) {
834 up(&secondary_console_sem);
835 return;
836 }
78944e54
AD
837
838 console_may_schedule = 0;
839
1da177e4
LT
840 for ( ; ; ) {
841 spin_lock_irqsave(&logbuf_lock, flags);
842 wake_klogd |= log_start - log_end;
843 if (con_start == log_end)
844 break; /* Nothing to print */
845 _con_start = con_start;
846 _log_end = log_end;
847 con_start = log_end; /* Flush */
848 spin_unlock(&logbuf_lock);
849 call_console_drivers(_con_start, _log_end);
850 local_irq_restore(flags);
851 }
852 console_locked = 0;
1da177e4
LT
853 up(&console_sem);
854 spin_unlock_irqrestore(&logbuf_lock, flags);
e3e8a75d
KK
855 if (wake_klogd)
856 wake_up_klogd();
1da177e4
LT
857}
858EXPORT_SYMBOL(release_console_sem);
859
ddad86c2
MW
860/**
861 * console_conditional_schedule - yield the CPU if required
1da177e4
LT
862 *
863 * If the console code is currently allowed to sleep, and
864 * if this CPU should yield the CPU to another task, do
865 * so here.
866 *
867 * Must be called within acquire_console_sem().
868 */
869void __sched console_conditional_schedule(void)
870{
871 if (console_may_schedule)
872 cond_resched();
873}
874EXPORT_SYMBOL(console_conditional_schedule);
875
876void console_print(const char *s)
877{
878 printk(KERN_EMERG "%s", s);
879}
880EXPORT_SYMBOL(console_print);
881
882void console_unblank(void)
883{
884 struct console *c;
885
886 /*
887 * console_unblank can no longer be called in interrupt context unless
888 * oops_in_progress is set to 1..
889 */
890 if (oops_in_progress) {
891 if (down_trylock(&console_sem) != 0)
892 return;
893 } else
894 acquire_console_sem();
895
896 console_locked = 1;
897 console_may_schedule = 0;
898 for (c = console_drivers; c != NULL; c = c->next)
899 if ((c->flags & CON_ENABLED) && c->unblank)
900 c->unblank();
901 release_console_sem();
902}
1da177e4
LT
903
904/*
905 * Return the console tty driver structure and its associated index
906 */
907struct tty_driver *console_device(int *index)
908{
909 struct console *c;
910 struct tty_driver *driver = NULL;
911
912 acquire_console_sem();
913 for (c = console_drivers; c != NULL; c = c->next) {
914 if (!c->device)
915 continue;
916 driver = c->device(c, index);
917 if (driver)
918 break;
919 }
920 release_console_sem();
921 return driver;
922}
923
924/*
925 * Prevent further output on the passed console device so that (for example)
926 * serial drivers can disable console output before suspending a port, and can
927 * re-enable output afterwards.
928 */
929void console_stop(struct console *console)
930{
931 acquire_console_sem();
932 console->flags &= ~CON_ENABLED;
933 release_console_sem();
934}
935EXPORT_SYMBOL(console_stop);
936
937void console_start(struct console *console)
938{
939 acquire_console_sem();
940 console->flags |= CON_ENABLED;
941 release_console_sem();
942}
943EXPORT_SYMBOL(console_start);
944
945/*
946 * The console driver calls this routine during kernel initialization
947 * to register the console printing procedure with printk() and to
948 * print any messages that were printed by the kernel before the
949 * console driver was initialized.
950 */
40dc5651 951void register_console(struct console *console)
1da177e4 952{
40dc5651 953 int i;
1da177e4 954 unsigned long flags;
69331af7 955 struct console *bootconsole = NULL;
1da177e4 956
69331af7
GH
957 if (console_drivers) {
958 if (console->flags & CON_BOOT)
959 return;
960 if (console_drivers->flags & CON_BOOT)
961 bootconsole = console_drivers;
962 }
963
964 if (preferred_console < 0 || bootconsole || !console_drivers)
1da177e4
LT
965 preferred_console = selected_console;
966
18a8bd94
YL
967 if (console->early_setup)
968 console->early_setup();
969
1da177e4
LT
970 /*
971 * See if we want to use this console driver. If we
972 * didn't select a console we take the first one
973 * that registers here.
974 */
975 if (preferred_console < 0) {
976 if (console->index < 0)
977 console->index = 0;
978 if (console->setup == NULL ||
979 console->setup(console, NULL) == 0) {
980 console->flags |= CON_ENABLED | CON_CONSDEV;
981 preferred_console = 0;
982 }
983 }
984
985 /*
986 * See if this console matches one we selected on
987 * the command line.
988 */
40dc5651
JJ
989 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
990 i++) {
1da177e4
LT
991 if (strcmp(console_cmdline[i].name, console->name) != 0)
992 continue;
993 if (console->index >= 0 &&
994 console->index != console_cmdline[i].index)
995 continue;
996 if (console->index < 0)
997 console->index = console_cmdline[i].index;
998 if (console->setup &&
999 console->setup(console, console_cmdline[i].options) != 0)
1000 break;
1001 console->flags |= CON_ENABLED;
1002 console->index = console_cmdline[i].index;
ab4af03a 1003 if (i == selected_console) {
1da177e4 1004 console->flags |= CON_CONSDEV;
ab4af03a
GE
1005 preferred_console = selected_console;
1006 }
1da177e4
LT
1007 break;
1008 }
1009
1010 if (!(console->flags & CON_ENABLED))
1011 return;
1012
d37bf60d 1013 if (bootconsole && (console->flags & CON_CONSDEV)) {
69331af7
GH
1014 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1015 bootconsole->name, bootconsole->index,
1016 console->name, console->index);
1017 unregister_console(bootconsole);
1da177e4 1018 console->flags &= ~CON_PRINTBUFFER;
d37bf60d
YL
1019 } else {
1020 printk(KERN_INFO "console [%s%d] enabled\n",
1021 console->name, console->index);
1da177e4
LT
1022 }
1023
1024 /*
1025 * Put this console in the list - keep the
1026 * preferred driver at the head of the list.
1027 */
1028 acquire_console_sem();
1029 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1030 console->next = console_drivers;
1031 console_drivers = console;
ab4af03a
GE
1032 if (console->next)
1033 console->next->flags &= ~CON_CONSDEV;
1da177e4
LT
1034 } else {
1035 console->next = console_drivers->next;
1036 console_drivers->next = console;
1037 }
1038 if (console->flags & CON_PRINTBUFFER) {
1039 /*
1040 * release_console_sem() will print out the buffered messages
1041 * for us.
1042 */
1043 spin_lock_irqsave(&logbuf_lock, flags);
1044 con_start = log_start;
1045 spin_unlock_irqrestore(&logbuf_lock, flags);
1046 }
1047 release_console_sem();
1048}
1049EXPORT_SYMBOL(register_console);
1050
40dc5651 1051int unregister_console(struct console *console)
1da177e4 1052{
40dc5651 1053 struct console *a, *b;
1da177e4
LT
1054 int res = 1;
1055
1056 acquire_console_sem();
1057 if (console_drivers == console) {
1058 console_drivers=console->next;
1059 res = 0;
e9b15b54 1060 } else if (console_drivers) {
1da177e4
LT
1061 for (a=console_drivers->next, b=console_drivers ;
1062 a; b=a, a=b->next) {
1063 if (a == console) {
1064 b->next = a->next;
1065 res = 0;
1066 break;
40dc5651 1067 }
1da177e4
LT
1068 }
1069 }
40dc5651 1070
69331af7 1071 /*
ab4af03a
GE
1072 * If this isn't the last console and it has CON_CONSDEV set, we
1073 * need to set it on the next preferred console.
1da177e4 1074 */
69331af7 1075 if (console_drivers != NULL && console->flags & CON_CONSDEV)
ab4af03a 1076 console_drivers->flags |= CON_CONSDEV;
1da177e4
LT
1077
1078 release_console_sem();
1079 return res;
1080}
1081EXPORT_SYMBOL(unregister_console);
d59745ce 1082
1da177e4
LT
1083/**
1084 * tty_write_message - write a message to a certain tty, not just the console.
ddad86c2
MW
1085 * @tty: the destination tty_struct
1086 * @msg: the message to write
1da177e4
LT
1087 *
1088 * This is used for messages that need to be redirected to a specific tty.
1089 * We don't put it into the syslog queue right now maybe in the future if
1090 * really needed.
1091 */
1092void tty_write_message(struct tty_struct *tty, char *msg)
1093{
1094 if (tty && tty->driver->write)
1095 tty->driver->write(tty, msg, strlen(msg));
1096 return;
1097}
1098
1099/*
1100 * printk rate limiting, lifted from the networking subsystem.
1101 *
1102 * This enforces a rate limit: not more than one kernel message
1103 * every printk_ratelimit_jiffies to make a denial-of-service
1104 * attack impossible.
1105 */
1106int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1107{
1108 static DEFINE_SPINLOCK(ratelimit_lock);
40dc5651 1109 static unsigned long toks = 10 * 5 * HZ;
1da177e4
LT
1110 static unsigned long last_msg;
1111 static int missed;
1112 unsigned long flags;
1113 unsigned long now = jiffies;
1114
1115 spin_lock_irqsave(&ratelimit_lock, flags);
1116 toks += now - last_msg;
1117 last_msg = now;
1118 if (toks > (ratelimit_burst * ratelimit_jiffies))
1119 toks = ratelimit_burst * ratelimit_jiffies;
1120 if (toks >= ratelimit_jiffies) {
1121 int lost = missed;
40dc5651 1122
1da177e4
LT
1123 missed = 0;
1124 toks -= ratelimit_jiffies;
1125 spin_unlock_irqrestore(&ratelimit_lock, flags);
1126 if (lost)
1127 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1128 return 1;
1129 }
1130 missed++;
1131 spin_unlock_irqrestore(&ratelimit_lock, flags);
1132 return 0;
1133}
1134EXPORT_SYMBOL(__printk_ratelimit);
1135
1136/* minimum time in jiffies between messages */
40dc5651 1137int printk_ratelimit_jiffies = 5 * HZ;
1da177e4
LT
1138
1139/* number of messages we send before ratelimiting */
1140int printk_ratelimit_burst = 10;
1141
1142int printk_ratelimit(void)
1143{
1144 return __printk_ratelimit(printk_ratelimit_jiffies,
1145 printk_ratelimit_burst);
1146}
1147EXPORT_SYMBOL(printk_ratelimit);
f46c4833
AM
1148
1149/**
1150 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1151 * @caller_jiffies: pointer to caller's state
1152 * @interval_msecs: minimum interval between prints
1153 *
1154 * printk_timed_ratelimit() returns true if more than @interval_msecs
1155 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1156 * returned true.
1157 */
1158bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1159 unsigned int interval_msecs)
1160{
1161 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1162 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1163 return true;
1164 }
1165 return false;
1166}
1167EXPORT_SYMBOL(printk_timed_ratelimit);
This page took 0.312898 seconds and 5 git commands to generate.