How can I find which physical device /dev/console connects to?

11,985

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

Share:
11,985

Related videos on Youtube

user1937358
Author by

user1937358

Updated on June 04, 2022

Comments

  • user1937358
    user1937358 almost 2 years

    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.
    • sawdust
      sawdust about 11 years
      Try looking at how the console= parameter is handled from the kernel command line.
    • user1937358
      user1937358 about 11 years
      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
      user1937358 about 11 years
      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.
    • John Slegers
      John Slegers about 11 years
      Do you have a file /sys/dev/char/5:1/active? It should contain the name of the actual device.
    • user1937358
      user1937358 about 11 years
      No, my system doesn't have the file.
    • Raúl Salinas-Monteagudo
      Raúl Salinas-Monteagudo about 9 years
      Doesn't "tty" give you it?
    • user1937358
      user1937358 about 9 years
      @esperanto IIRC, tty would show /dev/console in the case that HID devices are used.
  • user1937358
    user1937358 about 11 years
    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.
  • agent.smith
    agent.smith about 11 years
    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.
  • pts
    pts over 10 years
    FYI Linux 2.6.35 doesn't have the file, some newer Linux systems have it.
  • CMCDragonkai
    CMCDragonkai over 6 years
    On my desktop linux, this shows tty0 and then /sys/devices/virtual/tty/tty0/active shows tty7 and tty7 is my X server.
  • Dolda2000
    Dolda2000 over 6 years
    @CMCDragonkai: I believe that means that tty7 is the active VC, but that /dev/console output goes to tty0.
  • Dolda2000
    Dolda2000 over 6 years
    @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.
  • CMCDragonkai
    CMCDragonkai over 6 years
    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.