Skip to content

Commit 757055a

Browse files
committed
init/console: Use ttynull as a fallback when there is no console
stdin, stdout, and stderr standard I/O stream are created for the init process. They are not available when there is no console registered for /dev/console. It might lead to a crash when the init process tries to use them, see the commit 48021f9 ("printk: handle blank console arguments passed in."). Normally, ttySX and ttyX consoles are used as a fallback when no consoles are defined via the command line, device tree, or SPCR. But there will be no console registered when an invalid console name is configured or when the configured consoles do not exist on the system. Users even try to avoid the console intentionally, for example, by using console="" or console=null. It is used on production systems where the serial port or terminal are not visible to users. Pushing messages to these consoles would just unnecessary slowdown the system. Make sure that stdin, stdout, stderr, and /dev/console are always available by a fallback to the existing ttynull driver. It has been implemented for exactly this purpose but it was used only when explicitly configured. Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Guenter Roeck <linux@roeck-us.net> Tested-by: Guenter Roeck <linux@roeck-us.net> Acked-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20201111135450.11214-2-pmladek@suse.com
1 parent 8119c43 commit 757055a

5 files changed

Lines changed: 30 additions & 18 deletions

File tree

‎drivers/tty/Kconfig‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -428,20 +428,6 @@ config MIPS_EJTAG_FDC_KGDB_CHAN
428428
help
429429
FDC channel number to use for KGDB.
430430

431-
config NULL_TTY
432-
tristate "NULL TTY driver"
433-
help
434-
Say Y here if you want a NULL TTY which simply discards messages.
435-
436-
This is useful to allow userspace applications which expect a console
437-
device to work without modifications even when no console is
438-
available or desired.
439-
440-
In order to use this driver, you should redirect the console to this
441-
TTY, or boot the kernel with console=ttynull.
442-
443-
If unsure, say N.
444-
445431
config TRACE_ROUTER
446432
tristate "Trace data router for MIPI P1149.7 cJTAG standard"
447433
depends on TRACE_SINK

‎drivers/tty/Makefile‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
obj-$(CONFIG_TTY) += tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o \
33
tty_buffer.o tty_port.o tty_mutex.o \
44
tty_ldsem.o tty_baudrate.o tty_jobctrl.o \
5-
n_null.o
5+
n_null.o ttynull.o
66
obj-$(CONFIG_LEGACY_PTYS) += pty.o
77
obj-$(CONFIG_UNIX98_PTYS) += pty.o
88
obj-$(CONFIG_AUDIT) += tty_audit.o
@@ -25,7 +25,6 @@ obj-$(CONFIG_ISI) += isicom.o
2525
obj-$(CONFIG_MOXA_INTELLIO) += moxa.o
2626
obj-$(CONFIG_MOXA_SMARTIO) += mxser.o
2727
obj-$(CONFIG_NOZOMI) += nozomi.o
28-
obj-$(CONFIG_NULL_TTY) += ttynull.o
2928
obj-$(CONFIG_ROCKETPORT) += rocket.o
3029
obj-$(CONFIG_SYNCLINK_GT) += synclink_gt.o
3130
obj-$(CONFIG_SYNCLINKMP) += synclinkmp.o

‎drivers/tty/ttynull.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
/*
33
* Copyright (C) 2019 Axis Communications AB
44
*
5+
* The console is useful for userspace applications which expect a console
6+
* device to work without modifications even when no console is available
7+
* or desired.
8+
*
9+
* In order to use this driver, you should redirect the console to this
10+
* TTY, or boot the kernel with console=ttynull.
11+
*
512
* Based on ttyprintk.c:
613
* Copyright (C) 2010 Samo Pogacnik
714
*/
@@ -59,6 +66,17 @@ static struct console ttynull_console = {
5966
.device = ttynull_device,
6067
};
6168

69+
void __init register_ttynull_console(void)
70+
{
71+
if (!ttynull_driver)
72+
return;
73+
74+
if (add_preferred_console(ttynull_console.name, 0, NULL))
75+
return;
76+
77+
register_console(&ttynull_console);
78+
}
79+
6280
static int __init ttynull_init(void)
6381
{
6482
struct tty_driver *driver;

‎include/linux/console.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,12 @@ extern int braille_register_console(struct console *, int index,
187187
extern int braille_unregister_console(struct console *);
188188
#ifdef CONFIG_TTY
189189
extern void console_sysfs_notify(void);
190+
extern void register_ttynull_console(void);
190191
#else
191192
static inline void console_sysfs_notify(void)
192193
{ }
194+
static inline void register_ttynull_console(void)
195+
{ }
193196
#endif
194197
extern bool console_suspend_enabled;
195198

‎init/main.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,8 +1468,14 @@ void __init console_on_rootfs(void)
14681468
struct file *file = filp_open("/dev/console", O_RDWR, 0);
14691469

14701470
if (IS_ERR(file)) {
1471-
pr_err("Warning: unable to open an initial console.\n");
1472-
return;
1471+
pr_err("Warning: unable to open an initial console. Fallback to ttynull.\n");
1472+
register_ttynull_console();
1473+
1474+
file = filp_open("/dev/console", O_RDWR, 0);
1475+
if (IS_ERR(file)) {
1476+
pr_err("Warning: Failed to add ttynull console. No stdin, stdout, and stderr for the init process!\n");
1477+
return;
1478+
}
14731479
}
14741480
init_dup(file);
14751481
init_dup(file);

0 commit comments

Comments
 (0)