This commit was manufactured by cvs2svn to create branch
[linux-2.6.git] / kernel / printk.c
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.
13  * Fixed SMP synchronization, 08/08/99, Manfred Spraul 
14  *     manfreds@colorfullife.com
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>
23 #include <linux/smp_lock.h>
24 #include <linux/console.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/interrupt.h>                    /* For in_interrupt() */
28 #include <linux/config.h>
29 #include <linux/delay.h>
30 #include <linux/smp.h>
31 #include <linux/security.h>
32 #include <linux/bootmem.h>
33 #include <linux/syscalls.h>
34 #include <linux/vserver/cvirt.h>
35
36 #include <asm/uaccess.h>
37
38 #define __LOG_BUF_LEN   (1 << CONFIG_LOG_BUF_SHIFT)
39
40 /* printk's without a loglevel use this.. */
41 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
42
43 /* We show everything that is MORE important than this.. */
44 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
45 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
46
47 DECLARE_WAIT_QUEUE_HEAD(log_wait);
48
49 int console_printk[4] = {
50         DEFAULT_CONSOLE_LOGLEVEL,       /* console_loglevel */
51         DEFAULT_MESSAGE_LOGLEVEL,       /* default_message_loglevel */
52         MINIMUM_CONSOLE_LOGLEVEL,       /* minimum_console_loglevel */
53         DEFAULT_CONSOLE_LOGLEVEL,       /* default_console_loglevel */
54 };
55
56 EXPORT_SYMBOL(console_printk);
57
58 /*
59  * Low lever drivers may need that to know if they can schedule in
60  * their unblank() callback or not. So let's export it.
61  */
62 int oops_in_progress;
63 EXPORT_SYMBOL(oops_in_progress);
64
65 /*
66  * console_sem protects the console_drivers list, and also
67  * provides serialisation for access to the entire console
68  * driver system.
69  */
70 static DECLARE_MUTEX(console_sem);
71 struct 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  */
80 static int console_locked;
81
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  */
87 static DEFINE_SPINLOCK(logbuf_lock);
88
89 #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  */
96 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
97 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
98 static unsigned long log_end;   /* Index into log_buf: most-recently-written-char + 1 */
99
100 /*
101  *      Array of consoles built from command line options (console=)
102  */
103 struct 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
112 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
113 static int selected_console = -1;
114 static int preferred_console = -1;
115
116 /* Flag: console code may call schedule() */
117 static int console_may_schedule;
118
119 #ifdef CONFIG_PRINTK
120
121 static char __log_buf[__LOG_BUF_LEN];
122 static char *log_buf = __log_buf;
123 static int log_buf_len = __LOG_BUF_LEN;
124 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
125
126 /*
127  *      Setup a list of consoles. Called from init/main.c
128  */
129 static int __init console_setup(char *str)
130 {
131         char name[sizeof(console_cmdline[0].name)];
132         char *s, *options;
133         int idx;
134
135         /*
136          *      Decode str into name, index, options.
137          */
138         if (str[0] >= '0' && str[0] <= '9') {
139                 strcpy(name, "ttyS");
140                 strncpy(name + 4, str, sizeof(name) - 5);
141         } else
142                 strncpy(name, str, sizeof(name) - 1);
143         name[sizeof(name) - 1] = 0;
144         if ((options = strchr(str, ',')) != NULL)
145                 *(options++) = 0;
146 #ifdef __sparc__
147         if (!strcmp(str, "ttya"))
148                 strcpy(name, "ttyS0");
149         if (!strcmp(str, "ttyb"))
150                 strcpy(name, "ttyS1");
151 #endif
152         for(s = name; *s; s++)
153                 if ((*s >= '0' && *s <= '9') || *s == ',')
154                         break;
155         idx = simple_strtoul(s, NULL, 10);
156         *s = 0;
157
158         add_preferred_console(name, idx, options);
159         return 1;
160 }
161
162 __setup("console=", console_setup);
163
164 static int __init log_buf_len_setup(char *str)
165 {
166         unsigned long size = memparse(str, &str);
167         unsigned long flags;
168
169         if (size)
170                 size = roundup_pow_of_two(size);
171         if (size > log_buf_len) {
172                 unsigned long start, dest_idx, offset;
173                 char * new_log_buf;
174
175                 new_log_buf = alloc_bootmem(size);
176                 if (!new_log_buf) {
177                         printk("log_buf_len: allocation failed\n");
178                         goto out;
179                 }
180
181                 spin_lock_irqsave(&logbuf_lock, flags);
182                 log_buf_len = size;
183                 log_buf = new_log_buf;
184
185                 offset = start = min(con_start, log_start);
186                 dest_idx = 0;
187                 while (start != log_end) {
188                         log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
189                         start++;
190                         dest_idx++;
191                 }
192                 log_start -= offset;
193                 con_start -= offset;
194                 log_end -= offset;
195                 spin_unlock_irqrestore(&logbuf_lock, flags);
196
197                 printk("log_buf_len: %d\n", log_buf_len);
198         }
199 out:
200
201         return 1;
202 }
203
204 __setup("log_buf_len=", log_buf_len_setup);
205
206 /*
207  * Commands to do_syslog:
208  *
209  *      0 -- Close the log.  Currently a NOP.
210  *      1 -- Open the log. Currently a NOP.
211  *      2 -- Read from the log.
212  *      3 -- Read all messages remaining in the ring buffer.
213  *      4 -- Read and clear all messages remaining in the ring buffer
214  *      5 -- Clear ring buffer.
215  *      6 -- Disable printk's to console
216  *      7 -- Enable printk's to console
217  *      8 -- Set level of messages printed to console
218  *      9 -- Return number of unread characters in the log buffer
219  *     10 -- Return size of the log buffer
220  */
221 int do_syslog(int type, char __user * buf, int len)
222 {
223         unsigned long i, j, limit, count;
224         int do_clear = 0;
225         char c;
226         int error;
227
228         error = security_syslog(type);
229         if (error)
230                 return error;
231
232         if ((type >= 2) && (type <= 4)) {
233                 error = -EINVAL;
234                 if (!buf || len < 0)
235                         goto out;
236                 error = 0;
237                 if (!len)
238                         goto out;
239                 if (!access_ok(VERIFY_WRITE, buf, len)) {
240                         error = -EFAULT;
241                         goto out;
242                 }
243         }
244         if (!vx_check(0, VX_ADMIN|VX_WATCH))
245                 return vx_do_syslog(type, buf, len);
246
247         switch (type) {
248         case 0:         /* Close log */
249                 break;
250         case 1:         /* Open log */
251                 break;
252         case 2:         /* Read from log */
253                 error = wait_event_interruptible(log_wait, (log_start - log_end));
254                 if (error)
255                         goto out;
256                 i = 0;
257                 spin_lock_irq(&logbuf_lock);
258                 while (!error && (log_start != log_end) && i < len) {
259                         c = LOG_BUF(log_start);
260                         log_start++;
261                         spin_unlock_irq(&logbuf_lock);
262                         error = __put_user(c,buf);
263                         buf++;
264                         i++;
265                         cond_resched();
266                         spin_lock_irq(&logbuf_lock);
267                 }
268                 spin_unlock_irq(&logbuf_lock);
269                 if (!error)
270                         error = i;
271                 break;
272         case 4:         /* Read/clear last kernel messages */
273                 do_clear = 1; 
274                 /* FALL THRU */
275         case 3:         /* Read last kernel messages */
276                 count = len;
277                 if (count > log_buf_len)
278                         count = log_buf_len;
279                 spin_lock_irq(&logbuf_lock);
280                 if (count > logged_chars)
281                         count = logged_chars;
282                 if (do_clear)
283                         logged_chars = 0;
284                 limit = log_end;
285                 /*
286                  * __put_user() could sleep, and while we sleep
287                  * printk() could overwrite the messages 
288                  * we try to copy to user space. Therefore
289                  * the messages are copied in reverse. <manfreds>
290                  */
291                 for(i = 0; i < count && !error; i++) {
292                         j = limit-1-i;
293                         if (j + log_buf_len < log_end)
294                                 break;
295                         c = LOG_BUF(j);
296                         spin_unlock_irq(&logbuf_lock);
297                         error = __put_user(c,&buf[count-1-i]);
298                         cond_resched();
299                         spin_lock_irq(&logbuf_lock);
300                 }
301                 spin_unlock_irq(&logbuf_lock);
302                 if (error)
303                         break;
304                 error = i;
305                 if(i != count) {
306                         int offset = count-error;
307                         /* buffer overflow during copy, correct user buffer. */
308                         for(i=0;i<error;i++) {
309                                 if (__get_user(c,&buf[i+offset]) ||
310                                     __put_user(c,&buf[i])) {
311                                         error = -EFAULT;
312                                         break;
313                                 }
314                                 cond_resched();
315                         }
316                 }
317                 break;
318         case 5:         /* Clear ring buffer */
319                 logged_chars = 0;
320                 break;
321         case 6:         /* Disable logging to console */
322                 console_loglevel = minimum_console_loglevel;
323                 break;
324         case 7:         /* Enable logging to console */
325                 console_loglevel = default_console_loglevel;
326                 break;
327         case 8:         /* Set level of messages printed to console */
328                 error = -EINVAL;
329                 if (len < 1 || len > 8)
330                         goto out;
331                 if (len < minimum_console_loglevel)
332                         len = minimum_console_loglevel;
333                 console_loglevel = len;
334                 error = 0;
335                 break;
336         case 9:         /* Number of chars in the log buffer */
337                 error = log_end - log_start;
338                 break;
339         case 10:        /* Size of the log buffer */
340                 error = log_buf_len;
341                 break;
342         default:
343                 error = -EINVAL;
344                 break;
345         }
346 out:
347         return error;
348 }
349
350 asmlinkage long sys_syslog(int type, char __user * buf, int len)
351 {
352         return do_syslog(type, buf, len);
353 }
354
355 /*
356  * Crashdump special routine. Don't print to global log_buf, just to the
357  * actual console device(s).
358  */
359 static void crashdump_call_console_drivers(const char *buf, unsigned long len)
360 {
361         struct console *con;
362
363         for (con = console_drivers; con; con = con->next) {
364                 if ((con->flags & CON_ENABLED) && con->write)
365                         con->write(con, buf, len);
366         }
367 }
368
369 /*
370  * Call the console drivers on a range of log_buf
371  */
372 static void __call_console_drivers(unsigned long start, unsigned long end)
373 {
374         struct console *con;
375
376         for (con = console_drivers; con; con = con->next) {
377                 if ((con->flags & CON_ENABLED) && con->write)
378                         con->write(con, &LOG_BUF(start), end - start);
379         }
380 }
381
382 /*
383  * Write out chars from start to end - 1 inclusive
384  */
385 static void _call_console_drivers(unsigned long start,
386                                 unsigned long end, int msg_log_level)
387 {
388         if (msg_log_level < console_loglevel &&
389                         console_drivers && start != end) {
390                 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
391                         /* wrapped write */
392                         __call_console_drivers(start & LOG_BUF_MASK,
393                                                 log_buf_len);
394                         __call_console_drivers(0, end & LOG_BUF_MASK);
395                 } else {
396                         __call_console_drivers(start, end);
397                 }
398         }
399 }
400
401 /*
402  * Call the console drivers, asking them to write out
403  * log_buf[start] to log_buf[end - 1].
404  * The console_sem must be held.
405  */
406 static void call_console_drivers(unsigned long start, unsigned long end)
407 {
408         unsigned long cur_index, start_print;
409         static int msg_level = -1;
410
411         if (((long)(start - end)) > 0)
412                 BUG();
413
414         cur_index = start;
415         start_print = start;
416         while (cur_index != end) {
417                 if (    msg_level < 0 &&
418                         ((end - cur_index) > 2) &&
419                         LOG_BUF(cur_index + 0) == '<' &&
420                         LOG_BUF(cur_index + 1) >= '0' &&
421                         LOG_BUF(cur_index + 1) <= '7' &&
422                         LOG_BUF(cur_index + 2) == '>')
423                 {
424                         msg_level = LOG_BUF(cur_index + 1) - '0';
425                         cur_index += 3;
426                         start_print = cur_index;
427                 }
428                 while (cur_index != end) {
429                         char c = LOG_BUF(cur_index);
430                         cur_index++;
431
432                         if (c == '\n') {
433                                 if (msg_level < 0) {
434                                         /*
435                                          * printk() has already given us loglevel tags in
436                                          * the buffer.  This code is here in case the
437                                          * log buffer has wrapped right round and scribbled
438                                          * on those tags
439                                          */
440                                         msg_level = default_message_loglevel;
441                                 }
442                                 _call_console_drivers(start_print, cur_index, msg_level);
443                                 msg_level = -1;
444                                 start_print = cur_index;
445                                 break;
446                         }
447                 }
448         }
449         _call_console_drivers(start_print, end, msg_level);
450 }
451
452 static void emit_log_char(char c)
453 {
454         LOG_BUF(log_end) = c;
455         log_end++;
456         if (log_end - log_start > log_buf_len)
457                 log_start = log_end - log_buf_len;
458         if (log_end - con_start > log_buf_len)
459                 con_start = log_end - log_buf_len;
460         if (logged_chars < log_buf_len)
461                 logged_chars++;
462 }
463
464 /*
465  * Zap console related locks when oopsing. Only zap at most once
466  * every 10 seconds, to leave time for slow consoles to print a
467  * full oops.
468  */
469 static void zap_locks(void)
470 {
471         static unsigned long oops_timestamp;
472
473         if (time_after_eq(jiffies, oops_timestamp) &&
474                         !time_after(jiffies, oops_timestamp + 30*HZ))
475                 return;
476
477         oops_timestamp = jiffies;
478
479         /* If a crash is occurring, make sure we can't deadlock */
480         spin_lock_init(&logbuf_lock);
481         /* And make sure that we print immediately */
482         init_MUTEX(&console_sem);
483 }
484
485 #if defined(CONFIG_PRINTK_TIME)
486 static int printk_time = 1;
487 #else
488 static int printk_time = 0;
489 #endif
490
491 static int __init printk_time_setup(char *str)
492 {
493         if (*str)
494                 return 0;
495         printk_time = 1;
496         return 1;
497 }
498
499 __setup("time", printk_time_setup);
500
501 /*
502  * This is printk.  It can be called from any context.  We want it to work.
503  * 
504  * We try to grab the console_sem.  If we succeed, it's easy - we log the output and
505  * call the console drivers.  If we fail to get the semaphore we place the output
506  * into the log buffer and return.  The current holder of the console_sem will
507  * notice the new output in release_console_sem() and will send it to the
508  * consoles before releasing the semaphore.
509  *
510  * One effect of this deferred printing is that code which calls printk() and
511  * then changes console_loglevel may break. This is because console_loglevel
512  * is inspected when the actual printing occurs.
513  */
514
515 asmlinkage int printk(const char *fmt, ...)
516 {
517         va_list args;
518         int r;
519
520         va_start(args, fmt);
521         r = vprintk(fmt, args);
522         va_end(args);
523
524         return r;
525 }
526
527 static volatile int printk_cpu = -1;
528
529 asmlinkage int vprintk(const char *fmt, va_list args)
530 {
531         unsigned long flags;
532         int printed_len;
533         char *p;
534         static char printk_buf[1024];
535         static int log_level_unknown = 1;
536
537         if (unlikely(oops_in_progress && printk_cpu == smp_processor_id()))
538                 zap_locks();
539
540         /* This stops the holder of console_sem just where we want him */
541         spin_lock_irqsave(&logbuf_lock, flags);
542         printk_cpu = smp_processor_id();
543
544         /* Emit the output into the temporary buffer */
545         printed_len = vscnprintf(printk_buf, sizeof(printk_buf), fmt, args);
546
547         if (unlikely(crashdump_mode())) {
548                 crashdump_call_console_drivers(printk_buf, printed_len);
549                 spin_unlock_irqrestore(&logbuf_lock, flags);
550                 goto out;
551         }
552
553         /*
554          * Copy the output into log_buf.  If the caller didn't provide
555          * appropriate log level tags, we insert them here
556          */
557         for (p = printk_buf; *p; p++) {
558                 if (log_level_unknown) {
559                         /* log_level_unknown signals the start of a new line */
560                         if (printk_time) {
561                                 int loglev_char;
562                                 char tbuf[50], *tp;
563                                 unsigned tlen;
564                                 unsigned long long t;
565                                 unsigned long nanosec_rem;
566
567                                 /*
568                                  * force the log level token to be
569                                  * before the time output.
570                                  */
571                                 if (p[0] == '<' && p[1] >='0' &&
572                                    p[1] <= '7' && p[2] == '>') {
573                                         loglev_char = p[1];
574                                         p += 3;
575                                         printed_len += 3;
576                                 } else {
577                                         loglev_char = default_message_loglevel
578                                                 + '0';
579                                 }
580                                 t = sched_clock();
581                                 nanosec_rem = do_div(t, 1000000000);
582                                 tlen = sprintf(tbuf,
583                                                 "<%c>[%5lu.%06lu] ",
584                                                 loglev_char,
585                                                 (unsigned long)t,
586                                                 nanosec_rem/1000);
587
588                                 for (tp = tbuf; tp < tbuf + tlen; tp++)
589                                         emit_log_char(*tp);
590                                 printed_len += tlen - 3;
591                         } else {
592                                 if (p[0] != '<' || p[1] < '0' ||
593                                    p[1] > '7' || p[2] != '>') {
594                                         emit_log_char('<');
595                                         emit_log_char(default_message_loglevel
596                                                 + '0');
597                                         emit_log_char('>');
598                                 }
599                                 printed_len += 3;
600                         }
601                         log_level_unknown = 0;
602                         if (!*p)
603                                 break;
604                 }
605                 emit_log_char(*p);
606                 if (*p == '\n')
607                         log_level_unknown = 1;
608         }
609
610         if (!cpu_online(smp_processor_id()) &&
611             system_state != SYSTEM_RUNNING) {
612                 /*
613                  * Some console drivers may assume that per-cpu resources have
614                  * been allocated.  So don't allow them to be called by this
615                  * CPU until it is officially up.  We shouldn't be calling into
616                  * random console drivers on a CPU which doesn't exist yet..
617                  */
618                 spin_unlock_irqrestore(&logbuf_lock, flags);
619                 goto out;
620         }
621         if (!down_trylock(&console_sem)) {
622                 console_locked = 1;
623                 /*
624                  * We own the drivers.  We can drop the spinlock and let
625                  * release_console_sem() print the text
626                  */
627                 spin_unlock_irqrestore(&logbuf_lock, flags);
628                 console_may_schedule = 0;
629                 release_console_sem();
630         } else {
631                 /*
632                  * Someone else owns the drivers.  We drop the spinlock, which
633                  * allows the semaphore holder to proceed and to call the
634                  * console drivers with the output which we just produced.
635                  */
636                 spin_unlock_irqrestore(&logbuf_lock, flags);
637         }
638 out:
639         return printed_len;
640 }
641 EXPORT_SYMBOL(printk);
642 EXPORT_SYMBOL(vprintk);
643
644 #else
645
646 asmlinkage long sys_syslog(int type, char __user * buf, int len)
647 {
648         return 0;
649 }
650
651 int do_syslog(int type, char __user * buf, int len) { return 0; }
652 static void call_console_drivers(unsigned long start, unsigned long end) {}
653
654 #endif
655
656 /**
657  * add_preferred_console - add a device to the list of preferred consoles.
658  *
659  * The last preferred console added will be used for kernel messages
660  * and stdin/out/err for init.  Normally this is used by console_setup
661  * above to handle user-supplied console arguments; however it can also
662  * be used by arch-specific code either to override the user or more
663  * commonly to provide a default console (ie from PROM variables) when
664  * the user has not supplied one.
665  */
666 int __init add_preferred_console(char *name, int idx, char *options)
667 {
668         struct console_cmdline *c;
669         int i;
670
671         /*
672          *      See if this tty is not yet registered, and
673          *      if we have a slot free.
674          */
675         for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
676                 if (strcmp(console_cmdline[i].name, name) == 0 &&
677                           console_cmdline[i].index == idx) {
678                                 selected_console = i;
679                                 return 0;
680                 }
681         if (i == MAX_CMDLINECONSOLES)
682                 return -E2BIG;
683         selected_console = i;
684         c = &console_cmdline[i];
685         memcpy(c->name, name, sizeof(c->name));
686         c->name[sizeof(c->name) - 1] = 0;
687         c->options = options;
688         c->index = idx;
689         return 0;
690 }
691
692 /**
693  * acquire_console_sem - lock the console system for exclusive use.
694  *
695  * Acquires a semaphore which guarantees that the caller has
696  * exclusive access to the console system and the console_drivers list.
697  *
698  * Can sleep, returns nothing.
699  */
700 void acquire_console_sem(void)
701 {
702         if (in_interrupt())
703                 BUG();
704         down(&console_sem);
705         console_locked = 1;
706         console_may_schedule = 1;
707 }
708 EXPORT_SYMBOL(acquire_console_sem);
709
710 int try_acquire_console_sem(void)
711 {
712         if (down_trylock(&console_sem))
713                 return -1;
714         console_locked = 1;
715         console_may_schedule = 0;
716         return 0;
717 }
718 EXPORT_SYMBOL(try_acquire_console_sem);
719
720 int is_console_locked(void)
721 {
722         return console_locked;
723 }
724 EXPORT_SYMBOL(is_console_locked);
725
726 /**
727  * release_console_sem - unlock the console system
728  *
729  * Releases the semaphore which the caller holds on the console system
730  * and the console driver list.
731  *
732  * While the semaphore was held, console output may have been buffered
733  * by printk().  If this is the case, release_console_sem() emits
734  * the output prior to releasing the semaphore.
735  *
736  * If there is output waiting for klogd, we wake it up.
737  *
738  * release_console_sem() may be called from any context.
739  */
740 void release_console_sem(void)
741 {
742         unsigned long flags;
743         unsigned long _con_start, _log_end;
744         unsigned long wake_klogd = 0;
745
746         for ( ; ; ) {
747                 spin_lock_irqsave(&logbuf_lock, flags);
748                 wake_klogd |= log_start - log_end;
749                 if (con_start == log_end)
750                         break;                  /* Nothing to print */
751                 _con_start = con_start;
752                 _log_end = log_end;
753                 con_start = log_end;            /* Flush */
754                 spin_unlock(&logbuf_lock);
755                 call_console_drivers(_con_start, _log_end);
756                 local_irq_restore(flags);
757         }
758         console_locked = 0;
759         console_may_schedule = 0;
760         up(&console_sem);
761         spin_unlock_irqrestore(&logbuf_lock, flags);
762         if (wake_klogd && !oops_in_progress && waitqueue_active(&log_wait))
763                 wake_up_interruptible(&log_wait);
764 }
765 EXPORT_SYMBOL(release_console_sem);
766
767 /** console_conditional_schedule - yield the CPU if required
768  *
769  * If the console code is currently allowed to sleep, and
770  * if this CPU should yield the CPU to another task, do
771  * so here.
772  *
773  * Must be called within acquire_console_sem().
774  */
775 void __sched console_conditional_schedule(void)
776 {
777         if (console_may_schedule)
778                 cond_resched();
779 }
780 EXPORT_SYMBOL(console_conditional_schedule);
781
782 void console_print(const char *s)
783 {
784         printk(KERN_EMERG "%s", s);
785 }
786 EXPORT_SYMBOL(console_print);
787
788 void console_unblank(void)
789 {
790         struct console *c;
791
792         /*
793          * console_unblank can no longer be called in interrupt context unless
794          * oops_in_progress is set to 1..
795          */
796         if (oops_in_progress) {
797                 if (down_trylock(&console_sem) != 0)
798                         return;
799         } else
800                 acquire_console_sem();
801
802         console_locked = 1;
803         console_may_schedule = 0;
804         for (c = console_drivers; c != NULL; c = c->next)
805                 if ((c->flags & CON_ENABLED) && c->unblank)
806                         c->unblank();
807         release_console_sem();
808 }
809 EXPORT_SYMBOL(console_unblank);
810
811 /*
812  * Return the console tty driver structure and its associated index
813  */
814 struct tty_driver *console_device(int *index)
815 {
816         struct console *c;
817         struct tty_driver *driver = NULL;
818
819         acquire_console_sem();
820         for (c = console_drivers; c != NULL; c = c->next) {
821                 if (!c->device)
822                         continue;
823                 driver = c->device(c, index);
824                 if (driver)
825                         break;
826         }
827         release_console_sem();
828         return driver;
829 }
830
831 /*
832  * Prevent further output on the passed console device so that (for example)
833  * serial drivers can disable console output before suspending a port, and can
834  * re-enable output afterwards.
835  */
836 void console_stop(struct console *console)
837 {
838         acquire_console_sem();
839         console->flags &= ~CON_ENABLED;
840         release_console_sem();
841 }
842 EXPORT_SYMBOL(console_stop);
843
844 void console_start(struct console *console)
845 {
846         acquire_console_sem();
847         console->flags |= CON_ENABLED;
848         release_console_sem();
849 }
850 EXPORT_SYMBOL(console_start);
851
852 /*
853  * The console driver calls this routine during kernel initialization
854  * to register the console printing procedure with printk() and to
855  * print any messages that were printed by the kernel before the
856  * console driver was initialized.
857  */
858 void register_console(struct console * console)
859 {
860         int     i;
861         unsigned long flags;
862
863         if (preferred_console < 0)
864                 preferred_console = selected_console;
865
866         /*
867          *      See if we want to use this console driver. If we
868          *      didn't select a console we take the first one
869          *      that registers here.
870          */
871         if (preferred_console < 0) {
872                 if (console->index < 0)
873                         console->index = 0;
874                 if (console->setup == NULL ||
875                     console->setup(console, NULL) == 0) {
876                         console->flags |= CON_ENABLED | CON_CONSDEV;
877                         preferred_console = 0;
878                 }
879         }
880
881         /*
882          *      See if this console matches one we selected on
883          *      the command line.
884          */
885         for(i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) {
886                 if (strcmp(console_cmdline[i].name, console->name) != 0)
887                         continue;
888                 if (console->index >= 0 &&
889                     console->index != console_cmdline[i].index)
890                         continue;
891                 if (console->index < 0)
892                         console->index = console_cmdline[i].index;
893                 if (console->setup &&
894                     console->setup(console, console_cmdline[i].options) != 0)
895                         break;
896                 console->flags |= CON_ENABLED;
897                 console->index = console_cmdline[i].index;
898                 if (i == preferred_console)
899                         console->flags |= CON_CONSDEV;
900                 break;
901         }
902
903         if (!(console->flags & CON_ENABLED))
904                 return;
905
906         if (console_drivers && (console_drivers->flags & CON_BOOT)) {
907                 unregister_console(console_drivers);
908                 console->flags &= ~CON_PRINTBUFFER;
909         }
910
911         /*
912          *      Put this console in the list - keep the
913          *      preferred driver at the head of the list.
914          */
915         acquire_console_sem();
916         if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
917                 console->next = console_drivers;
918                 console_drivers = console;
919         } else {
920                 console->next = console_drivers->next;
921                 console_drivers->next = console;
922         }
923         if (console->flags & CON_PRINTBUFFER) {
924                 /*
925                  * release_console_sem() will print out the buffered messages
926                  * for us.
927                  */
928                 spin_lock_irqsave(&logbuf_lock, flags);
929                 con_start = log_start;
930                 spin_unlock_irqrestore(&logbuf_lock, flags);
931         }
932         release_console_sem();
933 }
934 EXPORT_SYMBOL(register_console);
935
936 int unregister_console(struct console * console)
937 {
938         struct console *a,*b;
939         int res = 1;
940
941         acquire_console_sem();
942         if (console_drivers == console) {
943                 console_drivers=console->next;
944                 res = 0;
945         } else {
946                 for (a=console_drivers->next, b=console_drivers ;
947                      a; b=a, a=b->next) {
948                         if (a == console) {
949                                 b->next = a->next;
950                                 res = 0;
951                                 break;
952                         }  
953                 }
954         }
955         
956         /* If last console is removed, we re-enable picking the first
957          * one that gets registered. Without that, pmac early boot console
958          * would prevent fbcon from taking over.
959          */
960         if (console_drivers == NULL)
961                 preferred_console = selected_console;
962                 
963
964         release_console_sem();
965         return res;
966 }
967 EXPORT_SYMBOL(unregister_console);
968
969 /**
970  * tty_write_message - write a message to a certain tty, not just the console.
971  *
972  * This is used for messages that need to be redirected to a specific tty.
973  * We don't put it into the syslog queue right now maybe in the future if
974  * really needed.
975  */
976 void tty_write_message(struct tty_struct *tty, char *msg)
977 {
978         if (tty && tty->driver->write)
979                 tty->driver->write(tty, msg, strlen(msg));
980         return;
981 }
982
983 /*
984  * printk rate limiting, lifted from the networking subsystem.
985  *
986  * This enforces a rate limit: not more than one kernel message
987  * every printk_ratelimit_jiffies to make a denial-of-service
988  * attack impossible.
989  */
990 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
991 {
992         static DEFINE_SPINLOCK(ratelimit_lock);
993         static unsigned long toks = 10*5*HZ;
994         static unsigned long last_msg;
995         static int missed;
996         unsigned long flags;
997         unsigned long now = jiffies;
998
999         spin_lock_irqsave(&ratelimit_lock, flags);
1000         toks += now - last_msg;
1001         last_msg = now;
1002         if (toks > (ratelimit_burst * ratelimit_jiffies))
1003                 toks = ratelimit_burst * ratelimit_jiffies;
1004         if (toks >= ratelimit_jiffies) {
1005                 int lost = missed;
1006                 missed = 0;
1007                 toks -= ratelimit_jiffies;
1008                 spin_unlock_irqrestore(&ratelimit_lock, flags);
1009                 if (lost)
1010                         printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1011                 return 1;
1012         }
1013         missed++;
1014         spin_unlock_irqrestore(&ratelimit_lock, flags);
1015         return 0;
1016 }
1017 EXPORT_SYMBOL(__printk_ratelimit);
1018
1019 /* minimum time in jiffies between messages */
1020 int printk_ratelimit_jiffies = 5*HZ;
1021
1022 /* number of messages we send before ratelimiting */
1023 int printk_ratelimit_burst = 10;
1024
1025 int printk_ratelimit(void)
1026 {
1027         return __printk_ratelimit(printk_ratelimit_jiffies,
1028                                 printk_ratelimit_burst);
1029 }
1030 EXPORT_SYMBOL(printk_ratelimit);