blob: 423a8c765a5788fc1b422d87add42d8649ec9d31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Jesper Juhl40dc5652005-10-30 15:02:46 -080013 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
Christian Kujau624dffc2006-01-15 02:43:54 +010014 * manfred@colorfullife.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/console.h>
24#include <linux/init.h>
Randy Dunlapbfe8df32007-10-16 01:23:46 -070025#include <linux/jiffies.h>
26#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Jan Engelhardt3b9c0412006-06-25 05:48:15 -070028#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h> /* For in_interrupt() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
31#include <linux/smp.h>
32#include <linux/security.h>
33#include <linux/bootmem.h>
34#include <linux/syscalls.h>
Andrew Mortonf46c4832006-11-02 22:07:16 -080035#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/uaccess.h>
38
39#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
40
41/* printk's without a loglevel use this.. */
42#define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
43
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 */
47
48DECLARE_WAIT_QUEUE_HEAD(log_wait);
49
50int 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 */
55};
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010058 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * their unblank() callback or not. So let's export it.
60 */
61int oops_in_progress;
62EXPORT_SYMBOL(oops_in_progress);
63
64/*
65 * console_sem protects the console_drivers list, and also
66 * provides serialisation for access to the entire console
67 * driver system.
68 */
69static DECLARE_MUTEX(console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -070070static DECLARE_MUTEX(secondary_console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct console *console_drivers;
72/*
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
79 */
Linus Torvalds557240b2006-06-19 18:16:01 -070080static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
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().
86 */
87static DEFINE_SPINLOCK(logbuf_lock);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define LOG_BUF_MASK (log_buf_len-1)
90#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
91
92/*
93 * The indices into log_buf are not constrained to log_buf_len - they
94 * must be masked before subscripting
95 */
96static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
97static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
98static unsigned long log_end; /* Index into log_buf: most-recently-written-char + 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
101 * Array of consoles built from command line options (console=)
102 */
103struct console_cmdline
104{
105 char name[8]; /* Name of the driver */
106 int index; /* Minor dev. to use */
107 char *options; /* Options for the driver */
108};
109
110#define MAX_CMDLINECONSOLES 8
111
112static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
113static int selected_console = -1;
114static int preferred_console = -1;
115
116/* Flag: console code may call schedule() */
117static int console_may_schedule;
118
Matt Mackalld59745c2005-05-01 08:59:02 -0700119#ifdef CONFIG_PRINTK
120
121static char __log_buf[__LOG_BUF_LEN];
122static char *log_buf = __log_buf;
123static int log_buf_len = __LOG_BUF_LEN;
124static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static int __init log_buf_len_setup(char *str)
127{
128 unsigned long size = memparse(str, &str);
129 unsigned long flags;
130
131 if (size)
132 size = roundup_pow_of_two(size);
133 if (size > log_buf_len) {
134 unsigned long start, dest_idx, offset;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800135 char *new_log_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 new_log_buf = alloc_bootmem(size);
138 if (!new_log_buf) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800139 printk(KERN_WARNING "log_buf_len: allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto out;
141 }
142
143 spin_lock_irqsave(&logbuf_lock, flags);
144 log_buf_len = size;
145 log_buf = new_log_buf;
146
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++;
153 }
154 log_start -= offset;
155 con_start -= offset;
156 log_end -= offset;
157 spin_unlock_irqrestore(&logbuf_lock, flags);
158
Jesper Juhl40dc5652005-10-30 15:02:46 -0800159 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return 1;
163}
164
165__setup("log_buf_len=", log_buf_len_setup);
166
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700167#ifdef CONFIG_BOOT_PRINTK_DELAY
168
169static unsigned int boot_delay; /* msecs delay after each printk during bootup */
170static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
171
172static int __init boot_delay_setup(char *str)
173{
174 unsigned long lpj;
175 unsigned long long loops_per_msec;
176
177 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
178 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
179
180 get_option(&str, &boot_delay);
181 if (boot_delay > 10 * 1000)
182 boot_delay = 0;
183
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;
189}
190__setup("boot_delay=", boot_delay_setup);
191
192static void boot_delay_msec(void)
193{
194 unsigned long long k;
195 unsigned long timeout;
196
197 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
198 return;
199
200 k = (unsigned long long)printk_delay_msec * boot_delay;
201
202 timeout = jiffies + msecs_to_jiffies(boot_delay);
203 while (k) {
204 k--;
205 cpu_relax();
206 /*
207 * use (volatile) jiffies to prevent
208 * compiler reduction; loop termination via jiffies
209 * is secondary and may or may not happen.
210 */
211 if (time_after(jiffies, timeout))
212 break;
213 touch_nmi_watchdog();
214 }
215}
216#else
217static inline void boot_delay_msec(void)
218{
219}
220#endif
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
Mike Frysinger0b15d042007-10-16 23:26:43 -0700223 * Return the number of unread characters in the log buffer.
224 */
225int log_buf_get_len(void)
226{
227 return logged_chars;
228}
229
230/*
231 * Copy a range of characters from the log buffer.
232 */
233int log_buf_copy(char *dest, int idx, int len)
234{
235 int ret, max;
236 bool took_lock = false;
237
238 if (!oops_in_progress) {
239 spin_lock_irq(&logbuf_lock);
240 took_lock = true;
241 }
242
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);
253 }
254
255 if (took_lock)
256 spin_unlock_irq(&logbuf_lock);
257
258 return ret;
259}
260
261/*
262 * Extract a single character from the log buffer.
263 */
264int log_buf_read(int idx)
265{
266 char ret;
267
268 if (log_buf_copy(&ret, idx, 1) == 1)
269 return ret;
270 else
271 return -1;
272}
273
274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Commands to do_syslog:
276 *
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
288 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800289int do_syslog(int type, char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 unsigned long i, j, limit, count;
292 int do_clear = 0;
293 char c;
294 int error = 0;
295
296 error = security_syslog(type);
297 if (error)
298 return error;
299
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;
315 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800316 error = wait_event_interruptible(log_wait,
317 (log_start - log_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 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);
331 }
332 spin_unlock_irq(&logbuf_lock);
333 if (!error)
334 error = i;
335 break;
336 case 4: /* Read/clear last kernel messages */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800337 do_clear = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* 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;
349 }
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;
359 /*
360 * __put_user() could sleep, and while we sleep
Jesper Juhl40dc5652005-10-30 15:02:46 -0800361 * printk() could overwrite the messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * we try to copy to user space. Therefore
363 * the messages are copied in reverse. <manfreds>
364 */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800365 for (i = 0; i < count && !error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 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);
374 }
375 spin_unlock_irq(&logbuf_lock);
376 if (error)
377 break;
378 error = i;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800379 if (i != count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int offset = count-error;
381 /* buffer overflow during copy, correct user buffer. */
Jesper Juhl40dc5652005-10-30 15:02:46 -0800382 for (i = 0; i < error; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (__get_user(c,&buf[i+offset]) ||
384 __put_user(c,&buf[i])) {
385 error = -EFAULT;
386 break;
387 }
388 cond_resched();
389 }
390 }
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;
419 }
420out:
421 return error;
422}
423
Jesper Juhl40dc5652005-10-30 15:02:46 -0800424asmlinkage long sys_syslog(int type, char __user *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 return do_syslog(type, buf, len);
427}
428
429/*
430 * Call the console drivers on a range of log_buf
431 */
432static void __call_console_drivers(unsigned long start, unsigned long end)
433{
434 struct console *con;
435
436 for (con = console_drivers; con; con = con->next) {
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700437 if ((con->flags & CON_ENABLED) && con->write &&
438 (cpu_online(smp_processor_id()) ||
439 (con->flags & CON_ANYTIME)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 con->write(con, &LOG_BUF(start), end - start);
441 }
442}
443
Ingo Molnar792908222006-12-06 20:40:51 -0800444static int __read_mostly ignore_loglevel;
445
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800446static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800447{
448 ignore_loglevel = 1;
449 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
450
451 return 1;
452}
453
454__setup("ignore_loglevel", ignore_loglevel_setup);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*
457 * Write out chars from start to end - 1 inclusive
458 */
459static void _call_console_drivers(unsigned long start,
460 unsigned long end, int msg_log_level)
461{
Ingo Molnar792908222006-12-06 20:40:51 -0800462 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 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);
471 }
472 }
473}
474
475/*
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.
479 */
480static void call_console_drivers(unsigned long start, unsigned long end)
481{
482 unsigned long cur_index, start_print;
483 static int msg_level = -1;
484
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +0200485 BUG_ON(((long)(start - end)) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 cur_index = start;
488 start_print = start;
489 while (cur_index != end) {
Jesper Juhl40dc5652005-10-30 15:02:46 -0800490 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) == '>') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 msg_level = LOG_BUF(cur_index + 1) - '0';
496 cur_index += 3;
497 start_print = cur_index;
498 }
499 while (cur_index != end) {
500 char c = LOG_BUF(cur_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Jesper Juhl40dc5652005-10-30 15:02:46 -0800502 cur_index++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (c == '\n') {
504 if (msg_level < 0) {
505 /*
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
510 */
511 msg_level = default_message_loglevel;
512 }
513 _call_console_drivers(start_print, cur_index, msg_level);
514 msg_level = -1;
515 start_print = cur_index;
516 break;
517 }
518 }
519 }
520 _call_console_drivers(start_print, end, msg_level);
521}
522
523static void emit_log_char(char c)
524{
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++;
533}
534
535/*
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.
539 */
540static void zap_locks(void)
541{
542 static unsigned long oops_timestamp;
543
544 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800545 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return;
547
548 oops_timestamp = jiffies;
549
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);
554}
555
556#if defined(CONFIG_PRINTK_TIME)
557static int printk_time = 1;
558#else
559static int printk_time = 0;
560#endif
Randy Dunlape84845c2007-07-15 23:40:25 -0700561module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563static int __init printk_time_setup(char *str)
564{
565 if (*str)
566 return 0;
567 printk_time = 1;
Randy Dunlape84845c2007-07-15 23:40:25 -0700568 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return 1;
572}
573
574__setup("time", printk_time_setup);
575
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700576/* Check if we have any console registered that can be called early in boot. */
577static int have_callable_console(void)
578{
579 struct console *con;
580
581 for (con = console_drivers; con; con = con->next)
582 if (con->flags & CON_ANYTIME)
583 return 1;
584
585 return 0;
586}
587
Martin Waitzddad86c2005-11-13 16:08:14 -0800588/**
589 * printk - print a kernel message
590 * @fmt: format string
591 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800592 * This is printk(). It can be called from any context. We want it to work.
Jiri Kosina14921922007-07-15 23:41:51 -0700593 * Be aware of the fact that if oops_in_progress is not set, we might try to
594 * wake klogd up which could deadlock on runqueue lock if printk() is called
595 * from scheduler code.
Jesper Juhl40dc5652005-10-30 15:02:46 -0800596 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
598 * call the console drivers. If we fail to get the semaphore we place the output
599 * into the log buffer and return. The current holder of the console_sem will
600 * notice the new output in release_console_sem() and will send it to the
601 * consoles before releasing the semaphore.
602 *
603 * One effect of this deferred printing is that code which calls printk() and
604 * then changes console_loglevel may break. This is because console_loglevel
605 * is inspected when the actual printing occurs.
Martin Waitzddad86c2005-11-13 16:08:14 -0800606 *
607 * See also:
608 * printf(3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611asmlinkage int printk(const char *fmt, ...)
612{
613 va_list args;
614 int r;
615
616 va_start(args, fmt);
617 r = vprintk(fmt, args);
618 va_end(args);
619
620 return r;
621}
622
David Howellsfe217732005-09-06 15:16:34 -0700623/* cpu currently holding logbuf_lock */
624static volatile unsigned int printk_cpu = UINT_MAX;
625
Ingo Molnar32a76002008-01-25 21:07:58 +0100626const char printk_recursion_bug_msg [] =
627 KERN_CRIT "BUG: recent printk recursion!\n";
628static int printk_recursion_bug;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630asmlinkage int vprintk(const char *fmt, va_list args)
631{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 static int log_level_unknown = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100633 static char printk_buf[1024];
634
635 unsigned long flags;
636 int printed_len = 0;
637 int this_cpu;
638 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700640 boot_delay_msec();
641
David Howellsfe217732005-09-06 15:16:34 -0700642 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* This stops the holder of console_sem just where we want him */
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800644 raw_local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100645 this_cpu = smp_processor_id();
646
647 /*
648 * Ouch, printk recursed into itself!
649 */
650 if (unlikely(printk_cpu == this_cpu)) {
651 /*
652 * If a crash is occurring during printk() on this CPU,
653 * then try to get the crash message out but make sure
654 * we can't deadlock. Otherwise just return to avoid the
655 * recursion and return - but flag the recursion so that
656 * it can be printed at the next appropriate moment:
657 */
658 if (!oops_in_progress) {
659 printk_recursion_bug = 1;
660 goto out_restore_irqs;
661 }
662 zap_locks();
663 }
664
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700665 lockdep_off();
666 spin_lock(&logbuf_lock);
Ingo Molnar32a76002008-01-25 21:07:58 +0100667 printk_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Ingo Molnar32a76002008-01-25 21:07:58 +0100669 if (printk_recursion_bug) {
670 printk_recursion_bug = 0;
671 strcpy(printk_buf, printk_recursion_bug_msg);
672 printed_len = sizeof(printk_recursion_bug_msg);
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* Emit the output into the temporary buffer */
Ingo Molnar32a76002008-01-25 21:07:58 +0100675 printed_len += vscnprintf(printk_buf + printed_len,
676 sizeof(printk_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /*
679 * Copy the output into log_buf. If the caller didn't provide
680 * appropriate log level tags, we insert them here
681 */
682 for (p = printk_buf; *p; p++) {
683 if (log_level_unknown) {
684 /* log_level_unknown signals the start of a new line */
685 if (printk_time) {
686 int loglev_char;
687 char tbuf[50], *tp;
688 unsigned tlen;
689 unsigned long long t;
690 unsigned long nanosec_rem;
691
692 /*
693 * force the log level token to be
694 * before the time output.
695 */
696 if (p[0] == '<' && p[1] >='0' &&
697 p[1] <= '7' && p[2] == '>') {
698 loglev_char = p[1];
699 p += 3;
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800700 printed_len -= 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 } else {
702 loglev_char = default_message_loglevel
703 + '0';
704 }
Ingo Molnar19ef9302008-01-25 21:08:34 +0100705 t = 0;
706 if (system_state != SYSTEM_BOOTING)
707 t = ktime_to_ns(ktime_get());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 nanosec_rem = do_div(t, 1000000000);
709 tlen = sprintf(tbuf,
710 "<%c>[%5lu.%06lu] ",
711 loglev_char,
712 (unsigned long)t,
713 nanosec_rem/1000);
714
715 for (tp = tbuf; tp < tbuf + tlen; tp++)
716 emit_log_char(*tp);
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800717 printed_len += tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } else {
719 if (p[0] != '<' || p[1] < '0' ||
720 p[1] > '7' || p[2] != '>') {
721 emit_log_char('<');
722 emit_log_char(default_message_loglevel
723 + '0');
724 emit_log_char('>');
Guillaume Chazarain025510c2006-01-08 01:02:41 -0800725 printed_len += 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728 log_level_unknown = 0;
729 if (!*p)
730 break;
731 }
732 emit_log_char(*p);
733 if (*p == '\n')
734 log_level_unknown = 1;
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!down_trylock(&console_sem)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /*
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700739 * We own the drivers. We can drop the spinlock and
740 * let release_console_sem() print the text, maybe ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700742 console_locked = 1;
David Howellsfe217732005-09-06 15:16:34 -0700743 printk_cpu = UINT_MAX;
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700744 spin_unlock(&logbuf_lock);
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700745
746 /*
747 * Console drivers may assume that per-cpu resources have
748 * been allocated. So unless they're explicitly marked as
749 * being able to cope (CON_ANYTIME) don't call them until
750 * this CPU is officially up.
751 */
752 if (cpu_online(smp_processor_id()) || have_callable_console()) {
753 console_may_schedule = 0;
754 release_console_sem();
755 } else {
756 /* Release by hand to avoid flushing the buffer. */
757 console_locked = 0;
758 up(&console_sem);
759 }
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700760 lockdep_on();
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800761 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 } else {
763 /*
764 * Someone else owns the drivers. We drop the spinlock, which
765 * allows the semaphore holder to proceed and to call the
766 * console drivers with the output which we just produced.
767 */
David Howellsfe217732005-09-06 15:16:34 -0700768 printk_cpu = UINT_MAX;
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700769 spin_unlock(&logbuf_lock);
770 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +0100771out_restore_irqs:
Mathieu Desnoyers1efc5da2007-02-10 01:46:29 -0800772 raw_local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700774
David Howellsfe217732005-09-06 15:16:34 -0700775 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return printed_len;
777}
778EXPORT_SYMBOL(printk);
779EXPORT_SYMBOL(vprintk);
780
Matt Mackalld59745c2005-05-01 08:59:02 -0700781#else
782
Jesper Juhl40dc5652005-10-30 15:02:46 -0800783asmlinkage long sys_syslog(int type, char __user *buf, int len)
Matt Mackalld59745c2005-05-01 08:59:02 -0700784{
Mike Galbraithc36264d2006-12-06 20:37:42 -0800785 return -ENOSYS;
Jesper Juhl40dc5652005-10-30 15:02:46 -0800786}
787
788static void call_console_drivers(unsigned long start, unsigned long end)
789{
790}
Matt Mackalld59745c2005-05-01 08:59:02 -0700791
792#endif
793
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800794/*
795 * Set up a list of consoles. Called from init/main.c
796 */
797static int __init console_setup(char *str)
798{
Yinghai Lueaa944a2007-07-15 23:37:27 -0700799 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800800 char *s, *options;
801 int idx;
802
803 /*
804 * Decode str into name, index, options.
805 */
806 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700807 strcpy(buf, "ttyS");
808 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800809 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -0700810 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800811 }
Yinghai Lueaa944a2007-07-15 23:37:27 -0700812 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800813 if ((options = strchr(str, ',')) != NULL)
814 *(options++) = 0;
815#ifdef __sparc__
816 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700817 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800818 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -0700819 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800820#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -0700821 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800822 if ((*s >= '0' && *s <= '9') || *s == ',')
823 break;
824 idx = simple_strtoul(s, NULL, 10);
825 *s = 0;
826
Yinghai Lueaa944a2007-07-15 23:37:27 -0700827 add_preferred_console(buf, idx, options);
John Z. Bohach2ea1c532006-03-24 03:18:19 -0800828 return 1;
829}
830__setup("console=", console_setup);
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832/**
Matt Mackall3c0547b2005-05-16 21:53:47 -0700833 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -0800834 * @name: device name
835 * @idx: device index
836 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -0700837 *
838 * The last preferred console added will be used for kernel messages
839 * and stdin/out/err for init. Normally this is used by console_setup
840 * above to handle user-supplied console arguments; however it can also
841 * be used by arch-specific code either to override the user or more
842 * commonly to provide a default console (ie from PROM variables) when
843 * the user has not supplied one.
844 */
David S. Millerfb445ee2007-12-29 01:19:49 -0800845int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -0700846{
847 struct console_cmdline *c;
848 int i;
849
850 /*
851 * See if this tty is not yet registered, and
852 * if we have a slot free.
853 */
Yinghai Lueaa944a2007-07-15 23:37:27 -0700854 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
Matt Mackall3c0547b2005-05-16 21:53:47 -0700855 if (strcmp(console_cmdline[i].name, name) == 0 &&
856 console_cmdline[i].index == idx) {
857 selected_console = i;
858 return 0;
859 }
860 if (i == MAX_CMDLINECONSOLES)
861 return -E2BIG;
862 selected_console = i;
863 c = &console_cmdline[i];
864 memcpy(c->name, name, sizeof(c->name));
865 c->name[sizeof(c->name) - 1] = 0;
866 c->options = options;
867 c->index = idx;
868 return 0;
869}
870
Daniel Ritzb6b1d872007-08-03 16:07:43 +0200871int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700872{
873 struct console_cmdline *c;
874 int i;
875
876 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
877 if (strcmp(console_cmdline[i].name, name) == 0 &&
878 console_cmdline[i].index == idx) {
879 c = &console_cmdline[i];
880 memcpy(c->name, name_new, sizeof(c->name));
881 c->name[sizeof(c->name) - 1] = 0;
882 c->options = options;
883 c->index = idx_new;
884 return i;
885 }
886 /* not found */
887 return -1;
888}
889
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700890int console_suspend_enabled = 1;
891EXPORT_SYMBOL(console_suspend_enabled);
892
893static int __init console_suspend_disable(char *str)
894{
895 console_suspend_enabled = 0;
896 return 1;
897}
898__setup("no_console_suspend", console_suspend_disable);
899
Matt Mackall3c0547b2005-05-16 21:53:47 -0700900/**
Linus Torvalds557240b2006-06-19 18:16:01 -0700901 * suspend_console - suspend the console subsystem
902 *
903 * This disables printk() while we go into suspend states
904 */
905void suspend_console(void)
906{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700907 if (!console_suspend_enabled)
908 return;
Rafael J. Wysockic8eb8b42006-09-25 23:32:56 -0700909 printk("Suspending console(s)\n");
Linus Torvalds557240b2006-06-19 18:16:01 -0700910 acquire_console_sem();
911 console_suspended = 1;
912}
913
914void resume_console(void)
915{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -0700916 if (!console_suspend_enabled)
917 return;
Linus Torvalds557240b2006-06-19 18:16:01 -0700918 console_suspended = 0;
919 release_console_sem();
920}
921
922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * acquire_console_sem - lock the console system for exclusive use.
924 *
925 * Acquires a semaphore which guarantees that the caller has
926 * exclusive access to the console system and the console_drivers list.
927 *
928 * Can sleep, returns nothing.
929 */
930void acquire_console_sem(void)
931{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +0200932 BUG_ON(in_interrupt());
Linus Torvalds557240b2006-06-19 18:16:01 -0700933 if (console_suspended) {
934 down(&secondary_console_sem);
935 return;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 down(&console_sem);
938 console_locked = 1;
939 console_may_schedule = 1;
940}
941EXPORT_SYMBOL(acquire_console_sem);
942
943int try_acquire_console_sem(void)
944{
945 if (down_trylock(&console_sem))
946 return -1;
947 console_locked = 1;
948 console_may_schedule = 0;
949 return 0;
950}
951EXPORT_SYMBOL(try_acquire_console_sem);
952
953int is_console_locked(void)
954{
955 return console_locked;
956}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Kirill Korotaeve3e8a752007-02-10 01:46:19 -0800958void wake_up_klogd(void)
959{
960 if (!oops_in_progress && waitqueue_active(&log_wait))
961 wake_up_interruptible(&log_wait);
962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/**
965 * release_console_sem - unlock the console system
966 *
967 * Releases the semaphore which the caller holds on the console system
968 * and the console driver list.
969 *
970 * While the semaphore was held, console output may have been buffered
971 * by printk(). If this is the case, release_console_sem() emits
972 * the output prior to releasing the semaphore.
973 *
974 * If there is output waiting for klogd, we wake it up.
975 *
976 * release_console_sem() may be called from any context.
977 */
978void release_console_sem(void)
979{
980 unsigned long flags;
981 unsigned long _con_start, _log_end;
982 unsigned long wake_klogd = 0;
983
Linus Torvalds557240b2006-06-19 18:16:01 -0700984 if (console_suspended) {
985 up(&secondary_console_sem);
986 return;
987 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -0700988
989 console_may_schedule = 0;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 for ( ; ; ) {
992 spin_lock_irqsave(&logbuf_lock, flags);
993 wake_klogd |= log_start - log_end;
994 if (con_start == log_end)
995 break; /* Nothing to print */
996 _con_start = con_start;
997 _log_end = log_end;
998 con_start = log_end; /* Flush */
999 spin_unlock(&logbuf_lock);
1000 call_console_drivers(_con_start, _log_end);
1001 local_irq_restore(flags);
1002 }
1003 console_locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 up(&console_sem);
1005 spin_unlock_irqrestore(&logbuf_lock, flags);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001006 if (wake_klogd)
1007 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009EXPORT_SYMBOL(release_console_sem);
1010
Martin Waitzddad86c2005-11-13 16:08:14 -08001011/**
1012 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *
1014 * If the console code is currently allowed to sleep, and
1015 * if this CPU should yield the CPU to another task, do
1016 * so here.
1017 *
1018 * Must be called within acquire_console_sem().
1019 */
1020void __sched console_conditional_schedule(void)
1021{
1022 if (console_may_schedule)
1023 cond_resched();
1024}
1025EXPORT_SYMBOL(console_conditional_schedule);
1026
1027void console_print(const char *s)
1028{
1029 printk(KERN_EMERG "%s", s);
1030}
1031EXPORT_SYMBOL(console_print);
1032
1033void console_unblank(void)
1034{
1035 struct console *c;
1036
1037 /*
1038 * console_unblank can no longer be called in interrupt context unless
1039 * oops_in_progress is set to 1..
1040 */
1041 if (oops_in_progress) {
1042 if (down_trylock(&console_sem) != 0)
1043 return;
1044 } else
1045 acquire_console_sem();
1046
1047 console_locked = 1;
1048 console_may_schedule = 0;
1049 for (c = console_drivers; c != NULL; c = c->next)
1050 if ((c->flags & CON_ENABLED) && c->unblank)
1051 c->unblank();
1052 release_console_sem();
1053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055/*
1056 * Return the console tty driver structure and its associated index
1057 */
1058struct tty_driver *console_device(int *index)
1059{
1060 struct console *c;
1061 struct tty_driver *driver = NULL;
1062
1063 acquire_console_sem();
1064 for (c = console_drivers; c != NULL; c = c->next) {
1065 if (!c->device)
1066 continue;
1067 driver = c->device(c, index);
1068 if (driver)
1069 break;
1070 }
1071 release_console_sem();
1072 return driver;
1073}
1074
1075/*
1076 * Prevent further output on the passed console device so that (for example)
1077 * serial drivers can disable console output before suspending a port, and can
1078 * re-enable output afterwards.
1079 */
1080void console_stop(struct console *console)
1081{
1082 acquire_console_sem();
1083 console->flags &= ~CON_ENABLED;
1084 release_console_sem();
1085}
1086EXPORT_SYMBOL(console_stop);
1087
1088void console_start(struct console *console)
1089{
1090 acquire_console_sem();
1091 console->flags |= CON_ENABLED;
1092 release_console_sem();
1093}
1094EXPORT_SYMBOL(console_start);
1095
1096/*
1097 * The console driver calls this routine during kernel initialization
1098 * to register the console printing procedure with printk() and to
1099 * print any messages that were printed by the kernel before the
1100 * console driver was initialized.
1101 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001102void register_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001104 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 unsigned long flags;
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001106 struct console *bootconsole = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001108 if (console_drivers) {
1109 if (console->flags & CON_BOOT)
1110 return;
1111 if (console_drivers->flags & CON_BOOT)
1112 bootconsole = console_drivers;
1113 }
1114
1115 if (preferred_console < 0 || bootconsole || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 preferred_console = selected_console;
1117
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001118 if (console->early_setup)
1119 console->early_setup();
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /*
1122 * See if we want to use this console driver. If we
1123 * didn't select a console we take the first one
1124 * that registers here.
1125 */
1126 if (preferred_console < 0) {
1127 if (console->index < 0)
1128 console->index = 0;
1129 if (console->setup == NULL ||
1130 console->setup(console, NULL) == 0) {
1131 console->flags |= CON_ENABLED | CON_CONSDEV;
1132 preferred_console = 0;
1133 }
1134 }
1135
1136 /*
1137 * See if this console matches one we selected on
1138 * the command line.
1139 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001140 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1141 i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (strcmp(console_cmdline[i].name, console->name) != 0)
1143 continue;
1144 if (console->index >= 0 &&
1145 console->index != console_cmdline[i].index)
1146 continue;
1147 if (console->index < 0)
1148 console->index = console_cmdline[i].index;
1149 if (console->setup &&
1150 console->setup(console, console_cmdline[i].options) != 0)
1151 break;
1152 console->flags |= CON_ENABLED;
1153 console->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001154 if (i == selected_console) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 console->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001156 preferred_console = selected_console;
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159 }
1160
1161 if (!(console->flags & CON_ENABLED))
1162 return;
1163
Yinghai Lud37bf602007-07-15 23:37:28 -07001164 if (bootconsole && (console->flags & CON_CONSDEV)) {
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001165 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1166 bootconsole->name, bootconsole->index,
1167 console->name, console->index);
1168 unregister_console(bootconsole);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 console->flags &= ~CON_PRINTBUFFER;
Yinghai Lud37bf602007-07-15 23:37:28 -07001170 } else {
1171 printk(KERN_INFO "console [%s%d] enabled\n",
1172 console->name, console->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174
1175 /*
1176 * Put this console in the list - keep the
1177 * preferred driver at the head of the list.
1178 */
1179 acquire_console_sem();
1180 if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1181 console->next = console_drivers;
1182 console_drivers = console;
Greg Edwardsab4af032005-06-23 00:09:05 -07001183 if (console->next)
1184 console->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 } else {
1186 console->next = console_drivers->next;
1187 console_drivers->next = console;
1188 }
1189 if (console->flags & CON_PRINTBUFFER) {
1190 /*
1191 * release_console_sem() will print out the buffered messages
1192 * for us.
1193 */
1194 spin_lock_irqsave(&logbuf_lock, flags);
1195 con_start = log_start;
1196 spin_unlock_irqrestore(&logbuf_lock, flags);
1197 }
1198 release_console_sem();
1199}
1200EXPORT_SYMBOL(register_console);
1201
Jesper Juhl40dc5652005-10-30 15:02:46 -08001202int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001204 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int res = 1;
1206
1207 acquire_console_sem();
1208 if (console_drivers == console) {
1209 console_drivers=console->next;
1210 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001211 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 for (a=console_drivers->next, b=console_drivers ;
1213 a; b=a, a=b->next) {
1214 if (a == console) {
1215 b->next = a->next;
1216 res = 0;
1217 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001221
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001222 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001223 * If this isn't the last console and it has CON_CONSDEV set, we
1224 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001226 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001227 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 release_console_sem();
1230 return res;
1231}
1232EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001233
Robin Getz0c5564b2007-08-20 15:22:47 -04001234static int __init disable_boot_consoles(void)
1235{
Robin Getzcb00e992007-08-21 23:14:58 -04001236 if (console_drivers != NULL) {
1237 if (console_drivers->flags & CON_BOOT) {
1238 printk(KERN_INFO "turn off boot console %s%d\n",
1239 console_drivers->name, console_drivers->index);
1240 return unregister_console(console_drivers);
1241 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001242 }
1243 return 0;
1244}
1245late_initcall(disable_boot_consoles);
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/**
1248 * tty_write_message - write a message to a certain tty, not just the console.
Martin Waitzddad86c2005-11-13 16:08:14 -08001249 * @tty: the destination tty_struct
1250 * @msg: the message to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 *
1252 * This is used for messages that need to be redirected to a specific tty.
1253 * We don't put it into the syslog queue right now maybe in the future if
1254 * really needed.
1255 */
1256void tty_write_message(struct tty_struct *tty, char *msg)
1257{
1258 if (tty && tty->driver->write)
1259 tty->driver->write(tty, msg, strlen(msg));
1260 return;
1261}
1262
1263/*
1264 * printk rate limiting, lifted from the networking subsystem.
1265 *
1266 * This enforces a rate limit: not more than one kernel message
1267 * every printk_ratelimit_jiffies to make a denial-of-service
1268 * attack impossible.
1269 */
1270int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1271{
1272 static DEFINE_SPINLOCK(ratelimit_lock);
Jesper Juhl40dc5652005-10-30 15:02:46 -08001273 static unsigned long toks = 10 * 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 static unsigned long last_msg;
1275 static int missed;
1276 unsigned long flags;
1277 unsigned long now = jiffies;
1278
1279 spin_lock_irqsave(&ratelimit_lock, flags);
1280 toks += now - last_msg;
1281 last_msg = now;
1282 if (toks > (ratelimit_burst * ratelimit_jiffies))
1283 toks = ratelimit_burst * ratelimit_jiffies;
1284 if (toks >= ratelimit_jiffies) {
1285 int lost = missed;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 missed = 0;
1288 toks -= ratelimit_jiffies;
1289 spin_unlock_irqrestore(&ratelimit_lock, flags);
1290 if (lost)
1291 printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1292 return 1;
1293 }
1294 missed++;
1295 spin_unlock_irqrestore(&ratelimit_lock, flags);
1296 return 0;
1297}
1298EXPORT_SYMBOL(__printk_ratelimit);
1299
1300/* minimum time in jiffies between messages */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001301int printk_ratelimit_jiffies = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303/* number of messages we send before ratelimiting */
1304int printk_ratelimit_burst = 10;
1305
1306int printk_ratelimit(void)
1307{
1308 return __printk_ratelimit(printk_ratelimit_jiffies,
1309 printk_ratelimit_burst);
1310}
1311EXPORT_SYMBOL(printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001312
1313/**
1314 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1315 * @caller_jiffies: pointer to caller's state
1316 * @interval_msecs: minimum interval between prints
1317 *
1318 * printk_timed_ratelimit() returns true if more than @interval_msecs
1319 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1320 * returned true.
1321 */
1322bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1323 unsigned int interval_msecs)
1324{
1325 if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1326 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1327 return true;
1328 }
1329 return false;
1330}
1331EXPORT_SYMBOL(printk_timed_ratelimit);