From 949f02eff4bef7a87078ee08a113942fa5e05437 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 23 Oct 2017 17:24:46 +0200 Subject: [PATCH] tty: Add support for auto-detecting serial console from SysFS This patch adds support for a new TTY device keyword `@console` which auto-detects the system console by reading the SysFS file: /sys/class/tty/console/active Embedded systems with one of /dev/ttyAMA0, /tty/ttyMXC0 or the original /dev/ttyS0, as their active serial console can now instead use a single line to /etc/finit.d/getty.conf to start the built-in getty on the same TTY as the kernel already runs on: tty [12345789] @console 115200 noclear With no TTY defined Finit will either do nothing (default), or start a fallback shell if enabled in the configure script. Signed-off-by: Joachim Nilsson --- src/tty.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tty.c b/src/tty.c index c1198676..ce36b632 100644 --- a/src/tty.c +++ b/src/tty.c @@ -45,12 +45,28 @@ static LIST_HEAD(, tty_node) tty_list = LIST_HEAD_INITIALIZER(); static char *canonicalize(char *tty) { + char buf[42]; struct stat st; static char path[80]; if (!tty) return NULL; + /* Auto-detect serial console, for embedded devices mostly */ + if (!strcmp(tty, "@console")) { + FILE *fp; + + fp = fopen("/sys/class/tty/console/active", "r"); + if (!fp) { + _e("Cannot find system console, is sysfs not mounted?"); + return NULL; + } + + if (fgets(buf, sizeof(buf), fp)) + tty = chomp(buf); + fclose(fp); + } + strlcpy(path, tty, sizeof(path)); if (stat(path, &st)) { if (!strncmp(path, _PATH_DEV, strlen(_PATH_DEV))) { @@ -168,6 +184,8 @@ int tty_register(char *line, struct timeval *mtime) if (!dev && !cmd) { if (args[i][0] == '[') runlevels = line; + if (!strcmp(args[i], "@console")) + dev = args[i]; if (!strncmp(args[i], "/dev", 4)) dev = args[i]; if (!strncmp(args[i], "tty", 3))