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 <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2016-07-21 14:57:58 +02:00
parent 4cf5b055cf
commit 44f8a2666f
3 changed files with 21 additions and 25 deletions
+19 -23
View File
@@ -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);
}
+1 -1
View File
@@ -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_ */
+1 -1
View File
@@ -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)