diff options
author | Vincent Whitchurch <vincent.whitchurch@axis.com> | 2019-04-03 13:33:27 +0200 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2019-04-16 15:21:34 +0200 |
commit | 3117ff13f104e98b05b61e19cc754d1377e92e15 (patch) | |
tree | 466a0a4bb36d0786128d170bcb43f60e40c2c918 /drivers/tty/ttynull.c | |
parent | 89bb1e1ee529d9d06ca694ba22a35dc2a3d6ac67 (diff) | |
download | linux-stable-3117ff13f104e98b05b61e19cc754d1377e92e15.tar.gz linux-stable-3117ff13f104e98b05b61e19cc754d1377e92e15.tar.bz2 linux-stable-3117ff13f104e98b05b61e19cc754d1377e92e15.zip |
tty: Add NULL TTY driver
If no console driver is enabled (or if a non-present driver is selected
with something like console=null in an attempt to disable the console),
opening /dev/console errors out, and init scripts and other userspace
code that relies on the existence of a console will fail. Symlinking
/dev/null to /dev/console does not solve the problem since /dev/null
does not behave like a real TTY.
To just provide a dummy console to userspace when no console driver is
available or desired, add a ttynull driver which simply discards all
writes. It can be chosen on the command line in the standard way, i.e.
with console=ttynull.
Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/tty/ttynull.c')
-rw-r--r-- | drivers/tty/ttynull.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/drivers/tty/ttynull.c b/drivers/tty/ttynull.c new file mode 100644 index 000000000000..17f05b7eb6d3 --- /dev/null +++ b/drivers/tty/ttynull.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2019 Axis Communications AB + * + * Based on ttyprintk.c: + * Copyright (C) 2010 Samo Pogacnik + */ + +#include <linux/console.h> +#include <linux/module.h> +#include <linux/tty.h> + +static const struct tty_port_operations ttynull_port_ops; +static struct tty_driver *ttynull_driver; +static struct tty_port ttynull_port; + +static int ttynull_open(struct tty_struct *tty, struct file *filp) +{ + return tty_port_open(&ttynull_port, tty, filp); +} + +static void ttynull_close(struct tty_struct *tty, struct file *filp) +{ + tty_port_close(&ttynull_port, tty, filp); +} + +static void ttynull_hangup(struct tty_struct *tty) +{ + tty_port_hangup(&ttynull_port); +} + +static int ttynull_write(struct tty_struct *tty, const unsigned char *buf, + int count) +{ + return count; +} + +static int ttynull_write_room(struct tty_struct *tty) +{ + return 65536; +} + +static const struct tty_operations ttynull_ops = { + .open = ttynull_open, + .close = ttynull_close, + .hangup = ttynull_hangup, + .write = ttynull_write, + .write_room = ttynull_write_room, +}; + +static struct tty_driver *ttynull_device(struct console *c, int *index) +{ + *index = 0; + return ttynull_driver; +} + +static struct console ttynull_console = { + .name = "ttynull", + .device = ttynull_device, +}; + +static int __init ttynull_init(void) +{ + struct tty_driver *driver; + int ret; + + driver = tty_alloc_driver(1, + TTY_DRIVER_RESET_TERMIOS | + TTY_DRIVER_REAL_RAW | + TTY_DRIVER_UNNUMBERED_NODE); + if (IS_ERR(driver)) + return PTR_ERR(driver); + + tty_port_init(&ttynull_port); + ttynull_port.ops = &ttynull_port_ops; + + driver->driver_name = "ttynull"; + driver->name = "ttynull"; + driver->type = TTY_DRIVER_TYPE_CONSOLE; + driver->init_termios = tty_std_termios; + driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET; + tty_set_operations(driver, &ttynull_ops); + tty_port_link_device(&ttynull_port, driver, 0); + + ret = tty_register_driver(driver); + if (ret < 0) { + put_tty_driver(driver); + tty_port_destroy(&ttynull_port); + return ret; + } + + ttynull_driver = driver; + register_console(&ttynull_console); + + return 0; +} + +static void __exit ttynull_exit(void) +{ + unregister_console(&ttynull_console); + tty_unregister_driver(ttynull_driver); + put_tty_driver(ttynull_driver); + tty_port_destroy(&ttynull_port); +} + +module_init(ttynull_init); +module_exit(ttynull_exit); + +MODULE_LICENSE("GPL v2"); |