2 * linux/kernel/printk.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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).
12 * Modified for sysctl support, 1/8/97, Chris Horn.
17 #include <linux/errno.h>
18 #include <linux/sched.h>
20 #include <linux/tty_driver.h>
21 #include <linux/smp_lock.h>
22 #include <linux/console.h>
23 #include <linux/init.h>
25 #include <asm/uaccess.h>
27 #define LOG_BUF_LEN (16384)
29 static char buf
[1024];
31 /* printk's without a loglevel use this.. */
32 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
34 /* We show everything that is MORE important than this.. */
35 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
36 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
38 unsigned long log_size
= 0;
39 struct wait_queue
* log_wait
= NULL
;
41 /* Keep together for sysctl support */
42 int console_loglevel
= DEFAULT_CONSOLE_LOGLEVEL
;
43 int default_message_loglevel
= DEFAULT_MESSAGE_LOGLEVEL
;
44 int minimum_console_loglevel
= MINIMUM_CONSOLE_LOGLEVEL
;
45 int default_console_loglevel
= DEFAULT_CONSOLE_LOGLEVEL
;
47 struct console
*console_drivers
= NULL
;
48 static char log_buf
[LOG_BUF_LEN
];
49 static unsigned long log_start
= 0;
50 static unsigned long logged_chars
= 0;
51 struct console_cmdline console_cmdline
[MAX_CMDLINECONSOLES
];
52 static int preferred_console
= -1;
55 * Setup a list of consoles. Called from init/main.c
57 void __init
console_setup(char *str
, int *ints
)
59 struct console_cmdline
*c
;
60 char name
[sizeof(c
->name
)];
65 * Decode str into name, index, options.
67 if (str
[0] >= '0' && str
[0] <= '9') {
69 strncpy(name
+ 4, str
, sizeof(name
) - 5);
71 strncpy(name
, str
, sizeof(name
) - 1);
72 name
[sizeof(name
) - 1] = 0;
73 if ((options
= strchr(str
, ',')) != NULL
)
76 if (!strcmp(str
, "ttya"))
77 strcpy(name
, "ttyS0");
78 if (!strcmp(str
, "ttyb"))
79 strcpy(name
, "ttyS1");
81 for(s
= name
; *s
; s
++)
82 if (*s
>= '0' && *s
<= '9')
84 idx
= simple_strtoul(s
, NULL
, 10);
88 * See if this tty is not yet registered, and
89 * if we have a slot free.
91 for(i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0]; i
++)
92 if (strcmp(console_cmdline
[i
].name
, name
) == 0 &&
93 console_cmdline
[i
].index
== idx
) {
94 preferred_console
= i
;
97 if (i
== MAX_CMDLINECONSOLES
)
99 preferred_console
= i
;
100 c
= &console_cmdline
[i
];
101 memcpy(c
->name
, name
, sizeof(c
->name
));
102 c
->options
= options
;
108 * Commands to do_syslog:
110 * 0 -- Close the log. Currently a NOP.
111 * 1 -- Open the log. Currently a NOP.
112 * 2 -- Read from the log.
113 * 3 -- Read up to the last 4k of messages in the ring buffer.
114 * 4 -- Read and clear last 4k of messages in the ring buffer
115 * 5 -- Clear ring buffer.
116 * 6 -- Disable printk's to console
117 * 7 -- Enable printk's to console
118 * 8 -- Set level of messages printed to console
120 int do_syslog(int type
, char * buf
, int len
)
122 unsigned long i
, j
, count
, flags
;
130 case 0: /* Close log */
132 case 1: /* Open log */
134 case 2: /* Read from log */
141 error
= verify_area(VERIFY_WRITE
,buf
,len
);
145 error
= -ERESTARTSYS
;
147 if (signal_pending(current
)) {
151 interruptible_sleep_on(&log_wait
);
154 while (log_size
&& i
< len
) {
155 c
= *((char *) log_buf
+log_start
);
158 log_start
&= LOG_BUF_LEN
-1;
168 case 4: /* Read/clear last kernel messages */
171 case 3: /* Read last kernel messages */
178 error
= verify_area(VERIFY_WRITE
,buf
,len
);
182 * The logged_chars, log_start, and log_size values may
183 * change from an interrupt, so we disable interrupts.
188 if (count
> LOG_BUF_LEN
)
190 if (count
> logged_chars
)
191 count
= logged_chars
;
192 j
= log_start
+ log_size
- count
;
193 __restore_flags(flags
);
194 for (i
= 0; i
< count
; i
++) {
195 c
= *((char *) log_buf
+(j
++ & (LOG_BUF_LEN
-1)));
196 __put_user(c
, buf
++);
202 case 5: /* Clear ring buffer */
205 case 6: /* Disable logging to console */
206 console_loglevel
= minimum_console_loglevel
;
208 case 7: /* Enable logging to console */
209 console_loglevel
= default_console_loglevel
;
213 if (len
< 1 || len
> 8)
215 if (len
< minimum_console_loglevel
)
216 len
= minimum_console_loglevel
;
217 console_loglevel
= len
;
229 asmlinkage
int sys_syslog(int type
, char * buf
, int len
)
231 if ((type
!= 3) && !capable(CAP_SYS_ADMIN
))
233 return do_syslog(type
, buf
, len
);
237 spinlock_t console_lock
;
239 asmlinkage
int printk(const char *fmt
, ...)
243 char *msg
, *p
, *buf_end
;
245 static signed char msg_level
= -1;
248 spin_lock_irqsave(&console_lock
, flags
);
250 i
= vsprintf(buf
+ 3, fmt
, args
); /* hopefully i < sizeof(buf)-4 */
251 buf_end
= buf
+ 3 + i
;
253 for (p
= buf
+ 3; p
< buf_end
; p
++) {
264 p
[1] = default_message_loglevel
+ '0';
268 msg_level
= p
[1] - '0';
271 for (; p
< buf_end
; p
++) {
272 log_buf
[(log_start
+log_size
) & (LOG_BUF_LEN
-1)] = *p
;
273 if (log_size
< LOG_BUF_LEN
)
277 log_start
&= LOG_BUF_LEN
-1;
285 if (msg_level
< console_loglevel
&& console_drivers
) {
286 struct console
*c
= console_drivers
;
288 if ((c
->flags
& CON_ENABLED
) && c
->write
)
289 c
->write(c
, msg
, p
- msg
+ line_feed
);
296 spin_unlock_irqrestore(&console_lock
, flags
);
297 wake_up_interruptible(&log_wait
);
301 void console_print(const char *s
)
303 struct console
*c
= console_drivers
;
307 if ((c
->flags
& CON_ENABLED
) && c
->write
)
313 void unblank_console(void)
315 struct console
*c
= console_drivers
;
317 if ((c
->flags
& CON_ENABLED
) && c
->unblank
)
324 * The console driver calls this routine during kernel initialization
325 * to register the console printing procedure with printk() and to
326 * print any messages that were printed by the kernel before the
327 * console driver was initialized.
329 void register_console(struct console
* console
)
334 signed char msg_level
= -1;
338 * See if we want to use this console driver. If we
339 * didn't select a console we take the first one
340 * that registers here.
342 if (preferred_console
< 0) {
343 if (console
->index
< 0)
345 if (console
->setup
== NULL
||
346 console
->setup(console
, NULL
) == 0) {
347 console
->flags
|= CON_ENABLED
| CON_CONSDEV
;
348 preferred_console
= 0;
353 * See if this console matches one we selected on
356 for(i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0]; i
++) {
357 if (strcmp(console_cmdline
[i
].name
, console
->name
) != 0)
359 if (console
->index
>= 0 &&
360 console
->index
!= console_cmdline
[i
].index
)
362 if (console
->index
< 0)
363 console
->index
= console_cmdline
[i
].index
;
364 if (console
->setup
&&
365 console
->setup(console
, console_cmdline
[i
].options
) != 0)
367 console
->flags
|= CON_ENABLED
;
368 console
->index
= console_cmdline
[i
].index
;
369 if (i
== preferred_console
)
370 console
->flags
|= CON_CONSDEV
;
374 if (!(console
->flags
& CON_ENABLED
))
378 * Put this console in the list - keep the
379 * preferred driver at the head of the list.
381 if ((console
->flags
& CON_CONSDEV
) || console_drivers
== NULL
) {
382 console
->next
= console_drivers
;
383 console_drivers
= console
;
385 console
->next
= console_drivers
->next
;
386 console_drivers
->next
= console
;
388 if ((console
->flags
& CON_PRINTBUFFER
) == 0) return;
391 * Print out buffered log messages.
393 for (i
=0,j
=0; i
< log_size
; i
++) {
394 buf
[j
++] = log_buf
[p
];
395 p
++; p
&= LOG_BUF_LEN
-1;
396 if (buf
[j
-1] != '\n' && i
< log_size
- 1 && j
< sizeof(buf
)-1)
402 msg_level
= buf
[1] - '0';
406 if (msg_level
< console_loglevel
)
407 console
->write(console
, q
, len
);
408 if (buf
[j
-1] == '\n')
415 int unregister_console(struct console
* console
)
417 struct console
*a
,*b
;
419 if (console_drivers
== console
) {
420 console_drivers
=console
->next
;
423 for (a
=console_drivers
->next
, b
=console_drivers
;
435 * Write a message to a certain tty, not just the console. This is used for
436 * messages that need to be redirected to a specific tty.
437 * We don't put it into the syslog queue right now maybe in the future if
440 void tty_write_message(struct tty_struct
*tty
, char *msg
)
442 if (tty
&& tty
->driver
.write
)
443 tty
->driver
.write(tty
, 0, msg
, strlen(msg
));