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 <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2016-12-23 23:11:53 +01:00
parent 2c0421f2e6
commit 88039f3d65
+26
View File
@@ -23,6 +23,7 @@
*/
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -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);
}