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 the log. Currently a NOP.
  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 log */
  60                         return 0;
  61                 case 2:         /* Read from log */
  62                         if (!buf || len < 0)
  63                                 return -EINVAL;
  64                         if (!len)
  65                                 return 0;
  66                         verify_area(VERIFY_WRITE,buf,len);
  67                         while (!log_size) {
  68                                 if (current->signal & ~current->blocked)
  69                                         return -ERESTARTSYS;
  70                                 cli();
  71                                 if (!log_size)
  72                                         interruptible_sleep_on(&log_wait);
  73                                 sti();
  74                         }
  75                         i = 0;
  76                         while (log_size && i < len) {
  77                                 c = *((char *) log_buf+log_start);
  78                                 log_start++;
  79                                 log_size--;
  80                                 log_start &= 4095;
  81                                 put_fs_byte(c,buf);
  82                                 buf++;
  83                                 i++;
  84                         }
  85                         return i;
  86                 case 4:         /* Read/clear last 4k of kernel messages */
  87                         do_clear = 1; 
  88                 case 3:         /* Read last 4k of kernel messages */
  89                         if (!buf || len < 0)
  90                                 return -EINVAL;
  91                         if (!len)
  92                                 return 0;
  93                         verify_area(VERIFY_WRITE,buf,len);
  94                         count = len;
  95                         if (count > 4096)
  96                                 count = 4096;
  97                         if (count > logged_chars)
  98                                 count = logged_chars;
  99                         j = log_start + log_size - count;
 100                         for (i = 0; i < count; i++) {
 101                                 c = *((char *) log_buf + (j++ & 4095));
 102                                 put_fs_byte(c, buf++);
 103                         }
 104                         if (do_clear)
 105                                 logged_chars = 0;
 106                         return i;
 107                 case 5:         /* Clear ring buffer */
 108                         logged_chars = 0;
 109                         return 0;
 110                 case 6:         /* Disable logging to console */
 111                         log_to_console = 0;
 112                         return 0;
 113                 case 7:         /* Enable logging to console */
 114                         log_to_console = 1;
 115                         return 0;
 116         }
 117         return -EINVAL;
 118 }
 119                         
 120 
 121 int printk(const char *fmt, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 122 {
 123         va_list args;
 124         int i,j;
 125 
 126         va_start(args, fmt);
 127         i=vsprintf(buf,fmt,args);
 128         va_end(args);
 129         for (j = 0; j < i ; j++) {
 130                 log_buf[(log_start+log_size) & 4095] = buf[j];
 131                 if (log_size < 4096)
 132                         log_size++;
 133                 else
 134                         log_start++;
 135                 logged_chars++;
 136         }
 137         wake_up_interruptible(&log_wait);
 138         if (log_to_console && console_print_proc)
 139                 (*console_print_proc)(buf);
 140         return i;
 141 }
 142 
 143 /*
 144  * The console driver calls this routine during kernel initialization
 145  * to register the console printing procedure with printk() and to
 146  * print any messages that were printed by the kernel before the
 147  * console priver was initialized.
 148  */
 149 void register_console(void (*proc)(const char *))
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151         int     i,j;
 152         int     p = log_start;
 153         char    buf[16];
 154 
 155         console_print_proc = proc;
 156 
 157         for (i=0,j=0; i < log_size; i++) {
 158                 buf[j++] = log_buf[p];
 159                 p++; p &= 4095;
 160                 if (j < sizeof(buf)-1)
 161                         continue;
 162                 buf[j] = 0;
 163                 (*proc)(buf);
 164                 j = 0;
 165         }
 166         buf[j] = 0;
 167         (*proc)(buf);
 168 }

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