From 86707c1f0b5e0e7797486243d03961e3b8538496 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sun, 25 Apr 2021 19:28:03 +0200 Subject: [PATCH] sulogin: minor refactor, splice out sh() as separate function - splice out sh() as a separate function - make sh() session leader and set controlling TTY (TIOCSCTTY) to make it a proper login shell with Ctrl-C functionality - simplify call to execl() Signed-off-by: Joachim Wiberg --- src/sulogin.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/sulogin.c b/src/sulogin.c index 9866e96d..9094ccd6 100644 --- a/src/sulogin.c +++ b/src/sulogin.c @@ -30,6 +30,7 @@ #include #include #include +#include /* getpwnam() cannot be used when statically linked */ static int get_passwd(struct passwd *pw) @@ -109,6 +110,19 @@ static void freepw(struct passwd *pw) free(pw->pw_name); } +static int sh(void) +{ + /* + * Become session leader and set controlling TTY + * to enable Ctrl-C and job control in shell. + */ + setsid(); + ioctl(STDIN_FILENO, TIOCSCTTY, 1); + setenv("PS1", "# ", 1); + + return execl(_PATH_BSHELL, "-sh", NULL); +} + int main(void) { struct termios old, raw; @@ -156,18 +170,10 @@ int main(void) passwd = crypt(pwd, pw.pw_passwd); if (passwd && !strcmp(passwd, pw.pw_passwd)) { - char arg0[strlen(_PATH_BSHELL) + 2]; - char *args[2] = { - arg0, - NULL - }; - tcsetattr(1, TCSANOW, &old); /* restore tty */ freepw(&pw); - snprintf(arg0, sizeof(arg0), "-%s", _PATH_BSHELL); - setenv("PS1", "# ", 1); - return execv(_PATH_BSHELL, args); + return sh(); } }