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.
16 #include <linux/tty_driver.h>
17 #include <linux/smp_lock.h>
18 #include <linux/console.h>
19 #include <linux/init.h>
21 #include <asm/uaccess.h>
23 #define LOG_BUF_LEN (16384)
25 static char buf
[1024];
27 /* printk's without a loglevel use this.. */
28 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
30 /* We show everything that is MORE important than this.. */
31 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
32 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
34 unsigned long log_size
= 0;
35 DECLARE_WAIT_QUEUE_HEAD(log_wait
);
37 /* Keep together for sysctl support */
38 int console_loglevel
= DEFAULT_CONSOLE_LOGLEVEL
;
39 int default_message_loglevel
= DEFAULT_MESSAGE_LOGLEVEL
;
40 int minimum_console_loglevel
= MINIMUM_CONSOLE_LOGLEVEL
;
41 int default_console_loglevel
= DEFAULT_CONSOLE_LOGLEVEL
;
43 struct console
*console_drivers
= NULL
;
44 static char log_buf
[LOG_BUF_LEN
];
45 static unsigned long log_start
= 0;
46 static unsigned long logged_chars
= 0;
47 struct console_cmdline console_cmdline
[MAX_CMDLINECONSOLES
];
48 static int preferred_console
= -1;
51 * Setup a list of consoles. Called from init/main.c
53 void __init
console_setup(char *str
, int *ints
)
55 struct console_cmdline
*c
;
56 char name
[sizeof(c
->name
)];
61 * Decode str into name, index, options.
63 if (str
[0] >= '0' && str
[0] <= '9') {
65 strncpy(name
+ 4, str
, sizeof(name
) - 5);
67 strncpy(name
, str
, sizeof(name
) - 1);
68 name
[sizeof(name
) - 1] = 0;
69 if ((options
= strchr(str
, ',')) != NULL
)
72 if (!strcmp(str
, "ttya"))
73 strcpy(name
, "ttyS0");
74 if (!strcmp(str
, "ttyb"))
75 strcpy(name
, "ttyS1");
77 for(s
= name
; *s
; s
++)
78 if (*s
>= '0' && *s
<= '9')
80 idx
= simple_strtoul(s
, NULL
, 10);
84 * See if this tty is not yet registered, and
85 * if we have a slot free.
87 for(i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0]; i
++)
88 if (strcmp(console_cmdline
[i
].name
, name
) == 0 &&
89 console_cmdline
[i
].index
== idx
) {
90 preferred_console
= i
;
93 if (i
== MAX_CMDLINECONSOLES
)
95 preferred_console
= i
;
96 c
= &console_cmdline
[i
];
97 memcpy(c
->name
, name
, sizeof(c
->name
));
104 * Commands to do_syslog:
106 * 0 -- Close the log. Currently a NOP.
107 * 1 -- Open the log. Currently a NOP.
108 * 2 -- Read from the log.
109 * 3 -- Read up to the last 4k of messages in the ring buffer.
110 * 4 -- Read and clear last 4k of messages in the ring buffer
111 * 5 -- Clear ring buffer.
112 * 6 -- Disable printk's to console
113 * 7 -- Enable printk's to console
114 * 8 -- Set level of messages printed to console
116 int do_syslog(int type
, char * buf
, int len
)
118 unsigned long i
, j
, count
, flags
;
126 case 0: /* Close log */
128 case 1: /* Open log */
130 case 2: /* Read from log */
137 error
= verify_area(VERIFY_WRITE
,buf
,len
);
140 error
= wait_event_interruptible(log_wait
, log_size
);
144 while (log_size
&& i
< len
) {
145 c
= *((char *) log_buf
+log_start
);
148 log_start
&= LOG_BUF_LEN
-1;
158 case 4: /* Read/clear last kernel messages */
161 case 3: /* Read last kernel messages */
168 error
= verify_area(VERIFY_WRITE
,buf
,len
);
172 * The logged_chars, log_start, and log_size values may
173 * change from an interrupt, so we disable interrupts.
178 if (count
> LOG_BUF_LEN
)
180 if (count
> logged_chars
)
181 count
= logged_chars
;
182 j
= log_start
+ log_size
- count
;
183 __restore_flags(flags
);
184 for (i
= 0; i
< count
; i
++) {
185 c
= *((char *) log_buf
+(j
++ & (LOG_BUF_LEN
-1)));
186 __put_user(c
, buf
++);
192 case 5: /* Clear ring buffer */
195 case 6: /* Disable logging to console */
196 console_loglevel
= minimum_console_loglevel
;
198 case 7: /* Enable logging to console */
199 console_loglevel
= default_console_loglevel
;
203 if (len
< 1 || len
> 8)
205 if (len
< minimum_console_loglevel
)
206 len
= minimum_console_loglevel
;
207 console_loglevel
= len
;
219 asmlinkage
int sys_syslog(int type
, char * buf
, int len
)
221 if ((type
!= 3) && !capable(CAP_SYS_ADMIN
))
223 return do_syslog(type
, buf
, len
);
227 spinlock_t console_lock
;
229 asmlinkage
int printk(const char *fmt
, ...)
233 char *msg
, *p
, *buf_end
;
235 static signed char msg_level
= -1;
238 spin_lock_irqsave(&console_lock
, flags
);
240 i
= vsprintf(buf
+ 3, fmt
, args
); /* hopefully i < sizeof(buf)-4 */
241 buf_end
= buf
+ 3 + i
;
243 for (p
= buf
+ 3; p
< buf_end
; p
++) {
254 p
[1] = default_message_loglevel
+ '0';
258 msg_level
= p
[1] - '0';
261 for (; p
< buf_end
; p
++) {
262 log_buf
[(log_start
+log_size
) & (LOG_BUF_LEN
-1)] = *p
;
263 if (log_size
< LOG_BUF_LEN
)
267 log_start
&= LOG_BUF_LEN
-1;
275 if (msg_level
< console_loglevel
&& console_drivers
) {
276 struct console
*c
= console_drivers
;
278 if ((c
->flags
& CON_ENABLED
) && c
->write
)
279 c
->write(c
, msg
, p
- msg
+ line_feed
);
286 spin_unlock_irqrestore(&console_lock
, flags
);
287 wake_up_interruptible(&log_wait
);
291 void console_print(const char *s
)
293 struct console
*c
= console_drivers
;
297 if ((c
->flags
& CON_ENABLED
) && c
->write
)
303 void unblank_console(void)
305 struct console
*c
= console_drivers
;
307 if ((c
->flags
& CON_ENABLED
) && c
->unblank
)
314 * The console driver calls this routine during kernel initialization
315 * to register the console printing procedure with printk() and to
316 * print any messages that were printed by the kernel before the
317 * console driver was initialized.
319 void register_console(struct console
* console
)
324 signed char msg_level
= -1;
328 * See if we want to use this console driver. If we
329 * didn't select a console we take the first one
330 * that registers here.
332 if (preferred_console
< 0) {
333 if (console
->index
< 0)
335 if (console
->setup
== NULL
||
336 console
->setup(console
, NULL
) == 0) {
337 console
->flags
|= CON_ENABLED
| CON_CONSDEV
;
338 preferred_console
= 0;
343 * See if this console matches one we selected on
346 for(i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0]; i
++) {
347 if (strcmp(console_cmdline
[i
].name
, console
->name
) != 0)
349 if (console
->index
>= 0 &&
350 console
->index
!= console_cmdline
[i
].index
)
352 if (console
->index
< 0)
353 console
->index
= console_cmdline
[i
].index
;
354 if (console
->setup
&&
355 console
->setup(console
, console_cmdline
[i
].options
) != 0)
357 console
->flags
|= CON_ENABLED
;
358 console
->index
= console_cmdline
[i
].index
;
359 if (i
== preferred_console
)
360 console
->flags
|= CON_CONSDEV
;
364 if (!(console
->flags
& CON_ENABLED
))
368 * Put this console in the list - keep the
369 * preferred driver at the head of the list.
371 if ((console
->flags
& CON_CONSDEV
) || console_drivers
== NULL
) {
372 console
->next
= console_drivers
;
373 console_drivers
= console
;
375 console
->next
= console_drivers
->next
;
376 console_drivers
->next
= console
;
378 if ((console
->flags
& CON_PRINTBUFFER
) == 0) return;
381 * Print out buffered log messages.
383 for (i
=0,j
=0; i
< log_size
; i
++) {
384 buf
[j
++] = log_buf
[p
];
385 p
++; p
&= LOG_BUF_LEN
-1;
386 if (buf
[j
-1] != '\n' && i
< log_size
- 1 && j
< sizeof(buf
)-1)
392 msg_level
= buf
[1] - '0';
396 if (msg_level
< console_loglevel
)
397 console
->write(console
, q
, len
);
398 if (buf
[j
-1] == '\n')
405 int unregister_console(struct console
* console
)
407 struct console
*a
,*b
;
409 if (console_drivers
== console
) {
410 console_drivers
=console
->next
;
413 for (a
=console_drivers
->next
, b
=console_drivers
;
425 * Write a message to a certain tty, not just the console. This is used for
426 * messages that need to be redirected to a specific tty.
427 * We don't put it into the syslog queue right now maybe in the future if
430 void tty_write_message(struct tty_struct
*tty
, char *msg
)
432 if (tty
&& tty
->driver
.write
)
433 tty
->driver
.write(tty
, 0, msg
, strlen(msg
));