From 88039f3d653f52493c5f78090450332e1012f093 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Fri, 23 Dec 2016 23:11:53 +0100 Subject: [PATCH] Fix #56: Check TTY before trying to start getty This (initial) patch for issue #56 adds a very basic validation of the device before attempting to start a getty on it. TODO: Verify a USB/serial device can be plugged in at any point at runtime, either automatically start getty or by `initctl reload` Signed-off-by: Joachim Nilsson --- tty.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tty.c b/tty.c index 0e8c492d..919b58cd 100644 --- a/tty.c +++ b/tty.c @@ -23,6 +23,7 @@ */ #include +#include #include #include #include @@ -169,6 +170,28 @@ static char *canonicalize(char *tty) return path; } +int tty_check(char *dev) +{ + int fd; + struct termios c; + + if (access(dev, F_OK)) + return 1; + + fd = open(dev, O_RDONLY); + if (-1 == fd) + return 1; + + /* XXX: Add check for errno == EIO? */ + if (tcgetattr(fd, &c)) { + close(fd); + return 1; + } + close(fd); + + return 0; +} + void tty_start(finit_tty_t *fitty) { int is_console = 0; @@ -184,6 +207,9 @@ void tty_start(finit_tty_t *fitty) if (console && !strcmp(tty, console)) is_console = 1; + if (tty_check(tty)) + return; + fitty->pid = run_getty(tty, fitty->baud, fitty->term, is_console); }