bb_askpass: handle Ctrl-C, restore termoios on Ctrl-C.

sulogin: remove alarm handling, as it is redundant there.
code shrink. After all differences cancel out:

   text    data     bss     dec     hex filename
 777543    1000    9532  788075   c066b busybox_old
 777543    1000    9532  788075   c066b busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-10-20 19:20:22 +00:00
parent 037576d77b
commit e5387a0574
2 changed files with 49 additions and 46 deletions

View File

@ -17,7 +17,7 @@ static void askpass_timeout(int ATTRIBUTE_UNUSED ignore)
{ {
} }
char *bb_askpass(int timeout, const char * prompt) char *bb_askpass(int timeout, const char *prompt)
{ {
/* Was static char[BIGNUM] */ /* Was static char[BIGNUM] */
enum { sizeof_passwd = 128 }; enum { sizeof_passwd = 128 };
@ -25,35 +25,36 @@ char *bb_askpass(int timeout, const char * prompt)
char *ret; char *ret;
int i; int i;
struct sigaction sa; struct sigaction sa, oldsa;
struct termios old, new; struct termios tio, oldtio;
if (!passwd) if (!passwd)
passwd = xmalloc(sizeof_passwd); passwd = xmalloc(sizeof_passwd);
memset(passwd, 0, sizeof_passwd); memset(passwd, 0, sizeof_passwd);
tcgetattr(STDIN_FILENO, &old); tcgetattr(STDIN_FILENO, &oldtio);
tcflush(STDIN_FILENO, TCIFLUSH); tcflush(STDIN_FILENO, TCIFLUSH);
tio = oldtio;
tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
tcsetattr(STDIN_FILENO, TCSANOW, &tio);
fputs(prompt, stdout); memset(&sa, 0, sizeof(sa));
fflush(stdout); /* sa.sa_flags = 0; - no SA_RESTART! */
/* SIGINT and SIGALRM will interrupt read below */
tcgetattr(STDIN_FILENO, &new); sa.sa_handler = askpass_timeout;
new.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); sigaction(SIGINT, &sa, &oldsa);
new.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
tcsetattr(STDIN_FILENO, TCSANOW, &new);
if (timeout) { if (timeout) {
sa.sa_flags = 0;
sa.sa_handler = askpass_timeout;
sigaction(SIGALRM, &sa, NULL); sigaction(SIGALRM, &sa, NULL);
alarm(timeout); alarm(timeout);
} }
fputs(prompt, stdout);
fflush(stdout);
ret = NULL; ret = NULL;
/* On timeout, read will hopefully be interrupted by SIGALRM, /* On timeout or Ctrl-C, read will hopefully be interrupted,
* and we return NULL */ * and we return NULL */
if (read(STDIN_FILENO, passwd, sizeof_passwd-1) > 0) { if (read(STDIN_FILENO, passwd, sizeof_passwd - 1) > 0) {
ret = passwd; ret = passwd;
i = 0; i = 0;
/* Last byte is guaranteed to be 0 /* Last byte is guaranteed to be 0
@ -67,8 +68,9 @@ char *bb_askpass(int timeout, const char * prompt)
if (timeout) { if (timeout) {
alarm(0); alarm(0);
} }
sigaction(SIGINT, &oldsa, NULL);
tcsetattr(STDIN_FILENO, TCSANOW, &old); tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
bb_putchar('\n'); bb_putchar('\n');
fflush(stdout); fflush(stdout);
return ret; return ret;

View File

@ -9,29 +9,26 @@
#include "libbb.h" #include "libbb.h"
static const char *const forbid[] = { static const char forbid[] ALIGN1 =
"ENV", "ENV" "\0"
"BASH_ENV", "BASH_ENV" "\0"
"HOME", "HOME" "\0"
"IFS", "IFS" "\0"
"PATH", "PATH" "\0"
"SHELL", "SHELL" "\0"
"LD_LIBRARY_PATH", "LD_LIBRARY_PATH" "\0"
"LD_PRELOAD", "LD_PRELOAD" "\0"
"LD_TRACE_LOADED_OBJECTS", "LD_TRACE_LOADED_OBJECTS" "\0"
"LD_BIND_NOW", "LD_BIND_NOW" "\0"
"LD_AOUT_LIBRARY_PATH", "LD_AOUT_LIBRARY_PATH" "\0"
"LD_AOUT_PRELOAD", "LD_AOUT_PRELOAD" "\0"
"LD_NOWARN", "LD_NOWARN" "\0"
"LD_KEEPDIR", "LD_KEEPDIR" "\0";
(char *) 0
};
//static void catchalarm(int ATTRIBUTE_UNUSED junk)
static void catchalarm(int ATTRIBUTE_UNUSED junk) //{
{ // exit(EXIT_FAILURE);
exit(EXIT_FAILURE); //}
}
int sulogin_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int sulogin_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@ -40,7 +37,7 @@ int sulogin_main(int argc, char **argv)
char *cp; char *cp;
int timeout = 0; int timeout = 0;
char *timeout_arg; char *timeout_arg;
const char *const *p; const char *p;
struct passwd *pwd; struct passwd *pwd;
const char *shell; const char *shell;
#if ENABLE_FEATURE_SHADOWPASSWDS #if ENABLE_FEATURE_SHADOWPASSWDS
@ -71,10 +68,14 @@ int sulogin_main(int argc, char **argv)
} }
/* Clear out anything dangerous from the environment */ /* Clear out anything dangerous from the environment */
for (p = forbid; *p; p++) p = forbid;
unsetenv(*p); do {
unsetenv(p);
p += strlen(p) + 1;
} while (*p);
signal(SIGALRM, catchalarm); // bb_askpass() already handles this
// signal(SIGALRM, catchalarm);
pwd = getpwuid(0); pwd = getpwuid(0);
if (!pwd) { if (!pwd) {
@ -105,7 +106,7 @@ int sulogin_main(int argc, char **argv)
bb_error_msg("login incorrect"); bb_error_msg("login incorrect");
} }
memset(cp, 0, strlen(cp)); memset(cp, 0, strlen(cp));
signal(SIGALRM, SIG_DFL); // signal(SIGALRM, SIG_DFL);
bb_info_msg("System Maintenance Mode"); bb_info_msg("System Maintenance Mode");
@ -122,6 +123,6 @@ int sulogin_main(int argc, char **argv)
/* Exec login shell with no additional parameters. Never returns. */ /* Exec login shell with no additional parameters. Never returns. */
run_shell(shell, 1, NULL, NULL); run_shell(shell, 1, NULL, NULL);
auth_error: auth_error:
bb_error_msg_and_die("no password entry for 'root'"); bb_error_msg_and_die("no password entry for root");
} }