source: MondoRescue/branches/2.2.9/mindi-busybox/loginutils/getty.c@ 2725

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