root/kernel/printk.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_syslog
  2. printk
  3. register_console

   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  */
  13 
  14 #include <stdarg.h>
  15 
  16 #include <asm/segment.h>
  17 #include <asm/system.h>
  18 
  19 #include <linux/errno.h>
  20 #include <linux/sched.h>
  21 #include <linux/kernel.h>
  22 
  23 static char buf[1024];
  24 
  25 extern int vsprintf(char * buf, const char * fmt, va_list args);
  26 extern void console_print(const char *);
  27 
  28 static void (*console_print_proc)(const char *) = 0;
  29 static char log_buf[4096];
  30 static unsigned long log_start = 0;
  31 static unsigned long logged_chars = 0;
  32 unsigned long log_size = 0;
  33 int log_to_console = 1;
  34 struct wait_queue * log_wait = NULL;
  35 
  36 /*
  37  * Commands to sys_syslog:
  38  *
  39  *      0 -- Close the log.  Currently a NOP.
  40  *      1 -- Open and reset log.
  41  *      2 -- Read from the log.
  42  *      3 -- Read up to the last 4k of messages in the ring buffer.
  43  *      4 -- Read and clear last 4k of messages in the ring buffer
  44  *      5 -- Clear ring buffer.
  45  *      6 -- Disable printk's to console
  46  *      7 -- Enable printk's to console
  47  */
  48 int sys_syslog(int type, char * buf, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50         unsigned long i, j, count;
  51         int do_clear = 0;
  52         char c;
  53 
  54         if ((type != 3) && !suser())
  55                 return -EPERM;
  56         switch (type) {
  57                 case 0:         /* Close log */
  58                         return 0;
  59                 case 1:         /* Open and reset log */
  60                         log_start += log_size;
  61                         log_size = 0;
  62                         return 0;
  63                 case 2:         /* Read from log */
  64                         if (!buf || len < 0)
  65                                 return -EINVAL;
  66                         if (!len)
  67                                 return 0;
  68                         verify_area(VERIFY_WRITE,buf,len);
  69                         while (!log_size) {
  70                                 if (current->signal & ~current->blocked)
  71                                         return -ERESTARTSYS;
  72                                 cli();
  73                                 if (!log_size)
  74                                         interruptible_sleep_on(&log_wait);
  75                                 sti();
  76                         }
  77                         i = 0;
  78                         while (log_size && i < len) {
  79                                 c = *((char *) log_buf+log_start);
  80                                 log_start++;
  81                                 log_size--;
  82                                 log_start &= 4095;
  83                                 put_fs_byte(c,buf);
  84                                 buf++;
  85                                 i++;
  86                         }
  87                         return i;
  88                 case 4:         /* Read/clear last 4k of kernel messages */
  89                         do_clear = 1; 
  90                 case 3:         /* Read last 4k of kernel messages */
  91                         if (!buf || len < 0)
  92                                 return -EINVAL;
  93                         if (!len)
  94                                 return 0;
  95                         verify_area(VERIFY_WRITE,buf,len);
  96                         count = len;
  97                         if (count > 4096)
  98                                 count = 4096;
  99                         if (count > logged_chars)
 100                                 count = logged_chars;
 101                         j = log_start + log_size - count;
 102                         for (i = 0; i < count; i++) {
 103                                 c = *((char *) log_buf + (j++ & 4095));
 104                                 put_fs_byte(c, buf++);
 105                         }
 106                         if (do_clear)
 107                                 logged_chars = 0;
 108                         return i;
 109                 case 5:         /* Clear ring buffer */
 110                         logged_chars = 0;
 111                         return 0;
 112                 case 6:         /* Disable logging to console */
 113                         log_to_console = 0;
 114                         return 0;
 115                 case 7:         /* Enable logging to console */
 116                         log_to_console = 1;
 117                         return 0;
 118         }
 119         return -EINVAL;
 120 }
 121                         
 122 
 123 int printk(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         va_list args;
 126         int i,j;
 127 
 128         va_start(args, fmt);
 129         i=vsprintf(buf,fmt,args);
 130         va_end(args);
 131         for (j = 0; j < i ; j++) {
 132                 log_buf[(log_start+log_size) & 4095] = buf[j];
 133                 if (log_size < 4096)
 134                         log_size++;
 135                 else
 136                         log_start++;
 137                 logged_chars++;
 138         }
 139         wake_up_interruptible(&log_wait);
 140         if (log_to_console && console_print_proc)
 141                 (*console_print_proc)(buf);
 142         return i;
 143 }
 144 
 145 /*
 146  * The console driver calls this routine during kernel initialization
 147  * to register the console printing procedure with printk() and to
 148  * print any messages that were printed by the kernel before the
 149  * console priver was initialized.
 150  */
 151 void register_console(void (*proc)(const char *))
     /* [previous][next][first][last][top][bottom][index][help] */
 152 {
 153         int     i,j;
 154         int     p = log_start;
 155         char    buf[16];
 156 
 157         console_print_proc = proc;
 158 
 159         for (i=0,j=0; i < log_size; i++) {
 160                 buf[j++] = log_buf[p];
 161                 p++; p &= 4095;
 162                 if (j < sizeof(buf)-1)
 163                         continue;
 164                 buf[j] = 0;
 165                 (*proc)(buf);
 166                 j = 0;
 167         }
 168         buf[j] = 0;
 169         (*proc)(buf);
 170 }

/* [previous][next][first][last][top][bottom][index][help] */