Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Based on agetty - another getty program for Linux. By W. Z. Venema 1989 |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 4 | * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk> |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 5 | * This program is freely distributable. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 6 | * |
| 7 | * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95 |
| 8 | * |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 9 | * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org> |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 10 | * - Added Native Language Support |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 11 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 12 | * 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net> |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 13 | * - 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" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 21 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 22 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 23 | */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Denis Vlasenko | f702652 | 2006-09-19 13:50:55 +0000 | [diff] [blame] | 26 | #include <syslog.h> |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 27 | #ifndef IUCLC |
| 28 | # define IUCLC 0 |
| 29 | #endif |
| 30 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 31 | #ifndef LOGIN_PROCESS |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 32 | # undef ENABLE_FEATURE_UTMP |
| 33 | # undef ENABLE_FEATURE_WTMP |
| 34 | # define ENABLE_FEATURE_UTMP 0 |
| 35 | # define ENABLE_FEATURE_WTMP 0 |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 36 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 37 | |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 38 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 39 | /* The following is used for understandable diagnostics */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 40 | #ifdef DEBUGGING |
| 41 | static 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 Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 49 | /* |
| 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 Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 56 | #undef _PATH_LOGIN |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 57 | #ifdef __BIONIC__ |
| 58 | #define cfsetspeed(t,s) cfsetispeed(t,s) |
| 59 | #define _PATH_LOGIN "/system/xbin/login" |
| 60 | #else |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 61 | #define _PATH_LOGIN "/bin/login" |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 62 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 63 | |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 64 | /* Displayed before the login prompt. |
| 65 | * If ISSUE is not defined, getty will never display the contents of the |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 66 | * /etc/issue file. You will not want to spit out large "issue" files at the |
| 67 | * wrong baud rate. |
| 68 | */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 69 | #define ISSUE "/etc/issue" |
Eric Andersen | 77804ce | 2005-07-27 06:05:38 +0000 | [diff] [blame] | 70 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 71 | /* Macro to build Ctrl-LETTER. Assumes ASCII dialect */ |
| 72 | #define CTL(x) ((x) ^ 0100) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 74 | /* |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 75 | * When multiple baud rates are specified on the command line, |
| 76 | * the first one we will try is the first one specified. |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 77 | */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 78 | #define MAX_SPEED 10 /* max. nr. of baud rates */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 79 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 80 | struct globals { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 81 | unsigned timeout; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 82 | const char *login; /* login program */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 83 | const char *fakehost; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 84 | const char *tty_name; |
Denys Vlasenko | 726ebba | 2011-03-06 23:09:42 +0100 | [diff] [blame] | 85 | char *initstring; /* modem init string */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 86 | const char *issue; /* alternative issue file */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 87 | int numspeed; /* number of baud rates to try */ |
| 88 | int speeds[MAX_SPEED]; /* baud rates to be tried */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 89 | unsigned char eol; /* end-of-line char seen (CR or NL) */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 90 | struct termios tty_attrs; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 91 | char line_buf[128]; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 94 | #define G (*ptr_to_globals) |
| 95 | #define INIT_G() do { \ |
| 96 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 97 | } while (0) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 98 | |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 99 | //usage:#define getty_trivial_usage |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 100 | //usage: "[OPTIONS] BAUD_RATE[,BAUD_RATE]... TTY [TERMTYPE]" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 101 | //usage:#define getty_full_usage "\n\n" |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 102 | //usage: "Open TTY, prompt for login name, then invoke /system/xbin/login\n" |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 103 | //usage: "\n -h Enable hardware RTS/CTS flow control" |
| 104 | //usage: "\n -L Set CLOCAL (ignore Carrier Detect state)" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 105 | //usage: "\n -m Get baud rate from modem's CONNECT status message" |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 106 | //usage: "\n -n Don't prompt for login name" |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 107 | //usage: "\n -w Wait for CR or LF before sending /etc/issue" |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 108 | //usage: "\n -i Don't display /etc/issue" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 109 | //usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue" |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 110 | //usage: "\n -l LOGIN Invoke LOGIN instead of /system/xbin/login" |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 111 | //usage: "\n -t SEC Terminate after SEC if no login name is read" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 112 | //usage: "\n -I INITSTR Send INITSTR before anything else" |
| 113 | //usage: "\n -H HOST Log HOST into the utmp file as the hostname" |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 114 | //usage: "\n" |
| 115 | //usage: "\nBAUD_RATE of 0 leaves it unchanged" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 116 | |
| 117 | static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn"; |
| 118 | #define F_INITSTRING (1 << 0) /* -I */ |
| 119 | #define F_LOCAL (1 << 1) /* -L */ |
| 120 | #define F_FAKEHOST (1 << 2) /* -H */ |
| 121 | #define F_CUSTISSUE (1 << 3) /* -f */ |
| 122 | #define F_RTSCTS (1 << 4) /* -h */ |
| 123 | #define F_NOISSUE (1 << 5) /* -i */ |
| 124 | #define F_LOGIN (1 << 6) /* -l */ |
| 125 | #define F_PARSE (1 << 7) /* -m */ |
| 126 | #define F_TIMEOUT (1 << 8) /* -t */ |
| 127 | #define F_WAITCRLF (1 << 9) /* -w */ |
| 128 | #define F_NOPROMPT (1 << 10) /* -n */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 129 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 130 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 131 | /* convert speed string to speed code; return <= 0 on failure */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 132 | static int bcode(const char *s) |
| 133 | { |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 134 | int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */ |
| 135 | if (value < 0) /* bad terminating char, overflow, etc */ |
| 136 | return value; |
| 137 | return tty_value_to_baud(value); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 140 | /* parse alternate baud rates */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 141 | static void parse_speeds(char *arg) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 142 | { |
| 143 | char *cp; |
| 144 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 145 | /* NB: at least one iteration is always done */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 146 | debug("entered parse_speeds\n"); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 147 | while ((cp = strsep(&arg, ",")) != NULL) { |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 148 | G.speeds[G.numspeed] = bcode(cp); |
| 149 | if (G.speeds[G.numspeed] < 0) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 150 | bb_error_msg_and_die("bad speed: %s", cp); |
Denis Vlasenko | 32a385f | 2009-04-12 13:05:40 +0000 | [diff] [blame] | 151 | /* note: arg "0" turns into speed B0 */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 152 | G.numspeed++; |
| 153 | if (G.numspeed > MAX_SPEED) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 154 | bb_error_msg_and_die("too many alternate speeds"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 155 | } |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 156 | debug("exiting parse_speeds\n"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 159 | /* parse command-line arguments */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 160 | static void parse_args(char **argv) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 161 | { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 162 | char *ts; |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 163 | int flags; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 165 | opt_complementary = "-2:t+"; /* at least 2 args; -t N */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 166 | flags = getopt32(argv, opt_string, |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 167 | &G.initstring, &G.fakehost, &G.issue, |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 168 | &G.login, &G.timeout |
| 169 | ); |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 170 | if (flags & F_INITSTRING) { |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 171 | G.initstring = xstrdup(G.initstring); |
Denys Vlasenko | 5360059 | 2010-10-23 21:06:06 +0200 | [diff] [blame] | 172 | /* decode \ddd octal codes into chars */ |
Denys Vlasenko | 726ebba | 2011-03-06 23:09:42 +0100 | [diff] [blame] | 173 | strcpy_and_process_escape_sequences(G.initstring, G.initstring); |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 174 | } |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 175 | argv += optind; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 176 | debug("after getopt\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 177 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 178 | /* We loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 179 | G.tty_name = argv[0]; |
| 180 | ts = argv[1]; /* baud rate(s) */ |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 181 | if (isdigit(argv[0][0])) { |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 182 | /* A number first, assume it's a speed (BSD style) */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 183 | G.tty_name = ts; /* tty name is in argv[1] */ |
| 184 | ts = argv[0]; /* baud rate(s) */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 185 | } |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 186 | parse_speeds(ts); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 187 | |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 188 | if (argv[2]) |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 189 | xsetenv("TERM", argv[2]); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 191 | debug("exiting parse_args\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 194 | /* set up tty as standard input, output, error */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 195 | static void open_tty(void) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 196 | { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 197 | /* Set up new standard input, unless we are given an already opened port */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 198 | if (NOT_LONE_DASH(G.tty_name)) { |
| 199 | if (G.tty_name[0] != '/') |
| 200 | G.tty_name = xasprintf("/dev/%s", G.tty_name); /* will leak it */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 201 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 202 | /* Open the tty as standard input */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 203 | debug("open(2)\n"); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 204 | close(0); |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 205 | xopen(G.tty_name, O_RDWR | O_NONBLOCK); /* uses fd 0 */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 206 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 207 | /* Set proper protections and ownership */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 208 | fchown(0, 0, 0); /* 0:0 */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 209 | fchmod(0, 0620); /* crw--w---- */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 210 | } else { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 211 | char *n; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 212 | /* |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 213 | * Standard input should already be connected to an open port. |
| 214 | * Make sure it is open for read/write. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 215 | */ |
Denys Vlasenko | 006416e | 2011-01-22 17:29:53 +0100 | [diff] [blame] | 216 | if ((fcntl(0, F_GETFL) & (O_RDWR|O_RDONLY|O_WRONLY)) != O_RDWR) |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 217 | bb_error_msg_and_die("stdin is not open for read/write"); |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 218 | |
| 219 | /* Try to get real tty name instead of "-" */ |
| 220 | n = xmalloc_ttyname(0); |
| 221 | if (n) |
| 222 | G.tty_name = n; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 223 | } |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 224 | applet_name = xasprintf("getty: %s", skip_dev_pfx(G.tty_name)); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 227 | static void set_tty_attrs(void) |
Peter Korsgaard | ddd1ec1 | 2011-01-26 15:15:19 +0100 | [diff] [blame] | 228 | { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 229 | if (tcsetattr_stdin_TCSANOW(&G.tty_attrs) < 0) |
Peter Korsgaard | ddd1ec1 | 2011-01-26 15:15:19 +0100 | [diff] [blame] | 230 | bb_perror_msg_and_die("tcsetattr"); |
| 231 | } |
| 232 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 233 | /* We manipulate tty_attrs this way: |
| 234 | * - first, we read existing tty_attrs |
| 235 | * - init_tty_attrs modifies some parts and sets it |
| 236 | * - auto_baud and/or BREAK processing can set different speed and set tty attrs |
| 237 | * - finalize_tty_attrs again modifies some parts and sets tty attrs before |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 238 | * execing login |
| 239 | */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 240 | static void init_tty_attrs(int speed) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 241 | { |
Peter Korsgaard | ddd1ec1 | 2011-01-26 15:15:19 +0100 | [diff] [blame] | 242 | /* 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 Vlasenko | dc6cd12 | 2011-01-24 00:28:43 +0100 | [diff] [blame] | 247 | */ |
Peter Korsgaard | ddd1ec1 | 2011-01-26 15:15:19 +0100 | [diff] [blame] | 248 | signal_no_SA_RESTART_empty_mask(SIGALRM, record_signo); |
| 249 | alarm(5); |
| 250 | tcdrain(STDIN_FILENO); |
| 251 | alarm(0); |
Peter Korsgaard | ddd1ec1 | 2011-01-26 15:15:19 +0100 | [diff] [blame] | 252 | |
| 253 | /* Flush input and output queues, important for modems! */ |
Denys Vlasenko | dc6cd12 | 2011-01-24 00:28:43 +0100 | [diff] [blame] | 254 | tcflush(STDIN_FILENO, TCIOFLUSH); |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 255 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 256 | /* Set speed if it wasn't specified as "0" on command line */ |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 257 | if (speed != B0) |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 258 | cfsetspeed(&G.tty_attrs, speed); |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 259 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 260 | /* Initial settings: 8-bit characters, raw mode, blocking i/o. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 261 | * Special characters are set after we have read the login name; all |
Denys Vlasenko | cf9d33a | 2011-01-26 15:56:51 +0100 | [diff] [blame] | 262 | * reads will be done in raw mode anyway. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 263 | */ |
Denys Vlasenko | cf9d33a | 2011-01-26 15:56:51 +0100 | [diff] [blame] | 264 | /* Clear all bits except: */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 265 | G.tty_attrs.c_cflag &= (0 |
Denys Vlasenko | cf9d33a | 2011-01-26 15:56:51 +0100 | [diff] [blame] | 266 | /* 2 stop bits (1 otherwise) |
| 267 | * Enable parity bit (both on input and output) |
| 268 | * Odd parity (else even) |
| 269 | */ |
| 270 | | CSTOPB | PARENB | PARODD |
Denys Vlasenko | 4907f6e | 2011-01-26 16:20:54 +0100 | [diff] [blame] | 271 | #ifdef CMSPAR |
Denys Vlasenko | cf9d33a | 2011-01-26 15:56:51 +0100 | [diff] [blame] | 272 | | CMSPAR /* mark or space parity */ |
| 273 | #endif |
Denys Vlasenko | 8a90e61 | 2012-02-04 19:55:27 +0100 | [diff] [blame] | 274 | #ifdef CBAUD |
Denys Vlasenko | cf9d33a | 2011-01-26 15:56:51 +0100 | [diff] [blame] | 275 | | CBAUD /* (output) baud rate */ |
Denys Vlasenko | 8a90e61 | 2012-02-04 19:55:27 +0100 | [diff] [blame] | 276 | #endif |
Denys Vlasenko | cf9d33a | 2011-01-26 15:56:51 +0100 | [diff] [blame] | 277 | #ifdef CBAUDEX |
| 278 | | CBAUDEX /* (output) baud rate */ |
| 279 | #endif |
| 280 | #ifdef CIBAUD |
| 281 | | CIBAUD /* input baud rate */ |
| 282 | #endif |
Denys Vlasenko | cf9d33a | 2011-01-26 15:56:51 +0100 | [diff] [blame] | 283 | ); |
| 284 | /* Set: 8 bits; hang up (drop DTR) on last close; enable receive */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 285 | G.tty_attrs.c_cflag |= CS8 | HUPCL | CREAD; |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 286 | if (option_mask32 & F_LOCAL) { |
| 287 | /* ignore Carrier Detect pin: |
| 288 | * opens don't block when CD is low, |
| 289 | * losing CD doesn't hang up processes whose ctty is this tty |
| 290 | */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 291 | G.tty_attrs.c_cflag |= CLOCAL; |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 292 | } |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 293 | #ifdef CRTSCTS |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 294 | if (option_mask32 & F_RTSCTS) |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 295 | G.tty_attrs.c_cflag |= CRTSCTS; /* flow control using RTS/CTS pins */ |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 296 | #endif |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 297 | G.tty_attrs.c_iflag = 0; |
| 298 | G.tty_attrs.c_lflag = 0; |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 299 | /* non-raw output; add CR to each NL */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 300 | G.tty_attrs.c_oflag = OPOST | ONLCR; |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 301 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 302 | /* reads would block only if < 1 char is available */ |
| 303 | G.tty_attrs.c_cc[VMIN] = 1; |
| 304 | /* no timeout (reads block forever) */ |
| 305 | G.tty_attrs.c_cc[VTIME] = 0; |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 306 | #ifdef __linux__ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 307 | G.tty_attrs.c_line = 0; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 308 | #endif |
| 309 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 310 | set_tty_attrs(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 311 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 312 | debug("term_io 2\n"); |
| 313 | } |
| 314 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 315 | static void finalize_tty_attrs(void) |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 316 | { |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 317 | /* software flow control on output (stop sending if XOFF is recvd); |
| 318 | * and on input (send XOFF when buffer is full) |
| 319 | */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 320 | G.tty_attrs.c_iflag |= IXON | IXOFF; |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 321 | if (G.eol == '\r') { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 322 | G.tty_attrs.c_iflag |= ICRNL; /* map CR on input to NL */ |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 323 | } |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 324 | /* Other bits in c_iflag: |
| 325 | * IXANY Any recvd char enables output (any char is also a XON) |
| 326 | * INPCK Enable parity check |
| 327 | * IGNPAR Ignore parity errors (drop bad bytes) |
| 328 | * PARMRK Mark parity errors with 0xff, 0x00 prefix |
| 329 | * (else bad byte is received as 0x00) |
| 330 | * ISTRIP Strip parity bit |
| 331 | * IGNBRK Ignore break condition |
| 332 | * BRKINT Send SIGINT on break - maybe set this? |
| 333 | * INLCR Map NL to CR |
| 334 | * IGNCR Ignore CR |
| 335 | * ICRNL Map CR to NL |
| 336 | * IUCLC Map uppercase to lowercase |
| 337 | * IMAXBEL Echo BEL on input line too long |
Peter Korsgaard | ddd1ec1 | 2011-01-26 15:15:19 +0100 | [diff] [blame] | 338 | * IUTF8 Appears to affect tty's idea of char widths, |
| 339 | * observed to improve backspacing through Unicode chars |
Denys Vlasenko | b7c9fb2 | 2011-02-03 00:05:48 +0100 | [diff] [blame] | 340 | */ |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 341 | |
| 342 | /* line buffered input (NL or EOL or EOF chars end a line); |
| 343 | * recognize INT/QUIT/SUSP chars; |
| 344 | * echo input chars; |
| 345 | * echo BS-SP-BS on erase character; |
| 346 | * echo kill char specially, not as ^c (ECHOKE controls how exactly); |
| 347 | * erase all input via BS-SP-BS on kill char (else go to next line) |
| 348 | */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 349 | G.tty_attrs.c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE; |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 350 | /* Other bits in c_lflag: |
| 351 | * XCASE Map uppercase to \lowercase [tried, doesn't work] |
| 352 | * ECHONL Echo NL even if ECHO is not set |
Peter Korsgaard | ddd1ec1 | 2011-01-26 15:15:19 +0100 | [diff] [blame] | 353 | * ECHOCTL Echo ctrl chars as ^c (else don't echo) - maybe set this? |
| 354 | * ECHOPRT On erase, echo erased chars |
| 355 | * [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen] |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 356 | * NOFLSH Don't flush input buffer after interrupt or quit chars |
| 357 | * IEXTEN Enable extended functions (??) |
| 358 | * [glibc says it enables c_cc[LNEXT] "enter literal char" |
| 359 | * and c_cc[VDISCARD] "toggle discard buffered output" chars] |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 360 | * FLUSHO Output being flushed (c_cc[VDISCARD] is in effect) |
| 361 | * PENDIN Retype pending input at next read or input char |
Peter Korsgaard | ddd1ec1 | 2011-01-26 15:15:19 +0100 | [diff] [blame] | 362 | * (c_cc[VREPRINT] is being processed) |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 363 | * TOSTOP Send SIGTTOU for background output |
| 364 | * (why "stty sane" unsets this bit?) |
| 365 | */ |
Denys Vlasenko | d849493 | 2011-01-26 03:26:38 +0100 | [diff] [blame] | 366 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 367 | G.tty_attrs.c_cc[VINTR] = CTL('C'); |
| 368 | G.tty_attrs.c_cc[VQUIT] = CTL('\\'); |
| 369 | G.tty_attrs.c_cc[VEOF] = CTL('D'); |
| 370 | G.tty_attrs.c_cc[VEOL] = '\n'; |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 371 | #ifdef VSWTC |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 372 | G.tty_attrs.c_cc[VSWTC] = 0; |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 373 | #endif |
Denys Vlasenko | 7073964 | 2011-01-24 18:17:19 +0100 | [diff] [blame] | 374 | #ifdef VSWTCH |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 375 | G.tty_attrs.c_cc[VSWTCH] = 0; |
Denys Vlasenko | 7073964 | 2011-01-24 18:17:19 +0100 | [diff] [blame] | 376 | #endif |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 377 | G.tty_attrs.c_cc[VKILL] = CTL('U'); |
Denys Vlasenko | 3ff9be3 | 2011-01-26 11:28:43 +0100 | [diff] [blame] | 378 | /* Other control chars: |
| 379 | * VEOL2 |
| 380 | * VERASE, VWERASE - (word) erase. we may set VERASE in get_logname |
| 381 | * VREPRINT - reprint current input buffer |
| 382 | * VLNEXT, VDISCARD, VSTATUS |
| 383 | * VSUSP, VDSUSP - send (delayed) SIGTSTP |
| 384 | * VSTART, VSTOP - chars used for IXON/IXOFF |
| 385 | */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 386 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 387 | set_tty_attrs(); |
| 388 | |
| 389 | /* Now the newline character should be properly written */ |
| 390 | full_write(STDOUT_FILENO, "\n", 1); |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 393 | /* extract baud rate from modem status message */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 394 | static void auto_baud(void) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 395 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 396 | int nread; |
| 397 | |
| 398 | /* |
| 399 | * This works only if the modem produces its status code AFTER raising |
| 400 | * the DCD line, and if the computer is fast enough to set the proper |
| 401 | * baud rate before the message has gone by. We expect a message of the |
| 402 | * following format: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 403 | * |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 404 | * <junk><number><junk> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 405 | * |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 406 | * The number is interpreted as the baud rate of the incoming call. If the |
| 407 | * modem does not tell us the baud rate within one second, we will keep |
| 408 | * using the current baud rate. It is advisable to enable BREAK |
| 409 | * processing (comma-separated list of baud rates) if the processing of |
| 410 | * modem status messages is enabled. |
| 411 | */ |
| 412 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 413 | G.tty_attrs.c_cc[VMIN] = 0; /* don't block reads (min read is 0 chars) */ |
| 414 | set_tty_attrs(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * Wait for a while, then read everything the modem has said so far and |
| 418 | * try to extract the speed of the dial-in call. |
| 419 | */ |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 420 | sleep(1); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 421 | nread = safe_read(STDIN_FILENO, G.line_buf, sizeof(G.line_buf) - 1); |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 422 | if (nread > 0) { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 423 | int speed; |
| 424 | char *bp; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 425 | G.line_buf[nread] = '\0'; |
| 426 | for (bp = G.line_buf; bp < G.line_buf + nread; bp++) { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 427 | if (isdigit(*bp)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 428 | speed = bcode(bp); |
Jeremie Koenig | f812eac | 2010-05-27 15:37:32 +0200 | [diff] [blame] | 429 | if (speed > 0) |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 430 | cfsetspeed(&G.tty_attrs, speed); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 431 | break; |
| 432 | } |
| 433 | } |
| 434 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 435 | |
Denys Vlasenko | cf9d33a | 2011-01-26 15:56:51 +0100 | [diff] [blame] | 436 | /* Restore terminal settings */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 437 | G.tty_attrs.c_cc[VMIN] = 1; /* restore to value set by init_tty_attrs */ |
| 438 | set_tty_attrs(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 441 | /* get user name, establish parity, speed, erase, kill, eol; |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 442 | * return NULL on BREAK, logname on success |
| 443 | */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 444 | static char *get_logname(void) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 445 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 446 | char *bp; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 447 | char c; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 448 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 449 | /* Flush pending input (esp. after parsing or switching the baud rate) */ |
Denys Vlasenko | dc6cd12 | 2011-01-24 00:28:43 +0100 | [diff] [blame] | 450 | usleep(100*1000); /* 0.1 sec */ |
Denys Vlasenko | 1360918 | 2011-01-24 23:45:22 +0100 | [diff] [blame] | 451 | tcflush(STDIN_FILENO, TCIFLUSH); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 452 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 453 | /* Prompt for and read a login name */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 454 | do { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 455 | /* Write issue file and prompt */ |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 456 | #ifdef ISSUE |
| 457 | if (!(option_mask32 & F_NOISSUE)) |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 458 | print_login_issue(G.issue, G.tty_name); |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 459 | #endif |
| 460 | print_login_prompt(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 461 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 462 | /* Read name, watch for break, erase, kill, end-of-line */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 463 | bp = G.line_buf; |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 464 | while (1) { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 465 | /* Do not report trivial EINTR/EIO errors */ |
Denys Vlasenko | 765b0ee | 2010-05-22 21:17:46 +0200 | [diff] [blame] | 466 | errno = EINTR; /* make read of 0 bytes be silent too */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 467 | if (read(STDIN_FILENO, &c, 1) < 1) { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 468 | finalize_tty_attrs(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 469 | if (errno == EINTR || errno == EIO) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 470 | exit(EXIT_SUCCESS); |
Tanguy Pruvot | f7ae0a2 | 2011-07-04 05:30:48 +0200 | [diff] [blame] | 471 | bb_perror_msg_and_die(bb_msg_read_error); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 472 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 473 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 474 | switch (c) { |
Denys Vlasenko | 7073964 | 2011-01-24 18:17:19 +0100 | [diff] [blame] | 475 | case '\r': |
| 476 | case '\n': |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 477 | *bp = '\0'; |
| 478 | G.eol = c; |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 479 | goto got_logname; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 480 | case CTL('H'): |
| 481 | case 0x7f: |
| 482 | G.tty_attrs.c_cc[VERASE] = c; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 483 | if (bp > G.line_buf) { |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 484 | full_write(STDOUT_FILENO, "\010 \010", 3); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 485 | bp--; |
| 486 | } |
| 487 | break; |
| 488 | case CTL('U'): |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 489 | while (bp > G.line_buf) { |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 490 | full_write(STDOUT_FILENO, "\010 \010", 3); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 491 | bp--; |
| 492 | } |
| 493 | break; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 494 | case CTL('C'): |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 495 | case CTL('D'): |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 496 | finalize_tty_attrs(); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 497 | exit(EXIT_SUCCESS); |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 498 | case '\0': |
| 499 | /* BREAK. If we have speeds to try, |
| 500 | * return NULL (will switch speeds and return here) */ |
| 501 | if (G.numspeed > 1) |
| 502 | return NULL; |
| 503 | /* fall through and ignore it */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 504 | default: |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 505 | if ((unsigned char)c < ' ') { |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 506 | /* ignore garbage characters */ |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 507 | } else if ((int)(bp - G.line_buf) < (int)sizeof(G.line_buf) - 1) { |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 508 | /* echo and store the character */ |
| 509 | full_write(STDOUT_FILENO, &c, 1); |
| 510 | *bp++ = c; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 511 | } |
| 512 | break; |
| 513 | } |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 514 | } /* end of get char loop */ |
| 515 | got_logname: ; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 516 | } while (G.line_buf[0] == '\0'); /* while logname is empty */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 517 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 518 | return G.line_buf; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 521 | static void alarm_handler(int sig UNUSED_PARAM) |
| 522 | { |
| 523 | finalize_tty_attrs(); |
| 524 | _exit(EXIT_SUCCESS); |
| 525 | } |
| 526 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 527 | int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 528 | int getty_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 529 | { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 530 | int n; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 531 | pid_t pid, tsid; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 532 | char *logname; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 533 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 534 | INIT_G(); |
| 535 | G.login = _PATH_LOGIN; /* default login program */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 536 | #ifdef ISSUE |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 537 | G.issue = ISSUE; /* default issue file */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 538 | #endif |
Denys Vlasenko | 7073964 | 2011-01-24 18:17:19 +0100 | [diff] [blame] | 539 | G.eol = '\r'; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 540 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 541 | /* Parse command-line arguments */ |
| 542 | parse_args(argv); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 543 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 544 | /* Create new session and pgrp, lose controlling tty */ |
| 545 | pid = setsid(); /* this also gives us our pid :) */ |
| 546 | if (pid < 0) { |
| 547 | int fd; |
| 548 | /* :( |
| 549 | * docs/ctty.htm says: |
| 550 | * "This is allowed only when the current process |
| 551 | * is not a process group leader". |
| 552 | * Thus, setsid() will fail if we _already_ are |
| 553 | * a session leader - which is quite possible for getty! |
| 554 | */ |
| 555 | pid = getpid(); |
Tanguy Pruvot | 823694d | 2012-11-18 13:20:29 +0100 | [diff] [blame] | 556 | if (getsid(0) != pid) { |
| 557 | //for debugging: |
| 558 | //bb_perror_msg_and_die("setsid failed:" |
| 559 | // " pid %d ppid %d" |
| 560 | // " sid %d pgid %d", |
| 561 | // pid, getppid(), |
| 562 | // getsid(0), getpgid(0)); |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 563 | bb_perror_msg_and_die("setsid"); |
Tanguy Pruvot | 823694d | 2012-11-18 13:20:29 +0100 | [diff] [blame] | 564 | } |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 565 | /* Looks like we are already a session leader. |
| 566 | * In this case (setsid failed) we may still have ctty, |
| 567 | * and it may be different from tty we need to control! |
| 568 | * If we still have ctty, on Linux ioctl(TIOCSCTTY) |
| 569 | * (which we are going to use a bit later) always fails - |
| 570 | * even if we try to take ctty which is already ours! |
| 571 | * Try to drop old ctty now to prevent that. |
| 572 | * Use O_NONBLOCK: old ctty may be a serial line. |
| 573 | */ |
| 574 | fd = open("/dev/tty", O_RDWR | O_NONBLOCK); |
| 575 | if (fd >= 0) { |
Denys Vlasenko | c1b7d1d | 2012-05-28 02:48:55 +0200 | [diff] [blame] | 576 | /* TIOCNOTTY sends SIGHUP to the foreground |
| 577 | * process group - which may include us! |
| 578 | * Make sure to not die on it: |
| 579 | */ |
| 580 | sighandler_t old = signal(SIGHUP, SIG_IGN); |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 581 | ioctl(fd, TIOCNOTTY); |
| 582 | close(fd); |
Denys Vlasenko | c1b7d1d | 2012-05-28 02:48:55 +0200 | [diff] [blame] | 583 | signal(SIGHUP, old); |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 584 | } |
| 585 | } |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 586 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 587 | /* Close stdio, and stray descriptors, just in case */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 588 | n = xopen(bb_dev_null, O_RDWR); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 589 | /* dup2(n, 0); - no, we need to handle "getty - 9600" too */ |
| 590 | xdup2(n, 1); |
| 591 | xdup2(n, 2); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 592 | while (n > 2) |
| 593 | close(n--); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 594 | |
| 595 | /* Logging. We want special flavor of error_msg_and_die */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 596 | die_sleep = 10; |
| 597 | msg_eol = "\r\n"; |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 598 | /* most likely will internally use fd #3 in CLOEXEC mode: */ |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 599 | openlog(applet_name, LOG_PID, LOG_AUTH); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 600 | logmode = LOGMODE_BOTH; |
| 601 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 602 | #ifdef DEBUGGING |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 603 | dbf = xfopen_for_write(DEBUGTERM); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 604 | for (n = 1; argv[n]; n++) { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 605 | debug(argv[n]); |
| 606 | debug("\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 607 | } |
| 608 | #endif |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 609 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 610 | /* Open the tty as standard input, if it is not "-" */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 611 | debug("calling open_tty\n"); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 612 | open_tty(); |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 613 | ndelay_off(STDIN_FILENO); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 614 | debug("duping\n"); |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 615 | xdup2(STDIN_FILENO, 1); |
| 616 | xdup2(STDIN_FILENO, 2); |
| 617 | |
| 618 | /* Steal ctty if we don't have it yet */ |
| 619 | tsid = tcgetsid(STDIN_FILENO); |
| 620 | if (tsid < 0 || pid != tsid) { |
| 621 | if (ioctl(STDIN_FILENO, TIOCSCTTY, /*force:*/ (long)1) < 0) |
| 622 | bb_perror_msg_and_die("TIOCSCTTY"); |
| 623 | } |
| 624 | |
| 625 | #ifdef __linux__ |
| 626 | /* Make ourself a foreground process group within our session */ |
| 627 | if (tcsetpgrp(STDIN_FILENO, pid) < 0) |
| 628 | bb_perror_msg_and_die("tcsetpgrp"); |
| 629 | #endif |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 630 | |
| 631 | /* |
| 632 | * The following ioctl will fail if stdin is not a tty, but also when |
| 633 | * there is noise on the modem control lines. In the latter case, the |
| 634 | * common course of action is (1) fix your cables (2) give the modem more |
| 635 | * time to properly reset after hanging up. SunOS users can achieve (2) |
| 636 | * by patching the SunOS kernel variable "zsadtrlow" to a larger value; |
| 637 | * 5 seconds seems to be a good value. |
| 638 | */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 639 | if (tcgetattr(STDIN_FILENO, &G.tty_attrs) < 0) |
Denys Vlasenko | 9dc0412 | 2010-09-01 11:31:43 +0200 | [diff] [blame] | 640 | bb_perror_msg_and_die("tcgetattr"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 641 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 642 | /* Update the utmp file. This tty is ours now! */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 643 | update_utmp(pid, LOGIN_PROCESS, G.tty_name, "LOGIN", G.fakehost); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 644 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 645 | /* Initialize tty attrs (raw mode, eight-bit, blocking i/o) */ |
| 646 | debug("calling init_tty_attrs\n"); |
| 647 | init_tty_attrs(G.speeds[0]); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 648 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 649 | /* Write the modem init string and DON'T flush the buffers */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 650 | if (option_mask32 & F_INITSTRING) { |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 651 | debug("writing init string\n"); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 652 | full_write1_str(G.initstring); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 655 | /* Optionally detect the baud rate from the modem status message */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 656 | debug("before autobaud\n"); |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 657 | if (option_mask32 & F_PARSE) |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 658 | auto_baud(); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 659 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 660 | /* Set the optional timer */ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 661 | signal(SIGALRM, alarm_handler); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 662 | alarm(G.timeout); /* if 0, alarm is not set */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 663 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 664 | /* Optionally wait for CR or LF before writing /etc/issue */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 665 | if (option_mask32 & F_WAITCRLF) { |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 666 | char ch; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 667 | debug("waiting for cr-lf\n"); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 668 | while (safe_read(STDIN_FILENO, &ch, 1) == 1) { |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 669 | debug("read %x\n", (unsigned char)ch); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 670 | if (ch == '\n' || ch == '\r') |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 675 | logname = NULL; |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 676 | if (!(option_mask32 & F_NOPROMPT)) { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 677 | /* NB: init_tty_attrs already set line speed |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 678 | * to G.speeds[0] */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 679 | int baud_index = 0; |
| 680 | |
| 681 | while (1) { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 682 | /* Read the login name */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 683 | debug("reading login name\n"); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 684 | logname = get_logname(); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 685 | if (logname) |
| 686 | break; |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 687 | /* We are here only if G.numspeed > 1 */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 688 | baud_index = (baud_index + 1) % G.numspeed; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 689 | cfsetspeed(&G.tty_attrs, G.speeds[baud_index]); |
| 690 | set_tty_attrs(); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 691 | } |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 694 | /* Disable timer */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 695 | alarm(0); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 696 | |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 697 | finalize_tty_attrs(); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 698 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 699 | /* Let the login program take care of password validation */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 700 | /* We use PATH because we trust that root doesn't set "bad" PATH, |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 701 | * and getty is not suid-root applet */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 702 | /* With -n, logname == NULL, and login will ask for username instead */ |
maxwen | 27116ba | 2015-08-14 21:41:28 +0200 | [diff] [blame] | 703 | BB_EXECLP(G.login, G.login, "--", logname, (char *)0); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 704 | bb_error_msg_and_die("can't execute '%s'", G.login); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 705 | } |