| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 13 | * Fixed SMP synchronization, 08/08/99, Manfred Spraul | 
| Christian Kujau | 624dffc | 2006-01-15 02:43:54 +0100 | [diff] [blame] | 14 | *     manfred@colorfullife.com | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/console.h> | 
|  | 24 | #include <linux/init.h> | 
|  | 25 | #include <linux/module.h> | 
| Jan Engelhardt | 3b9c041 | 2006-06-25 05:48:15 -0700 | [diff] [blame] | 26 | #include <linux/moduleparam.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/interrupt.h>			/* For in_interrupt() */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Andrew Morton | f46c483 | 2006-11-02 22:07:16 -0800 | [diff] [blame] | 33 | #include <linux/jiffies.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
|  | 46 | DECLARE_WAIT_QUEUE_HEAD(log_wait); | 
|  | 47 |  | 
|  | 48 | int console_printk[4] = { | 
|  | 49 | DEFAULT_CONSOLE_LOGLEVEL,	/* console_loglevel */ | 
|  | 50 | DEFAULT_MESSAGE_LOGLEVEL,	/* default_message_loglevel */ | 
|  | 51 | MINIMUM_CONSOLE_LOGLEVEL,	/* minimum_console_loglevel */ | 
|  | 52 | DEFAULT_CONSOLE_LOGLEVEL,	/* default_console_loglevel */ | 
|  | 53 | }; | 
|  | 54 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* | 
| Patrick Pletscher | 0bbfb7c | 2007-02-17 20:10:16 +0100 | [diff] [blame] | 56 | * Low level drivers may need that to know if they can schedule in | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * their unblank() callback or not. So let's export it. | 
|  | 58 | */ | 
|  | 59 | int oops_in_progress; | 
|  | 60 | EXPORT_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 | */ | 
|  | 67 | static DECLARE_MUTEX(console_sem); | 
| Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 68 | static DECLARE_MUTEX(secondary_console_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | struct 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 | */ | 
| Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 78 | static int console_locked, console_suspended; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ | 
|  | 85 | static DEFINE_SPINLOCK(logbuf_lock); | 
|  | 86 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ | 
|  | 94 | static unsigned long log_start;	/* Index into log_buf: next char to be read by syslog() */ | 
|  | 95 | static unsigned long con_start;	/* Index into log_buf: next char to be sent to consoles */ | 
|  | 96 | static unsigned long log_end;	/* Index into log_buf: most-recently-written-char + 1 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 |  | 
|  | 98 | /* | 
|  | 99 | *	Array of consoles built from command line options (console=) | 
|  | 100 | */ | 
|  | 101 | struct 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 |  | 
|  | 110 | static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; | 
|  | 111 | static int selected_console = -1; | 
|  | 112 | static int preferred_console = -1; | 
|  | 113 |  | 
|  | 114 | /* Flag: console code may call schedule() */ | 
|  | 115 | static int console_may_schedule; | 
|  | 116 |  | 
| Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 117 | #ifdef CONFIG_PRINTK | 
|  | 118 |  | 
|  | 119 | static char __log_buf[__LOG_BUF_LEN]; | 
|  | 120 | static char *log_buf = __log_buf; | 
|  | 121 | static int log_buf_len = __LOG_BUF_LEN; | 
|  | 122 | static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */ | 
|  | 123 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | static 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; | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 133 | char *new_log_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 |  | 
|  | 135 | new_log_buf = alloc_bootmem(size); | 
|  | 136 | if (!new_log_buf) { | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 137 | printk(KERN_WARNING "log_buf_len: allocation failed\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 157 | printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } | 
|  | 159 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 180 | int do_syslog(int type, char __user *buf, int len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 207 | error = wait_event_interruptible(log_wait, | 
|  | 208 | (log_start - log_end)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 228 | do_clear = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 252 | * printk() could overwrite the messages | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | * we try to copy to user space. Therefore | 
|  | 254 | * the messages are copied in reverse. <manfreds> | 
|  | 255 | */ | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 256 | for (i = 0; i < count && !error; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 270 | if (i != count) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | int offset = count-error; | 
|  | 272 | /* buffer overflow during copy, correct user buffer. */ | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 273 | for (i = 0; i < error; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } | 
|  | 311 | out: | 
|  | 312 | return error; | 
|  | 313 | } | 
|  | 314 |  | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 315 | asmlinkage long sys_syslog(int type, char __user *buf, int len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { | 
|  | 317 | return do_syslog(type, buf, len); | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | /* | 
|  | 321 | * Call the console drivers on a range of log_buf | 
|  | 322 | */ | 
|  | 323 | static 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) { | 
| Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 328 | if ((con->flags & CON_ENABLED) && con->write && | 
|  | 329 | (cpu_online(smp_processor_id()) || | 
|  | 330 | (con->flags & CON_ANYTIME))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | con->write(con, &LOG_BUF(start), end - start); | 
|  | 332 | } | 
|  | 333 | } | 
|  | 334 |  | 
| Ingo Molnar | 79290822 | 2006-12-06 20:40:51 -0800 | [diff] [blame] | 335 | static int __read_mostly ignore_loglevel; | 
|  | 336 |  | 
| Adrian Bunk | 99eea6a | 2006-12-22 01:07:00 -0800 | [diff] [blame] | 337 | static int __init ignore_loglevel_setup(char *str) | 
| Ingo Molnar | 79290822 | 2006-12-06 20:40:51 -0800 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | /* | 
|  | 348 | * Write out chars from start to end - 1 inclusive | 
|  | 349 | */ | 
|  | 350 | static void _call_console_drivers(unsigned long start, | 
|  | 351 | unsigned long end, int msg_log_level) | 
|  | 352 | { | 
| Ingo Molnar | 79290822 | 2006-12-06 20:40:51 -0800 | [diff] [blame] | 353 | if ((msg_log_level < console_loglevel || ignore_loglevel) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ | 
|  | 371 | static 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 |  | 
| Eric Sesterhenn | 8abd8e2 | 2006-04-01 01:21:17 +0200 | [diff] [blame] | 376 | BUG_ON(((long)(start - end)) > 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 |  | 
|  | 378 | cur_index = start; | 
|  | 379 | start_print = start; | 
|  | 380 | while (cur_index != end) { | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 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) == '>') { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 |  | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 393 | cur_index++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
|  | 414 | static 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 | */ | 
|  | 431 | static void zap_locks(void) | 
|  | 432 | { | 
|  | 433 | static unsigned long oops_timestamp; | 
|  | 434 |  | 
|  | 435 | if (time_after_eq(jiffies, oops_timestamp) && | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 436 | !time_after(jiffies, oops_timestamp + 30 * HZ)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) | 
|  | 448 | static int printk_time = 1; | 
|  | 449 | #else | 
|  | 450 | static int printk_time = 0; | 
|  | 451 | #endif | 
| Randy Dunlap | e84845c | 2007-07-15 23:40:25 -0700 | [diff] [blame] | 452 | module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 |  | 
|  | 454 | static int __init printk_time_setup(char *str) | 
|  | 455 | { | 
|  | 456 | if (*str) | 
|  | 457 | return 0; | 
|  | 458 | printk_time = 1; | 
| Randy Dunlap | e84845c | 2007-07-15 23:40:25 -0700 | [diff] [blame] | 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"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return 1; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | __setup("time", printk_time_setup); | 
|  | 466 |  | 
| Andrew Morton | 31f6d9d | 2005-09-21 09:55:30 -0700 | [diff] [blame] | 467 | __attribute__((weak)) unsigned long long printk_clock(void) | 
|  | 468 | { | 
|  | 469 | return sched_clock(); | 
|  | 470 | } | 
|  | 471 |  | 
| Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 472 | /* Check if we have any console registered that can be called early in boot. */ | 
|  | 473 | static 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 |  | 
| Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 484 | /** | 
|  | 485 | * printk - print a kernel message | 
|  | 486 | * @fmt: format string | 
|  | 487 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 488 | * This is printk().  It can be called from any context.  We want it to work. | 
| Jiri Kosina | 1492192 | 2007-07-15 23:41:51 -0700 | [diff] [blame] | 489 | * Be aware of the fact that if oops_in_progress is not set, we might try to | 
|  | 490 | * wake klogd up which could deadlock on runqueue lock if printk() is called | 
|  | 491 | * from scheduler code. | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 492 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | * We try to grab the console_sem.  If we succeed, it's easy - we log the output and | 
|  | 494 | * call the console drivers.  If we fail to get the semaphore we place the output | 
|  | 495 | * into the log buffer and return.  The current holder of the console_sem will | 
|  | 496 | * notice the new output in release_console_sem() and will send it to the | 
|  | 497 | * consoles before releasing the semaphore. | 
|  | 498 | * | 
|  | 499 | * One effect of this deferred printing is that code which calls printk() and | 
|  | 500 | * then changes console_loglevel may break. This is because console_loglevel | 
|  | 501 | * is inspected when the actual printing occurs. | 
| Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 502 | * | 
|  | 503 | * See also: | 
|  | 504 | * printf(3) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | */ | 
| Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 506 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | asmlinkage int printk(const char *fmt, ...) | 
|  | 508 | { | 
|  | 509 | va_list args; | 
|  | 510 | int r; | 
|  | 511 |  | 
|  | 512 | va_start(args, fmt); | 
|  | 513 | r = vprintk(fmt, args); | 
|  | 514 | va_end(args); | 
|  | 515 |  | 
|  | 516 | return r; | 
|  | 517 | } | 
|  | 518 |  | 
| David Howells | fe21773 | 2005-09-06 15:16:34 -0700 | [diff] [blame] | 519 | /* cpu currently holding logbuf_lock */ | 
|  | 520 | static volatile unsigned int printk_cpu = UINT_MAX; | 
|  | 521 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | asmlinkage int vprintk(const char *fmt, va_list args) | 
|  | 523 | { | 
|  | 524 | unsigned long flags; | 
|  | 525 | int printed_len; | 
|  | 526 | char *p; | 
|  | 527 | static char printk_buf[1024]; | 
|  | 528 | static int log_level_unknown = 1; | 
|  | 529 |  | 
| David Howells | fe21773 | 2005-09-06 15:16:34 -0700 | [diff] [blame] | 530 | preempt_disable(); | 
|  | 531 | if (unlikely(oops_in_progress) && printk_cpu == smp_processor_id()) | 
|  | 532 | /* If a crash is occurring during printk() on this CPU, | 
|  | 533 | * make sure we can't deadlock */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | zap_locks(); | 
|  | 535 |  | 
|  | 536 | /* This stops the holder of console_sem just where we want him */ | 
| Mathieu Desnoyers | 1efc5da | 2007-02-10 01:46:29 -0800 | [diff] [blame] | 537 | raw_local_irq_save(flags); | 
| Ingo Molnar | a0f1ccf | 2006-07-03 00:24:58 -0700 | [diff] [blame] | 538 | lockdep_off(); | 
|  | 539 | spin_lock(&logbuf_lock); | 
| David Howells | fe21773 | 2005-09-06 15:16:34 -0700 | [diff] [blame] | 540 | printk_cpu = smp_processor_id(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 |  | 
|  | 542 | /* Emit the output into the temporary buffer */ | 
|  | 543 | printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args); | 
|  | 544 |  | 
|  | 545 | /* | 
|  | 546 | * Copy the output into log_buf.  If the caller didn't provide | 
|  | 547 | * appropriate log level tags, we insert them here | 
|  | 548 | */ | 
|  | 549 | for (p = printk_buf; *p; p++) { | 
|  | 550 | if (log_level_unknown) { | 
|  | 551 | /* log_level_unknown signals the start of a new line */ | 
|  | 552 | if (printk_time) { | 
|  | 553 | int loglev_char; | 
|  | 554 | char tbuf[50], *tp; | 
|  | 555 | unsigned tlen; | 
|  | 556 | unsigned long long t; | 
|  | 557 | unsigned long nanosec_rem; | 
|  | 558 |  | 
|  | 559 | /* | 
|  | 560 | * force the log level token to be | 
|  | 561 | * before the time output. | 
|  | 562 | */ | 
|  | 563 | if (p[0] == '<' && p[1] >='0' && | 
|  | 564 | p[1] <= '7' && p[2] == '>') { | 
|  | 565 | loglev_char = p[1]; | 
|  | 566 | p += 3; | 
| Guillaume Chazarain | 025510c | 2006-01-08 01:02:41 -0800 | [diff] [blame] | 567 | printed_len -= 3; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } else { | 
|  | 569 | loglev_char = default_message_loglevel | 
|  | 570 | + '0'; | 
|  | 571 | } | 
| Andrew Morton | 31f6d9d | 2005-09-21 09:55:30 -0700 | [diff] [blame] | 572 | t = printk_clock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | nanosec_rem = do_div(t, 1000000000); | 
|  | 574 | tlen = sprintf(tbuf, | 
|  | 575 | "<%c>[%5lu.%06lu] ", | 
|  | 576 | loglev_char, | 
|  | 577 | (unsigned long)t, | 
|  | 578 | nanosec_rem/1000); | 
|  | 579 |  | 
|  | 580 | for (tp = tbuf; tp < tbuf + tlen; tp++) | 
|  | 581 | emit_log_char(*tp); | 
| Guillaume Chazarain | 025510c | 2006-01-08 01:02:41 -0800 | [diff] [blame] | 582 | printed_len += tlen; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } else { | 
|  | 584 | if (p[0] != '<' || p[1] < '0' || | 
|  | 585 | p[1] > '7' || p[2] != '>') { | 
|  | 586 | emit_log_char('<'); | 
|  | 587 | emit_log_char(default_message_loglevel | 
|  | 588 | + '0'); | 
|  | 589 | emit_log_char('>'); | 
| Guillaume Chazarain | 025510c | 2006-01-08 01:02:41 -0800 | [diff] [blame] | 590 | printed_len += 3; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } | 
|  | 593 | log_level_unknown = 0; | 
|  | 594 | if (!*p) | 
|  | 595 | break; | 
|  | 596 | } | 
|  | 597 | emit_log_char(*p); | 
|  | 598 | if (*p == '\n') | 
|  | 599 | log_level_unknown = 1; | 
|  | 600 | } | 
|  | 601 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | if (!down_trylock(&console_sem)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | /* | 
| Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 604 | * We own the drivers.  We can drop the spinlock and | 
|  | 605 | * let release_console_sem() print the text, maybe ... | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | */ | 
| Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 607 | console_locked = 1; | 
| David Howells | fe21773 | 2005-09-06 15:16:34 -0700 | [diff] [blame] | 608 | printk_cpu = UINT_MAX; | 
| Ingo Molnar | a0f1ccf | 2006-07-03 00:24:58 -0700 | [diff] [blame] | 609 | spin_unlock(&logbuf_lock); | 
| Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 610 |  | 
|  | 611 | /* | 
|  | 612 | * Console drivers may assume that per-cpu resources have | 
|  | 613 | * been allocated. So unless they're explicitly marked as | 
|  | 614 | * being able to cope (CON_ANYTIME) don't call them until | 
|  | 615 | * this CPU is officially up. | 
|  | 616 | */ | 
|  | 617 | if (cpu_online(smp_processor_id()) || have_callable_console()) { | 
|  | 618 | console_may_schedule = 0; | 
|  | 619 | release_console_sem(); | 
|  | 620 | } else { | 
|  | 621 | /* Release by hand to avoid flushing the buffer. */ | 
|  | 622 | console_locked = 0; | 
|  | 623 | up(&console_sem); | 
|  | 624 | } | 
| Ingo Molnar | a0f1ccf | 2006-07-03 00:24:58 -0700 | [diff] [blame] | 625 | lockdep_on(); | 
| Mathieu Desnoyers | 1efc5da | 2007-02-10 01:46:29 -0800 | [diff] [blame] | 626 | raw_local_irq_restore(flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } else { | 
|  | 628 | /* | 
|  | 629 | * Someone else owns the drivers.  We drop the spinlock, which | 
|  | 630 | * allows the semaphore holder to proceed and to call the | 
|  | 631 | * console drivers with the output which we just produced. | 
|  | 632 | */ | 
| David Howells | fe21773 | 2005-09-06 15:16:34 -0700 | [diff] [blame] | 633 | printk_cpu = UINT_MAX; | 
| Ingo Molnar | a0f1ccf | 2006-07-03 00:24:58 -0700 | [diff] [blame] | 634 | spin_unlock(&logbuf_lock); | 
|  | 635 | lockdep_on(); | 
| Mathieu Desnoyers | 1efc5da | 2007-02-10 01:46:29 -0800 | [diff] [blame] | 636 | raw_local_irq_restore(flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } | 
| Michael Ellerman | 76a8ad2 | 2006-06-25 05:47:40 -0700 | [diff] [blame] | 638 |  | 
| David Howells | fe21773 | 2005-09-06 15:16:34 -0700 | [diff] [blame] | 639 | preempt_enable(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | return printed_len; | 
|  | 641 | } | 
|  | 642 | EXPORT_SYMBOL(printk); | 
|  | 643 | EXPORT_SYMBOL(vprintk); | 
|  | 644 |  | 
| Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 645 | #else | 
|  | 646 |  | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 647 | asmlinkage long sys_syslog(int type, char __user *buf, int len) | 
| Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 648 | { | 
| Mike Galbraith | c36264d | 2006-12-06 20:37:42 -0800 | [diff] [blame] | 649 | return -ENOSYS; | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 650 | } | 
|  | 651 |  | 
|  | 652 | static void call_console_drivers(unsigned long start, unsigned long end) | 
|  | 653 | { | 
|  | 654 | } | 
| Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 655 |  | 
|  | 656 | #endif | 
|  | 657 |  | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 658 | /* | 
|  | 659 | * Set up a list of consoles.  Called from init/main.c | 
|  | 660 | */ | 
|  | 661 | static int __init console_setup(char *str) | 
|  | 662 | { | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 663 | char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */ | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 664 | char *s, *options; | 
|  | 665 | int idx; | 
|  | 666 |  | 
|  | 667 | /* | 
|  | 668 | * Decode str into name, index, options. | 
|  | 669 | */ | 
|  | 670 | if (str[0] >= '0' && str[0] <= '9') { | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 671 | strcpy(buf, "ttyS"); | 
|  | 672 | strncpy(buf + 4, str, sizeof(buf) - 5); | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 673 | } else { | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 674 | strncpy(buf, str, sizeof(buf) - 1); | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 675 | } | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 676 | buf[sizeof(buf) - 1] = 0; | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 677 | if ((options = strchr(str, ',')) != NULL) | 
|  | 678 | *(options++) = 0; | 
|  | 679 | #ifdef __sparc__ | 
|  | 680 | if (!strcmp(str, "ttya")) | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 681 | strcpy(buf, "ttyS0"); | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 682 | if (!strcmp(str, "ttyb")) | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 683 | strcpy(buf, "ttyS1"); | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 684 | #endif | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 685 | for (s = buf; *s; s++) | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 686 | if ((*s >= '0' && *s <= '9') || *s == ',') | 
|  | 687 | break; | 
|  | 688 | idx = simple_strtoul(s, NULL, 10); | 
|  | 689 | *s = 0; | 
|  | 690 |  | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 691 | add_preferred_console(buf, idx, options); | 
| John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 692 | return 1; | 
|  | 693 | } | 
|  | 694 | __setup("console=", console_setup); | 
|  | 695 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | /** | 
| Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 697 | * add_preferred_console - add a device to the list of preferred consoles. | 
| Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 698 | * @name: device name | 
|  | 699 | * @idx: device index | 
|  | 700 | * @options: options for this console | 
| Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 701 | * | 
|  | 702 | * The last preferred console added will be used for kernel messages | 
|  | 703 | * and stdin/out/err for init.  Normally this is used by console_setup | 
|  | 704 | * above to handle user-supplied console arguments; however it can also | 
|  | 705 | * be used by arch-specific code either to override the user or more | 
|  | 706 | * commonly to provide a default console (ie from PROM variables) when | 
|  | 707 | * the user has not supplied one. | 
|  | 708 | */ | 
|  | 709 | int __init add_preferred_console(char *name, int idx, char *options) | 
|  | 710 | { | 
|  | 711 | struct console_cmdline *c; | 
|  | 712 | int i; | 
|  | 713 |  | 
|  | 714 | /* | 
|  | 715 | *	See if this tty is not yet registered, and | 
|  | 716 | *	if we have a slot free. | 
|  | 717 | */ | 
| Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 718 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) | 
| Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 719 | if (strcmp(console_cmdline[i].name, name) == 0 && | 
|  | 720 | console_cmdline[i].index == idx) { | 
|  | 721 | selected_console = i; | 
|  | 722 | return 0; | 
|  | 723 | } | 
|  | 724 | if (i == MAX_CMDLINECONSOLES) | 
|  | 725 | return -E2BIG; | 
|  | 726 | selected_console = i; | 
|  | 727 | c = &console_cmdline[i]; | 
|  | 728 | memcpy(c->name, name, sizeof(c->name)); | 
|  | 729 | c->name[sizeof(c->name) - 1] = 0; | 
|  | 730 | c->options = options; | 
|  | 731 | c->index = idx; | 
|  | 732 | return 0; | 
|  | 733 | } | 
|  | 734 |  | 
| Daniel Ritz | b6b1d87 | 2007-08-03 16:07:43 +0200 | [diff] [blame] | 735 | int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options) | 
| Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 736 | { | 
|  | 737 | struct console_cmdline *c; | 
|  | 738 | int i; | 
|  | 739 |  | 
|  | 740 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) | 
|  | 741 | if (strcmp(console_cmdline[i].name, name) == 0 && | 
|  | 742 | console_cmdline[i].index == idx) { | 
|  | 743 | c = &console_cmdline[i]; | 
|  | 744 | memcpy(c->name, name_new, sizeof(c->name)); | 
|  | 745 | c->name[sizeof(c->name) - 1] = 0; | 
|  | 746 | c->options = options; | 
|  | 747 | c->index = idx_new; | 
|  | 748 | return i; | 
|  | 749 | } | 
|  | 750 | /* not found */ | 
|  | 751 | return -1; | 
|  | 752 | } | 
|  | 753 |  | 
| Rafael J. Wysocki | c8eb8b4 | 2006-09-25 23:32:56 -0700 | [diff] [blame] | 754 | #ifndef CONFIG_DISABLE_CONSOLE_SUSPEND | 
| Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 755 | /** | 
| Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 756 | * suspend_console - suspend the console subsystem | 
|  | 757 | * | 
|  | 758 | * This disables printk() while we go into suspend states | 
|  | 759 | */ | 
|  | 760 | void suspend_console(void) | 
|  | 761 | { | 
| Rafael J. Wysocki | c8eb8b4 | 2006-09-25 23:32:56 -0700 | [diff] [blame] | 762 | printk("Suspending console(s)\n"); | 
| Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 763 | acquire_console_sem(); | 
|  | 764 | console_suspended = 1; | 
|  | 765 | } | 
|  | 766 |  | 
|  | 767 | void resume_console(void) | 
|  | 768 | { | 
|  | 769 | console_suspended = 0; | 
|  | 770 | release_console_sem(); | 
|  | 771 | } | 
| Rafael J. Wysocki | c8eb8b4 | 2006-09-25 23:32:56 -0700 | [diff] [blame] | 772 | #endif /* CONFIG_DISABLE_CONSOLE_SUSPEND */ | 
| Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 773 |  | 
|  | 774 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | * acquire_console_sem - lock the console system for exclusive use. | 
|  | 776 | * | 
|  | 777 | * Acquires a semaphore which guarantees that the caller has | 
|  | 778 | * exclusive access to the console system and the console_drivers list. | 
|  | 779 | * | 
|  | 780 | * Can sleep, returns nothing. | 
|  | 781 | */ | 
|  | 782 | void acquire_console_sem(void) | 
|  | 783 | { | 
| Eric Sesterhenn | 8abd8e2 | 2006-04-01 01:21:17 +0200 | [diff] [blame] | 784 | BUG_ON(in_interrupt()); | 
| Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 785 | if (console_suspended) { | 
|  | 786 | down(&secondary_console_sem); | 
|  | 787 | return; | 
|  | 788 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | down(&console_sem); | 
|  | 790 | console_locked = 1; | 
|  | 791 | console_may_schedule = 1; | 
|  | 792 | } | 
|  | 793 | EXPORT_SYMBOL(acquire_console_sem); | 
|  | 794 |  | 
|  | 795 | int try_acquire_console_sem(void) | 
|  | 796 | { | 
|  | 797 | if (down_trylock(&console_sem)) | 
|  | 798 | return -1; | 
|  | 799 | console_locked = 1; | 
|  | 800 | console_may_schedule = 0; | 
|  | 801 | return 0; | 
|  | 802 | } | 
|  | 803 | EXPORT_SYMBOL(try_acquire_console_sem); | 
|  | 804 |  | 
|  | 805 | int is_console_locked(void) | 
|  | 806 | { | 
|  | 807 | return console_locked; | 
|  | 808 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 |  | 
| Kirill Korotaev | e3e8a75 | 2007-02-10 01:46:19 -0800 | [diff] [blame] | 810 | void wake_up_klogd(void) | 
|  | 811 | { | 
|  | 812 | if (!oops_in_progress && waitqueue_active(&log_wait)) | 
|  | 813 | wake_up_interruptible(&log_wait); | 
|  | 814 | } | 
|  | 815 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | /** | 
|  | 817 | * release_console_sem - unlock the console system | 
|  | 818 | * | 
|  | 819 | * Releases the semaphore which the caller holds on the console system | 
|  | 820 | * and the console driver list. | 
|  | 821 | * | 
|  | 822 | * While the semaphore was held, console output may have been buffered | 
|  | 823 | * by printk().  If this is the case, release_console_sem() emits | 
|  | 824 | * the output prior to releasing the semaphore. | 
|  | 825 | * | 
|  | 826 | * If there is output waiting for klogd, we wake it up. | 
|  | 827 | * | 
|  | 828 | * release_console_sem() may be called from any context. | 
|  | 829 | */ | 
|  | 830 | void release_console_sem(void) | 
|  | 831 | { | 
|  | 832 | unsigned long flags; | 
|  | 833 | unsigned long _con_start, _log_end; | 
|  | 834 | unsigned long wake_klogd = 0; | 
|  | 835 |  | 
| Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 836 | if (console_suspended) { | 
|  | 837 | up(&secondary_console_sem); | 
|  | 838 | return; | 
|  | 839 | } | 
| Antonino A. Daplas | 78944e5 | 2006-08-05 12:14:16 -0700 | [diff] [blame] | 840 |  | 
|  | 841 | console_may_schedule = 0; | 
|  | 842 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | for ( ; ; ) { | 
|  | 844 | spin_lock_irqsave(&logbuf_lock, flags); | 
|  | 845 | wake_klogd |= log_start - log_end; | 
|  | 846 | if (con_start == log_end) | 
|  | 847 | break;			/* Nothing to print */ | 
|  | 848 | _con_start = con_start; | 
|  | 849 | _log_end = log_end; | 
|  | 850 | con_start = log_end;		/* Flush */ | 
|  | 851 | spin_unlock(&logbuf_lock); | 
|  | 852 | call_console_drivers(_con_start, _log_end); | 
|  | 853 | local_irq_restore(flags); | 
|  | 854 | } | 
|  | 855 | console_locked = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | up(&console_sem); | 
|  | 857 | spin_unlock_irqrestore(&logbuf_lock, flags); | 
| Kirill Korotaev | e3e8a75 | 2007-02-10 01:46:19 -0800 | [diff] [blame] | 858 | if (wake_klogd) | 
|  | 859 | wake_up_klogd(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | } | 
|  | 861 | EXPORT_SYMBOL(release_console_sem); | 
|  | 862 |  | 
| Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 863 | /** | 
|  | 864 | * console_conditional_schedule - yield the CPU if required | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | * | 
|  | 866 | * If the console code is currently allowed to sleep, and | 
|  | 867 | * if this CPU should yield the CPU to another task, do | 
|  | 868 | * so here. | 
|  | 869 | * | 
|  | 870 | * Must be called within acquire_console_sem(). | 
|  | 871 | */ | 
|  | 872 | void __sched console_conditional_schedule(void) | 
|  | 873 | { | 
|  | 874 | if (console_may_schedule) | 
|  | 875 | cond_resched(); | 
|  | 876 | } | 
|  | 877 | EXPORT_SYMBOL(console_conditional_schedule); | 
|  | 878 |  | 
|  | 879 | void console_print(const char *s) | 
|  | 880 | { | 
|  | 881 | printk(KERN_EMERG "%s", s); | 
|  | 882 | } | 
|  | 883 | EXPORT_SYMBOL(console_print); | 
|  | 884 |  | 
|  | 885 | void console_unblank(void) | 
|  | 886 | { | 
|  | 887 | struct console *c; | 
|  | 888 |  | 
|  | 889 | /* | 
|  | 890 | * console_unblank can no longer be called in interrupt context unless | 
|  | 891 | * oops_in_progress is set to 1.. | 
|  | 892 | */ | 
|  | 893 | if (oops_in_progress) { | 
|  | 894 | if (down_trylock(&console_sem) != 0) | 
|  | 895 | return; | 
|  | 896 | } else | 
|  | 897 | acquire_console_sem(); | 
|  | 898 |  | 
|  | 899 | console_locked = 1; | 
|  | 900 | console_may_schedule = 0; | 
|  | 901 | for (c = console_drivers; c != NULL; c = c->next) | 
|  | 902 | if ((c->flags & CON_ENABLED) && c->unblank) | 
|  | 903 | c->unblank(); | 
|  | 904 | release_console_sem(); | 
|  | 905 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 |  | 
|  | 907 | /* | 
|  | 908 | * Return the console tty driver structure and its associated index | 
|  | 909 | */ | 
|  | 910 | struct tty_driver *console_device(int *index) | 
|  | 911 | { | 
|  | 912 | struct console *c; | 
|  | 913 | struct tty_driver *driver = NULL; | 
|  | 914 |  | 
|  | 915 | acquire_console_sem(); | 
|  | 916 | for (c = console_drivers; c != NULL; c = c->next) { | 
|  | 917 | if (!c->device) | 
|  | 918 | continue; | 
|  | 919 | driver = c->device(c, index); | 
|  | 920 | if (driver) | 
|  | 921 | break; | 
|  | 922 | } | 
|  | 923 | release_console_sem(); | 
|  | 924 | return driver; | 
|  | 925 | } | 
|  | 926 |  | 
|  | 927 | /* | 
|  | 928 | * Prevent further output on the passed console device so that (for example) | 
|  | 929 | * serial drivers can disable console output before suspending a port, and can | 
|  | 930 | * re-enable output afterwards. | 
|  | 931 | */ | 
|  | 932 | void console_stop(struct console *console) | 
|  | 933 | { | 
|  | 934 | acquire_console_sem(); | 
|  | 935 | console->flags &= ~CON_ENABLED; | 
|  | 936 | release_console_sem(); | 
|  | 937 | } | 
|  | 938 | EXPORT_SYMBOL(console_stop); | 
|  | 939 |  | 
|  | 940 | void console_start(struct console *console) | 
|  | 941 | { | 
|  | 942 | acquire_console_sem(); | 
|  | 943 | console->flags |= CON_ENABLED; | 
|  | 944 | release_console_sem(); | 
|  | 945 | } | 
|  | 946 | EXPORT_SYMBOL(console_start); | 
|  | 947 |  | 
|  | 948 | /* | 
|  | 949 | * The console driver calls this routine during kernel initialization | 
|  | 950 | * to register the console printing procedure with printk() and to | 
|  | 951 | * print any messages that were printed by the kernel before the | 
|  | 952 | * console driver was initialized. | 
|  | 953 | */ | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 954 | void register_console(struct console *console) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | { | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 956 | int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | unsigned long flags; | 
| Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 958 | struct console *bootconsole = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 |  | 
| Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 960 | if (console_drivers) { | 
|  | 961 | if (console->flags & CON_BOOT) | 
|  | 962 | return; | 
|  | 963 | if (console_drivers->flags & CON_BOOT) | 
|  | 964 | bootconsole = console_drivers; | 
|  | 965 | } | 
|  | 966 |  | 
|  | 967 | if (preferred_console < 0 || bootconsole || !console_drivers) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | preferred_console = selected_console; | 
|  | 969 |  | 
| Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 970 | if (console->early_setup) | 
|  | 971 | console->early_setup(); | 
|  | 972 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | /* | 
|  | 974 | *	See if we want to use this console driver. If we | 
|  | 975 | *	didn't select a console we take the first one | 
|  | 976 | *	that registers here. | 
|  | 977 | */ | 
|  | 978 | if (preferred_console < 0) { | 
|  | 979 | if (console->index < 0) | 
|  | 980 | console->index = 0; | 
|  | 981 | if (console->setup == NULL || | 
|  | 982 | console->setup(console, NULL) == 0) { | 
|  | 983 | console->flags |= CON_ENABLED | CON_CONSDEV; | 
|  | 984 | preferred_console = 0; | 
|  | 985 | } | 
|  | 986 | } | 
|  | 987 |  | 
|  | 988 | /* | 
|  | 989 | *	See if this console matches one we selected on | 
|  | 990 | *	the command line. | 
|  | 991 | */ | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 992 | for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; | 
|  | 993 | i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | if (strcmp(console_cmdline[i].name, console->name) != 0) | 
|  | 995 | continue; | 
|  | 996 | if (console->index >= 0 && | 
|  | 997 | console->index != console_cmdline[i].index) | 
|  | 998 | continue; | 
|  | 999 | if (console->index < 0) | 
|  | 1000 | console->index = console_cmdline[i].index; | 
|  | 1001 | if (console->setup && | 
|  | 1002 | console->setup(console, console_cmdline[i].options) != 0) | 
|  | 1003 | break; | 
|  | 1004 | console->flags |= CON_ENABLED; | 
|  | 1005 | console->index = console_cmdline[i].index; | 
| Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1006 | if (i == selected_console) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | console->flags |= CON_CONSDEV; | 
| Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1008 | preferred_console = selected_console; | 
|  | 1009 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | break; | 
|  | 1011 | } | 
|  | 1012 |  | 
|  | 1013 | if (!(console->flags & CON_ENABLED)) | 
|  | 1014 | return; | 
|  | 1015 |  | 
| Yinghai Lu | d37bf60 | 2007-07-15 23:37:28 -0700 | [diff] [blame] | 1016 | if (bootconsole && (console->flags & CON_CONSDEV)) { | 
| Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 1017 | printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n", | 
|  | 1018 | bootconsole->name, bootconsole->index, | 
|  | 1019 | console->name, console->index); | 
|  | 1020 | unregister_console(bootconsole); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | console->flags &= ~CON_PRINTBUFFER; | 
| Yinghai Lu | d37bf60 | 2007-07-15 23:37:28 -0700 | [diff] [blame] | 1022 | } else { | 
|  | 1023 | printk(KERN_INFO "console [%s%d] enabled\n", | 
|  | 1024 | console->name, console->index); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | } | 
|  | 1026 |  | 
|  | 1027 | /* | 
|  | 1028 | *	Put this console in the list - keep the | 
|  | 1029 | *	preferred driver at the head of the list. | 
|  | 1030 | */ | 
|  | 1031 | acquire_console_sem(); | 
|  | 1032 | if ((console->flags & CON_CONSDEV) || console_drivers == NULL) { | 
|  | 1033 | console->next = console_drivers; | 
|  | 1034 | console_drivers = console; | 
| Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1035 | if (console->next) | 
|  | 1036 | console->next->flags &= ~CON_CONSDEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } else { | 
|  | 1038 | console->next = console_drivers->next; | 
|  | 1039 | console_drivers->next = console; | 
|  | 1040 | } | 
|  | 1041 | if (console->flags & CON_PRINTBUFFER) { | 
|  | 1042 | /* | 
|  | 1043 | * release_console_sem() will print out the buffered messages | 
|  | 1044 | * for us. | 
|  | 1045 | */ | 
|  | 1046 | spin_lock_irqsave(&logbuf_lock, flags); | 
|  | 1047 | con_start = log_start; | 
|  | 1048 | spin_unlock_irqrestore(&logbuf_lock, flags); | 
|  | 1049 | } | 
|  | 1050 | release_console_sem(); | 
|  | 1051 | } | 
|  | 1052 | EXPORT_SYMBOL(register_console); | 
|  | 1053 |  | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1054 | int unregister_console(struct console *console) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | { | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1056 | struct console *a, *b; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | int res = 1; | 
|  | 1058 |  | 
|  | 1059 | acquire_console_sem(); | 
|  | 1060 | if (console_drivers == console) { | 
|  | 1061 | console_drivers=console->next; | 
|  | 1062 | res = 0; | 
| Benjamin Herrenschmidt | e9b15b5 | 2005-11-23 13:37:44 -0800 | [diff] [blame] | 1063 | } else if (console_drivers) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | for (a=console_drivers->next, b=console_drivers ; | 
|  | 1065 | a; b=a, a=b->next) { | 
|  | 1066 | if (a == console) { | 
|  | 1067 | b->next = a->next; | 
|  | 1068 | res = 0; | 
|  | 1069 | break; | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1070 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } | 
|  | 1072 | } | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1073 |  | 
| Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 1074 | /* | 
| Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1075 | * If this isn't the last console and it has CON_CONSDEV set, we | 
|  | 1076 | * need to set it on the next preferred console. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | */ | 
| Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 1078 | if (console_drivers != NULL && console->flags & CON_CONSDEV) | 
| Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 1079 | console_drivers->flags |= CON_CONSDEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 |  | 
|  | 1081 | release_console_sem(); | 
|  | 1082 | return res; | 
|  | 1083 | } | 
|  | 1084 | EXPORT_SYMBOL(unregister_console); | 
| Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 1085 |  | 
| Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 1086 | static int __init disable_boot_consoles(void) | 
|  | 1087 | { | 
| Robin Getz | cb00e99 | 2007-08-21 23:14:58 -0400 | [diff] [blame] | 1088 | if (console_drivers != NULL) { | 
|  | 1089 | if (console_drivers->flags & CON_BOOT) { | 
|  | 1090 | printk(KERN_INFO "turn off boot console %s%d\n", | 
|  | 1091 | console_drivers->name, console_drivers->index); | 
|  | 1092 | return unregister_console(console_drivers); | 
|  | 1093 | } | 
| Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 1094 | } | 
|  | 1095 | return 0; | 
|  | 1096 | } | 
|  | 1097 | late_initcall(disable_boot_consoles); | 
|  | 1098 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | /** | 
|  | 1100 | * tty_write_message - write a message to a certain tty, not just the console. | 
| Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 1101 | * @tty: the destination tty_struct | 
|  | 1102 | * @msg: the message to write | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | * | 
|  | 1104 | * This is used for messages that need to be redirected to a specific tty. | 
|  | 1105 | * We don't put it into the syslog queue right now maybe in the future if | 
|  | 1106 | * really needed. | 
|  | 1107 | */ | 
|  | 1108 | void tty_write_message(struct tty_struct *tty, char *msg) | 
|  | 1109 | { | 
|  | 1110 | if (tty && tty->driver->write) | 
|  | 1111 | tty->driver->write(tty, msg, strlen(msg)); | 
|  | 1112 | return; | 
|  | 1113 | } | 
|  | 1114 |  | 
|  | 1115 | /* | 
|  | 1116 | * printk rate limiting, lifted from the networking subsystem. | 
|  | 1117 | * | 
|  | 1118 | * This enforces a rate limit: not more than one kernel message | 
|  | 1119 | * every printk_ratelimit_jiffies to make a denial-of-service | 
|  | 1120 | * attack impossible. | 
|  | 1121 | */ | 
|  | 1122 | int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst) | 
|  | 1123 | { | 
|  | 1124 | static DEFINE_SPINLOCK(ratelimit_lock); | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1125 | static unsigned long toks = 10 * 5 * HZ; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | static unsigned long last_msg; | 
|  | 1127 | static int missed; | 
|  | 1128 | unsigned long flags; | 
|  | 1129 | unsigned long now = jiffies; | 
|  | 1130 |  | 
|  | 1131 | spin_lock_irqsave(&ratelimit_lock, flags); | 
|  | 1132 | toks += now - last_msg; | 
|  | 1133 | last_msg = now; | 
|  | 1134 | if (toks > (ratelimit_burst * ratelimit_jiffies)) | 
|  | 1135 | toks = ratelimit_burst * ratelimit_jiffies; | 
|  | 1136 | if (toks >= ratelimit_jiffies) { | 
|  | 1137 | int lost = missed; | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1138 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | missed = 0; | 
|  | 1140 | toks -= ratelimit_jiffies; | 
|  | 1141 | spin_unlock_irqrestore(&ratelimit_lock, flags); | 
|  | 1142 | if (lost) | 
|  | 1143 | printk(KERN_WARNING "printk: %d messages suppressed.\n", lost); | 
|  | 1144 | return 1; | 
|  | 1145 | } | 
|  | 1146 | missed++; | 
|  | 1147 | spin_unlock_irqrestore(&ratelimit_lock, flags); | 
|  | 1148 | return 0; | 
|  | 1149 | } | 
|  | 1150 | EXPORT_SYMBOL(__printk_ratelimit); | 
|  | 1151 |  | 
|  | 1152 | /* minimum time in jiffies between messages */ | 
| Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 1153 | int printk_ratelimit_jiffies = 5 * HZ; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 |  | 
|  | 1155 | /* number of messages we send before ratelimiting */ | 
|  | 1156 | int printk_ratelimit_burst = 10; | 
|  | 1157 |  | 
|  | 1158 | int printk_ratelimit(void) | 
|  | 1159 | { | 
|  | 1160 | return __printk_ratelimit(printk_ratelimit_jiffies, | 
|  | 1161 | printk_ratelimit_burst); | 
|  | 1162 | } | 
|  | 1163 | EXPORT_SYMBOL(printk_ratelimit); | 
| Andrew Morton | f46c483 | 2006-11-02 22:07:16 -0800 | [diff] [blame] | 1164 |  | 
|  | 1165 | /** | 
|  | 1166 | * printk_timed_ratelimit - caller-controlled printk ratelimiting | 
|  | 1167 | * @caller_jiffies: pointer to caller's state | 
|  | 1168 | * @interval_msecs: minimum interval between prints | 
|  | 1169 | * | 
|  | 1170 | * printk_timed_ratelimit() returns true if more than @interval_msecs | 
|  | 1171 | * milliseconds have elapsed since the last time printk_timed_ratelimit() | 
|  | 1172 | * returned true. | 
|  | 1173 | */ | 
|  | 1174 | bool printk_timed_ratelimit(unsigned long *caller_jiffies, | 
|  | 1175 | unsigned int interval_msecs) | 
|  | 1176 | { | 
|  | 1177 | if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) { | 
|  | 1178 | *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs); | 
|  | 1179 | return true; | 
|  | 1180 | } | 
|  | 1181 | return false; | 
|  | 1182 | } | 
|  | 1183 | EXPORT_SYMBOL(printk_timed_ratelimit); |