From 44f8a2666f5169c315c993cc0b91ae1394fabdbf Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 21 Jul 2016 14:57:58 +0200 Subject: [PATCH] tty: Always detach from initial TTY and open actual /dev/ttyN This patch fixes a long standing problem with TTY's. The original code tried to workaround a limitation in the console of some embedded systems that was detrimental on actual TTYs. As an added benefit, the console specific "Press Enter" prompt for the console port is now available on all TTYs -- meaning no getty needs to be started and kept needlessly in the background until it is needed. Signed-off-by: Joachim Nilsson --- exec.c | 42 +++++++++++++++++++----------------------- helpers.h | 2 +- tty.c | 2 +- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/exec.c b/exec.c index 531d0527..adee69b7 100644 --- a/exec.c +++ b/exec.c @@ -224,7 +224,7 @@ int run_interactive(char *cmd, char *fmt, ...) return status; } -pid_t run_getty(char *cmd, char *args[], int console) +pid_t run_getty(char *cmd, char *args[], char *tty, int console) { pid_t pid = fork(); @@ -232,28 +232,26 @@ pid_t run_getty(char *cmd, char *args[], int console) int fd; char c; - if (console) { - /* Detach from initial controlling TTY */ - vhangup(); + /* Detach from initial controlling TTY */ + vhangup(); - close(STDERR_FILENO); - close(STDOUT_FILENO); - close(STDIN_FILENO); + close(STDERR_FILENO); + close(STDOUT_FILENO); + close(STDIN_FILENO); - /* Attach TTY to console */ - fd = open(CONSOLE, O_RDWR); - if (fd != STDIN_FILENO) - exit(1); + /* Attach TTY to console */ + fd = open(tty, O_RDWR); + if (fd != STDIN_FILENO) + exit(1); - dup2(fd, STDIN_FILENO); - dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + if (console) prctl(PR_SET_NAME, "console", 0, 0, 0); - } sig_unblock(); - setsid(); if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) < 0) @@ -267,20 +265,18 @@ pid_t run_getty(char *cmd, char *args[], int console) continue; } - if (console) { - (void)write(STDERR_FILENO, msg, sizeof(msg)); - while (read(STDIN_FILENO, &c, 1) == 1 && c != '\n') + (void)write(STDERR_FILENO, msg, sizeof(msg)); + while (read(STDIN_FILENO, &c, 1) == 1 && c != '\n') continue; - } + if (fexist(SYNC_STOPPED)) continue; execv(cmd, args); } - if (console) - close(fd); - + close(fd); + vhangup(); exit(0); } diff --git a/helpers.h b/helpers.h index 82cac7d5..f864d19e 100644 --- a/helpers.h +++ b/helpers.h @@ -81,7 +81,7 @@ void set_hostname (char **hostname); int complete (char *cmd, int pid); int run (char *cmd); int run_interactive (char *cmd, char *fmt, ...); -pid_t run_getty (char *cmd, char *args[], int console); +pid_t run_getty (char *cmd, char *args[], char *tty, int console); int run_parts (char *dir, char *cmd); #endif /* FINIT_HELPERS_H_ */ diff --git a/tty.c b/tty.c index 78d1afd1..e0092436 100644 --- a/tty.c +++ b/tty.c @@ -160,7 +160,7 @@ void tty_start(finit_tty_t *tty) args[i] = NULL; _d("Starting %s: %s on %s", is_console ? "console" : "TTY", cmd, tty->name); - tty->pid = run_getty(cmd, args, is_console); + tty->pid = run_getty(cmd, args, tty->name, is_console); } void tty_stop(finit_tty_t *tty)