15

There is a device file called /dev/console, whose (major,minor) is (5,1). When I boot with a serial console, it connects to my UART port as /dev/ttyS0 does. But when I boot with serial console disabled, the /dev/console seems to connect to elsewhere, where /dev/ttyS0 always connects to the same physical device, namely UART0. Is there any indication (like something in /proc or /sys) showing such information? I have tried there but found nothing I want. I am starting to trace the Linux kernel source to clarify their relationships. Can anyone give me some hints? When, where, how and what to determine the physical device to which /dev/console connects?

EDIT: The latest finding is that:

  1. the /dev/console is configured by console= of the kernel parameters, which in turn is used by getty to open stdin/stdout/stderr. If no console= is specified, /dev/null is opened as stdin/stdout/stderr. But I am not sure they are exactly the same with /dev/console, which implies /dev/console can have its I/O connecting to different physical devices.
  2. The /dev/console can be read if a USB HID keyboard is plugged and the console= is not configured as UART. Therefore /dev/console == /dev/null seems to make little sense. Need more investigations.
Grodriguez
  • 21,501
  • 10
  • 63
  • 107
user1937358
  • 239
  • 1
  • 2
  • 8
  • 1
    Try looking at how the `console=` parameter is handled from the kernel command line. – sawdust Mar 19 '13 at 09:45
  • 1
    Yeah, I have looked at that and it turns out the parameters are passed to `init` and in turn to `getty_main()`, which is the implementation of `getty` within `busybox`. The relevant part is to `close` the original standard input and then `open` a new one according to the passed parameters. So I am looking for a way which can retrieve the path from a file descriptor; since I want to know what the 'default' device is if no `console=` specified. – user1937358 Mar 19 '13 at 11:47
  • It seems that the very fist stdin/stdout/stderr opened by `init` is `/dev/null` if no console is specified for `getty`. For those who are interested, it's in `init_main()` -> `console_init()` -> `bb_sanitize_stdio()` of the source of `busybox`. – user1937358 Mar 19 '13 at 12:44
  • 2
    Do you have a file `/sys/dev/char/5:1/active`? It should contain the name of the actual device. – Niall C. Mar 19 '13 at 21:24
  • No, my system doesn't have the file. – user1937358 Mar 20 '13 at 12:01
  • Doesn't "tty" give you it? – Raúl Salinas-Monteagudo Mar 24 '15 at 17:18
  • @esperanto IIRC, `tty` would show /dev/console in the case that HID devices are used. – user1937358 Mar 26 '15 at 04:01

2 Answers2

9

I think /sys/devices/virtual/tty/console/active is what you're looking for.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • Unfortunately there is no such file on my system, which is an ARM-based embedded device. So far what I have learned is that the console can be configured by `/etc/inittab`. – user1937358 Mar 19 '13 at 08:06
  • 2
    Inside kernel, whichever is the last module initialized with console= is configured as console. If I understand correctly then inittab is used to configure which tty terminal will be used to run login program. – agent.smith Mar 19 '13 at 23:25
  • 3
    FYI Linux 2.6.35 doesn't have the file, some newer Linux systems have it. – pts Nov 01 '13 at 09:41
  • On my desktop linux, this shows `tty0` and then `/sys/devices/virtual/tty/tty0/active` shows `tty7` and `tty7` is my X server. – CMCDragonkai Nov 06 '17 at 04:48
  • 1
    @CMCDragonkai: I believe that means that `tty7` is the active VC, but that `/dev/console` output goes to `tty0`. – Dolda2000 Nov 06 '17 at 05:37
  • @CMCDragonkai: How so? It just means that `/dev/console` is set to go to `/dev/tty0` (which means the current VC), but it doesn't have to be set up that way. – Dolda2000 Nov 06 '17 at 16:01
  • Currently on my system, `/sys/devices/virtual/tty/console/active` says tty0, and `/sys/devices/virtual/tty/tty0/active` says tty7. Then this probably means that both are pointing to tty7 in the end. – CMCDragonkai Nov 07 '17 at 11:42
0

From http://man7.org/linux/man-pages/man4/console.4.html : "The current console is also addressed by /dev/console or /dev/tty0, the character device with major number 4 and minor number 0."

I hope that was what you were looking for.

lamiaani
  • 1
  • 1