blob: bbb5a96b4237731e767041e711bda8eb6d7dceee [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
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020066/* Macro to build Ctrl-LETTER. Assumes ASCII dialect */
67#define CTL(x) ((x) ^ 0100)
Robert Griebl1fca5582002-06-04 20:45:46 +000068
Denis Vlasenko6476cc12006-11-07 01:52:10 +000069/*
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010070 * When multiple baud rates are specified on the command line,
71 * the first one we will try is the first one specified.
Denis Vlasenko6476cc12006-11-07 01:52:10 +000072 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000073#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +000074
Denys Vlasenko65963802011-01-24 04:43:04 +010075struct globals {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020076 unsigned timeout;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000077 const char *login; /* login program */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010078 const char *fakehost;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020079 const char *tty_name;
Denys Vlasenko726ebba2011-03-06 23:09:42 +010080 char *initstring; /* modem init string */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000081 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000082 int numspeed; /* number of baud rates to try */
83 int speeds[MAX_SPEED]; /* baud rates to be tried */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +010084 unsigned char eol; /* end-of-line char seen (CR or NL) */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020085 struct termios tty_attrs;
Denys Vlasenko65963802011-01-24 04:43:04 +010086 char line_buf[128];
Robert Griebl1fca5582002-06-04 20:45:46 +000087};
88
Denys Vlasenko65963802011-01-24 04:43:04 +010089#define G (*ptr_to_globals)
90#define INIT_G() do { \
91 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
92} while (0)
Robert Griebl1fca5582002-06-04 20:45:46 +000093
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010094//usage:#define getty_trivial_usage
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +010095//usage: "[OPTIONS] BAUD_RATE[,BAUD_RATE]... TTY [TERMTYPE]"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010096//usage:#define getty_full_usage "\n\n"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020097//usage: "Open TTY, prompt for login name, then invoke /bin/login\n"
Denys Vlasenkod8494932011-01-26 03:26:38 +010098//usage: "\n -h Enable hardware RTS/CTS flow control"
99//usage: "\n -L Set CLOCAL (ignore Carrier Detect state)"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100100//usage: "\n -m Get baud rate from modem's CONNECT status message"
Denys Vlasenkod8494932011-01-26 03:26:38 +0100101//usage: "\n -n Don't prompt for login name"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100102//usage: "\n -w Wait for CR or LF before sending /etc/issue"
Denys Vlasenkod8494932011-01-26 03:26:38 +0100103//usage: "\n -i Don't display /etc/issue"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100104//usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue"
105//usage: "\n -l LOGIN Invoke LOGIN instead of /bin/login"
Denys Vlasenkod8494932011-01-26 03:26:38 +0100106//usage: "\n -t SEC Terminate after SEC if no login name is read"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100107//usage: "\n -I INITSTR Send INITSTR before anything else"
108//usage: "\n -H HOST Log HOST into the utmp file as the hostname"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100109//usage: "\n"
110//usage: "\nBAUD_RATE of 0 leaves it unchanged"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100111
112static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
113#define F_INITSTRING (1 << 0) /* -I */
114#define F_LOCAL (1 << 1) /* -L */
115#define F_FAKEHOST (1 << 2) /* -H */
116#define F_CUSTISSUE (1 << 3) /* -f */
117#define F_RTSCTS (1 << 4) /* -h */
118#define F_NOISSUE (1 << 5) /* -i */
119#define F_LOGIN (1 << 6) /* -l */
120#define F_PARSE (1 << 7) /* -m */
121#define F_TIMEOUT (1 << 8) /* -t */
122#define F_WAITCRLF (1 << 9) /* -w */
123#define F_NOPROMPT (1 << 10) /* -n */
Robert Griebl1fca5582002-06-04 20:45:46 +0000124
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000125
Denys Vlasenko1f084042011-01-24 00:16:50 +0100126/* convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000127static int bcode(const char *s)
128{
Denis Vlasenko397de612008-03-17 08:55:44 +0000129 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
130 if (value < 0) /* bad terminating char, overflow, etc */
131 return value;
132 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000133}
134
Denys Vlasenko1f084042011-01-24 00:16:50 +0100135/* parse alternate baud rates */
Denys Vlasenko65963802011-01-24 04:43:04 +0100136static void parse_speeds(char *arg)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000137{
138 char *cp;
139
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000140 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000141 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000142 while ((cp = strsep(&arg, ",")) != NULL) {
Denys Vlasenko65963802011-01-24 04:43:04 +0100143 G.speeds[G.numspeed] = bcode(cp);
144 if (G.speeds[G.numspeed] < 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000145 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000146 /* note: arg "0" turns into speed B0 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100147 G.numspeed++;
148 if (G.numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000149 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000150 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000151 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000152}
153
Denys Vlasenko1f084042011-01-24 00:16:50 +0100154/* parse command-line arguments */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100155static void parse_args(char **argv)
Robert Griebl1fca5582002-06-04 20:45:46 +0000156{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000157 char *ts;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100158 int flags;
Robert Griebl1fca5582002-06-04 20:45:46 +0000159
Denis Vlasenko397de612008-03-17 08:55:44 +0000160 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100161 flags = getopt32(argv, opt_string,
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100162 &G.initstring, &G.fakehost, &G.issue,
Denys Vlasenko65963802011-01-24 04:43:04 +0100163 &G.login, &G.timeout
164 );
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100165 if (flags & F_INITSTRING) {
Denys Vlasenko65963802011-01-24 04:43:04 +0100166 G.initstring = xstrdup(G.initstring);
Denys Vlasenko53600592010-10-23 21:06:06 +0200167 /* decode \ddd octal codes into chars */
Denys Vlasenko726ebba2011-03-06 23:09:42 +0100168 strcpy_and_process_escape_sequences(G.initstring, G.initstring);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000169 }
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100170 argv += optind;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000171 debug("after getopt\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000172
Denys Vlasenko65963802011-01-24 04:43:04 +0100173 /* We loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200174 G.tty_name = argv[0];
175 ts = argv[1]; /* baud rate(s) */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000176 if (isdigit(argv[0][0])) {
Denys Vlasenko65963802011-01-24 04:43:04 +0100177 /* A number first, assume it's a speed (BSD style) */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200178 G.tty_name = ts; /* tty name is in argv[1] */
179 ts = argv[0]; /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000180 }
Denys Vlasenko65963802011-01-24 04:43:04 +0100181 parse_speeds(ts);
Denis Vlasenko397de612008-03-17 08:55:44 +0000182
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000183 if (argv[2])
Denis Vlasenko397de612008-03-17 08:55:44 +0000184 xsetenv("TERM", argv[2]);
Robert Griebl1fca5582002-06-04 20:45:46 +0000185
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000186 debug("exiting parse_args\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000187}
188
Denys Vlasenko1f084042011-01-24 00:16:50 +0100189/* set up tty as standard input, output, error */
Denys Vlasenko65963802011-01-24 04:43:04 +0100190static void open_tty(void)
Robert Griebl1fca5582002-06-04 20:45:46 +0000191{
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100192 /* Set up new standard input, unless we are given an already opened port */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200193 if (NOT_LONE_DASH(G.tty_name)) {
194 if (G.tty_name[0] != '/')
195 G.tty_name = xasprintf("/dev/%s", G.tty_name); /* will leak it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000196
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100197 /* Open the tty as standard input */
Robert Griebl1fca5582002-06-04 20:45:46 +0000198 debug("open(2)\n");
Denis Vlasenko397de612008-03-17 08:55:44 +0000199 close(0);
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200200 xopen(G.tty_name, O_RDWR | O_NONBLOCK); /* uses fd 0 */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000201
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100202 /* Set proper protections and ownership */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000203 fchown(0, 0, 0); /* 0:0 */
Denis Vlasenko397de612008-03-17 08:55:44 +0000204 fchmod(0, 0620); /* crw--w---- */
Robert Griebl1fca5582002-06-04 20:45:46 +0000205 } else {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200206 char *n;
Robert Griebl1fca5582002-06-04 20:45:46 +0000207 /*
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200208 * Standard input should already be connected to an open port.
209 * Make sure it is open for read/write.
Robert Griebl1fca5582002-06-04 20:45:46 +0000210 */
Denys Vlasenko006416e2011-01-22 17:29:53 +0100211 if ((fcntl(0, F_GETFL) & (O_RDWR|O_RDONLY|O_WRONLY)) != O_RDWR)
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000212 bb_error_msg_and_die("stdin is not open for read/write");
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200213
214 /* Try to get real tty name instead of "-" */
215 n = xmalloc_ttyname(0);
216 if (n)
217 G.tty_name = n;
Robert Griebl1fca5582002-06-04 20:45:46 +0000218 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200219 applet_name = xasprintf("getty: %s", skip_dev_pfx(G.tty_name));
Robert Griebl1fca5582002-06-04 20:45:46 +0000220}
221
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200222static void set_tty_attrs(void)
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100223{
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200224 if (tcsetattr_stdin_TCSANOW(&G.tty_attrs) < 0)
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100225 bb_perror_msg_and_die("tcsetattr");
226}
227
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200228/* We manipulate tty_attrs this way:
229 * - first, we read existing tty_attrs
230 * - init_tty_attrs modifies some parts and sets it
231 * - auto_baud and/or BREAK processing can set different speed and set tty attrs
232 * - finalize_tty_attrs again modifies some parts and sets tty attrs before
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100233 * execing login
234 */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200235static void init_tty_attrs(int speed)
Robert Griebl1fca5582002-06-04 20:45:46 +0000236{
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100237 /* Try to drain output buffer, with 5 sec timeout.
238 * Added on request from users of ~600 baud serial interface
239 * with biggish buffer on a 90MHz CPU.
240 * They were losing hundreds of bytes of buffered output
241 * on tcflush.
Denys Vlasenkodc6cd122011-01-24 00:28:43 +0100242 */
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100243 signal_no_SA_RESTART_empty_mask(SIGALRM, record_signo);
244 alarm(5);
245 tcdrain(STDIN_FILENO);
246 alarm(0);
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100247
248 /* Flush input and output queues, important for modems! */
Denys Vlasenkodc6cd122011-01-24 00:28:43 +0100249 tcflush(STDIN_FILENO, TCIOFLUSH);
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100250
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100251 /* Set speed if it wasn't specified as "0" on command line */
Denys Vlasenko1f084042011-01-24 00:16:50 +0100252 if (speed != B0)
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200253 cfsetspeed(&G.tty_attrs, speed);
Denys Vlasenko1f084042011-01-24 00:16:50 +0100254
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200255 /* Initial settings: 8-bit characters, raw mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000256 * Special characters are set after we have read the login name; all
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100257 * reads will be done in raw mode anyway.
Robert Griebl1fca5582002-06-04 20:45:46 +0000258 */
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100259 /* Clear all bits except: */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200260 G.tty_attrs.c_cflag &= (0
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100261 /* 2 stop bits (1 otherwise)
262 * Enable parity bit (both on input and output)
263 * Odd parity (else even)
264 */
265 | CSTOPB | PARENB | PARODD
Denys Vlasenko4907f6e2011-01-26 16:20:54 +0100266#ifdef CMSPAR
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100267 | CMSPAR /* mark or space parity */
268#endif
Denys Vlasenko8a90e612012-02-04 19:55:27 +0100269#ifdef CBAUD
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100270 | CBAUD /* (output) baud rate */
Denys Vlasenko8a90e612012-02-04 19:55:27 +0100271#endif
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100272#ifdef CBAUDEX
273 | CBAUDEX /* (output) baud rate */
274#endif
275#ifdef CIBAUD
276 | CIBAUD /* input baud rate */
277#endif
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100278 );
279 /* Set: 8 bits; hang up (drop DTR) on last close; enable receive */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200280 G.tty_attrs.c_cflag |= CS8 | HUPCL | CREAD;
Denys Vlasenkod8494932011-01-26 03:26:38 +0100281 if (option_mask32 & F_LOCAL) {
282 /* ignore Carrier Detect pin:
283 * opens don't block when CD is low,
284 * losing CD doesn't hang up processes whose ctty is this tty
285 */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200286 G.tty_attrs.c_cflag |= CLOCAL;
Denys Vlasenkod8494932011-01-26 03:26:38 +0100287 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000288#ifdef CRTSCTS
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100289 if (option_mask32 & F_RTSCTS)
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200290 G.tty_attrs.c_cflag |= CRTSCTS; /* flow control using RTS/CTS pins */
Denys Vlasenkod8494932011-01-26 03:26:38 +0100291#endif
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200292 G.tty_attrs.c_iflag = 0;
293 G.tty_attrs.c_lflag = 0;
Denys Vlasenkod8494932011-01-26 03:26:38 +0100294 /* non-raw output; add CR to each NL */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200295 G.tty_attrs.c_oflag = OPOST | ONLCR;
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100296
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200297 /* reads would block only if < 1 char is available */
298 G.tty_attrs.c_cc[VMIN] = 1;
299 /* no timeout (reads block forever) */
300 G.tty_attrs.c_cc[VTIME] = 0;
Denys Vlasenkod8494932011-01-26 03:26:38 +0100301#ifdef __linux__
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200302 G.tty_attrs.c_line = 0;
Robert Griebl1fca5582002-06-04 20:45:46 +0000303#endif
304
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200305 set_tty_attrs();
Robert Griebl1fca5582002-06-04 20:45:46 +0000306
Robert Griebl1fca5582002-06-04 20:45:46 +0000307 debug("term_io 2\n");
308}
309
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200310static void finalize_tty_attrs(void)
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100311{
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100312 /* software flow control on output (stop sending if XOFF is recvd);
313 * and on input (send XOFF when buffer is full)
314 */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200315 G.tty_attrs.c_iflag |= IXON | IXOFF;
Denys Vlasenkod8494932011-01-26 03:26:38 +0100316 if (G.eol == '\r') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200317 G.tty_attrs.c_iflag |= ICRNL; /* map CR on input to NL */
Denys Vlasenkod8494932011-01-26 03:26:38 +0100318 }
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100319 /* Other bits in c_iflag:
320 * IXANY Any recvd char enables output (any char is also a XON)
321 * INPCK Enable parity check
322 * IGNPAR Ignore parity errors (drop bad bytes)
323 * PARMRK Mark parity errors with 0xff, 0x00 prefix
324 * (else bad byte is received as 0x00)
325 * ISTRIP Strip parity bit
326 * IGNBRK Ignore break condition
327 * BRKINT Send SIGINT on break - maybe set this?
328 * INLCR Map NL to CR
329 * IGNCR Ignore CR
330 * ICRNL Map CR to NL
331 * IUCLC Map uppercase to lowercase
332 * IMAXBEL Echo BEL on input line too long
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100333 * IUTF8 Appears to affect tty's idea of char widths,
334 * observed to improve backspacing through Unicode chars
Denys Vlasenkob7c9fb22011-02-03 00:05:48 +0100335 */
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100336
337 /* line buffered input (NL or EOL or EOF chars end a line);
338 * recognize INT/QUIT/SUSP chars;
339 * echo input chars;
340 * echo BS-SP-BS on erase character;
341 * echo kill char specially, not as ^c (ECHOKE controls how exactly);
342 * erase all input via BS-SP-BS on kill char (else go to next line)
343 */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200344 G.tty_attrs.c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100345 /* Other bits in c_lflag:
346 * XCASE Map uppercase to \lowercase [tried, doesn't work]
347 * ECHONL Echo NL even if ECHO is not set
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100348 * ECHOCTL Echo ctrl chars as ^c (else don't echo) - maybe set this?
349 * ECHOPRT On erase, echo erased chars
350 * [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen]
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100351 * NOFLSH Don't flush input buffer after interrupt or quit chars
352 * IEXTEN Enable extended functions (??)
353 * [glibc says it enables c_cc[LNEXT] "enter literal char"
354 * and c_cc[VDISCARD] "toggle discard buffered output" chars]
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100355 * FLUSHO Output being flushed (c_cc[VDISCARD] is in effect)
356 * PENDIN Retype pending input at next read or input char
Peter Korsgaardddd1ec12011-01-26 15:15:19 +0100357 * (c_cc[VREPRINT] is being processed)
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100358 * TOSTOP Send SIGTTOU for background output
359 * (why "stty sane" unsets this bit?)
360 */
Denys Vlasenkod8494932011-01-26 03:26:38 +0100361
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200362 G.tty_attrs.c_cc[VINTR] = CTL('C');
363 G.tty_attrs.c_cc[VQUIT] = CTL('\\');
364 G.tty_attrs.c_cc[VEOF] = CTL('D');
365 G.tty_attrs.c_cc[VEOL] = '\n';
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100366#ifdef VSWTC
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200367 G.tty_attrs.c_cc[VSWTC] = 0;
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100368#endif
Denys Vlasenko70739642011-01-24 18:17:19 +0100369#ifdef VSWTCH
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200370 G.tty_attrs.c_cc[VSWTCH] = 0;
Denys Vlasenko70739642011-01-24 18:17:19 +0100371#endif
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200372 G.tty_attrs.c_cc[VKILL] = CTL('U');
Denys Vlasenko3ff9be32011-01-26 11:28:43 +0100373 /* Other control chars:
374 * VEOL2
375 * VERASE, VWERASE - (word) erase. we may set VERASE in get_logname
376 * VREPRINT - reprint current input buffer
377 * VLNEXT, VDISCARD, VSTATUS
378 * VSUSP, VDSUSP - send (delayed) SIGTSTP
379 * VSTART, VSTOP - chars used for IXON/IXOFF
380 */
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100381
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200382 set_tty_attrs();
383
384 /* Now the newline character should be properly written */
385 full_write(STDOUT_FILENO, "\n", 1);
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100386}
387
Denys Vlasenko1f084042011-01-24 00:16:50 +0100388/* extract baud rate from modem status message */
Denys Vlasenko65963802011-01-24 04:43:04 +0100389static void auto_baud(void)
Robert Griebl1fca5582002-06-04 20:45:46 +0000390{
Robert Griebl1fca5582002-06-04 20:45:46 +0000391 int nread;
392
393 /*
394 * This works only if the modem produces its status code AFTER raising
395 * the DCD line, and if the computer is fast enough to set the proper
396 * baud rate before the message has gone by. We expect a message of the
397 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000398 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000399 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000400 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000401 * The number is interpreted as the baud rate of the incoming call. If the
402 * modem does not tell us the baud rate within one second, we will keep
403 * using the current baud rate. It is advisable to enable BREAK
404 * processing (comma-separated list of baud rates) if the processing of
405 * modem status messages is enabled.
406 */
407
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200408 G.tty_attrs.c_cc[VMIN] = 0; /* don't block reads (min read is 0 chars) */
409 set_tty_attrs();
Robert Griebl1fca5582002-06-04 20:45:46 +0000410
411 /*
412 * Wait for a while, then read everything the modem has said so far and
413 * try to extract the speed of the dial-in call.
414 */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000415 sleep(1);
Denys Vlasenko65963802011-01-24 04:43:04 +0100416 nread = safe_read(STDIN_FILENO, G.line_buf, sizeof(G.line_buf) - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000417 if (nread > 0) {
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100418 int speed;
419 char *bp;
Denys Vlasenko65963802011-01-24 04:43:04 +0100420 G.line_buf[nread] = '\0';
421 for (bp = G.line_buf; bp < G.line_buf + nread; bp++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000422 if (isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000423 speed = bcode(bp);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200424 if (speed > 0)
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200425 cfsetspeed(&G.tty_attrs, speed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000426 break;
427 }
428 }
429 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000430
Denys Vlasenkocf9d33a2011-01-26 15:56:51 +0100431 /* Restore terminal settings */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200432 G.tty_attrs.c_cc[VMIN] = 1; /* restore to value set by init_tty_attrs */
433 set_tty_attrs();
Robert Griebl1fca5582002-06-04 20:45:46 +0000434}
435
Denys Vlasenko1f084042011-01-24 00:16:50 +0100436/* get user name, establish parity, speed, erase, kill, eol;
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100437 * return NULL on BREAK, logname on success
438 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100439static char *get_logname(void)
Robert Griebl1fca5582002-06-04 20:45:46 +0000440{
Robert Griebl1fca5582002-06-04 20:45:46 +0000441 char *bp;
Denys Vlasenko65963802011-01-24 04:43:04 +0100442 char c;
Robert Griebl1fca5582002-06-04 20:45:46 +0000443
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100444 /* Flush pending input (esp. after parsing or switching the baud rate) */
Denys Vlasenkodc6cd122011-01-24 00:28:43 +0100445 usleep(100*1000); /* 0.1 sec */
Denys Vlasenko13609182011-01-24 23:45:22 +0100446 tcflush(STDIN_FILENO, TCIFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000447
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100448 /* Prompt for and read a login name */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200449 do {
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))
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200453 print_login_issue(G.issue, G.tty_name);
Denys Vlasenko1f084042011-01-24 00:16:50 +0100454#endif
455 print_login_prompt();
Robert Griebl1fca5582002-06-04 20:45:46 +0000456
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200457 /* Read name, watch for break, erase, kill, end-of-line */
Denys Vlasenko65963802011-01-24 04:43:04 +0100458 bp = G.line_buf;
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100459 while (1) {
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100460 /* Do not report trivial EINTR/EIO errors */
Denys Vlasenko765b0ee2010-05-22 21:17:46 +0200461 errno = EINTR; /* make read of 0 bytes be silent too */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000462 if (read(STDIN_FILENO, &c, 1) < 1) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200463 finalize_tty_attrs();
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);
Tanguy Pruvotf7ae0a22011-07-04 05:30:48 +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
Denys Vlasenko65963802011-01-24 04:43:04 +0100469 switch (c) {
Denys Vlasenko70739642011-01-24 18:17:19 +0100470 case '\r':
471 case '\n':
Denys Vlasenko65963802011-01-24 04:43:04 +0100472 *bp = '\0';
473 G.eol = c;
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100474 goto got_logname;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200475 case CTL('H'):
476 case 0x7f:
477 G.tty_attrs.c_cc[VERASE] = c;
Denys Vlasenko65963802011-01-24 04:43:04 +0100478 if (bp > G.line_buf) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100479 full_write(STDOUT_FILENO, "\010 \010", 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000480 bp--;
481 }
482 break;
483 case CTL('U'):
Denys Vlasenko65963802011-01-24 04:43:04 +0100484 while (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;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200489 case CTL('C'):
Robert Griebl1fca5582002-06-04 20:45:46 +0000490 case CTL('D'):
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200491 finalize_tty_attrs();
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000492 exit(EXIT_SUCCESS);
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200493 case '\0':
494 /* BREAK. If we have speeds to try,
495 * return NULL (will switch speeds and return here) */
496 if (G.numspeed > 1)
497 return NULL;
498 /* fall through and ignore it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000499 default:
Denys Vlasenko65963802011-01-24 04:43:04 +0100500 if ((unsigned char)c < ' ') {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000501 /* ignore garbage characters */
Denys Vlasenko65963802011-01-24 04:43:04 +0100502 } else if ((int)(bp - G.line_buf) < sizeof(G.line_buf) - 1) {
503 /* echo and store the character */
504 full_write(STDOUT_FILENO, &c, 1);
505 *bp++ = c;
Robert Griebl1fca5582002-06-04 20:45:46 +0000506 }
507 break;
508 }
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100509 } /* end of get char loop */
510 got_logname: ;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200511 } while (G.line_buf[0] == '\0'); /* while logname is empty */
Robert Griebl1fca5582002-06-04 20:45:46 +0000512
Denys Vlasenko65963802011-01-24 04:43:04 +0100513 return G.line_buf;
Robert Griebl1fca5582002-06-04 20:45:46 +0000514}
515
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200516static void alarm_handler(int sig UNUSED_PARAM)
517{
518 finalize_tty_attrs();
519 _exit(EXIT_SUCCESS);
520}
521
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000522int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000523int getty_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000524{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000525 int n;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200526 pid_t pid, tsid;
Denys Vlasenko65963802011-01-24 04:43:04 +0100527 char *logname;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000528
Denys Vlasenko65963802011-01-24 04:43:04 +0100529 INIT_G();
530 G.login = _PATH_LOGIN; /* default login program */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000531#ifdef ISSUE
Denys Vlasenko65963802011-01-24 04:43:04 +0100532 G.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000533#endif
Denys Vlasenko70739642011-01-24 18:17:19 +0100534 G.eol = '\r';
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000535
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100536 /* Parse command-line arguments */
537 parse_args(argv);
Denis Vlasenko397de612008-03-17 08:55:44 +0000538
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200539 /* Create new session and pgrp, lose controlling tty */
540 pid = setsid(); /* this also gives us our pid :) */
541 if (pid < 0) {
542 int fd;
543 /* :(
544 * docs/ctty.htm says:
545 * "This is allowed only when the current process
546 * is not a process group leader".
547 * Thus, setsid() will fail if we _already_ are
548 * a session leader - which is quite possible for getty!
549 */
550 pid = getpid();
551 if (getsid(0) != pid)
552 bb_perror_msg_and_die("setsid");
553 /* Looks like we are already a session leader.
554 * In this case (setsid failed) we may still have ctty,
555 * and it may be different from tty we need to control!
556 * If we still have ctty, on Linux ioctl(TIOCSCTTY)
557 * (which we are going to use a bit later) always fails -
558 * even if we try to take ctty which is already ours!
559 * Try to drop old ctty now to prevent that.
560 * Use O_NONBLOCK: old ctty may be a serial line.
561 */
562 fd = open("/dev/tty", O_RDWR | O_NONBLOCK);
563 if (fd >= 0) {
Denys Vlasenkoc1b7d1d2012-05-28 02:48:55 +0200564 /* TIOCNOTTY sends SIGHUP to the foreground
565 * process group - which may include us!
566 * Make sure to not die on it:
567 */
568 sighandler_t old = signal(SIGHUP, SIG_IGN);
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200569 ioctl(fd, TIOCNOTTY);
570 close(fd);
Denys Vlasenkoc1b7d1d2012-05-28 02:48:55 +0200571 signal(SIGHUP, old);
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200572 }
573 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000574
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200575 /* Close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000576 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000577 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
578 xdup2(n, 1);
579 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000580 while (n > 2)
581 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000582
583 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000584 die_sleep = 10;
585 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000586 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000587 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000588 logmode = LOGMODE_BOTH;
589
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000590#ifdef DEBUGGING
Denis Vlasenko5415c852008-07-21 23:05:26 +0000591 dbf = xfopen_for_write(DEBUGTERM);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000592 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000593 debug(argv[n]);
594 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000595 }
596#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000597
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000598 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000599 debug("calling open_tty\n");
Denys Vlasenko65963802011-01-24 04:43:04 +0100600 open_tty();
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200601 ndelay_off(STDIN_FILENO);
Denis Vlasenko397de612008-03-17 08:55:44 +0000602 debug("duping\n");
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200603 xdup2(STDIN_FILENO, 1);
604 xdup2(STDIN_FILENO, 2);
605
606 /* Steal ctty if we don't have it yet */
607 tsid = tcgetsid(STDIN_FILENO);
608 if (tsid < 0 || pid != tsid) {
609 if (ioctl(STDIN_FILENO, TIOCSCTTY, /*force:*/ (long)1) < 0)
610 bb_perror_msg_and_die("TIOCSCTTY");
611 }
612
613#ifdef __linux__
614 /* Make ourself a foreground process group within our session */
615 if (tcsetpgrp(STDIN_FILENO, pid) < 0)
616 bb_perror_msg_and_die("tcsetpgrp");
617#endif
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000618
619 /*
620 * The following ioctl will fail if stdin is not a tty, but also when
621 * there is noise on the modem control lines. In the latter case, the
622 * common course of action is (1) fix your cables (2) give the modem more
623 * time to properly reset after hanging up. SunOS users can achieve (2)
624 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
625 * 5 seconds seems to be a good value.
626 */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200627 if (tcgetattr(STDIN_FILENO, &G.tty_attrs) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200628 bb_perror_msg_and_die("tcgetattr");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000629
Denis Vlasenko397de612008-03-17 08:55:44 +0000630 /* Update the utmp file. This tty is ours now! */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200631 update_utmp(pid, LOGIN_PROCESS, G.tty_name, "LOGIN", G.fakehost);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000632
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200633 /* Initialize tty attrs (raw mode, eight-bit, blocking i/o) */
634 debug("calling init_tty_attrs\n");
635 init_tty_attrs(G.speeds[0]);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000636
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000637 /* Write the modem init string and DON'T flush the buffers */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100638 if (option_mask32 & F_INITSTRING) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000639 debug("writing init string\n");
Denys Vlasenko65963802011-01-24 04:43:04 +0100640 full_write1_str(G.initstring);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000641 }
642
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000643 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000644 debug("before autobaud\n");
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100645 if (option_mask32 & F_PARSE)
Denys Vlasenko65963802011-01-24 04:43:04 +0100646 auto_baud();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000647
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000648 /* Set the optional timer */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200649 signal(SIGALRM, alarm_handler);
Denys Vlasenko65963802011-01-24 04:43:04 +0100650 alarm(G.timeout); /* if 0, alarm is not set */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000651
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000652 /* Optionally wait for CR or LF before writing /etc/issue */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100653 if (option_mask32 & F_WAITCRLF) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000654 char ch;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000655 debug("waiting for cr-lf\n");
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000656 while (safe_read(STDIN_FILENO, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000657 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000658 if (ch == '\n' || ch == '\r')
659 break;
660 }
661 }
662
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000663 logname = NULL;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100664 if (!(option_mask32 & F_NOPROMPT)) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200665 /* NB: init_tty_attrs already set line speed
Denys Vlasenko65963802011-01-24 04:43:04 +0100666 * to G.speeds[0] */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000667 int baud_index = 0;
668
669 while (1) {
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100670 /* Read the login name */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000671 debug("reading login name\n");
Denys Vlasenko65963802011-01-24 04:43:04 +0100672 logname = get_logname();
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000673 if (logname)
674 break;
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100675 /* We are here only if G.numspeed > 1 */
Denys Vlasenko65963802011-01-24 04:43:04 +0100676 baud_index = (baud_index + 1) % G.numspeed;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200677 cfsetspeed(&G.tty_attrs, G.speeds[baud_index]);
678 set_tty_attrs();
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000679 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000680 }
681
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100682 /* Disable timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000683 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000684
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200685 finalize_tty_attrs();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000686
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100687 /* Let the login program take care of password validation */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000688 /* We use PATH because we trust that root doesn't set "bad" PATH,
Denys Vlasenkofaaf8cb2011-01-24 14:35:09 +0100689 * and getty is not suid-root applet */
Denis Vlasenko397de612008-03-17 08:55:44 +0000690 /* With -n, logname == NULL, and login will ask for username instead */
Denys Vlasenko65963802011-01-24 04:43:04 +0100691 BB_EXECLP(G.login, G.login, "--", logname, NULL);
692 bb_error_msg_and_die("can't execute '%s'", G.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000693}