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 committed 2016-07-21 14:57:58 +02:00
1 parent 4cf5b055cf
commit 44f8a2666f
3 files changed
+21 -25

No files matched your search

+19 -23
View File
@@ -224,7 +224,7 @@ int run_interactive(char *cmd, char *fmt, ...)
return status; 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(); pid_t pid = fork();
@@ -232,28 +232,26 @@ pid_t run_getty(char *cmd, char *args[], int console)
int fd; int fd;
char c; char c;
if (console) { /* Detach from initial controlling TTY */
/* Detach from initial controlling TTY */ vhangup();
vhangup();
close(STDERR_FILENO); close(STDERR_FILENO);
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDIN_FILENO); close(STDIN_FILENO);
/* Attach TTY to console */ /* Attach TTY to console */
fd = open(CONSOLE, O_RDWR); fd = open(tty, O_RDWR);
if (fd != STDIN_FILENO) if (fd != STDIN_FILENO)
exit(1); exit(1);
dup2(fd, STDIN_FILENO); dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO); dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO); dup2(fd, STDERR_FILENO);
if (console)
prctl(PR_SET_NAME, "console", 0, 0, 0); prctl(PR_SET_NAME, "console", 0, 0, 0);
}
sig_unblock(); sig_unblock();
setsid(); setsid();
if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) < 0) if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) < 0)
@@ -267,20 +265,18 @@ pid_t run_getty(char *cmd, char *args[], int console)
continue; continue;
} }
if (console) { (void)write(STDERR_FILENO, msg, sizeof(msg));
(void)write(STDERR_FILENO, msg, sizeof(msg)); while (read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
while (read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
continue; continue;
}
if (fexist(SYNC_STOPPED)) if (fexist(SYNC_STOPPED))
continue; continue;
execv(cmd, args); execv(cmd, args);
} }
if (console) close(fd);
close(fd); vhangup();
exit(0); exit(0);
} }
+1 -1
View File
@@ -81,7 +81,7 @@ void set_hostname (char **hostname);
int complete (char *cmd, int pid); int complete (char *cmd, int pid);
int run (char *cmd); int run (char *cmd);
int run_interactive (char *cmd, char *fmt, ...); 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); int run_parts (char *dir, char *cmd);
#endif /* FINIT_HELPERS_H_ */ #endif /* FINIT_HELPERS_H_ */
+1 -1
View File
@@ -160,7 +160,7 @@ void tty_start(finit_tty_t *tty)
args[i] = NULL; args[i] = NULL;
_d("Starting %s: %s on %s", is_console ? "console" : "TTY", cmd, tty->name); _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) void tty_stop(finit_tty_t *tty)