Skip to contentSkip to conversation

tty: Add NULL TTY driver

PATCH In review Vincent Whitchurch 4 participants 13 messages Apr 3, 2019 – Apr 12, 2019 1 message not in the archive TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 drivers/tty/Kconfig   |  14 ++++++
 drivers/tty/Makefile  |   1 +
 drivers/tty/ttynull.c | 109 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 124 insertions(+)
 create mode 100644 drivers/tty/ttynull.c
drivers/tty/Kconfig +14 -0
index e0a04bfc873e..d862f442f389 100644
@@ -376,6 +376,20 @@ config PPC_EARLY_DEBUG_EHV_BC_HANDLE
 	  there simply will be no early console output.  This is true also
 	  if you don't boot under a hypervisor at all.
 
+config NULL_TTY
+	tristate "NULL TTY driver"
+	help
+	  Say Y here if you want a NULL TTY which simply discards messages.
+
+	  This is useful to allow userspace applications which expect a console
+	  device to work without modifications even when no console is
+	  available or desired.
+
+	  In order to use this driver, you should redirect the console to this
+	  TTY, or boot the kernel with console=ttynull.
+
+	  If unsure, say N.
+
 config GOLDFISH_TTY
 	tristate "Goldfish TTY Driver"
 	depends on GOLDFISH
drivers/tty/Makefile +1 -0
index c72cafdf32b4..020b1cd9294f 100644
@@ -25,6 +25,7 @@ obj-$(CONFIG_ISI)		+= isicom.o
 obj-$(CONFIG_MOXA_INTELLIO)	+= moxa.o
 obj-$(CONFIG_MOXA_SMARTIO)	+= mxser.o
 obj-$(CONFIG_NOZOMI)		+= nozomi.o
+obj-$(CONFIG_NULL_TTY)	        += ttynull.o
 obj-$(CONFIG_ROCKETPORT)	+= rocket.o
 obj-$(CONFIG_SYNCLINK_GT)	+= synclink_gt.o
 obj-$(CONFIG_SYNCLINKMP)	+= synclinkmp.o
drivers/tty/ttynull.c new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.20.0
maintainer
#1 lore raw
Quoted 11 lines · Vincent Whitchurch

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.

If they have a broken system that sets "console=null", why would they know to fix it to be "console=ttynull"?

I'm all for adding new functionality, but to provide kernel code because userspace just isn't configured properly, that feels really wrong to me.

Now if this were to be the "default" if nothing is set up at all, that might make a bit more sense, but as-is, this doesn't seem very useful.

thanks,

greg k-h

#2 lore raw
Quoted 13 lines · Greg KH
Quoted 11 lines · Vincent Whitchurch

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.

Quoted 5 lines

If they have a broken system that sets "console=null", why would they know to fix it to be "console=ttynull"?

I'm all for adding new functionality, but to provide kernel code because userspace just isn't configured properly, that feels really wrong to me.

Especially on embedded systems, it would be convenient to have a simple way to disable the console (both for kernel and userspace) on a system which normally uses it, to free up the UART for other things.

In my case some early init script in userspace was actually tring to handle the lack of a console in this way:

[ ! -w /dev/console ] || exec 2>/dev/console

but the problem is that /dev/console always exists but fails on write, so the obvious way of handling missing devices doesn't work. AFAICS the only way to get rid of /dev/console would be to disable the TTY layer entirely in the kernel or have early userspace delete the file from devtmpfs.

Quoted 2 lines

Now if this were to be the "default" if nothing is set up at all, that might make a bit more sense, but as-is, this doesn't seem very useful.

Making it the default now would break users, wouldn't it? Since IIRC if no console is selected, the first registered console will automatically be used by default.

#3 lore raw
Quoted 3 lines · Vincent Whitchurch

Especially on embedded systems, it would be convenient to have a simple way to disable the console (both for kernel and userspace) on a system which normally uses it, to free up the UART for other things.

Just symlinking to /dev/null does not work ?

OTOH, if you're introducing a dummy console, wouldn't a ringbuffer that, can be read out later, a better option ?

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
#4 lore raw
Quoted 7 lines · metux IT consult
Quoted 3 lines · Vincent Whitchurch

Especially on embedded systems, it would be convenient to have a simple way to disable the console (both for kernel and userspace) on a system which normally uses it, to free up the UART for other things.

Just symlinking to /dev/null does not work ?

No, /dev/null does not support the TTY ioctls.

Quoted 2 lines

OTOH, if you're introducing a dummy console, wouldn't a ringbuffer that, can be read out later, a better option ?

There is already a ttyprintk driver in mainline to send these messages to the printk ring buffer if one is actually intrested in what is written to the console. There's no option to enable it via console= in mainline but I have a patch for that too.

#5 lore raw
Quoted 10 lines · Vincent Whitchurch
Quoted 7 lines · metux IT consult
Quoted 3 lines · Vincent Whitchurch

Especially on embedded systems, it would be convenient to have a simple way to disable the console (both for kernel and userspace) on a system which normally uses it, to free up the UART for other things.

Just symlinking to /dev/null does not work ?

No, /dev/null does not support the TTY ioctls.

hmm, wo (which programs) do you need, that really need them ?

Quoted 7 lines
Quoted 2 lines

OTOH, if you're introducing a dummy console, wouldn't a ringbuffer that, can be read out later, a better option ?

There is already a ttyprintk driver in mainline to send these messages to the printk ring buffer if one is actually intrested in what is written to the console. There's no option to enable it via console= in mainline but I have a patch for that too.

Great. IMHO, that would be the better way.

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
#6 lore raw
Quoted 17 lines · metux IT consult
Quoted 10 lines · Vincent Whitchurch
Quoted 7 lines · metux IT consult
Quoted 3 lines · Vincent Whitchurch

Especially on embedded systems, it would be convenient to have a simple way to disable the console (both for kernel and userspace) on a system which normally uses it, to free up the UART for other things.

Just symlinking to /dev/null does not work ?

No, /dev/null does not support the TTY ioctls.

hmm, wo (which programs) do you need, that really need them ?

Quoted 3 lines
Quoted 2 lines

OTOH, if you're introducing a dummy console, wouldn't a ringbuffer that, can be read out later, a better option ?

Quoted 6 lines
Quoted 4 lines

There is already a ttyprintk driver in mainline to send these messages to the printk ring buffer if one is actually intrested in what is written to the console. There's no option to enable it via console= in mainline but I have a patch for that too.

Great. IMHO, that would be the better way.

There are (embedded) cases where the kernel ring buffer is stored for log inspection and all the logs that are *not* wanted there (like interactive debug logs, some progress bar, etc) are send specifically to /dev/console to avoid sending them to normal log store (as they are only useful when debugging over console). In non-debug mode /dev/console becomes /dev/null to speed things up.

I would personally benefit from Vincents patch. In fact I found this thread because I was considering doing exactly the same for our system.

Krzysztof

#7 lore raw
Quoted 1 line · Krzysztof (Nokia - PL/Wroclaw)

There are (embedded) cases where the kernel ring buffer is stored for> log inspection and all the logs that are *not* wanted there (like>

interactive debug logs, some progress bar, etc) are send specifically to> /dev/console to avoid sending them to normal log store (as they are only> useful when debugging over console). In non-debug mode /dev/console> becomes /dev/null to speed things up.
So, in your case, it doesn't need to be an actual tty, any file/chardev already do it, correct ? But then, why is that new driver needed ? --mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
#8 lore raw
Quoted 10 lines · metux IT consult
Quoted 1 line · Krzysztof (Nokia - PL/Wroclaw)

There are (embedded) cases where the kernel ring buffer is stored for> log inspection and all the logs that are *not* wanted there (like>

interactive debug logs, some progress bar, etc) are send specifically to> /dev/console to avoid sending them to normal log store (as they are only> useful when debugging over console). In non-debug mode /dev/console> becomes /dev/null to speed things up.
So, in your case, it doesn't need to be an actual tty, any file/chardev already do it, correct ? But then, why is that new driver needed ?

Well, that depends. If the program doing those writes expects /dev/console to be a tty device, then it cannot be any file. But, yes, there are usually other ways of achieving this goal that do not involve adding kernel driver but none of them is as convenient as having this kernel support. With this, we can only switch kernel cmdline without any modifications to the rootfs. The discussed kernel driver is very simple so there is little effort required for this convenience. Maintaining it would be very easy and, of course, anybody not needing it, can simply disable it in config so there is no cost.

Krzysztof

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw
Quoted 2 lines · Krzysztof (Nokia - PL/Wroclaw)

Well, that depends. If the program doing those writes expects /dev/console to be a tty device, then it cannot be any file.

According to Vincent's mail, the actual problem is just systemd. Changing the kernel just for making one specific userland program (which happens to be written by somebody, who even doesn't know the semantics of rm -R) happy, sounds pretty strange for me.

I'm not opposed to the general idea of having a dummy tty driver, but please for some actually sane usecases, not just working around broken userland :p

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
#11 lore raw
Quoted 5 lines · metux IT consult
Quoted 2 lines · Krzysztof (Nokia - PL/Wroclaw)

Well, that depends. If the program doing those writes expects /dev/console to be a tty device, then it cannot be any file.

Quoted 8 lines

According to Vincent's mail, the actual problem is just systemd. Changing the kernel just for making one specific userland program (which happens to be written by somebody, who even doesn't know the semantics of rm -R) happy, sounds pretty strange for me.

I'm not opposed to the general idea of having a dummy tty driver, but please for some actually sane usecases, not just working around broken userland :p

I don't want to start a flame but if that userspace would be written by somebody else, would that change anything? :) Anyways, historically we did not have many convinence usitlities in the kernel and now we do. So does the fact that "this might be convinient but is not strictly necessary" a blocker to get something into the kernel?

Krzysztof

#12 lore raw
In reply to Enrico Weigelt, metux IT consult
Quoted 8 lines · metux IT consult
Quoted 7 lines · Vincent Whitchurch
Quoted 6 lines · metux IT consult
Quoted 3 lines · Vincent Whitchurch

Especially on embedded systems, it would be convenient to have a simple way to disable the console (both for kernel and userspace) on a system which normally uses it, to free up the UART for other things.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Just symlinking to /dev/null does not work ?

No, /dev/null does not support the TTY ioctls.

hmm, wo (which programs) do you need, that really need them ?

I think it was systemd's debug-shell which complained about the ioctls.

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

OTOH, if you're introducing a dummy console, wouldn't a ringbuffer that, can be read out later, a better option ?

There is already a ttyprintk driver in mainline to send these messages to the printk ring buffer if one is actually intrested in what is written to the console. There's no option to enable it via console= in mainline but I have a patch for that too.

Great. IMHO, that would be the better way.

Like Krzysztof explained, sometimes you just want to discard these messages.

#13 lore raw

On 11.04.19 17:28, Vincent Whitchurch wrote:

<snip>

Quoted 1 line
Quoted 1 line

hmm, wo (which programs) do you need, that really need them ?> > I think it was systemd's debug-shell which complained about the ioctls.

Okay, why not fixing this broken userland ?

systemd is written by somebody who doesn't even know the semantics of rm -R (as specified by unix standard since many decades) and so causing funny things like complete disk wipe:

https://github.com/systemd/systemd/issues/5644

In the last 25 years, I've always been fine w/ just redirecting to /dev/null.

--mtx

Signature · 3 lines
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287