getty: cleanup part 1

This commit is contained in:
Denis Vlasenko 2006-10-23 02:10:45 +00:00
parent 7c4503d223
commit dce3fde414

View File

@ -147,7 +147,7 @@ struct chardata {
/* Initial values for the above. */ /* Initial values for the above. */
static struct chardata init_chardata = { static const struct chardata init_chardata = {
DEF_ERASE, /* default erase character */ DEF_ERASE, /* default erase character */
DEF_KILL, /* default kill character */ DEF_KILL, /* default kill character */
13, /* default eol char */ 13, /* default eol char */
@ -161,7 +161,7 @@ struct Speedtab {
int code; int code;
}; };
static struct Speedtab speedtab[] = { static const struct Speedtab speedtab[] = {
{50, B50}, {50, B50},
{75, B75}, {75, B75},
{110, B110}, {110, B110},
@ -283,23 +283,24 @@ static void parse_args(int argc, char **argv, struct options *op)
if (op->flags & F_TIMEOUT) { if (op->flags & F_TIMEOUT) {
op->timeout = xatoul_range(ts, 1, INT_MAX); op->timeout = xatoul_range(ts, 1, INT_MAX);
} }
argv += optind;
argc -= optind;
debug("after getopt loop\n"); debug("after getopt loop\n");
if (argc < optind + 2) /* check parameter count */ if (argc < 2) /* check parameter count */
bb_show_usage(); bb_show_usage();
/* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
if ('0' <= argv[optind][0] && argv[optind][0] <= '9') { if ('0' <= argv[0][0] && argv[0][0] <= '9') {
/* a number first, assume it's a speed (BSD style) */ /* a number first, assume it's a speed (BSD style) */
parse_speeds(op, argv[optind++]); /* baud rate(s) */ parse_speeds(op, argv[0]); /* baud rate(s) */
op->tty = argv[optind]; /* tty name */ op->tty = argv[1]; /* tty name */
} else { } else {
op->tty = argv[optind++]; /* tty name */ op->tty = argv[0]; /* tty name */
parse_speeds(op, argv[optind]); /* baud rate(s) */ parse_speeds(op, argv[1]); /* baud rate(s) */
} }
optind++; if (argc > 2 && argv[2])
if (argc > optind && argv[optind]) setenv("TERM", argv[2], 1);
setenv("TERM", argv[optind], 1);
debug("exiting parseargs\n"); debug("exiting parseargs\n");
} }
@ -372,12 +373,14 @@ static void open_tty(char *tty, struct termio *tp, int local)
*/ */
#ifdef DEBIAN #ifdef DEBIAN
#warning Debian /dev/vcs[a]NN hack is deprecated and will be removed
{ {
/* tty to root.dialout 660 */ /* tty to root.dialout 660 */
struct group *gr; struct group *gr;
int id; int id;
id = (gr = getgrnam("dialout")) ? gr->gr_gid : 0; gr = getgrnam("dialout");
id = gr ? gr->gr_gid : 0;
chown(tty, 0, id); chown(tty, 0, id);
chmod(tty, 0660); chmod(tty, 0660);
@ -491,7 +494,8 @@ static void auto_baud(struct termio *tp)
*/ */
(void) sleep(1); (void) sleep(1);
if ((nread = read(0, buf, sizeof(buf) - 1)) > 0) { nread = read(0, buf, sizeof(buf) - 1);
if (nread > 0) {
buf[nread] = '\0'; buf[nread] = '\0';
for (bp = buf; bp < buf + nread; bp++) { for (bp = buf; bp < buf + nread; bp++) {
if (isascii(*bp) && isdigit(*bp)) { if (isascii(*bp) && isdigit(*bp)) {
@ -536,15 +540,10 @@ static void do_prompt(struct options *op, struct termio *tp)
/* returns 1 if true, 0 if false */ /* returns 1 if true, 0 if false */
static int caps_lock(const char *s) static int caps_lock(const char *s)
{ {
int capslock; while (*s)
if (islower(*s++))
for (capslock = 0; *s; s++) { return 0;
if (islower(*s)) return 1;
return (0);
if (capslock == 0)
capslock = isupper(*s);
}
return (capslock);
} }
#define logname bb_common_bufsiz1 #define logname bb_common_bufsiz1
@ -557,7 +556,7 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
char ascval; /* low 7 bits of input character */ char ascval; /* low 7 bits of input character */
int bits; /* # of "1" bits per character */ int bits; /* # of "1" bits per character */
int mask; /* mask with 1 bit up */ int mask; /* mask with 1 bit up */
static char *erase[] = { /* backspace-space-backspace */ static const char *const erase[] = { /* backspace-space-backspace */
"\010\040\010", /* space parity */ "\010\040\010", /* space parity */
"\010\040\010", /* odd parity */ "\010\040\010", /* odd parity */
"\210\240\210", /* even parity */ "\210\240\210", /* even parity */
@ -575,7 +574,8 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
/* Prompt for and read a login name. */ /* Prompt for and read a login name. */
for (*logname = 0; *logname == 0; /* void */ ) { *logname = 0;
while (*logname) {
/* Write issue file and prompt, with "parity" bit == 0. */ /* Write issue file and prompt, with "parity" bit == 0. */
@ -583,7 +583,9 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
/* Read name, watch for break, parity, erase, kill, end-of-line. */ /* Read name, watch for break, parity, erase, kill, end-of-line. */
for (bp = logname, cp->eol = 0; cp->eol == 0; /* void */ ) { bp = logname;
cp->eol = 0;
while (cp->eol == 0) {
/* Do not report trivial EINTR/EIO errors. */ /* Do not report trivial EINTR/EIO errors. */
@ -594,13 +596,13 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
} }
/* Do BREAK handling elsewhere. */ /* Do BREAK handling elsewhere. */
if ((c == 0) && op->numspeed > 1) if (c == 0 && op->numspeed > 1)
/* return (0); */
return NULL; return NULL;
/* Do parity bit handling. */ /* Do parity bit handling. */
if (c != (ascval = (c & 0177))) { /* "parity" bit on ? */ ascval = c & 0177;
if (c != ascval) { /* "parity" bit on ? */
for (bits = 1, mask = 1; mask & 0177; mask <<= 1) for (bits = 1, mask = 1; mask & 0177; mask <<= 1)
if (mask & ascval) if (mask & ascval)
bits++; /* count "1" bits */ bits++; /* count "1" bits */
@ -648,12 +650,13 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
} }
/* Handle names with upper case and no lower case. */ /* Handle names with upper case and no lower case. */
if ((cp->capslock = caps_lock(logname))) { cp->capslock = caps_lock(logname);
if (cp->capslock) {
for (bp = logname; *bp; bp++) for (bp = logname; *bp; bp++)
if (isupper(*bp)) if (isupper(*bp))
*bp = tolower(*bp); /* map name to lower case */ *bp = tolower(*bp); /* map name to lower case */
} }
return (logname); return logname;
} }
/* termio_final - set the final tty mode bits */ /* termio_final - set the final tty mode bits */
@ -746,8 +749,8 @@ static void update_utmp(char *line)
utmpname(_PATH_UTMP); utmpname(_PATH_UTMP);
setutent(); setutent();
while ((utp = getutent()) while ((utp = getutent())
&& !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid)) /* nothing */ && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid))
; /* nothing */;
if (utp) { if (utp) {
memcpy(&ut, utp, sizeof(ut)); memcpy(&ut, utp, sizeof(ut));
@ -808,12 +811,10 @@ int getty_main(int argc, char **argv)
close(0); close(0);
close(1); close(1);
close(2); close(2);
logmode = LOGMODE_NONE;
#ifdef __linux__ #ifdef __linux__
setsid(); setsid();
#endif #endif
/* We want special flavor of error_msg_and_die */
die_sleep = 10;
msg_eol = "\r\n";
/* Was "/dev/console". Why should we spam *system console* /* Was "/dev/console". Why should we spam *system console*
* if there is a problem with getty on /dev/ttyS15?... */ * if there is a problem with getty on /dev/ttyS15?... */
nullfd = xopen(bb_dev_null, O_RDWR); nullfd = xopen(bb_dev_null, O_RDWR);
@ -822,6 +823,9 @@ int getty_main(int argc, char **argv)
dup2(nullfd, 2); dup2(nullfd, 2);
if (nullfd > 2) if (nullfd > 2)
close(nullfd); close(nullfd);
/* We want special flavor of error_msg_and_die */
die_sleep = 10;
msg_eol = "\r\n";
openlog(applet_name, LOG_PID, LOG_AUTH); openlog(applet_name, LOG_PID, LOG_AUTH);
logmode = LOGMODE_BOTH; logmode = LOGMODE_BOTH;
@ -927,4 +931,3 @@ int getty_main(int argc, char **argv)
(void) execl(options.login, options.login, "--", logname, (char *) 0); (void) execl(options.login, options.login, "--", logname, (char *) 0);
bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login); bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);
} }