Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* agetty.c - another getty program for Linux. By W. Z. Venema 1989 |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 3 | * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk> |
| 4 | * This program is freely distributable. The entire man-page used to |
| 5 | * be here. Now read the real man-page agetty.8 instead. |
| 6 | * |
| 7 | * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95 |
| 8 | * |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 9 | * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org> |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [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> |
| 13 | * - enable hardware flow control before displaying /etc/issue |
| 14 | * |
| 15 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 16 | */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Denis Vlasenko | f702652 | 2006-09-19 13:50:55 +0000 | [diff] [blame] | 19 | #include <syslog.h> |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 21 | #if ENABLE_FEATURE_UTMP |
Denis Vlasenko | 9725daa | 2008-09-11 09:54:23 +0000 | [diff] [blame] | 22 | #include <utmp.h> /* updwtmp() */ |
Mike Frysinger | b381009 | 2005-07-01 01:29:44 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 25 | /* |
| 26 | * Some heuristics to find out what environment we are in: if it is not |
| 27 | * System V, assume it is SunOS 4. |
| 28 | */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 29 | #ifdef LOGIN_PROCESS /* defined in System V utmp.h */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 30 | #include <sys/utsname.h> |
| 31 | #include <time.h> |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 32 | #else /* if !sysV style, wtmp/utmp code is off */ |
| 33 | #undef ENABLE_FEATURE_UTMP |
| 34 | #undef ENABLE_FEATURE_WTMP |
| 35 | #define ENABLE_FEATURE_UTMP 0 |
| 36 | #define ENABLE_FEATURE_WTMP 0 |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 37 | #endif /* LOGIN_PROCESS */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 39 | /* |
| 40 | * Things you may want to modify. |
| 41 | * |
| 42 | * You may disagree with the default line-editing etc. characters defined |
| 43 | * below. Note, however, that DEL cannot be used for interrupt generation |
| 44 | * and for line editing at the same time. |
| 45 | */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 47 | /* I doubt there are systems which still need this */ |
| 48 | #undef HANDLE_ALLCAPS |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 49 | #undef ANCIENT_BS_KILL_CHARS |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 51 | #define _PATH_LOGIN "/bin/login" |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 53 | /* If ISSUE is not defined, getty will never display the contents of the |
| 54 | * /etc/issue file. You will not want to spit out large "issue" files at the |
| 55 | * wrong baud rate. |
| 56 | */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 57 | #define ISSUE "/etc/issue" /* displayed before the login prompt */ |
Eric Andersen | 77804ce | 2005-07-27 06:05:38 +0000 | [diff] [blame] | 58 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 59 | /* Some shorthands for control characters. */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 60 | #define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 61 | #define CR CTL('M') /* carriage return */ |
| 62 | #define NL CTL('J') /* line feed */ |
| 63 | #define BS CTL('H') /* back space */ |
| 64 | #define DEL CTL('?') /* delete */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 65 | |
| 66 | /* 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] | 67 | #define DEF_ERASE DEL /* default erase character */ |
| 68 | #define DEF_INTR CTL('C') /* default interrupt character */ |
| 69 | #define DEF_QUIT CTL('\\') /* default quit char */ |
| 70 | #define DEF_KILL CTL('U') /* default kill char */ |
| 71 | #define DEF_EOF CTL('D') /* default EOF char */ |
| 72 | #define DEF_EOL '\n' |
| 73 | #define DEF_SWITCH 0 /* default switch char */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 75 | /* |
| 76 | * When multiple baud rates are specified on the command line, the first one |
| 77 | * we will try is the first one specified. |
| 78 | */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 79 | #define MAX_SPEED 10 /* max. nr. of baud rates */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 81 | /* Storage for command-line options. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 82 | struct options { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 83 | int flags; /* toggle switches, see below */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 84 | unsigned timeout; /* time-out period */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 85 | const char *login; /* login program */ |
| 86 | const char *tty; /* name of tty */ |
| 87 | const char *initstring; /* modem init string */ |
| 88 | const char *issue; /* alternative issue file */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 89 | int numspeed; /* number of baud rates to try */ |
| 90 | int speeds[MAX_SPEED]; /* baud rates to be tried */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 93 | /* Storage for things detected while the login name was read. */ |
Mike Frysinger | b381009 | 2005-07-01 01:29:44 +0000 | [diff] [blame] | 94 | struct chardata { |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 95 | unsigned char erase; /* erase character */ |
| 96 | unsigned char kill; /* kill character */ |
| 97 | unsigned char eol; /* end-of-line character */ |
| 98 | unsigned char parity; /* what parity did we see */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 99 | /* (parity & 1): saw odd parity char with 7th bit set */ |
| 100 | /* (parity & 2): saw even parity char with 7th bit set */ |
| 101 | /* parity == 0: probably 7-bit, space parity? */ |
| 102 | /* parity == 1: probably 7-bit, odd parity? */ |
| 103 | /* parity == 2: probably 7-bit, even parity? */ |
| 104 | /* parity == 3: definitely 8 bit, no parity! */ |
| 105 | /* Hmm... with any value of "parity" 8 bit, no parity is possible */ |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 106 | #ifdef HANDLE_ALLCAPS |
| 107 | unsigned char capslock; /* upper case without lower case */ |
| 108 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 111 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 112 | /* Initial values for the above. */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 113 | static const struct chardata init_chardata = { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 114 | DEF_ERASE, /* default erase character */ |
| 115 | DEF_KILL, /* default kill character */ |
| 116 | 13, /* default eol char */ |
| 117 | 0, /* space parity */ |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 118 | #ifdef HANDLE_ALLCAPS |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 119 | 0, /* no capslock */ |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 120 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 123 | static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn"; |
| 124 | #define F_INITSTRING (1 << 0) /* -I initstring is set */ |
| 125 | #define F_LOCAL (1 << 1) /* -L force local */ |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 126 | #define F_FAKEHOST (1 << 2) /* -H fake hostname */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 127 | #define F_CUSTISSUE (1 << 3) /* -f give alternative issue file */ |
| 128 | #define F_RTSCTS (1 << 4) /* -h enable RTS/CTS flow control */ |
| 129 | #define F_ISSUE (1 << 5) /* -i display /etc/issue */ |
| 130 | #define F_LOGIN (1 << 6) /* -l non-default login program */ |
| 131 | #define F_PARSE (1 << 7) /* -m process modem status messages */ |
| 132 | #define F_TIMEOUT (1 << 8) /* -t time out */ |
| 133 | #define F_WAITCRLF (1 << 9) /* -w wait for CR or LF */ |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 134 | #define F_NOPROMPT (1 << 10) /* -n don't ask for login name */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 135 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 137 | #define line_buf bb_common_bufsiz1 |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 139 | /* The following is used for understandable diagnostics. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 140 | #ifdef DEBUGGING |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 141 | static FILE *dbf; |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 142 | #define DEBUGTERM "/dev/ttyp0" |
| 143 | #define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 144 | #else |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 145 | #define debug(...) ((void)0) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 146 | #endif |
| 147 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 148 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 149 | /* bcode - convert speed string to speed code; return <= 0 on failure */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 150 | static int bcode(const char *s) |
| 151 | { |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 152 | int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */ |
| 153 | if (value < 0) /* bad terminating char, overflow, etc */ |
| 154 | return value; |
| 155 | return tty_value_to_baud(value); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 158 | /* parse_speeds - parse alternate baud rates */ |
| 159 | static void parse_speeds(struct options *op, char *arg) |
| 160 | { |
| 161 | char *cp; |
| 162 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 163 | /* NB: at least one iteration is always done */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 164 | debug("entered parse_speeds\n"); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 165 | while ((cp = strsep(&arg, ",")) != NULL) { |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 166 | op->speeds[op->numspeed] = bcode(cp); |
| 167 | if (op->speeds[op->numspeed] <= 0) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 168 | bb_error_msg_and_die("bad speed: %s", cp); |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame] | 169 | op->numspeed++; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 170 | if (op->numspeed > MAX_SPEED) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 171 | bb_error_msg_and_die("too many alternate speeds"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 172 | } |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 173 | debug("exiting parse_speeds\n"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 176 | /* parse_args - parse command-line arguments */ |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 177 | static void parse_args(char **argv, struct options *op, char **fakehost_p) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 178 | { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 179 | char *ts; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 181 | opt_complementary = "-2:t+"; /* at least 2 args; -t N */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 182 | op->flags = getopt32(argv, opt_string, |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 183 | &(op->initstring), fakehost_p, &(op->issue), |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 184 | &(op->login), &op->timeout); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 185 | argv += optind; |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 186 | if (op->flags & F_INITSTRING) { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 187 | const char *p = op->initstring; |
| 188 | char *q; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 189 | |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 190 | op->initstring = q = xstrdup(p); |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 191 | /* copy optarg into op->initstring decoding \ddd |
| 192 | octal codes into chars */ |
| 193 | while (*p) { |
| 194 | if (*p == '\\') { |
| 195 | p++; |
| 196 | *q++ = bb_process_escape_sequence(&p); |
| 197 | } else { |
| 198 | *q++ = *p++; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 199 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 200 | } |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 201 | *q = '\0'; |
| 202 | } |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 203 | op->flags ^= F_ISSUE; /* invert flag "show /etc/issue" */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 204 | debug("after getopt\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 205 | |
| 206 | /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 207 | op->tty = argv[0]; /* tty name */ |
| 208 | ts = argv[1]; /* baud rate(s) */ |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 209 | if (isdigit(argv[0][0])) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 210 | /* a number first, assume it's a speed (BSD style) */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 211 | op->tty = ts; /* tty name is in argv[1] */ |
| 212 | ts = argv[0]; /* baud rate(s) */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 213 | } |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 214 | parse_speeds(op, ts); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 216 | // TODO: if applet_name is set to "getty: TTY", bb_error_msg's get simpler! |
| 217 | // grep for "%s:" |
| 218 | |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 219 | if (argv[2]) |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 220 | xsetenv("TERM", argv[2]); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 221 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 222 | debug("exiting parse_args\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 225 | /* open_tty - set up tty as standard { input, output, error } */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 226 | static void open_tty(const char *tty) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 227 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 228 | /* Set up new standard input, unless we are given an already opened port. */ |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 229 | if (NOT_LONE_DASH(tty)) { |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 230 | // struct stat st; |
| 231 | // int cur_dir_fd; |
| 232 | // int fd; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 233 | |
| 234 | /* Sanity checks... */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 235 | // cur_dir_fd = xopen(".", O_DIRECTORY | O_NONBLOCK); |
| 236 | // xchdir("/dev"); |
| 237 | // xstat(tty, &st); |
| 238 | // if ((st.st_mode & S_IFMT) != S_IFCHR) |
| 239 | // bb_error_msg_and_die("%s: not a character device", tty); |
| 240 | |
| 241 | if (tty[0] != '/') |
| 242 | tty = xasprintf("/dev/%s", tty); /* will leak it */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 243 | |
| 244 | /* Open the tty as standard input. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 245 | debug("open(2)\n"); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 246 | close(0); |
| 247 | /*fd =*/ xopen(tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 248 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 249 | // /* Restore current directory */ |
| 250 | // fchdir(cur_dir_fd); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 251 | |
| 252 | /* Open the tty as standard input, continued */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 253 | // xmove_fd(fd, 0); |
| 254 | // /* fd is >= cur_dir_fd, and cur_dir_fd gets closed too here: */ |
| 255 | // while (fd > 2) |
| 256 | // close(fd--); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 257 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 258 | /* Set proper protections and ownership. */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 259 | fchown(0, 0, 0); /* 0:0 */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 260 | fchmod(0, 0620); /* crw--w---- */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 261 | } else { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 262 | /* |
| 263 | * Standard input should already be connected to an open port. Make |
| 264 | * sure it is open for read/write. |
| 265 | */ |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 266 | if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR) |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 267 | bb_error_msg_and_die("stdin is not open for read/write"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 268 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 271 | /* termios_init - initialize termios settings */ |
| 272 | static void termios_init(struct termios *tp, int speed, struct options *op) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 273 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 274 | /* |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 275 | * Initial termios settings: 8-bit characters, raw-mode, blocking i/o. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 276 | * Special characters are set after we have read the login name; all |
| 277 | * reads will be done in raw mode anyway. Errors will be dealt with |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 278 | * later on. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 279 | */ |
| 280 | #ifdef __linux__ |
| 281 | /* flush input and output queues, important for modems! */ |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 282 | ioctl(0, TCFLSH, TCIOFLUSH); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 283 | #endif |
| 284 | |
| 285 | tp->c_cflag = CS8 | HUPCL | CREAD | speed; |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 286 | if (op->flags & F_LOCAL) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 287 | tp->c_cflag |= CLOCAL; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 288 | |
Rob Landley | f78ab53 | 2006-08-24 20:00:44 +0000 | [diff] [blame] | 289 | tp->c_iflag = tp->c_lflag = tp->c_line = 0; |
| 290 | tp->c_oflag = OPOST | ONLCR; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 291 | tp->c_cc[VMIN] = 1; |
| 292 | tp->c_cc[VTIME] = 0; |
| 293 | |
| 294 | /* Optionally enable hardware flow control */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 295 | #ifdef CRTSCTS |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 296 | if (op->flags & F_RTSCTS) |
| 297 | tp->c_cflag |= CRTSCTS; |
| 298 | #endif |
| 299 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 300 | ioctl(0, TCSETS, tp); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 301 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 302 | debug("term_io 2\n"); |
| 303 | } |
| 304 | |
| 305 | /* auto_baud - extract baud rate from modem status message */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 306 | static void auto_baud(char *buf, unsigned size_buf, struct termios *tp) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 307 | { |
| 308 | int speed; |
| 309 | int vmin; |
| 310 | unsigned iflag; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 311 | char *bp; |
| 312 | int nread; |
| 313 | |
| 314 | /* |
| 315 | * This works only if the modem produces its status code AFTER raising |
| 316 | * the DCD line, and if the computer is fast enough to set the proper |
| 317 | * baud rate before the message has gone by. We expect a message of the |
| 318 | * following format: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 319 | * |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 320 | * <junk><number><junk> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 321 | * |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 322 | * The number is interpreted as the baud rate of the incoming call. If the |
| 323 | * modem does not tell us the baud rate within one second, we will keep |
| 324 | * using the current baud rate. It is advisable to enable BREAK |
| 325 | * processing (comma-separated list of baud rates) if the processing of |
| 326 | * modem status messages is enabled. |
| 327 | */ |
| 328 | |
| 329 | /* |
| 330 | * Use 7-bit characters, don't block if input queue is empty. Errors will |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 331 | * be dealt with later on. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 332 | */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 333 | iflag = tp->c_iflag; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 334 | tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 335 | vmin = tp->c_cc[VMIN]; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 336 | tp->c_cc[VMIN] = 0; /* don't block if queue empty */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 337 | ioctl(0, TCSETS, tp); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Wait for a while, then read everything the modem has said so far and |
| 341 | * try to extract the speed of the dial-in call. |
| 342 | */ |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 343 | sleep(1); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 344 | nread = safe_read(STDIN_FILENO, buf, size_buf - 1); |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 345 | if (nread > 0) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 346 | buf[nread] = '\0'; |
| 347 | for (bp = buf; bp < buf + nread; bp++) { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 348 | if (isdigit(*bp)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 349 | speed = bcode(bp); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 350 | if (speed > 0) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 351 | tp->c_cflag &= ~CBAUD; |
| 352 | tp->c_cflag |= speed; |
| 353 | } |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 358 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 359 | /* Restore terminal settings. Errors will be dealt with later on. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 360 | tp->c_iflag = iflag; |
| 361 | tp->c_cc[VMIN] = vmin; |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 362 | ioctl(0, TCSETS, tp); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 365 | /* do_prompt - show login prompt, optionally preceded by /etc/issue contents */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 366 | static void do_prompt(struct options *op) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 367 | { |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 368 | #ifdef ISSUE |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 369 | print_login_issue(op->issue, op->tty); |
| 370 | #endif |
| 371 | print_login_prompt(); |
| 372 | } |
| 373 | |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 374 | #ifdef HANDLE_ALLCAPS |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 375 | /* all_is_upcase - string contains upper case without lower case */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 376 | /* returns 1 if true, 0 if false */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 377 | static int all_is_upcase(const char *s) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 378 | { |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 379 | while (*s) |
| 380 | if (islower(*s++)) |
| 381 | return 0; |
| 382 | return 1; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 383 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 384 | #endif |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 385 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 386 | /* get_logname - get user name, establish parity, speed, erase, kill, eol; |
| 387 | * return NULL on BREAK, logname on success */ |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 388 | static char *get_logname(char *logname, unsigned size_logname, |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 389 | struct options *op, struct chardata *cp) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 390 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 391 | char *bp; |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 392 | char c; /* input character, full eight bits */ |
| 393 | char ascval; /* low 7 bits of input character */ |
| 394 | int bits; /* # of "1" bits per character */ |
| 395 | int mask; /* mask with 1 bit up */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 396 | static const char erase[][3] = {/* backspace-space-backspace */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 397 | "\010\040\010", /* space parity */ |
| 398 | "\010\040\010", /* odd parity */ |
| 399 | "\210\240\210", /* even parity */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 400 | "\010\040\010", /* 8 bit no parity */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 403 | /* NB: *cp is pre-initialized with init_chardata */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 404 | |
| 405 | /* Flush pending input (esp. after parsing or switching the baud rate). */ |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 406 | sleep(1); |
| 407 | ioctl(0, TCFLSH, TCIFLUSH); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 408 | |
| 409 | /* Prompt for and read a login name. */ |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 410 | logname[0] = '\0'; |
| 411 | while (!logname[0]) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 412 | /* Write issue file and prompt, with "parity" bit == 0. */ |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 413 | do_prompt(op); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 414 | |
| 415 | /* Read name, watch for break, parity, erase, kill, end-of-line. */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 416 | bp = logname; |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 417 | cp->eol = '\0'; |
| 418 | while (cp->eol == '\0') { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 419 | |
| 420 | /* Do not report trivial EINTR/EIO errors. */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 421 | if (read(STDIN_FILENO, &c, 1) < 1) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 422 | if (errno == EINTR || errno == EIO) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 423 | exit(EXIT_SUCCESS); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 424 | bb_perror_msg_and_die("%s: read", op->tty); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 425 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 426 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 427 | /* BREAK. If we have speeds to try, |
| 428 | * return NULL (will switch speeds and return here) */ |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 429 | if (c == '\0' && op->numspeed > 1) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 430 | return NULL; |
| 431 | |
| 432 | /* Do parity bit handling. */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 433 | if (!(op->flags & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */ |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 434 | bits = 1; |
| 435 | mask = 1; |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 436 | while (mask & 0x7f) { |
| 437 | if (mask & c) |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 438 | bits++; /* count "1" bits */ |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 439 | mask <<= 1; |
| 440 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 441 | /* ... |= 2 - even, 1 - odd */ |
| 442 | cp->parity |= 2 - (bits & 1); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 443 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 444 | |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 445 | /* Do erase, kill and end-of-line processing. */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 446 | ascval = c & 0x7f; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 447 | switch (ascval) { |
| 448 | case CR: |
| 449 | case NL: |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 450 | *bp = '\0'; /* terminate logname */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 451 | cp->eol = ascval; /* set end-of-line char */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 452 | break; |
| 453 | case BS: |
| 454 | case DEL: |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 455 | #ifdef ANCIENT_BS_KILL_CHARS |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 456 | case '#': |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 457 | #endif |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 458 | cp->erase = ascval; /* set erase character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 459 | if (bp > logname) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 460 | full_write(STDOUT_FILENO, erase[cp->parity], 3); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 461 | bp--; |
| 462 | } |
| 463 | break; |
| 464 | case CTL('U'): |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 465 | #ifdef ANCIENT_BS_KILL_CHARS |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 466 | case '@': |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 467 | #endif |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 468 | cp->kill = ascval; /* set kill character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 469 | while (bp > logname) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 470 | full_write(STDOUT_FILENO, erase[cp->parity], 3); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 471 | bp--; |
| 472 | } |
| 473 | break; |
| 474 | case CTL('D'): |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 475 | exit(EXIT_SUCCESS); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 476 | default: |
| 477 | if (!isascii(ascval) || !isprint(ascval)) { |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 478 | /* ignore garbage characters */ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 479 | } else if ((int)(bp - logname) >= size_logname - 1) { |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 480 | bb_error_msg_and_die("%s: input overrun", op->tty); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 481 | } else { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 482 | full_write(STDOUT_FILENO, &c, 1); /* echo the character */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 483 | *bp++ = ascval; /* and store it */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 484 | } |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | /* Handle names with upper case and no lower case. */ |
| 490 | |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 491 | #ifdef HANDLE_ALLCAPS |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 492 | cp->capslock = all_is_upcase(logname); |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 493 | if (cp->capslock) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 494 | for (bp = logname; *bp; bp++) |
| 495 | if (isupper(*bp)) |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 496 | *bp = tolower(*bp); /* map name to lower case */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 497 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 498 | #endif |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 499 | return logname; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 502 | /* termios_final - set the final tty mode bits */ |
| 503 | static void termios_final(struct options *op, struct termios *tp, struct chardata *cp) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 504 | { |
| 505 | /* General terminal-independent stuff. */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 506 | tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 507 | tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE; |
| 508 | /* no longer| ECHOCTL | ECHOPRT */ |
| 509 | tp->c_oflag |= OPOST; |
| 510 | /* tp->c_cflag = 0; */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 511 | tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */ |
| 512 | tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */ |
| 513 | tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 514 | tp->c_cc[VEOL] = DEF_EOL; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 515 | tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 516 | |
| 517 | /* Account for special characters seen in input. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 518 | if (cp->eol == CR) { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 519 | tp->c_iflag |= ICRNL; /* map CR in input to NL */ |
| 520 | tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 521 | } |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 522 | tp->c_cc[VERASE] = cp->erase; /* set erase character */ |
| 523 | tp->c_cc[VKILL] = cp->kill; /* set kill character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 524 | |
| 525 | /* Account for the presence or absence of parity bits in input. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 526 | switch (cp->parity) { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 527 | case 0: /* space (always 0) parity */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 528 | // I bet most people go here - they use only 7-bit chars in usernames.... |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 529 | break; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 530 | case 1: /* odd parity */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 531 | tp->c_cflag |= PARODD; |
| 532 | /* FALLTHROUGH */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 533 | case 2: /* even parity */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 534 | tp->c_cflag |= PARENB; |
| 535 | tp->c_iflag |= INPCK | ISTRIP; |
| 536 | /* FALLTHROUGH */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 537 | case (1 | 2): /* no parity bit */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 538 | tp->c_cflag &= ~CSIZE; |
| 539 | tp->c_cflag |= CS7; |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 540 | // FIXME: wtf? case 3: we saw both even and odd 8-bit bytes - |
| 541 | // it's probably some umlauts etc, but definitely NOT 7-bit!!! |
| 542 | // Entire parity detection madness here just begs for deletion... |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 543 | break; |
| 544 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 545 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 546 | /* Account for upper case without lower case. */ |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 547 | #ifdef HANDLE_ALLCAPS |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 548 | if (cp->capslock) { |
| 549 | tp->c_iflag |= IUCLC; |
| 550 | tp->c_lflag |= XCASE; |
| 551 | tp->c_oflag |= OLCUC; |
| 552 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 553 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 554 | /* Optionally enable hardware flow control */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 555 | #ifdef CRTSCTS |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 556 | if (op->flags & F_RTSCTS) |
| 557 | tp->c_cflag |= CRTSCTS; |
| 558 | #endif |
| 559 | |
| 560 | /* Finally, make the new settings effective */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 561 | ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 564 | #if ENABLE_FEATURE_UTMP |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 565 | static void touch(const char *filename) |
| 566 | { |
| 567 | if (access(filename, R_OK | W_OK) == -1) |
| 568 | close(open(filename, O_WRONLY | O_CREAT, 0664)); |
| 569 | } |
| 570 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 571 | /* update_utmp - update our utmp entry */ |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 572 | static void update_utmp(const char *line, char *fakehost) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 573 | { |
| 574 | struct utmp ut; |
| 575 | struct utmp *utp; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 576 | int mypid = getpid(); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 577 | |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 578 | /* In case we won't find an entry below... */ |
| 579 | memset(&ut, 0, sizeof(ut)); |
| 580 | safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id)); |
| 581 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 582 | /* |
| 583 | * The utmp file holds miscellaneous information about things started by |
| 584 | * /sbin/init and other system-related events. Our purpose is to update |
| 585 | * the utmp entry for the current process, in particular the process type |
| 586 | * and the tty line we are listening to. Return successfully only if the |
| 587 | * utmp file can be opened for update, and if we are able to find our |
| 588 | * entry in the utmp file. |
| 589 | */ |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 590 | touch(_PATH_UTMP); |
| 591 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 592 | utmpname(_PATH_UTMP); |
| 593 | setutent(); |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 594 | while ((utp = getutent()) != NULL) { |
| 595 | if (utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid) { |
| 596 | memcpy(&ut, utp, sizeof(ut)); |
| 597 | break; |
| 598 | } |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 599 | } |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 600 | |
Rob Landley | cf7577d | 2006-06-25 22:59:31 +0000 | [diff] [blame] | 601 | strcpy(ut.ut_user, "LOGIN"); |
| 602 | safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line)); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 603 | if (fakehost) |
Rob Landley | cf7577d | 2006-06-25 22:59:31 +0000 | [diff] [blame] | 604 | safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host)); |
Bernhard Reutner-Fischer | 62d8503 | 2008-06-01 10:10:22 +0000 | [diff] [blame] | 605 | ut.ut_tv.tv_sec = time(NULL); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 606 | ut.ut_type = LOGIN_PROCESS; |
| 607 | ut.ut_pid = mypid; |
| 608 | |
| 609 | pututline(&ut); |
| 610 | endutent(); |
| 611 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 612 | #if ENABLE_FEATURE_WTMP |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 613 | touch(bb_path_wtmp_file); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 614 | updwtmp(bb_path_wtmp_file, &ut); |
| 615 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 616 | } |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 617 | #endif /* CONFIG_FEATURE_UTMP */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 618 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 619 | int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 620 | int getty_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 621 | { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 622 | int n; |
Denis Vlasenko | f3fca91 | 2007-12-04 18:46:01 +0000 | [diff] [blame] | 623 | char *fakehost = NULL; /* Fake hostname for ut_host */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 624 | char *logname; /* login name, given to /bin/login */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 625 | /* Merging these into "struct local" may _seem_ to reduce |
| 626 | * parameter passing, but today's gcc will inline |
| 627 | * statics which are called once anyway, so don't do that */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 628 | struct chardata chardata; /* set by get_logname() */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 629 | struct termios termios; /* terminal mode bits */ |
| 630 | struct options options; |
| 631 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 632 | chardata = init_chardata; |
| 633 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 634 | memset(&options, 0, sizeof(options)); |
| 635 | options.login = _PATH_LOGIN; /* default login program */ |
| 636 | options.tty = "tty1"; /* default tty line */ |
| 637 | options.initstring = ""; /* modem init string */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 638 | #ifdef ISSUE |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 639 | options.issue = ISSUE; /* default issue file */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 640 | #endif |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 641 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 642 | /* Parse command-line arguments. */ |
| 643 | parse_args(argv, &options, &fakehost); |
| 644 | |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 645 | logmode = LOGMODE_NONE; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 646 | |
| 647 | /* Create new session, lose controlling tty, if any */ |
| 648 | /* docs/ctty.htm says: |
| 649 | * "This is allowed only when the current process |
| 650 | * is not a process group leader" - is this a problem? */ |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 651 | setsid(); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 652 | /* close stdio, and stray descriptors, just in case */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 653 | n = xopen(bb_dev_null, O_RDWR); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 654 | /* dup2(n, 0); - no, we need to handle "getty - 9600" too */ |
| 655 | xdup2(n, 1); |
| 656 | xdup2(n, 2); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 657 | while (n > 2) |
| 658 | close(n--); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 659 | |
| 660 | /* Logging. We want special flavor of error_msg_and_die */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 661 | die_sleep = 10; |
| 662 | msg_eol = "\r\n"; |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 663 | /* most likely will internally use fd #3 in CLOEXEC mode: */ |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 664 | openlog(applet_name, LOG_PID, LOG_AUTH); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 665 | logmode = LOGMODE_BOTH; |
| 666 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 667 | #ifdef DEBUGGING |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 668 | dbf = xfopen_for_write(DEBUGTERM); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 669 | for (n = 1; argv[n]; n++) { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 670 | debug(argv[n]); |
| 671 | debug("\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 672 | } |
| 673 | #endif |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 674 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 675 | /* Open the tty as standard input, if it is not "-" */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 676 | /* If it's not "-" and not taken yet, it will become our ctty */ |
| 677 | debug("calling open_tty\n"); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 678 | open_tty(options.tty); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 679 | ndelay_off(0); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 680 | debug("duping\n"); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 681 | xdup2(0, 1); |
| 682 | xdup2(0, 2); |
| 683 | |
| 684 | /* |
| 685 | * The following ioctl will fail if stdin is not a tty, but also when |
| 686 | * there is noise on the modem control lines. In the latter case, the |
| 687 | * common course of action is (1) fix your cables (2) give the modem more |
| 688 | * time to properly reset after hanging up. SunOS users can achieve (2) |
| 689 | * by patching the SunOS kernel variable "zsadtrlow" to a larger value; |
| 690 | * 5 seconds seems to be a good value. |
| 691 | */ |
| 692 | ioctl_or_perror_and_die(0, TCGETS, &termios, "%s: TCGETS", options.tty); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 693 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 694 | #ifdef __linux__ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 695 | // FIXME: do we need this? Otherwise "-" case seems to be broken... |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 696 | // /* Forcibly make fd 0 our controlling tty, even if another session |
| 697 | // * has it as a ctty. (Another session loses ctty). */ |
| 698 | // ioctl(0, TIOCSCTTY, (void*)1); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 699 | /* Make ourself a foreground process group within our session */ |
| 700 | tcsetpgrp(0, getpid()); |
| 701 | #endif |
| 702 | |
| 703 | #if ENABLE_FEATURE_UTMP |
| 704 | /* Update the utmp file. This tty is ours now! */ |
| 705 | update_utmp(options.tty, fakehost); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 706 | #endif |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 707 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 708 | /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */ |
| 709 | debug("calling termios_init\n"); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 710 | termios_init(&termios, options.speeds[0], &options); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 711 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 712 | /* Write the modem init string and DON'T flush the buffers */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 713 | if (options.flags & F_INITSTRING) { |
| 714 | debug("writing init string\n"); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 715 | full_write(STDOUT_FILENO, options.initstring, strlen(options.initstring)); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 718 | /* Optionally detect the baud rate from the modem status message */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 719 | debug("before autobaud\n"); |
| 720 | if (options.flags & F_PARSE) |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 721 | auto_baud(line_buf, sizeof(line_buf), &termios); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 722 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 723 | /* Set the optional timer */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 724 | alarm(options.timeout); /* if 0, alarm is not set */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 725 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 726 | /* Optionally wait for CR or LF before writing /etc/issue */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 727 | if (options.flags & F_WAITCRLF) { |
| 728 | char ch; |
| 729 | |
| 730 | debug("waiting for cr-lf\n"); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 731 | while (safe_read(STDIN_FILENO, &ch, 1) == 1) { |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 732 | debug("read %x\n", (unsigned char)ch); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 733 | ch &= 0x7f; /* strip "parity bit" */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 734 | if (ch == '\n' || ch == '\r') |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 739 | logname = NULL; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 740 | if (!(options.flags & F_NOPROMPT)) { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 741 | /* NB:termios_init already set line speed |
| 742 | * to options.speeds[0] */ |
| 743 | int baud_index = 0; |
| 744 | |
| 745 | while (1) { |
| 746 | /* Read the login name. */ |
| 747 | debug("reading login name\n"); |
| 748 | logname = get_logname(line_buf, sizeof(line_buf), |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 749 | &options, &chardata); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 750 | if (logname) |
| 751 | break; |
| 752 | /* we are here only if options.numspeed > 1 */ |
| 753 | baud_index = (baud_index + 1) % options.numspeed; |
| 754 | termios.c_cflag &= ~CBAUD; |
| 755 | termios.c_cflag |= options.speeds[baud_index]; |
| 756 | ioctl(0, TCSETS, &termios); |
| 757 | } |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /* Disable timer. */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 761 | alarm(0); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 762 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 763 | /* Finalize the termios settings. */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 764 | termios_final(&options, &termios, &chardata); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 765 | |
| 766 | /* Now the newline character should be properly written. */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 767 | full_write(STDOUT_FILENO, "\n", 1); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 768 | |
| 769 | /* Let the login program take care of password validation. */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 770 | /* We use PATH because we trust that root doesn't set "bad" PATH, |
| 771 | * and getty is not suid-root applet. */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 772 | /* With -n, logname == NULL, and login will ask for username instead */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 773 | BB_EXECLP(options.login, options.login, "--", logname, NULL); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 774 | bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 775 | } |