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