From cb904446ec7bc35a476fe1b71bd10f3a780857b7 Mon Sep 17 00:00:00 2001 From: sin Date: Mon, 10 Feb 2014 15:04:24 +0000 Subject: [PATCH] Ensure getty prompts for the username As Eckehard Berns reported: "On Slackware /bin/login times out after 60 seconds which will clutter the vt after a couple of minutes with timeout messages and login prompts. So it seems that getty should ask for the username first even if login could do it as well." --- getty.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/getty.c b/getty.c index 8a80808..fd84673 100644 --- a/getty.c +++ b/getty.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -23,7 +24,9 @@ main(int argc, char *argv[]) { int fd; struct sigaction sa; - char term[128]; + char term[128], logname[128], c; + int i = 0; + ssize_t n; ARGBEGIN { default: @@ -76,5 +79,27 @@ main(int argc, char *argv[]) sigaction(SIGHUP, &sa, NULL); putchar('\n'); - return execvp("/bin/login", (char *[]){ "login", NULL }); + printf("Login: "); + fflush(stdout); + + /* Flush pending input */ + ioctl(STDIN_FILENO, TCFLSH, (void *)0); + memset(logname, 0, sizeof(logname)); + while (1) { + n = read(STDIN_FILENO, &c, 1); + if (n < 0) + eprintf("read:"); + if (n == 0) + return EXIT_FAILURE; + if (i >= sizeof(logname) - 1) + eprintf("login name too long\n"); + if (c == '\n' || c == '\r') + break; + logname[i++] = c; + } + if (logname[0] == '-') + eprintf("login name cannot start with '-'\n"); + if (logname[0] == '\0') + return EXIT_FAILURE; + return execvp("/bin/login", (char *[]){ "login", logname, NULL }); }