blob: 3f20c8e8132afd2a01785709b9a93b206db3db20 [file] [log] [blame]
Robert Griebl1fca5582002-06-04 20:45:46 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +01002/*
3 * Based on agetty - another getty program for Linux. By W. Z. Venema 1989
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00004 * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk>
Denys Vlasenko1f084042011-01-24 00:16:50 +01005 * This program is freely distributable.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00006 *
7 * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95
8 *
Denis Vlasenkob44c7902008-03-17 09:29:43 +00009 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010010 * - Added Native Language Support
Denis Vlasenkob44c7902008-03-17 09:29:43 +000011 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000012 * 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010013 * - Enabled hardware flow control before displaying /etc/issue
14 *
15 * 2011-01 Venys Vlasenko
16 * - Removed parity detection code. It can't work reliably:
17 * if all chars received have bit 7 cleared and odd (or even) parity,
18 * it is impossible to determine whether other side is 8-bit,no-parity
19 * or 7-bit,odd(even)-parity. It also interferes with non-ASCII usernames.
20 * - From now on, we assume that parity is correctly set.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000021 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020022 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000023 */
Robert Griebl1fca5582002-06-04 20:45:46 +000024
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Denis Vlasenkof7026522006-09-19 13:50:55 +000026#include <syslog.h>
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020027#ifndef IUCLC
28# define IUCLC 0
29#endif
30
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010031#ifndef LOGIN_PROCESS
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010032# undef ENABLE_FEATURE_UTMP
33# undef ENABLE_FEATURE_WTMP
34# define ENABLE_FEATURE_UTMP 0
35# define ENABLE_FEATURE_WTMP 0
Denys Vlasenko65963802011-01-24 04:43:04 +010036#endif
Robert Griebl1fca5582002-06-04 20:45:46 +000037
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010038
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010039/* The following is used for understandable diagnostics */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010040#ifdef DEBUGGING
41static FILE *dbf;
42# define DEBUGTERM "/dev/ttyp0"
43# define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0)
44#else
45# define debug(...) ((void)0)
46#endif
47
48
Denis Vlasenko6476cc12006-11-07 01:52:10 +000049/*
50 * Things you may want to modify.
51 *
52 * You may disagree with the default line-editing etc. characters defined
53 * below. Note, however, that DEL cannot be used for interrupt generation
54 * and for line editing at the same time.
55 */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010056#undef _PATH_LOGIN
Denis Vlasenko6476cc12006-11-07 01:52:10 +000057#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000058
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010059/* Displayed before the login prompt.
60 * If ISSUE is not defined, getty will never display the contents of the
Denis Vlasenko6476cc12006-11-07 01:52:10 +000061 * /etc/issue file. You will not want to spit out large "issue" files at the
62 * wrong baud rate.
63 */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010064#define ISSUE "/etc/issue"
Eric Andersen77804ce2005-07-27 06:05:38 +000065
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010066/* Some shorthands for control characters */
Denis Vlasenko397de612008-03-17 08:55:44 +000067#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000068#define BS CTL('H') /* back space */
69#define DEL CTL('?') /* delete */
Robert Griebl1fca5582002-06-04 20:45:46 +000070
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010071/* Defaults for line-editing etc. characters; you may want to change this */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000072#define DEF_INTR CTL('C') /* default interrupt character */
73#define DEF_QUIT CTL('\\') /* default quit char */
74#define DEF_KILL CTL('U') /* default kill char */
75#define DEF_EOF CTL('D') /* default EOF char */
76#define DEF_EOL '\n'
Denys Vlasenko70739642011-01-24 18:17:19 +010077#define DEF_SWITCH 0 /* default switch char (none) */
Robert Griebl1fca5582002-06-04 20:45:46 +000078
Denis Vlasenko6476cc12006-11-07 01:52:10 +000079/*
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010080 * When multiple baud rates are specified on the command line,
81 * the first one we will try is the first one specified.
Denis Vlasenko6476cc12006-11-07 01:52:10 +000082 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000083#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +000084
Denys Vlasenko65963802011-01-24 04:43:04 +010085struct globals {
Denis Vlasenko13858992006-10-08 12:49:22 +000086 unsigned timeout; /* time-out period */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000087 const char *login; /* login program */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010088 const char *fakehost;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000089 const char *tty; /* name of tty */
90 const char *initstring; /* modem init string */
91 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000092 int numspeed; /* number of baud rates to try */
93 int speeds[MAX_SPEED]; /* baud rates to be tried */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010094 unsigned char eol; /* end-of-line char seen (CR or NL) */
Denys Vlasenko65963802011-01-24 04:43:04 +010095 struct termios termios; /* terminal mode bits */
Denys Vlasenko65963802011-01-24 04:43:04 +010096 char line_buf[128];
Robert Griebl1fca5582002-06-04 20:45:46 +000097};
98
Denys Vlasenko65963802011-01-24 04:43:04 +010099#define G (*ptr_to_globals)
100#define INIT_G() do { \
101 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
102} while (0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000103
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100104//usage:#define getty_trivial_usage
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100105//usage: "[OPTIONS] BAUD_RATE[,BAUD_RATE]... TTY [TERMTYPE]"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100106//usage:#define getty_full_usage "\n\n"
107//usage: "Open a tty, prompt for a login name, then invoke /bin/login\n"
108//usage: "\nOptions:"
Denys Vlasenkod8494932011-01-26 03:26:38 +0100109//usage: "\n -h Enable hardware RTS/CTS flow control"
110//usage: "\n -L Set CLOCAL (ignore Carrier Detect state)"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100111//usage: "\n -m Get baud rate from modem's CONNECT status message"
Denys Vlasenkod8494932011-01-26 03:26:38 +0100112//usage: "\n -n Don't prompt for login name"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100113//usage: "\n -w Wait for CR or LF before sending /etc/issue"
Denys Vlasenkod8494932011-01-26 03:26:38 +0100114//usage: "\n -i Don't display /etc/issue"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100115//usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue"
116//usage: "\n -l LOGIN Invoke LOGIN instead of /bin/login"
Denys Vlasenkod8494932011-01-26 03:26:38 +0100117//usage: "\n -t SEC Terminate after SEC if no login name is read"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100118//usage: "\n -I INITSTR Send INITSTR before anything else"
119//usage: "\n -H HOST Log HOST into the utmp file as the hostname"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100120//usage: "\n"
121//usage: "\nBAUD_RATE of 0 leaves it unchanged"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100122
123static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
124#define F_INITSTRING (1 << 0) /* -I */
125#define F_LOCAL (1 << 1) /* -L */
126#define F_FAKEHOST (1 << 2) /* -H */
127#define F_CUSTISSUE (1 << 3) /* -f */
128#define F_RTSCTS (1 << 4) /* -h */
129#define F_NOISSUE (1 << 5) /* -i */
130#define F_LOGIN (1 << 6) /* -l */
131#define F_PARSE (1 << 7) /* -m */
132#define F_TIMEOUT (1 << 8) /* -t */
133#define F_WAITCRLF (1 << 9) /* -w */
134#define F_NOPROMPT (1 << 10) /* -n */
Robert Griebl1fca5582002-06-04 20:45:46 +0000135
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000136
Denys Vlasenko1f084042011-01-24 00:16:50 +0100137/* convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000138static int bcode(const char *s)
139{
Denis Vlasenko397de612008-03-17 08:55:44 +0000140 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
141 if (value < 0) /* bad terminating char, overflow, etc */
142 return value;
143 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000144}
145
Denys Vlasenko1f084042011-01-24 00:16:50 +0100146/* parse alternate baud rates */
Denys Vlasenko65963802011-01-24 04:43:04 +0100147static void parse_speeds(char *arg)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000148{
149 char *cp;
150
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000151 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000152 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000153 while ((cp = strsep(&arg, ",")) != NULL) {
Denys Vlasenko65963802011-01-24 04:43:04 +0100154 G.speeds[G.numspeed] = bcode(cp);
155 if (G.speeds[G.numspeed] < 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000156 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000157 /* note: arg "0" turns into speed B0 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100158 G.numspeed++;
159 if (G.numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000160 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000161 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000162 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000163}
164
Denys Vlasenko1f084042011-01-24 00:16:50 +0100165/* parse command-line arguments */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100166static void parse_args(char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +0000167{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000168 char *ts;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100169 int flags;
Robert Griebl1fca5582002-06-04 20:45:46 +0000170
Denis Vlasenko397de612008-03-17 08:55:44 +0000171 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100172 flags = getopt32(argv, opt_string,
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100173 &G.initstring, &G.fakehost, &G.issue,
Denys Vlasenko65963802011-01-24 04:43:04 +0100174 &G.login, &G.timeout
175 );
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100176 if (flags & F_INITSTRING) {
Denys Vlasenko65963802011-01-24 04:43:04 +0100177 G.initstring = xstrdup(G.initstring);
Denys Vlasenko53600592010-10-23 21:06:06 +0200178 /* decode \ddd octal codes into chars */
Denys Vlasenko65963802011-01-24 04:43:04 +0100179 strcpy_and_process_escape_sequences((char*)G.initstring, G.initstring);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000180 }
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100181 argv += optind;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000182 debug("after getopt\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000183
Denys Vlasenko65963802011-01-24 04:43:04 +0100184 /* We loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
185 G.tty = argv[0]; /* tty name */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000186 ts = argv[1]; /* baud rate(s) */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000187 if (isdigit(argv[0][0])) {
Denys Vlasenko65963802011-01-24 04:43:04 +0100188 /* A number first, assume it's a speed (BSD style) */
189 G.tty = ts; /* tty name is in argv[1] */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000190 ts = argv[0]; /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000191 }
Denys Vlasenko65963802011-01-24 04:43:04 +0100192 parse_speeds(ts);
193 applet_name = xasprintf("getty: %s", G.tty);
Denis Vlasenko397de612008-03-17 08:55:44 +0000194
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000195 if (argv[2])
Denis Vlasenko397de612008-03-17 08:55:44 +0000196 xsetenv("TERM", argv[2]);
Robert Griebl1fca5582002-06-04 20:45:46 +0000197
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000198 debug("exiting parse_args\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000199}
200
Denys Vlasenko1f084042011-01-24 00:16:50 +0100201/* set up tty as standard input, output, error */
Denys Vlasenko65963802011-01-24 04:43:04 +0100202static void open_tty(void)
Robert Griebl1fca5582002-06-04 20:45:46 +0000203{
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100204 /* Set up new standard input, unless we are given an already opened port */
Denys Vlasenko65963802011-01-24 04:43:04 +0100205 if (NOT_LONE_DASH(G.tty)) {
206 if (G.tty[0] != '/')
207 G.tty = xasprintf("/dev/%s", G.tty); /* will leak it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000208
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100209 /* Open the tty as standard input */
Robert Griebl1fca5582002-06-04 20:45:46 +0000210 debug("open(2)\n");
Denis Vlasenko397de612008-03-17 08:55:44 +0000211 close(0);
Denys Vlasenko65963802011-01-24 04:43:04 +0100212 xopen(G.tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000213
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100214 /* Set proper protections and ownership */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000215 fchown(0, 0, 0); /* 0:0 */
Denis Vlasenko397de612008-03-17 08:55:44 +0000216 fchmod(0, 0620); /* crw--w---- */
Robert Griebl1fca5582002-06-04 20:45:46 +0000217 } else {
Robert Griebl1fca5582002-06-04 20:45:46 +0000218 /*
219 * Standard input should already be connected to an open port. Make
220 * sure it is open for read/write.
221 */
Denys Vlasenko006416e2011-01-22 17:29:53 +0100222 if ((fcntl(0, F_GETFL) & (O_RDWR|O_RDONLY|O_WRONLY)) != O_RDWR)
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000223 bb_error_msg_and_die("stdin is not open for read/write");
Robert Griebl1fca5582002-06-04 20:45:46 +0000224 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000225}
226
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100227static void set_termios(void)
228{
229 if (tcsetattr_stdin_TCSANOW(&G.termios) < 0)
230 bb_perror_msg_and_die("tcsetattr");
231}
232
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100233/* We manipulate termios this way:
234 * - first, we read existing termios settings
235 * - termios_init modifies some parts and sets it
236 * - auto_baud and/or BREAK processing can set different speed and set termios
237 * - termios_final again modifies some parts and sets termios before
238 * execing login
239 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100240static void termios_init(int speed)
Robert Griebl1fca5582002-06-04 20:45:46 +0000241{
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100242 /* Try to drain output buffer, with 5 sec timeout.
243 * Added on request from users of ~600 baud serial interface
244 * with biggish buffer on a 90MHz CPU.
245 * They were losing hundreds of bytes of buffered output
246 * on tcflush.
Denys Vlasenkodc6cd122011-01-24 00:28:43 +0100247 */
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100248 signal_no_SA_RESTART_empty_mask(SIGALRM, record_signo);
249 alarm(5);
250 tcdrain(STDIN_FILENO);
251 alarm(0);
252 signal(SIGALRM, SIG_DFL); /* do not break -t TIMEOUT! */
253
254 /* Flush input and output queues, important for modems! */
Denys Vlasenkodc6cd122011-01-24 00:28:43 +0100255 tcflush(STDIN_FILENO, TCIOFLUSH);
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100256
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100257 /* Set speed if it wasn't specified as "0" on command line */
Denys Vlasenko1f084042011-01-24 00:16:50 +0100258 if (speed != B0)
Denys Vlasenko65963802011-01-24 04:43:04 +0100259 cfsetspeed(&G.termios, speed);
Denys Vlasenko1f084042011-01-24 00:16:50 +0100260
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100261 /* Initial termios settings: 8-bit characters, raw mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000262 * Special characters are set after we have read the login name; all
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100263 * reads will be done in raw mode anyway.
Robert Griebl1fca5582002-06-04 20:45:46 +0000264 */
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100265 /* Clear all bits except: */
266 G.termios.c_cflag &= (0
267 /* 2 stop bits (1 otherwise)
268 * Enable parity bit (both on input and output)
269 * Odd parity (else even)
270 */
271 | CSTOPB | PARENB | PARODD
Denys Vlasenko4907f6e2011-01-26 16:20:54 +0100272#ifdef CMSPAR
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100273 | CMSPAR /* mark or space parity */
274#endif
275 | CBAUD /* (output) baud rate */
276#ifdef CBAUDEX
277 | CBAUDEX /* (output) baud rate */
278#endif
279#ifdef CIBAUD
280 | CIBAUD /* input baud rate */
281#endif
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100282 );
283 /* Set: 8 bits; hang up (drop DTR) on last close; enable receive */
284 G.termios.c_cflag |= CS8 | HUPCL | CREAD;
Denys Vlasenkod8494932011-01-26 03:26:38 +0100285 if (option_mask32 & F_LOCAL) {
286 /* ignore Carrier Detect pin:
287 * opens don't block when CD is low,
288 * losing CD doesn't hang up processes whose ctty is this tty
289 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100290 G.termios.c_cflag |= CLOCAL;
Denys Vlasenkod8494932011-01-26 03:26:38 +0100291 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000292#ifdef CRTSCTS
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100293 if (option_mask32 & F_RTSCTS)
Denys Vlasenkof36368e2011-01-26 16:14:47 +0100294 G.termios.c_cflag |= CRTSCTS; /* flow control using RTS/CTS pins */
Denys Vlasenkod8494932011-01-26 03:26:38 +0100295#endif
296 G.termios.c_iflag = 0;
297 G.termios.c_lflag = 0;
298 /* non-raw output; add CR to each NL */
299 G.termios.c_oflag = OPOST | ONLCR;
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100300
Denys Vlasenkod8494932011-01-26 03:26:38 +0100301 G.termios.c_cc[VMIN] = 1; /* block reads if < 1 char is available */
302 G.termios.c_cc[VTIME] = 0; /* no timeout (reads block forever) */
303#ifdef __linux__
304 G.termios.c_line = 0;
Robert Griebl1fca5582002-06-04 20:45:46 +0000305#endif
306
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100307 set_termios();
Robert Griebl1fca5582002-06-04 20:45:46 +0000308
Robert Griebl1fca5582002-06-04 20:45:46 +0000309 debug("term_io 2\n");
310}
311
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100312static void termios_final(void)
313{
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100314 /* software flow control on output (stop sending if XOFF is recvd);
315 * and on input (send XOFF when buffer is full)
316 */
Denys Vlasenkod8494932011-01-26 03:26:38 +0100317 G.termios.c_iflag |= IXON | IXOFF;
318 if (G.eol == '\r') {
319 G.termios.c_iflag |= ICRNL; /* map CR on input to NL */
320 }
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100321 /* Other bits in c_iflag:
322 * IXANY Any recvd char enables output (any char is also a XON)
323 * INPCK Enable parity check
324 * IGNPAR Ignore parity errors (drop bad bytes)
325 * PARMRK Mark parity errors with 0xff, 0x00 prefix
326 * (else bad byte is received as 0x00)
327 * ISTRIP Strip parity bit
328 * IGNBRK Ignore break condition
329 * BRKINT Send SIGINT on break - maybe set this?
330 * INLCR Map NL to CR
331 * IGNCR Ignore CR
332 * ICRNL Map CR to NL
333 * IUCLC Map uppercase to lowercase
334 * IMAXBEL Echo BEL on input line too long
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100335 * IUTF8 Appears to affect tty's idea of char widths,
336 * observed to improve backspacing through Unicode chars
Denys Vlasenkob7c9fb22011-02-03 00:05:48 +0100337 */
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100338
339 /* line buffered input (NL or EOL or EOF chars end a line);
340 * recognize INT/QUIT/SUSP chars;
341 * echo input chars;
342 * echo BS-SP-BS on erase character;
343 * echo kill char specially, not as ^c (ECHOKE controls how exactly);
344 * erase all input via BS-SP-BS on kill char (else go to next line)
345 */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100346 G.termios.c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100347 /* Other bits in c_lflag:
348 * XCASE Map uppercase to \lowercase [tried, doesn't work]
349 * ECHONL Echo NL even if ECHO is not set
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100350 * ECHOCTL Echo ctrl chars as ^c (else don't echo) - maybe set this?
351 * ECHOPRT On erase, echo erased chars
352 * [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen]
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100353 * NOFLSH Don't flush input buffer after interrupt or quit chars
354 * IEXTEN Enable extended functions (??)
355 * [glibc says it enables c_cc[LNEXT] "enter literal char"
356 * and c_cc[VDISCARD] "toggle discard buffered output" chars]
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100357 * FLUSHO Output being flushed (c_cc[VDISCARD] is in effect)
358 * PENDIN Retype pending input at next read or input char
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100359 * (c_cc[VREPRINT] is being processed)
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100360 * TOSTOP Send SIGTTOU for background output
361 * (why "stty sane" unsets this bit?)
362 */
Denys Vlasenkod8494932011-01-26 03:26:38 +0100363
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100364 G.termios.c_cc[VINTR] = DEF_INTR;
365 G.termios.c_cc[VQUIT] = DEF_QUIT;
366 G.termios.c_cc[VEOF] = DEF_EOF;
367 G.termios.c_cc[VEOL] = DEF_EOL;
368#ifdef VSWTC
369 G.termios.c_cc[VSWTC] = DEF_SWITCH;
370#endif
Denys Vlasenko70739642011-01-24 18:17:19 +0100371#ifdef VSWTCH
372 G.termios.c_cc[VSWTCH] = DEF_SWITCH;
373#endif
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100374 G.termios.c_cc[VKILL] = DEF_KILL;
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100375 /* Other control chars:
376 * VEOL2
377 * VERASE, VWERASE - (word) erase. we may set VERASE in get_logname
378 * VREPRINT - reprint current input buffer
379 * VLNEXT, VDISCARD, VSTATUS
380 * VSUSP, VDSUSP - send (delayed) SIGTSTP
381 * VSTART, VSTOP - chars used for IXON/IXOFF
382 */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100383
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100384 set_termios();
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100385}
386
Denys Vlasenko1f084042011-01-24 00:16:50 +0100387/* extract baud rate from modem status message */
Denys Vlasenko65963802011-01-24 04:43:04 +0100388static void auto_baud(void)
Robert Griebl1fca5582002-06-04 20:45:46 +0000389{
Robert Griebl1fca5582002-06-04 20:45:46 +0000390 int nread;
391
392 /*
393 * This works only if the modem produces its status code AFTER raising
394 * the DCD line, and if the computer is fast enough to set the proper
395 * baud rate before the message has gone by. We expect a message of the
396 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000397 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000398 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000399 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000400 * The number is interpreted as the baud rate of the incoming call. If the
401 * modem does not tell us the baud rate within one second, we will keep
402 * using the current baud rate. It is advisable to enable BREAK
403 * processing (comma-separated list of baud rates) if the processing of
404 * modem status messages is enabled.
405 */
406
Denys Vlasenkod8494932011-01-26 03:26:38 +0100407 G.termios.c_cc[VMIN] = 0; /* don't block reads (min read is 0 chars) */
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100408 set_termios();
Robert Griebl1fca5582002-06-04 20:45:46 +0000409
410 /*
411 * Wait for a while, then read everything the modem has said so far and
412 * try to extract the speed of the dial-in call.
413 */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000414 sleep(1);
Denys Vlasenko65963802011-01-24 04:43:04 +0100415 nread = safe_read(STDIN_FILENO, G.line_buf, sizeof(G.line_buf) - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000416 if (nread > 0) {
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100417 int speed;
418 char *bp;
Denys Vlasenko65963802011-01-24 04:43:04 +0100419 G.line_buf[nread] = '\0';
420 for (bp = G.line_buf; bp < G.line_buf + nread; bp++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000421 if (isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000422 speed = bcode(bp);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200423 if (speed > 0)
Denys Vlasenko65963802011-01-24 04:43:04 +0100424 cfsetspeed(&G.termios, speed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000425 break;
426 }
427 }
428 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000429
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100430 /* Restore terminal settings */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100431 G.termios.c_cc[VMIN] = 1; /* restore to value set by termios_init */
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100432 set_termios();
Robert Griebl1fca5582002-06-04 20:45:46 +0000433}
434
Denys Vlasenko1f084042011-01-24 00:16:50 +0100435/* get user name, establish parity, speed, erase, kill, eol;
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100436 * return NULL on BREAK, logname on success
437 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100438static char *get_logname(void)
Robert Griebl1fca5582002-06-04 20:45:46 +0000439{
Robert Griebl1fca5582002-06-04 20:45:46 +0000440 char *bp;
Denys Vlasenko65963802011-01-24 04:43:04 +0100441 char c;
Robert Griebl1fca5582002-06-04 20:45:46 +0000442
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100443 /* Flush pending input (esp. after parsing or switching the baud rate) */
Denys Vlasenkodc6cd122011-01-24 00:28:43 +0100444 usleep(100*1000); /* 0.1 sec */
Denys Vlasenko13609182011-01-24 23:45:22 +0100445 tcflush(STDIN_FILENO, TCIFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000446
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100447 /* Prompt for and read a login name */
Denys Vlasenko65963802011-01-24 04:43:04 +0100448 G.line_buf[0] = '\0';
449 while (!G.line_buf[0]) {
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100450 /* Write issue file and prompt */
Denys Vlasenko1f084042011-01-24 00:16:50 +0100451#ifdef ISSUE
452 if (!(option_mask32 & F_NOISSUE))
Denys Vlasenko65963802011-01-24 04:43:04 +0100453 print_login_issue(G.issue, G.tty);
Denys Vlasenko1f084042011-01-24 00:16:50 +0100454#endif
455 print_login_prompt();
Robert Griebl1fca5582002-06-04 20:45:46 +0000456
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100457 /* Read name, watch for break, parity, erase, kill, end-of-line */
Denys Vlasenko65963802011-01-24 04:43:04 +0100458 bp = G.line_buf;
459 G.eol = '\0';
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100460 while (1) {
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100461 /* Do not report trivial EINTR/EIO errors */
Denys Vlasenko765b0ee2010-05-22 21:17:46 +0200462 errno = EINTR; /* make read of 0 bytes be silent too */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000463 if (read(STDIN_FILENO, &c, 1) < 1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000464 if (errno == EINTR || errno == EIO)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000465 exit(EXIT_SUCCESS);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200466 bb_perror_msg_and_die(bb_msg_read_error);
Robert Griebl1fca5582002-06-04 20:45:46 +0000467 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000468
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000469 /* BREAK. If we have speeds to try,
470 * return NULL (will switch speeds and return here) */
Denys Vlasenko65963802011-01-24 04:43:04 +0100471 if (c == '\0' && G.numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000472 return NULL;
473
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100474 /* Do erase, kill and end-of-line processing */
Denys Vlasenko65963802011-01-24 04:43:04 +0100475 switch (c) {
Denys Vlasenko70739642011-01-24 18:17:19 +0100476 case '\r':
477 case '\n':
Denys Vlasenko65963802011-01-24 04:43:04 +0100478 *bp = '\0';
479 G.eol = c;
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100480 goto got_logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000481 case BS:
482 case DEL:
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100483 G.termios.c_cc[VERASE] = c;
Denys Vlasenko65963802011-01-24 04:43:04 +0100484 if (bp > G.line_buf) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100485 full_write(STDOUT_FILENO, "\010 \010", 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000486 bp--;
487 }
488 break;
489 case CTL('U'):
Denys Vlasenko65963802011-01-24 04:43:04 +0100490 while (bp > G.line_buf) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100491 full_write(STDOUT_FILENO, "\010 \010", 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000492 bp--;
493 }
494 break;
495 case CTL('D'):
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000496 exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000497 default:
Denys Vlasenko65963802011-01-24 04:43:04 +0100498 if ((unsigned char)c < ' ') {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000499 /* ignore garbage characters */
Denys Vlasenko65963802011-01-24 04:43:04 +0100500 } else if ((int)(bp - G.line_buf) < sizeof(G.line_buf) - 1) {
501 /* echo and store the character */
502 full_write(STDOUT_FILENO, &c, 1);
503 *bp++ = c;
Robert Griebl1fca5582002-06-04 20:45:46 +0000504 }
505 break;
506 }
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100507 } /* end of get char loop */
508 got_logname: ;
509 } /* while logname is empty */
Robert Griebl1fca5582002-06-04 20:45:46 +0000510
Denys Vlasenko65963802011-01-24 04:43:04 +0100511 return G.line_buf;
Robert Griebl1fca5582002-06-04 20:45:46 +0000512}
513
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000514int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000515int getty_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000516{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000517 int n;
Denys Vlasenko3a416112010-04-05 22:10:38 +0200518 pid_t pid;
Denys Vlasenko65963802011-01-24 04:43:04 +0100519 char *logname;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000520
Denys Vlasenko65963802011-01-24 04:43:04 +0100521 INIT_G();
522 G.login = _PATH_LOGIN; /* default login program */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000523#ifdef ISSUE
Denys Vlasenko65963802011-01-24 04:43:04 +0100524 G.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000525#endif
Denys Vlasenko70739642011-01-24 18:17:19 +0100526 G.eol = '\r';
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000527
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100528 /* Parse command-line arguments */
529 parse_args(argv);
Denis Vlasenko397de612008-03-17 08:55:44 +0000530
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000531 logmode = LOGMODE_NONE;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000532
533 /* Create new session, lose controlling tty, if any */
534 /* docs/ctty.htm says:
535 * "This is allowed only when the current process
536 * is not a process group leader" - is this a problem? */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000537 setsid();
Denis Vlasenko397de612008-03-17 08:55:44 +0000538 /* close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000539 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000540 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
541 xdup2(n, 1);
542 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000543 while (n > 2)
544 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000545
546 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000547 die_sleep = 10;
548 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000549 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000550 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000551 logmode = LOGMODE_BOTH;
552
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000553#ifdef DEBUGGING
Denis Vlasenko5415c852008-07-21 23:05:26 +0000554 dbf = xfopen_for_write(DEBUGTERM);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000555 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000556 debug(argv[n]);
557 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000558 }
559#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000560
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000561 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000562 /* If it's not "-" and not taken yet, it will become our ctty */
563 debug("calling open_tty\n");
Denys Vlasenko65963802011-01-24 04:43:04 +0100564 open_tty();
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000565 ndelay_off(0);
Denis Vlasenko397de612008-03-17 08:55:44 +0000566 debug("duping\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000567 xdup2(0, 1);
568 xdup2(0, 2);
569
570 /*
571 * The following ioctl will fail if stdin is not a tty, but also when
572 * there is noise on the modem control lines. In the latter case, the
573 * common course of action is (1) fix your cables (2) give the modem more
574 * time to properly reset after hanging up. SunOS users can achieve (2)
575 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
576 * 5 seconds seems to be a good value.
577 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100578 if (tcgetattr(STDIN_FILENO, &G.termios) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200579 bb_perror_msg_and_die("tcgetattr");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000580
Denys Vlasenko3a416112010-04-05 22:10:38 +0200581 pid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000582#ifdef __linux__
Denis Vlasenko397de612008-03-17 08:55:44 +0000583// FIXME: do we need this? Otherwise "-" case seems to be broken...
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000584 // /* Forcibly make fd 0 our controlling tty, even if another session
585 // * has it as a ctty. (Another session loses ctty). */
Denys Vlasenkodc6cd122011-01-24 00:28:43 +0100586 // ioctl(STDIN_FILENO, TIOCSCTTY, (void*)1);
Denis Vlasenko397de612008-03-17 08:55:44 +0000587 /* Make ourself a foreground process group within our session */
Denys Vlasenkodc6cd122011-01-24 00:28:43 +0100588 tcsetpgrp(STDIN_FILENO, pid);
Denis Vlasenko397de612008-03-17 08:55:44 +0000589#endif
590
Denis Vlasenko397de612008-03-17 08:55:44 +0000591 /* Update the utmp file. This tty is ours now! */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100592 update_utmp(pid, LOGIN_PROCESS, G.tty, "LOGIN", G.fakehost);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000593
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100594 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o) */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000595 debug("calling termios_init\n");
Denys Vlasenko65963802011-01-24 04:43:04 +0100596 termios_init(G.speeds[0]);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000597
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000598 /* Write the modem init string and DON'T flush the buffers */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100599 if (option_mask32 & F_INITSTRING) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000600 debug("writing init string\n");
Denys Vlasenko65963802011-01-24 04:43:04 +0100601 full_write1_str(G.initstring);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000602 }
603
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000604 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000605 debug("before autobaud\n");
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100606 if (option_mask32 & F_PARSE)
Denys Vlasenko65963802011-01-24 04:43:04 +0100607 auto_baud();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000608
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000609 /* Set the optional timer */
Denys Vlasenko65963802011-01-24 04:43:04 +0100610 alarm(G.timeout); /* if 0, alarm is not set */
Denys Vlasenkod8494932011-01-26 03:26:38 +0100611//BUG: death by signal won't restore termios
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000612
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000613 /* Optionally wait for CR or LF before writing /etc/issue */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100614 if (option_mask32 & F_WAITCRLF) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000615 char ch;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000616 debug("waiting for cr-lf\n");
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000617 while (safe_read(STDIN_FILENO, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000618 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000619 if (ch == '\n' || ch == '\r')
620 break;
621 }
622 }
623
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000624 logname = NULL;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100625 if (!(option_mask32 & F_NOPROMPT)) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100626 /* NB: termios_init already set line speed
Denys Vlasenko65963802011-01-24 04:43:04 +0100627 * to G.speeds[0] */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000628 int baud_index = 0;
629
630 while (1) {
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100631 /* Read the login name */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000632 debug("reading login name\n");
Denys Vlasenko65963802011-01-24 04:43:04 +0100633 logname = get_logname();
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000634 if (logname)
635 break;
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100636 /* We are here only if G.numspeed > 1 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100637 baud_index = (baud_index + 1) % G.numspeed;
638 cfsetspeed(&G.termios, G.speeds[baud_index]);
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100639 set_termios();
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000640 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000641 }
642
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100643 /* Disable timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000644 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000645
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100646 /* Finalize the termios settings */
Denys Vlasenko65963802011-01-24 04:43:04 +0100647 termios_final();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000648
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100649 /* Now the newline character should be properly written */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000650 full_write(STDOUT_FILENO, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000651
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100652 /* Let the login program take care of password validation */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000653 /* We use PATH because we trust that root doesn't set "bad" PATH,
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100654 * and getty is not suid-root applet */
Denis Vlasenko397de612008-03-17 08:55:44 +0000655 /* With -n, logname == NULL, and login will ask for username instead */
Denys Vlasenko65963802011-01-24 04:43:04 +0100656 BB_EXECLP(G.login, G.login, "--", logname, NULL);
657 bb_error_msg_and_die("can't execute '%s'", G.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000658}