1/* vi: set sw=4 ts=4: */ 2/* agetty.c - another getty program for Linux. By W. Z. Venema 1989 3 * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk> 4 * This program is freely distributable. The entire man-page used to 5 * be here. Now read the real man-page agetty.8 instead. 6 * 7 * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95 8 * 9 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org> 10 * - added Native Language Support 11 * 12 * 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net> 13 * - enable hardware flow control before displaying /etc/issue 14 * 15 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 16 */ 17 18#include "libbb.h" 19#include <syslog.h> 20 21#if ENABLE_FEATURE_UTMP 22# include <utmp.h> /* LOGIN_PROCESS */ 23#endif 24 25#ifndef IUCLC 26# define IUCLC 0 27#endif 28 29/* 30 * Some heuristics to find out what environment we are in: if it is not 31 * System V, assume it is SunOS 4. 32 */ 33#ifdef LOGIN_PROCESS /* defined in System V utmp.h */ 34#include <sys/utsname.h> 35#else /* if !sysV style, wtmp/utmp code is off */ 36#undef ENABLE_FEATURE_UTMP 37#undef ENABLE_FEATURE_WTMP 38#define ENABLE_FEATURE_UTMP 0 39#define ENABLE_FEATURE_WTMP 0 40#endif /* LOGIN_PROCESS */ 41 42/* 43 * Things you may want to modify. 44 * 45 * You may disagree with the default line-editing etc. characters defined 46 * below. Note, however, that DEL cannot be used for interrupt generation 47 * and for line editing at the same time. 48 */ 49 50/* I doubt there are systems which still need this */ 51#undef HANDLE_ALLCAPS 52#undef ANCIENT_BS_KILL_CHARS 53 54#define _PATH_LOGIN "/bin/login" 55 56/* If ISSUE is not defined, getty will never display the contents of the 57 * /etc/issue file. You will not want to spit out large "issue" files at the 58 * wrong baud rate. 59 */ 60#define ISSUE "/etc/issue" /* displayed before the login prompt */ 61 62/* Some shorthands for control characters. */ 63#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */ 64#define CR CTL('M') /* carriage return */ 65#define NL CTL('J') /* line feed */ 66#define BS CTL('H') /* back space */ 67#define DEL CTL('?') /* delete */ 68 69/* Defaults for line-editing etc. characters; you may want to change this. */ 70#define DEF_ERASE DEL /* default erase character */ 71#define DEF_INTR CTL('C') /* default interrupt character */ 72#define DEF_QUIT CTL('\\') /* default quit char */ 73#define DEF_KILL CTL('U') /* default kill char */ 74#define DEF_EOF CTL('D') /* default EOF char */ 75#define DEF_EOL '\n' 76#define DEF_SWITCH 0 /* default switch char */ 77 78/* 79 * When multiple baud rates are specified on the command line, the first one 80 * we will try is the first one specified. 81 */ 82#define MAX_SPEED 10 /* max. nr. of baud rates */ 83 84/* Storage for command-line options. */ 85struct options { 86 int flags; /* toggle switches, see below */ 87 unsigned timeout; /* time-out period */ 88 const char *login; /* login program */ 89 const char *tty; /* name of tty */ 90 const char *initstring; /* modem init string */ 91 const char *issue; /* alternative issue file */ 92 int numspeed; /* number of baud rates to try */ 93 int speeds[MAX_SPEED]; /* baud rates to be tried */ 94}; 95 96/* Storage for things detected while the login name was read. */ 97struct chardata { 98 unsigned char erase; /* erase character */ 99 unsigned char kill; /* kill character */ 100 unsigned char eol; /* end-of-line character */ 101 unsigned char parity; /* what parity did we see */ 102 /* (parity & 1): saw odd parity char with 7th bit set */ 103 /* (parity & 2): saw even parity char with 7th bit set */ 104 /* parity == 0: probably 7-bit, space parity? */ 105 /* parity == 1: probably 7-bit, odd parity? */ 106 /* parity == 2: probably 7-bit, even parity? */ 107 /* parity == 3: definitely 8 bit, no parity! */ 108 /* Hmm... with any value of "parity" 8 bit, no parity is possible */ 109#ifdef HANDLE_ALLCAPS 110 unsigned char capslock; /* upper case without lower case */ 111#endif 112}; 113 114 115/* Initial values for the above. */ 116static const struct chardata init_chardata = { 117 DEF_ERASE, /* default erase character */ 118 DEF_KILL, /* default kill character */ 119 13, /* default eol char */ 120 0, /* space parity */ 121#ifdef HANDLE_ALLCAPS 122 0, /* no capslock */ 123#endif 124}; 125 126static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn"; 127#define F_INITSTRING (1 << 0) /* -I initstring is set */ 128#define F_LOCAL (1 << 1) /* -L force local */ 129#define F_FAKEHOST (1 << 2) /* -H fake hostname */ 130#define F_CUSTISSUE (1 << 3) /* -f give alternative issue file */ 131#define F_RTSCTS (1 << 4) /* -h enable RTS/CTS flow control */ 132#define F_ISSUE (1 << 5) /* -i display /etc/issue */ 133#define F_LOGIN (1 << 6) /* -l non-default login program */ 134#define F_PARSE (1 << 7) /* -m process modem status messages */ 135#define F_TIMEOUT (1 << 8) /* -t time out */ 136#define F_WAITCRLF (1 << 9) /* -w wait for CR or LF */ 137#define F_NOPROMPT (1 << 10) /* -n don't ask for login name */ 138 139 140#define line_buf bb_common_bufsiz1 141 142/* The following is used for understandable diagnostics. */ 143#ifdef DEBUGGING 144static FILE *dbf; 145#define DEBUGTERM "/dev/ttyp0" 146#define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0) 147#else 148#define debug(...) ((void)0) 149#endif 150 151 152/* bcode - convert speed string to speed code; return <= 0 on failure */ 153static int bcode(const char *s) 154{ 155 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */ 156 if (value < 0) /* bad terminating char, overflow, etc */ 157 return value; 158 return tty_value_to_baud(value); 159} 160 161/* parse_speeds - parse alternate baud rates */ 162static void parse_speeds(struct options *op, char *arg) 163{ 164 char *cp; 165 166 /* NB: at least one iteration is always done */ 167 debug("entered parse_speeds\n"); 168 while ((cp = strsep(&arg, ",")) != NULL) { 169 op->speeds[op->numspeed] = bcode(cp); 170 if (op->speeds[op->numspeed] < 0) 171 bb_error_msg_and_die("bad speed: %s", cp); 172 /* note: arg "0" turns into speed B0 */ 173 op->numspeed++; 174 if (op->numspeed > MAX_SPEED) 175 bb_error_msg_and_die("too many alternate speeds"); 176 } 177 debug("exiting parse_speeds\n"); 178} 179 180/* parse_args - parse command-line arguments */ 181static void parse_args(char **argv, struct options *op, char **fakehost_p) 182{ 183 char *ts; 184 185 opt_complementary = "-2:t+"; /* at least 2 args; -t N */ 186 op->flags = getopt32(argv, opt_string, 187 &(op->initstring), fakehost_p, &(op->issue), 188 &(op->login), &op->timeout); 189 argv += optind; 190 if (op->flags & F_INITSTRING) { 191 const char *p = op->initstring; 192 char *q; 193 194 op->initstring = q = xstrdup(p); 195 /* copy optarg into op->initstring decoding \ddd 196 octal codes into chars */ 197 while (*p) { 198 if (*p == '\\') { 199 p++; 200 *q++ = bb_process_escape_sequence(&p); 201 } else { 202 *q++ = *p++; 203 } 204 } 205 *q = '\0'; 206 } 207 op->flags ^= F_ISSUE; /* invert flag "show /etc/issue" */ 208 debug("after getopt\n"); 209 210 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ 211 op->tty = argv[0]; /* tty name */ 212 ts = argv[1]; /* baud rate(s) */ 213 if (isdigit(argv[0][0])) { 214 /* a number first, assume it's a speed (BSD style) */ 215 op->tty = ts; /* tty name is in argv[1] */ 216 ts = argv[0]; /* baud rate(s) */ 217 } 218 parse_speeds(op, ts); 219 220// TODO: if applet_name is set to "getty: TTY", bb_error_msg's get simpler! 221// grep for "%s:" 222 223 if (argv[2]) 224 xsetenv("TERM", argv[2]); 225 226 debug("exiting parse_args\n"); 227} 228 229/* open_tty - set up tty as standard { input, output, error } */ 230static void open_tty(const char *tty) 231{ 232 /* Set up new standard input, unless we are given an already opened port. */ 233 if (NOT_LONE_DASH(tty)) { 234// struct stat st; 235// int cur_dir_fd; 236// int fd; 237 238 /* Sanity checks... */ 239// cur_dir_fd = xopen(".", O_DIRECTORY | O_NONBLOCK); 240// xchdir("/dev"); 241// xstat(tty, &st); 242// if (!S_ISCHR(st.st_mode)) 243// bb_error_msg_and_die("%s: not a character device", tty); 244 245 if (tty[0] != '/') 246 tty = xasprintf("/dev/%s", tty); /* will leak it */ 247 248 /* Open the tty as standard input. */ 249 debug("open(2)\n"); 250 close(0); 251 /*fd =*/ xopen(tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */ 252 253// /* Restore current directory */ 254// fchdir(cur_dir_fd); 255 256 /* Open the tty as standard input, continued */ 257// xmove_fd(fd, 0); 258// /* fd is >= cur_dir_fd, and cur_dir_fd gets closed too here: */ 259// while (fd > 2) 260// close(fd--); 261 262 /* Set proper protections and ownership. */ 263 fchown(0, 0, 0); /* 0:0 */ 264 fchmod(0, 0620); /* crw--w---- */ 265 } else { 266 /* 267 * Standard input should already be connected to an open port. Make 268 * sure it is open for read/write. 269 */ 270 if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR) 271 bb_error_msg_and_die("stdin is not open for read/write"); 272 } 273} 274 275/* termios_init - initialize termios settings */ 276static void termios_init(struct termios *tp, int speed, struct options *op) 277{ 278 speed_t ispeed, ospeed; 279 /* 280 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o. 281 * Special characters are set after we have read the login name; all 282 * reads will be done in raw mode anyway. Errors will be dealt with 283 * later on. 284 */ 285#ifdef __linux__ 286 /* flush input and output queues, important for modems! */ 287 ioctl(0, TCFLSH, TCIOFLUSH); /* tcflush(0, TCIOFLUSH)? - same */ 288#endif 289 ispeed = ospeed = speed; 290 if (speed == B0) { 291 /* Speed was specified as "0" on command line. 292 * Just leave it unchanged */ 293 ispeed = cfgetispeed(tp); 294 ospeed = cfgetospeed(tp); 295 } 296 tp->c_cflag = CS8 | HUPCL | CREAD; 297 if (op->flags & F_LOCAL) 298 tp->c_cflag |= CLOCAL; 299 cfsetispeed(tp, ispeed); 300 cfsetospeed(tp, ospeed); 301 302 tp->c_iflag = tp->c_lflag = tp->c_line = 0; 303 tp->c_oflag = OPOST | ONLCR; 304 tp->c_cc[VMIN] = 1; 305 tp->c_cc[VTIME] = 0; 306 307 /* Optionally enable hardware flow control */ 308#ifdef CRTSCTS 309 if (op->flags & F_RTSCTS) 310 tp->c_cflag |= CRTSCTS; 311#endif 312 313 tcsetattr_stdin_TCSANOW(tp); 314 315 debug("term_io 2\n"); 316} 317 318/* auto_baud - extract baud rate from modem status message */ 319static void auto_baud(char *buf, unsigned size_buf, struct termios *tp) 320{ 321 int speed; 322 int vmin; 323 unsigned iflag; 324 char *bp; 325 int nread; 326 327 /* 328 * This works only if the modem produces its status code AFTER raising 329 * the DCD line, and if the computer is fast enough to set the proper 330 * baud rate before the message has gone by. We expect a message of the 331 * following format: 332 * 333 * <junk><number><junk> 334 * 335 * The number is interpreted as the baud rate of the incoming call. If the 336 * modem does not tell us the baud rate within one second, we will keep 337 * using the current baud rate. It is advisable to enable BREAK 338 * processing (comma-separated list of baud rates) if the processing of 339 * modem status messages is enabled. 340 */ 341 342 /* 343 * Use 7-bit characters, don't block if input queue is empty. Errors will 344 * be dealt with later on. 345 */ 346 iflag = tp->c_iflag; 347 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */ 348 vmin = tp->c_cc[VMIN]; 349 tp->c_cc[VMIN] = 0; /* don't block if queue empty */ 350 tcsetattr_stdin_TCSANOW(tp); 351 352 /* 353 * Wait for a while, then read everything the modem has said so far and 354 * try to extract the speed of the dial-in call. 355 */ 356 sleep(1); 357 nread = safe_read(STDIN_FILENO, buf, size_buf - 1); 358 if (nread > 0) { 359 buf[nread] = '\0'; 360 for (bp = buf; bp < buf + nread; bp++) { 361 if (isdigit(*bp)) { 362 speed = bcode(bp); 363 if (speed > 0) { 364 tp->c_cflag &= ~CBAUD; 365 tp->c_cflag |= speed; 366 } 367 break; 368 } 369 } 370 } 371 372 /* Restore terminal settings. Errors will be dealt with later on. */ 373 tp->c_iflag = iflag; 374 tp->c_cc[VMIN] = vmin; 375 tcsetattr_stdin_TCSANOW(tp); 376} 377 378/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */ 379static void do_prompt(struct options *op) 380{ 381#ifdef ISSUE 382 print_login_issue(op->issue, op->tty); 383#endif 384 print_login_prompt(); 385} 386 387#ifdef HANDLE_ALLCAPS 388/* all_is_upcase - string contains upper case without lower case */ 389/* returns 1 if true, 0 if false */ 390static int all_is_upcase(const char *s) 391{ 392 while (*s) 393 if (islower(*s++)) 394 return 0; 395 return 1; 396} 397#endif 398 399/* get_logname - get user name, establish parity, speed, erase, kill, eol; 400 * return NULL on BREAK, logname on success */ 401static char *get_logname(char *logname, unsigned size_logname, 402 struct options *op, struct chardata *cp) 403{ 404 char *bp; 405 char c; /* input character, full eight bits */ 406 char ascval; /* low 7 bits of input character */ 407 int bits; /* # of "1" bits per character */ 408 int mask; /* mask with 1 bit up */ 409 static const char erase[][3] = {/* backspace-space-backspace */ 410 "\010\040\010", /* space parity */ 411 "\010\040\010", /* odd parity */ 412 "\210\240\210", /* even parity */ 413 "\010\040\010", /* 8 bit no parity */ 414 }; 415 416 /* NB: *cp is pre-initialized with init_chardata */ 417 418 /* Flush pending input (esp. after parsing or switching the baud rate). */ 419 sleep(1); 420 ioctl(0, TCFLSH, TCIFLUSH); /* tcflush(0, TCIOFLUSH)? - same */ 421 422 /* Prompt for and read a login name. */ 423 logname[0] = '\0'; 424 while (!logname[0]) { 425 /* Write issue file and prompt, with "parity" bit == 0. */ 426 do_prompt(op); 427 428 /* Read name, watch for break, parity, erase, kill, end-of-line. */ 429 bp = logname; 430 cp->eol = '\0'; 431 while (cp->eol == '\0') { 432 433 /* Do not report trivial EINTR/EIO errors. */ 434 errno = EINTR; /* make read of 0 bytes be silent too */ 435 if (read(STDIN_FILENO, &c, 1) < 1) { 436 if (errno == EINTR || errno == EIO) 437 exit(EXIT_SUCCESS); 438 bb_perror_msg_and_die("%s: read", op->tty); 439 } 440 441 /* BREAK. If we have speeds to try, 442 * return NULL (will switch speeds and return here) */ 443 if (c == '\0' && op->numspeed > 1) 444 return NULL; 445 446 /* Do parity bit handling. */ 447 if (!(op->flags & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */ 448 bits = 1; 449 mask = 1; 450 while (mask & 0x7f) { 451 if (mask & c) 452 bits++; /* count "1" bits */ 453 mask <<= 1; 454 } 455 /* ... |= 2 - even, 1 - odd */ 456 cp->parity |= 2 - (bits & 1); 457 } 458 459 /* Do erase, kill and end-of-line processing. */ 460 ascval = c & 0x7f; 461 switch (ascval) { 462 case CR: 463 case NL: 464 *bp = '\0'; /* terminate logname */ 465 cp->eol = ascval; /* set end-of-line char */ 466 break; 467 case BS: 468 case DEL: 469#ifdef ANCIENT_BS_KILL_CHARS 470 case '#': 471#endif 472 cp->erase = ascval; /* set erase character */ 473 if (bp > logname) { 474 full_write(STDOUT_FILENO, erase[cp->parity], 3); 475 bp--; 476 } 477 break; 478 case CTL('U'): 479#ifdef ANCIENT_BS_KILL_CHARS 480 case '@': 481#endif 482 cp->kill = ascval; /* set kill character */ 483 while (bp > logname) { 484 full_write(STDOUT_FILENO, erase[cp->parity], 3); 485 bp--; 486 } 487 break; 488 case CTL('D'): 489 exit(EXIT_SUCCESS); 490 default: 491 if (ascval < ' ') { 492 /* ignore garbage characters */ 493 } else if ((int)(bp - logname) >= size_logname - 1) { 494 bb_error_msg_and_die("%s: input overrun", op->tty); 495 } else { 496 full_write(STDOUT_FILENO, &c, 1); /* echo the character */ 497 *bp++ = ascval; /* and store it */ 498 } 499 break; 500 } 501 } 502 } 503 /* Handle names with upper case and no lower case. */ 504 505#ifdef HANDLE_ALLCAPS 506 cp->capslock = all_is_upcase(logname); 507 if (cp->capslock) { 508 for (bp = logname; *bp; bp++) 509 if (isupper(*bp)) 510 *bp = tolower(*bp); /* map name to lower case */ 511 } 512#endif 513 return logname; 514} 515 516/* termios_final - set the final tty mode bits */ 517static void termios_final(struct options *op, struct termios *tp, struct chardata *cp) 518{ 519 /* General terminal-independent stuff. */ 520 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */ 521 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE; 522 /* no longer| ECHOCTL | ECHOPRT */ 523 tp->c_oflag |= OPOST; 524 /* tp->c_cflag = 0; */ 525 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */ 526 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */ 527 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */ 528 tp->c_cc[VEOL] = DEF_EOL; 529 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */ 530 531 /* Account for special characters seen in input. */ 532 if (cp->eol == CR) { 533 tp->c_iflag |= ICRNL; /* map CR in input to NL */ 534 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */ 535 } 536 tp->c_cc[VERASE] = cp->erase; /* set erase character */ 537 tp->c_cc[VKILL] = cp->kill; /* set kill character */ 538 539 /* Account for the presence or absence of parity bits in input. */ 540 switch (cp->parity) { 541 case 0: /* space (always 0) parity */ 542// I bet most people go here - they use only 7-bit chars in usernames.... 543 break; 544 case 1: /* odd parity */ 545 tp->c_cflag |= PARODD; 546 /* FALLTHROUGH */ 547 case 2: /* even parity */ 548 tp->c_cflag |= PARENB; 549 tp->c_iflag |= INPCK | ISTRIP; 550 /* FALLTHROUGH */ 551 case (1 | 2): /* no parity bit */ 552 tp->c_cflag &= ~CSIZE; 553 tp->c_cflag |= CS7; 554// FIXME: wtf? case 3: we saw both even and odd 8-bit bytes - 555// it's probably some umlauts etc, but definitely NOT 7-bit!!! 556// Entire parity detection madness here just begs for deletion... 557 break; 558 } 559 560 /* Account for upper case without lower case. */ 561#ifdef HANDLE_ALLCAPS 562 if (cp->capslock) { 563 tp->c_iflag |= IUCLC; 564 tp->c_lflag |= XCASE; 565 tp->c_oflag |= OLCUC; 566 } 567#endif 568 /* Optionally enable hardware flow control */ 569#ifdef CRTSCTS 570 if (op->flags & F_RTSCTS) 571 tp->c_cflag |= CRTSCTS; 572#endif 573 574 /* Finally, make the new settings effective */ 575 /* It's tcsetattr_stdin_TCSANOW() + error check */ 576 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty); 577} 578 579int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 580int getty_main(int argc UNUSED_PARAM, char **argv) 581{ 582 int n; 583 pid_t pid; 584 char *fakehost = NULL; /* Fake hostname for ut_host */ 585 char *logname; /* login name, given to /bin/login */ 586 /* Merging these into "struct local" may _seem_ to reduce 587 * parameter passing, but today's gcc will inline 588 * statics which are called once anyway, so don't do that */ 589 struct chardata chardata; /* set by get_logname() */ 590 struct termios termios; /* terminal mode bits */ 591 struct options options; 592 593 chardata = init_chardata; 594 595 memset(&options, 0, sizeof(options)); 596 options.login = _PATH_LOGIN; /* default login program */ 597 options.tty = "tty1"; /* default tty line */ 598 options.initstring = ""; /* modem init string */ 599#ifdef ISSUE 600 options.issue = ISSUE; /* default issue file */ 601#endif 602 603 /* Parse command-line arguments. */ 604 parse_args(argv, &options, &fakehost); 605 606 logmode = LOGMODE_NONE; 607 608 /* Create new session, lose controlling tty, if any */ 609 /* docs/ctty.htm says: 610 * "This is allowed only when the current process 611 * is not a process group leader" - is this a problem? */ 612 setsid(); 613 /* close stdio, and stray descriptors, just in case */ 614 n = xopen(bb_dev_null, O_RDWR); 615 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */ 616 xdup2(n, 1); 617 xdup2(n, 2); 618 while (n > 2) 619 close(n--); 620 621 /* Logging. We want special flavor of error_msg_and_die */ 622 die_sleep = 10; 623 msg_eol = "\r\n"; 624 /* most likely will internally use fd #3 in CLOEXEC mode: */ 625 openlog(applet_name, LOG_PID, LOG_AUTH); 626 logmode = LOGMODE_BOTH; 627 628#ifdef DEBUGGING 629 dbf = xfopen_for_write(DEBUGTERM); 630 for (n = 1; argv[n]; n++) { 631 debug(argv[n]); 632 debug("\n"); 633 } 634#endif 635 636 /* Open the tty as standard input, if it is not "-" */ 637 /* If it's not "-" and not taken yet, it will become our ctty */ 638 debug("calling open_tty\n"); 639 open_tty(options.tty); 640 ndelay_off(0); 641 debug("duping\n"); 642 xdup2(0, 1); 643 xdup2(0, 2); 644 645 /* 646 * The following ioctl will fail if stdin is not a tty, but also when 647 * there is noise on the modem control lines. In the latter case, the 648 * common course of action is (1) fix your cables (2) give the modem more 649 * time to properly reset after hanging up. SunOS users can achieve (2) 650 * by patching the SunOS kernel variable "zsadtrlow" to a larger value; 651 * 5 seconds seems to be a good value. 652 */ 653 /* tcgetattr() + error check */ 654 ioctl_or_perror_and_die(0, TCGETS, &termios, "%s: TCGETS", options.tty); 655 656 pid = getpid(); 657#ifdef __linux__ 658// FIXME: do we need this? Otherwise "-" case seems to be broken... 659 // /* Forcibly make fd 0 our controlling tty, even if another session 660 // * has it as a ctty. (Another session loses ctty). */ 661 // ioctl(0, TIOCSCTTY, (void*)1); 662 /* Make ourself a foreground process group within our session */ 663 tcsetpgrp(0, pid); 664#endif 665 666 /* Update the utmp file. This tty is ours now! */ 667 update_utmp(pid, LOGIN_PROCESS, options.tty, "LOGIN", fakehost); 668 669 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */ 670 debug("calling termios_init\n"); 671 termios_init(&termios, options.speeds[0], &options); 672 673 /* Write the modem init string and DON'T flush the buffers */ 674 if (options.flags & F_INITSTRING) { 675 debug("writing init string\n"); 676 full_write1_str(options.initstring); 677 } 678 679 /* Optionally detect the baud rate from the modem status message */ 680 debug("before autobaud\n"); 681 if (options.flags & F_PARSE) 682 auto_baud(line_buf, sizeof(line_buf), &termios); 683 684 /* Set the optional timer */ 685 alarm(options.timeout); /* if 0, alarm is not set */ 686 687 /* Optionally wait for CR or LF before writing /etc/issue */ 688 if (options.flags & F_WAITCRLF) { 689 char ch; 690 691 debug("waiting for cr-lf\n"); 692 while (safe_read(STDIN_FILENO, &ch, 1) == 1) { 693 debug("read %x\n", (unsigned char)ch); 694 ch &= 0x7f; /* strip "parity bit" */ 695 if (ch == '\n' || ch == '\r') 696 break; 697 } 698 } 699 700 logname = NULL; 701 if (!(options.flags & F_NOPROMPT)) { 702 /* NB:termios_init already set line speed 703 * to options.speeds[0] */ 704 int baud_index = 0; 705 706 while (1) { 707 /* Read the login name. */ 708 debug("reading login name\n"); 709 logname = get_logname(line_buf, sizeof(line_buf), 710 &options, &chardata); 711 if (logname) 712 break; 713 /* we are here only if options.numspeed > 1 */ 714 baud_index = (baud_index + 1) % options.numspeed; 715 cfsetispeed(&termios, options.speeds[baud_index]); 716 cfsetospeed(&termios, options.speeds[baud_index]); 717 tcsetattr_stdin_TCSANOW(&termios); 718 } 719 } 720 721 /* Disable timer. */ 722 alarm(0); 723 724 /* Finalize the termios settings. */ 725 termios_final(&options, &termios, &chardata); 726 727 /* Now the newline character should be properly written. */ 728 full_write(STDOUT_FILENO, "\n", 1); 729 730 /* Let the login program take care of password validation. */ 731 /* We use PATH because we trust that root doesn't set "bad" PATH, 732 * and getty is not suid-root applet. */ 733 /* With -n, logname == NULL, and login will ask for username instead */ 734 BB_EXECLP(options.login, options.login, "--", logname, NULL); 735 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login); 736} 737