aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-23 02:10:45 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-23 02:10:45 +0000
commitdce3fde4148690e0323d36fbd1aa2e87a1316a49 (patch)
tree11791205a42519e23d8205359e0435502368f1b5
parent7c4503d2238648989b79a51edba725d9beec3546 (diff)
downloadbusybox-w32-dce3fde4148690e0323d36fbd1aa2e87a1316a49.tar.gz
busybox-w32-dce3fde4148690e0323d36fbd1aa2e87a1316a49.tar.bz2
busybox-w32-dce3fde4148690e0323d36fbd1aa2e87a1316a49.zip
getty: cleanup part 1
-rw-r--r--loginutils/getty.c91
1 files changed, 47 insertions, 44 deletions
diff --git a/loginutils/getty.c b/loginutils/getty.c
index d279dc3a4..f2bff061e 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -50,7 +50,7 @@ extern void updwtmp(const char *filename, const struct utmp *ut);
50 * and for line editing at the same time. 50 * and for line editing at the same time.
51 */ 51 */
52 52
53#ifdef SYSV_STYLE 53#ifdef SYSV_STYLE
54#include <sys/utsname.h> 54#include <sys/utsname.h>
55#include <time.h> 55#include <time.h>
56#endif 56#endif
@@ -147,7 +147,7 @@ struct chardata {
147 147
148/* Initial values for the above. */ 148/* Initial values for the above. */
149 149
150static struct chardata init_chardata = { 150static const struct chardata init_chardata = {
151 DEF_ERASE, /* default erase character */ 151 DEF_ERASE, /* default erase character */
152 DEF_KILL, /* default kill character */ 152 DEF_KILL, /* default kill character */
153 13, /* default eol char */ 153 13, /* default eol char */
@@ -161,7 +161,7 @@ struct Speedtab {
161 int code; 161 int code;
162}; 162};
163 163
164static struct Speedtab speedtab[] = { 164static const struct Speedtab speedtab[] = {
165 {50, B50}, 165 {50, B50},
166 {75, B75}, 166 {75, B75},
167 {110, B110}, 167 {110, B110},
@@ -201,7 +201,7 @@ static struct Speedtab speedtab[] = {
201#endif 201#endif
202 202
203 203
204#ifdef SYSV_STYLE 204#ifdef SYSV_STYLE
205#ifdef CONFIG_FEATURE_UTMP 205#ifdef CONFIG_FEATURE_UTMP
206static void update_utmp(char *line); 206static void update_utmp(char *line);
207#endif 207#endif
@@ -262,7 +262,7 @@ static void parse_args(int argc, char **argv, struct options *op)
262 op->flags = getopt32(argc, argv, opt_string, 262 op->flags = getopt32(argc, argv, opt_string,
263 &(op->initstring), &fakehost, &(op->issue), 263 &(op->initstring), &fakehost, &(op->issue),
264 &(op->login), &ts); 264 &(op->login), &ts);
265 if(op->flags & F_INITSTRING) { 265 if (op->flags & F_INITSTRING) {
266 const char *p = op->initstring; 266 const char *p = op->initstring;
267 char *q; 267 char *q;
268 268
@@ -280,33 +280,34 @@ static void parse_args(int argc, char **argv, struct options *op)
280 *q = '\0'; 280 *q = '\0';
281 } 281 }
282 op->flags ^= F_ISSUE; /* revert flag show /etc/issue */ 282 op->flags ^= F_ISSUE; /* revert flag show /etc/issue */
283 if(op->flags & F_TIMEOUT) { 283 if (op->flags & F_TIMEOUT) {
284 op->timeout = xatoul_range(ts, 1, INT_MAX); 284 op->timeout = xatoul_range(ts, 1, INT_MAX);
285 } 285 }
286 argv += optind;
287 argc -= optind;
286 debug("after getopt loop\n"); 288 debug("after getopt loop\n");
287 if (argc < optind + 2) /* check parameter count */ 289 if (argc < 2) /* check parameter count */
288 bb_show_usage(); 290 bb_show_usage();
289 291
290 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ 292 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
291 if ('0' <= argv[optind][0] && argv[optind][0] <= '9') { 293 if ('0' <= argv[0][0] && argv[0][0] <= '9') {
292 /* a number first, assume it's a speed (BSD style) */ 294 /* a number first, assume it's a speed (BSD style) */
293 parse_speeds(op, argv[optind++]); /* baud rate(s) */ 295 parse_speeds(op, argv[0]); /* baud rate(s) */
294 op->tty = argv[optind]; /* tty name */ 296 op->tty = argv[1]; /* tty name */
295 } else { 297 } else {
296 op->tty = argv[optind++]; /* tty name */ 298 op->tty = argv[0]; /* tty name */
297 parse_speeds(op, argv[optind]); /* baud rate(s) */ 299 parse_speeds(op, argv[1]); /* baud rate(s) */
298 } 300 }
299 301
300 optind++; 302 if (argc > 2 && argv[2])
301 if (argc > optind && argv[optind]) 303 setenv("TERM", argv[2], 1);
302 setenv("TERM", argv[optind], 1);
303 304
304 debug("exiting parseargs\n"); 305 debug("exiting parseargs\n");
305} 306}
306 307
307static void xdup2(int srcfd, int dstfd, const char *tty) 308static void xdup2(int srcfd, int dstfd, const char *tty)
308{ 309{
309 if(dup2(srcfd, dstfd) == -1) 310 if (dup2(srcfd, dstfd) == -1)
310 bb_perror_msg_and_die("%s: dup", tty); 311 bb_perror_msg_and_die("%s: dup", tty);
311} 312}
312 313
@@ -333,7 +334,7 @@ static void open_tty(char *tty, struct termio *tp, int local)
333 334
334 debug("open(2)\n"); 335 debug("open(2)\n");
335 fd = xopen(tty, O_RDWR | O_NONBLOCK); 336 fd = xopen(tty, O_RDWR | O_NONBLOCK);
336 if(fd) { 337 if (fd) {
337 xdup2(fd, 0, tty); 338 xdup2(fd, 0, tty);
338 close(fd); 339 close(fd);
339 } 340 }
@@ -372,12 +373,14 @@ static void open_tty(char *tty, struct termio *tp, int local)
372 */ 373 */
373 374
374#ifdef DEBIAN 375#ifdef DEBIAN
376#warning Debian /dev/vcs[a]NN hack is deprecated and will be removed
375 { 377 {
376 /* tty to root.dialout 660 */ 378 /* tty to root.dialout 660 */
377 struct group *gr; 379 struct group *gr;
378 int id; 380 int id;
379 381
380 id = (gr = getgrnam("dialout")) ? gr->gr_gid : 0; 382 gr = getgrnam("dialout");
383 id = gr ? gr->gr_gid : 0;
381 chown(tty, 0, id); 384 chown(tty, 0, id);
382 chmod(tty, 0660); 385 chmod(tty, 0660);
383 386
@@ -491,7 +494,8 @@ static void auto_baud(struct termio *tp)
491 */ 494 */
492 495
493 (void) sleep(1); 496 (void) sleep(1);
494 if ((nread = read(0, buf, sizeof(buf) - 1)) > 0) { 497 nread = read(0, buf, sizeof(buf) - 1);
498 if (nread > 0) {
495 buf[nread] = '\0'; 499 buf[nread] = '\0';
496 for (bp = buf; bp < buf + nread; bp++) { 500 for (bp = buf; bp < buf + nread; bp++) {
497 if (isascii(*bp) && isdigit(*bp)) { 501 if (isascii(*bp) && isdigit(*bp)) {
@@ -536,15 +540,10 @@ static void do_prompt(struct options *op, struct termio *tp)
536/* returns 1 if true, 0 if false */ 540/* returns 1 if true, 0 if false */
537static int caps_lock(const char *s) 541static int caps_lock(const char *s)
538{ 542{
539 int capslock; 543 while (*s)
540 544 if (islower(*s++))
541 for (capslock = 0; *s; s++) { 545 return 0;
542 if (islower(*s)) 546 return 1;
543 return (0);
544 if (capslock == 0)
545 capslock = isupper(*s);
546 }
547 return (capslock);
548} 547}
549 548
550#define logname bb_common_bufsiz1 549#define logname bb_common_bufsiz1
@@ -557,7 +556,7 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
557 char ascval; /* low 7 bits of input character */ 556 char ascval; /* low 7 bits of input character */
558 int bits; /* # of "1" bits per character */ 557 int bits; /* # of "1" bits per character */
559 int mask; /* mask with 1 bit up */ 558 int mask; /* mask with 1 bit up */
560 static char *erase[] = { /* backspace-space-backspace */ 559 static const char *const erase[] = { /* backspace-space-backspace */
561 "\010\040\010", /* space parity */ 560 "\010\040\010", /* space parity */
562 "\010\040\010", /* odd parity */ 561 "\010\040\010", /* odd parity */
563 "\210\240\210", /* even parity */ 562 "\210\240\210", /* even parity */
@@ -575,7 +574,8 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
575 574
576 /* Prompt for and read a login name. */ 575 /* Prompt for and read a login name. */
577 576
578 for (*logname = 0; *logname == 0; /* void */ ) { 577 *logname = 0;
578 while (*logname) {
579 579
580 /* Write issue file and prompt, with "parity" bit == 0. */ 580 /* Write issue file and prompt, with "parity" bit == 0. */
581 581
@@ -583,7 +583,9 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
583 583
584 /* Read name, watch for break, parity, erase, kill, end-of-line. */ 584 /* Read name, watch for break, parity, erase, kill, end-of-line. */
585 585
586 for (bp = logname, cp->eol = 0; cp->eol == 0; /* void */ ) { 586 bp = logname;
587 cp->eol = 0;
588 while (cp->eol == 0) {
587 589
588 /* Do not report trivial EINTR/EIO errors. */ 590 /* Do not report trivial EINTR/EIO errors. */
589 591
@@ -594,13 +596,13 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
594 } 596 }
595 /* Do BREAK handling elsewhere. */ 597 /* Do BREAK handling elsewhere. */
596 598
597 if ((c == 0) && op->numspeed > 1) 599 if (c == 0 && op->numspeed > 1)
598 /* return (0); */
599 return NULL; 600 return NULL;
600 601
601 /* Do parity bit handling. */ 602 /* Do parity bit handling. */
602 603
603 if (c != (ascval = (c & 0177))) { /* "parity" bit on ? */ 604 ascval = c & 0177;
605 if (c != ascval) { /* "parity" bit on ? */
604 for (bits = 1, mask = 1; mask & 0177; mask <<= 1) 606 for (bits = 1, mask = 1; mask & 0177; mask <<= 1)
605 if (mask & ascval) 607 if (mask & ascval)
606 bits++; /* count "1" bits */ 608 bits++; /* count "1" bits */
@@ -648,12 +650,13 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio
648 } 650 }
649 /* Handle names with upper case and no lower case. */ 651 /* Handle names with upper case and no lower case. */
650 652
651 if ((cp->capslock = caps_lock(logname))) { 653 cp->capslock = caps_lock(logname);
654 if (cp->capslock) {
652 for (bp = logname; *bp; bp++) 655 for (bp = logname; *bp; bp++)
653 if (isupper(*bp)) 656 if (isupper(*bp))
654 *bp = tolower(*bp); /* map name to lower case */ 657 *bp = tolower(*bp); /* map name to lower case */
655 } 658 }
656 return (logname); 659 return logname;
657} 660}
658 661
659/* termio_final - set the final tty mode bits */ 662/* termio_final - set the final tty mode bits */
@@ -746,8 +749,8 @@ static void update_utmp(char *line)
746 utmpname(_PATH_UTMP); 749 utmpname(_PATH_UTMP);
747 setutent(); 750 setutent();
748 while ((utp = getutent()) 751 while ((utp = getutent())
749 && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid)) /* nothing */ 752 && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid))
750 ; 753 /* nothing */;
751 754
752 if (utp) { 755 if (utp) {
753 memcpy(&ut, utp, sizeof(ut)); 756 memcpy(&ut, utp, sizeof(ut));
@@ -756,7 +759,7 @@ static void update_utmp(char *line)
756 memset(&ut, 0, sizeof(ut)); 759 memset(&ut, 0, sizeof(ut));
757 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id)); 760 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
758 } 761 }
759 /*endutent(); */ 762 /* endutent(); */
760 763
761 strcpy(ut.ut_user, "LOGIN"); 764 strcpy(ut.ut_user, "LOGIN");
762 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line)); 765 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
@@ -808,20 +811,21 @@ int getty_main(int argc, char **argv)
808 close(0); 811 close(0);
809 close(1); 812 close(1);
810 close(2); 813 close(2);
814 logmode = LOGMODE_NONE;
811#ifdef __linux__ 815#ifdef __linux__
812 setsid(); 816 setsid();
813#endif 817#endif
814 /* We want special flavor of error_msg_and_die */
815 die_sleep = 10;
816 msg_eol = "\r\n";
817 /* Was "/dev/console". Why should we spam *system console* 818 /* Was "/dev/console". Why should we spam *system console*
818 * if there is a problem with getty on /dev/ttyS15?... */ 819 * if there is a problem with getty on /dev/ttyS15?... */
819 nullfd = xopen(bb_dev_null, O_RDWR); 820 nullfd = xopen(bb_dev_null, O_RDWR);
820 dup2(nullfd, 0); 821 dup2(nullfd, 0);
821 dup2(nullfd, 1); 822 dup2(nullfd, 1);
822 dup2(nullfd, 2); 823 dup2(nullfd, 2);
823 if(nullfd > 2) 824 if (nullfd > 2)
824 close(nullfd); 825 close(nullfd);
826 /* We want special flavor of error_msg_and_die */
827 die_sleep = 10;
828 msg_eol = "\r\n";
825 openlog(applet_name, LOG_PID, LOG_AUTH); 829 openlog(applet_name, LOG_PID, LOG_AUTH);
826 logmode = LOGMODE_BOTH; 830 logmode = LOGMODE_BOTH;
827 831
@@ -927,4 +931,3 @@ int getty_main(int argc, char **argv)
927 (void) execl(options.login, options.login, "--", logname, (char *) 0); 931 (void) execl(options.login, options.login, "--", logname, (char *) 0);
928 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login); 932 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);
929} 933}
930