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 #define LOG_BUF_LEN     4096
  24 
  25 static char buf[1024];
  26 
  27 extern int vsprintf(char * buf, const char * fmt, va_list args);
  28 extern void console_print(const char *);
  29 
  30 #define DEFAULT_MESSAGE_LOGLEVEL 7 /* KERN_DEBUG */
  31 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything more serious than KERN_DEBUG */
  32 
  33 unsigned long log_size = 0;
  34 struct wait_queue * log_wait = NULL;
  35 int console_loglevel = DEFAULT_CONSOLE_LOGLEVEL;
  36 
  37 static void (*console_print_proc)(const char *) = 0;
  38 static char log_buf[LOG_BUF_LEN];
  39 static unsigned long log_start = 0;
  40 static unsigned long logged_chars = 0;
  41 
  42 /*
  43  * Commands to sys_syslog:
  44  *
  45  *      0 -- Close the log.  Currently a NOP.
  46  *      1 -- Open the log. Currently a NOP.
  47  *      2 -- Read from the log.
  48  *      3 -- Read up to the last 4k of messages in the ring buffer.
  49  *      4 -- Read and clear last 4k of messages in the ring buffer
  50  *      5 -- Clear ring buffer.
  51  *      6 -- Disable printk's to console
  52  *      7 -- Enable printk's to console
  53  *      8 -- Set level of messages printed to console
  54  */
  55 asmlinkage int sys_syslog(int type, char * buf, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57         unsigned long i, j, count;
  58         int do_clear = 0;
  59         char c;
  60         int error;
  61 
  62         if ((type != 3) && !suser())
  63                 return -EPERM;
  64         switch (type) {
  65                 case 0:         /* Close log */
  66                         return 0;
  67                 case 1:         /* Open log */
  68                         return 0;
  69                 case 2:         /* Read from log */
  70                         if (!buf || len < 0)
  71                                 return -EINVAL;
  72                         if (!len)
  73                                 return 0;
  74                         error = verify_area(VERIFY_WRITE,buf,len);
  75                         if (error)
  76                                 return error;
  77                         cli();
  78                         while (!log_size) {
  79                                 if (current->signal & ~current->blocked) {
  80                                         sti();
  81                                         return -ERESTARTSYS;
  82                                 }
  83                                         interruptible_sleep_on(&log_wait);
  84                         }
  85                         i = 0;
  86                         while (log_size && i < len) {
  87                                 c = *((char *) log_buf+log_start);
  88                                 log_start++;
  89                                 log_size--;
  90                                 log_start &= LOG_BUF_LEN-1;
  91                                 put_fs_byte(c,buf);
  92                                 buf++;
  93                                 i++;
  94                         }
  95                         sti();
  96                         return i;
  97                 case 4:         /* Read/clear last kernel messages */
  98                         do_clear = 1; 
  99                         /* FALL THRU */
 100                 case 3:         /* Read last kernel messages */
 101                         if (!buf || len < 0)
 102                                 return -EINVAL;
 103                         if (!len)
 104                                 return 0;
 105                         error = verify_area(VERIFY_WRITE,buf,len);
 106                         if (error)
 107                                 return error;
 108                         count = len;
 109                         if (count > LOG_BUF_LEN)
 110                                 count = LOG_BUF_LEN;
 111                         if (count > logged_chars)
 112                                 count = logged_chars;
 113                         j = log_start + log_size - count;
 114                         for (i = 0; i < count; i++) {
 115                                 c = *((char *) log_buf+(j++ & (LOG_BUF_LEN-1)));
 116                                 put_fs_byte(c, buf++);
 117                         }
 118                         if (do_clear)
 119                                 logged_chars = 0;
 120                         return i;
 121                 case 5:         /* Clear ring buffer */
 122                         logged_chars = 0;
 123                         return 0;
 124                 case 6:         /* Disable logging to console */
 125                         console_loglevel = 1; /* only panic messages shown */
 126                         return 0;
 127                 case 7:         /* Enable logging to console */
 128                         console_loglevel = DEFAULT_CONSOLE_LOGLEVEL;
 129                         return 0;
 130                 case 8:
 131                         if (len < 0 || len > 8)
 132                                 return -EINVAL;
 133                         console_loglevel = len;
 134                         return 0;
 135         }
 136         return -EINVAL;
 137 }
 138 
 139 
 140 asmlinkage int printk(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 141 {
 142         va_list args;
 143         int i;
 144         char *msg, *p, *buf_end;
 145         static char msg_level = -1;
 146         long flags;
 147 
 148         save_flags(flags);
 149         cli();
 150         va_start(args, fmt);
 151         i = vsprintf(buf + 3, fmt, args); /* hopefully i < sizeof(buf)-4 */
 152         buf_end = buf + 3 + i;
 153         va_end(args);
 154         for (p = buf + 3; p < buf_end; p++) {
 155                 msg = p;
 156                 if (msg_level < 0) {
 157                         if (
 158                                 p[0] != '<' ||
 159                                 p[1] < '0' || 
 160                                 p[1] > '7' ||
 161                                 p[2] != '>'
 162                         ) {
 163                                 p -= 3;
 164                                 p[0] = '<';
 165                                 p[1] = DEFAULT_MESSAGE_LOGLEVEL - 1 + '0';
 166                                 p[2] = '>';
 167                         } else
 168                                 msg += 3;
 169                         msg_level = p[1] - '0';
 170                 }
 171                 for (; p < buf_end; p++) {
 172                         log_buf[(log_start+log_size) & (LOG_BUF_LEN-1)] = *p;
 173                         if (log_size < LOG_BUF_LEN)
 174                                 log_size++;
 175                         else
 176                                 log_start++;
 177                         logged_chars++;
 178                         if (*p == '\n')
 179                                 break;
 180                 }
 181                 if (msg_level < console_loglevel && console_print_proc) {
 182                         char tmp = p[1];
 183                         p[1] = '\0';
 184                         (*console_print_proc)(msg);
 185                         p[1] = tmp;
 186                 }
 187                 if (*p == '\n')
 188                         msg_level = -1;
 189         }
 190         restore_flags(flags);
 191         wake_up_interruptible(&log_wait);
 192         return i;
 193 }
 194 
 195 /*
 196  * The console driver calls this routine during kernel initialization
 197  * to register the console printing procedure with printk() and to
 198  * print any messages that were printed by the kernel before the
 199  * console driver was initialized.
 200  */
 201 void register_console(void (*proc)(const char *))
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203         int     i,j;
 204         int     p = log_start;
 205         char    buf[16];
 206         char    msg_level = -1;
 207         char    *q;
 208 
 209         console_print_proc = proc;
 210 
 211         for (i=0,j=0; i < log_size; i++) {
 212                 buf[j++] = log_buf[p];
 213                 p++; p &= LOG_BUF_LEN-1;
 214                 if (buf[j-1] != '\n' && i < log_size - 1 && j < sizeof(buf)-1)
 215                         continue;
 216                 buf[j] = 0;
 217                 q = buf;
 218                 if (msg_level < 0) {
 219                         msg_level = buf[1] - '0';
 220                         q = buf + 3;
 221                 }
 222                 if (msg_level < console_loglevel)
 223                         (*proc)(q);
 224                 if (buf[j-1] == '\n')
 225                         msg_level = -1;
 226                 j = 0;
 227         }
 228 }

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