Skip to contentSkip to conversation

printk: restore previous console_loglevel when re-enabling logging

RFC PATCH In review Frans Pop 2 participants 3 messages Jul 6, 2009 – Jul 10, 2009

lore
Share & tools
lore

Frans Pop

author
#0 lore raw

RFC PATCH 1/1 printk: restore previous console_loglevel when re-enabling logging

When logging to console is disabled from userspace using klogctl() and later re-enabled, console_loglevel gets set to the default log level instead to the previous value.
This means that if the kernel was booted with 'quiet', the boot is suddenly no longer quiet after logging to console gets re-enabled.

Save the current console_loglevel when logging is disabled and restore to that value. If the log level is set to a specific value while disabled, this is interpreted as an implicit re-enabling of the logging.

---

Resend. The problem that prompted this patch is described in
http://lkml.org/lkml/2009/6/28/234

There are two variations possible on the patch below.

1) If klogctl(7) is called while logging is not disabled, then set level to default (partially preserving current functionality):
case 7: /* Enable logging to console */
- console_loglevel = default_console_loglevel;
+ if (saved_console_loglevel == -1)
+ console_loglevel = default_console_loglevel;
+ else {
+ console_loglevel = saved_console_loglevel;
+ saved_console_loglevel = -1;
+ }

2) If klogctl(8) is called while logging is disabled, then don't enable logging, but remember the requested value for when logging does get enabled again:
case 8: /* Set level of messages printed to console */
[...]
- console_loglevel = len;
+ if (saved_console_loglevel == -1)
+ console_loglevel = len;
+ else
+ saved_console_loglevel = len;

Yet another option would be to ignore the request.

kernel/printk.c +9 -1
index b4d97b5..acbf050 100644
@@ -271,6 +271,7 @@ int do_syslog(int type, char __user *buf, int len)
 	int do_clear = 0;
 	char c;
 	int error = 0;
+	static int saved_console_loglevel = -1;
 
 	error = security_syslog(type);
 	if (error)
@@ -372,10 +373,15 @@ int do_syslog(int type, char __user *buf, int len)
 		logged_chars = 0;
 		break;
 	case 6:		/* Disable logging to console */
+		if (saved_console_loglevel == -1)
+			saved_console_loglevel = console_loglevel;
 		console_loglevel = minimum_console_loglevel;
 		break;
 	case 7:		/* Enable logging to console */
-		console_loglevel = default_console_loglevel;
+		if (saved_console_loglevel != -1) {
+			console_loglevel = saved_console_loglevel;
+			saved_console_loglevel = -1;
+		}
 		break;
 	case 8:		/* Set level of messages printed to console */
 		error = -EINVAL;
@@ -384,6 +390,8 @@ int do_syslog(int type, char __user *buf, int len)
 		if (len < minimum_console_loglevel)
 			len = minimum_console_loglevel;
 		console_loglevel = len;
+		/* Implicitly re-enable logging to console */
+		saved_console_loglevel = -1;
 		error = 0;
 		break;
 	case 9:		/* Number of chars in the log buffer */
#1 lore raw
Quoted 41 lines · Frans Pop

When logging to console is disabled from userspace using klogctl() and later re-enabled, console_loglevel gets set to the default log level instead to the previous value.
This means that if the kernel was booted with 'quiet', the boot is suddenly no longer quiet after logging to console gets re-enabled.

Save the current console_loglevel when logging is disabled and restore to that value. If the log level is set to a specific value while disabled, this is interpreted as an implicit re-enabling of the logging.

---

Resend. The problem that prompted this patch is described in
http://lkml.org/lkml/2009/6/28/234

There are two variations possible on the patch below.

1) If klogctl(7) is called while logging is not disabled, then set level to default (partially preserving current functionality):

kernel/printk.c +6 -1
 	case 7:		/* Enable logging to console */
-		console_loglevel = default_console_loglevel;
+		if (saved_console_loglevel == -1)
+			console_loglevel = default_console_loglevel;
+		else {
+			console_loglevel = saved_console_loglevel;
+			saved_console_loglevel = -1;
+		}

2) If klogctl(8) is called while logging is disabled, then don't enable logging, but remember the requested value for when logging does get enabled again:

(quoted diff) +4 -1
 	case 8:		/* Set level of messages printed to console */
[...]
- 		console_loglevel = len;
+		if (saved_console_loglevel == -1)
+	 		console_loglevel = len;
+		else
+			saved_console_loglevel = len;
Quoted 1 line

Yet another option would be to ignore the request.

Looks like a possible solution to a real problem (most distros today boot with quiet) - albeit it does change previous behavior so objections are possible on those grounds.

I've queued it up in the tip:core/printk tree and will push it out to linux-next in a few days if there are no objections.

Small sidenote: i moved the (somewhat obscurely placed) saved_console_loglevel static variable out of local variable scope and next to the console loglevel definitions (such as console_printk[]), to increase its visibility as globally relevant state and to move it next to the state this variable affects.

Thanks,

Ingo

#2 lore raw

[tip:core/printk] printk: Restore previous console_loglevel when re-enabling logging

In reply to Frans Pop
Commit-ID:  1aaad49e856ce41adc07d8ae0c8ef35fc4483245
Gitweb:     http://git.kernel.org/tip/1aaad49e856ce41adc07d8ae0c8ef35fc4483245
Author:     Frans Pop <[email protected]>
AuthorDate: Mon, 6 Jul 2009 13:31:48 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 10 Jul 2009 16:02:23 +0200

printk: Restore previous console_loglevel when re-enabling logging

When logging to console is disabled from userspace using klogctl() and later re-enabled, console_loglevel gets set to the default log level instead to the previous value.

This means that if the kernel was booted with 'quiet', the boot is suddenly no longer quiet after logging to console gets re-enabled.

Save the current console_loglevel when logging is disabled and restore to that value. If the log level is set to a specific value while disabled, this is interpreted as an implicit re-enabling of the logging.

The problem that prompted this patch is described in:

    http://lkml.org/lkml/2009/6/28/234

There are two variations possible on the patch below:

1) If klogctl(7) is called while logging is not disabled, then set level to default (partially preserving current functionality):
case 7: /* Enable logging to console */
- console_loglevel = default_console_loglevel;
+ if (saved_console_loglevel == -1)
+ console_loglevel = default_console_loglevel;
+ else {
+ console_loglevel = saved_console_loglevel;
+ saved_console_loglevel = -1;
+ }

2) If klogctl(8) is called while logging is disabled, then don't enable logging, but remember the requested value for when logging does get enabled again:
case 8: /* Set level of messages printed to console */
[...]
- console_loglevel = len;
+ if (saved_console_loglevel == -1)
+ console_loglevel = len;
+ else
+ saved_console_loglevel = len;

Yet another option would be to ignore the request.

Cc Andrew Morton <[email protected]>
Cc Linus Torvalds <[email protected]>

LKML-Reference: <[email protected]>

---
 kernel/printk.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
kernel/printk.c +10 -1
index 668df35..e0daaf5 100644
@@ -67,6 +67,8 @@ int console_printk[4] = {
 	DEFAULT_CONSOLE_LOGLEVEL,	/* default_console_loglevel */
 };
 
+static int saved_console_loglevel = -1;
+
 /*
  * Low level drivers may need that to know if they can schedule in
  * their unblank() callback or not. So let's export it.
@@ -378,10 +380,15 @@ int do_syslog(int type, char __user *buf, int len)
 		logged_chars = 0;
 		break;
 	case 6:		/* Disable logging to console */
+		if (saved_console_loglevel == -1)
+			saved_console_loglevel = console_loglevel;
 		console_loglevel = minimum_console_loglevel;
 		break;
 	case 7:		/* Enable logging to console */
-		console_loglevel = default_console_loglevel;
+		if (saved_console_loglevel != -1) {
+			console_loglevel = saved_console_loglevel;
+			saved_console_loglevel = -1;
+		}
 		break;
 	case 8:		/* Set level of messages printed to console */
 		error = -EINVAL;
@@ -390,6 +397,8 @@ int do_syslog(int type, char __user *buf, int len)
 		if (len < minimum_console_loglevel)
 			len = minimum_console_loglevel;
 		console_loglevel = len;
+		/* Implicitly re-enable logging to console */
+		saved_console_loglevel = -1;
 		error = 0;
 		break;
 	case 9:		/* Number of chars in the log buffer */