blob: d32d18935c0dc6006bb0fa33fd4d96503d3b4fce [file] [log] [blame]
Robert Griebl1fca5582002-06-04 20:45:46 +00001/* vi: set sw=4 ts=4: */
2/* agetty.c - another getty program for Linux. By W. Z. Venema 1989
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * 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 *
9 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
10 * - added Native Language Support
Robert Griebl1fca5582002-06-04 20:45:46 +000011
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000012 * 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.
16 *
17 */
Robert Griebl1fca5582002-06-04 20:45:46 +000018
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
Denis Vlasenkof7026522006-09-19 13:50:55 +000020#include <syslog.h>
Robert Griebl1fca5582002-06-04 20:45:46 +000021
Denis Vlasenko6476cc12006-11-07 01:52:10 +000022#if ENABLE_FEATURE_UTMP
Mike Frysingerb3810092005-07-01 01:29:44 +000023#include <utmp.h>
24#endif
25
Denis Vlasenko6476cc12006-11-07 01:52:10 +000026/*
27 * Some heuristics to find out what environment we are in: if it is not
28 * System V, assume it is SunOS 4.
29 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000030#ifdef LOGIN_PROCESS /* defined in System V utmp.h */
31#define SYSV_STYLE /* select System V style getty */
Denis Vlasenko6476cc12006-11-07 01:52:10 +000032#include <sys/utsname.h>
33#include <time.h>
34#if ENABLE_FEATURE_WTMP
Eric Andersenfdfe2982002-10-10 03:55:09 +000035extern void updwtmp(const char *filename, const struct utmp *ut);
Denis Vlasenko06c0a712007-01-29 22:51:44 +000036static void update_utmp(const char *line);
Robert Griebl1fca5582002-06-04 20:45:46 +000037#endif
Eric Andersenfdfe2982002-10-10 03:55:09 +000038#endif /* LOGIN_PROCESS */
Robert Griebl1fca5582002-06-04 20:45:46 +000039
Denis Vlasenko6476cc12006-11-07 01:52:10 +000040/*
41 * Things you may want to modify.
42 *
43 * You may disagree with the default line-editing etc. characters defined
44 * below. Note, however, that DEL cannot be used for interrupt generation
45 * and for line editing at the same time.
46 */
Robert Griebl1fca5582002-06-04 20:45:46 +000047
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000048/* I doubt there are systems which still need this */
49#undef HANDLE_ALLCAPS
50
Denis Vlasenko6476cc12006-11-07 01:52:10 +000051#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000052
Denis Vlasenko6476cc12006-11-07 01:52:10 +000053/* 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"3e245c92005-09-29 11:31:26 +000057#define ISSUE "/etc/issue" /* displayed before the login prompt */
Eric Andersen77804ce2005-07-27 06:05:38 +000058
Robert Griebl1fca5582002-06-04 20:45:46 +000059/* Some shorthands for control characters. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000060#define CTL(x) (x ^ 0100) /* Assumes ASCII dialect */
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 Griebl1fca5582002-06-04 20:45:46 +000065
66/* Defaults for line-editing etc. characters; you may want to change this. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000067#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 Griebl1fca5582002-06-04 20:45:46 +000074
Denis Vlasenko6476cc12006-11-07 01:52:10 +000075/*
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"3e245c92005-09-29 11:31:26 +000079#define FIRST_SPEED 0
Robert Griebl1fca5582002-06-04 20:45:46 +000080
81/* Storage for command-line options. */
82
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000083#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +000084
85struct options {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000086 int flags; /* toggle switches, see below */
Denis Vlasenko13858992006-10-08 12:49:22 +000087 unsigned timeout; /* time-out period */
Denis Vlasenko06c0a712007-01-29 22:51:44 +000088 const char *login; /* login program */
89 const char *tty; /* name of tty */
90 const char *initstring; /* modem init string */
91 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000092 int numspeed; /* number of baud rates to try */
93 int speeds[MAX_SPEED]; /* baud rates to be tried */
Robert Griebl1fca5582002-06-04 20:45:46 +000094};
95
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000096static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000097#define F_INITSTRING (1<<0) /* initstring is set */
98#define F_LOCAL (1<<1) /* force local */
99#define F_FAKEHOST (1<<2) /* force fakehost */
100#define F_CUSTISSUE (1<<3) /* give alternative issue file */
101#define F_RTSCTS (1<<4) /* enable RTS/CTS flow control */
102#define F_ISSUE (1<<5) /* display /etc/issue */
103#define F_LOGIN (1<<6) /* non-default login program */
104#define F_PARSE (1<<7) /* process modem status messages */
105#define F_TIMEOUT (1<<8) /* time out */
106#define F_WAITCRLF (1<<9) /* wait for CR or LF */
107#define F_NOPROMPT (1<<10) /* don't ask for login name! */
Robert Griebl1fca5582002-06-04 20:45:46 +0000108
109/* Storage for things detected while the login name was read. */
Mike Frysingerb3810092005-07-01 01:29:44 +0000110struct chardata {
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000111 unsigned char erase; /* erase character */
112 unsigned char kill; /* kill character */
113 unsigned char eol; /* end-of-line character */
114 unsigned char parity; /* what parity did we see */
115#ifdef HANDLE_ALLCAPS
116 unsigned char capslock; /* upper case without lower case */
117#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000118};
119
120/* Initial values for the above. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000121static const struct chardata init_chardata = {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000122 DEF_ERASE, /* default erase character */
123 DEF_KILL, /* default kill character */
124 13, /* default eol char */
125 0, /* space parity */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000126#ifdef HANDLE_ALLCAPS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000127 0, /* no capslock */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000128#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000129};
130
Robert Griebl1fca5582002-06-04 20:45:46 +0000131/* The following is used for understandable diagnostics. */
132
133/* Fake hostname for ut_host specified on command line. */
134static char *fakehost = NULL;
135
136/* ... */
137#ifdef DEBUGGING
138#define debug(s) fprintf(dbf,s); fflush(dbf)
139#define DEBUGTERM "/dev/ttyp0"
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000140static FILE *dbf;
Robert Griebl1fca5582002-06-04 20:45:46 +0000141#else
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000142#define debug(s) /* nothing */
Robert Griebl1fca5582002-06-04 20:45:46 +0000143#endif
144
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000145
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000146/* bcode - convert speed string to speed code; return 0 on failure */
147static int bcode(const char *s)
148{
149 int r;
Denis Vlasenkod686a042006-11-27 14:43:21 +0000150 unsigned value = bb_strtou(s, NULL, 10);
151 if (errno) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000152 return -1;
153 }
Denis Vlasenko13858992006-10-08 12:49:22 +0000154 r = tty_value_to_baud(value);
155 if (r > 0) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000156 return r;
157 }
158 return 0;
159}
160
161
162/* parse_speeds - parse alternate baud rates */
163static void parse_speeds(struct options *op, char *arg)
164{
165 char *cp;
166
167 debug("entered parse_speeds\n");
168 for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000169 op->speeds[op->numspeed] = bcode(cp);
170 if (op->speeds[op->numspeed] <= 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000171 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000172 op->numspeed++;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000173 if (op->numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000174 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000175 }
176 debug("exiting parsespeeds\n");
177}
178
179
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000180/* parse_args - parse command-line arguments */
Robert Griebl1fca5582002-06-04 20:45:46 +0000181static void parse_args(int argc, char **argv, struct options *op)
182{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000183 char *ts;
Robert Griebl1fca5582002-06-04 20:45:46 +0000184
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000185 op->flags = getopt32(argv, opt_string,
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000186 &(op->initstring), &fakehost, &(op->issue),
187 &(op->login), &ts);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000188 if (op->flags & F_INITSTRING) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000189 const char *p = op->initstring;
190 char *q;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000191
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000192 op->initstring = q = xstrdup(op->initstring);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000193 /* copy optarg into op->initstring decoding \ddd
194 octal codes into chars */
195 while (*p) {
196 if (*p == '\\') {
197 p++;
198 *q++ = bb_process_escape_sequence(&p);
199 } else {
200 *q++ = *p++;
Robert Griebl1fca5582002-06-04 20:45:46 +0000201 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000202 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000203 *q = '\0';
204 }
205 op->flags ^= F_ISSUE; /* revert flag show /etc/issue */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000206 if (op->flags & F_TIMEOUT) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000207 op->timeout = xatoul_range(ts, 1, INT_MAX);
Robert Griebl1fca5582002-06-04 20:45:46 +0000208 }
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000209 argv += optind;
210 argc -= optind;
Robert Griebl1fca5582002-06-04 20:45:46 +0000211 debug("after getopt loop\n");
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000212 if (argc < 2) /* check parameter count */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000213 bb_show_usage();
Robert Griebl1fca5582002-06-04 20:45:46 +0000214
215 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000216 if (isdigit(argv[0][0])) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000217 /* a number first, assume it's a speed (BSD style) */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000218 parse_speeds(op, argv[0]); /* baud rate(s) */
219 op->tty = argv[1]; /* tty name */
Robert Griebl1fca5582002-06-04 20:45:46 +0000220 } else {
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000221 op->tty = argv[0]; /* tty name */
222 parse_speeds(op, argv[1]); /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000223 }
224
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000225 if (argv[2])
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000226 setenv("TERM", argv[2], 1);
Robert Griebl1fca5582002-06-04 20:45:46 +0000227
228 debug("exiting parseargs\n");
229}
230
Robert Griebl1fca5582002-06-04 20:45:46 +0000231/* open_tty - set up tty as standard { input, output, error } */
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000232static void open_tty(const char *tty, struct termios *tp, int local)
Robert Griebl1fca5582002-06-04 20:45:46 +0000233{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000234 int chdir_to_root = 0;
235
Robert Griebl1fca5582002-06-04 20:45:46 +0000236 /* Set up new standard input, unless we are given an already opened port. */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000237 if (NOT_LONE_DASH(tty)) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000238 struct stat st;
Mike Frysingerb3810092005-07-01 01:29:44 +0000239 int fd;
Robert Griebl1fca5582002-06-04 20:45:46 +0000240
241 /* Sanity checks... */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000242 xchdir("/dev");
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000243 chdir_to_root = 1;
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000244 xstat(tty, &st);
Robert Griebl1fca5582002-06-04 20:45:46 +0000245 if ((st.st_mode & S_IFMT) != S_IFCHR)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000246 bb_error_msg_and_die("%s: not a character device", tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000247
248 /* Open the tty as standard input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000249 debug("open(2)\n");
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000250 fd = xopen(tty, O_RDWR | O_NONBLOCK);
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000251 xdup2(fd, 0);
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000252 while (fd > 2)
253 close(fd--);
Robert Griebl1fca5582002-06-04 20:45:46 +0000254 } else {
Robert Griebl1fca5582002-06-04 20:45:46 +0000255 /*
256 * Standard input should already be connected to an open port. Make
257 * sure it is open for read/write.
258 */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000259 if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR)
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000260 bb_error_msg_and_die("stdin is not open for read/write");
Robert Griebl1fca5582002-06-04 20:45:46 +0000261 }
262
Mike Frysingerb3810092005-07-01 01:29:44 +0000263 /* Replace current standard output/error fd's with new ones */
Robert Griebl1fca5582002-06-04 20:45:46 +0000264 debug("duping\n");
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000265 xdup2(0, 1);
266 xdup2(0, 2);
Robert Griebl1fca5582002-06-04 20:45:46 +0000267
268 /*
269 * The following ioctl will fail if stdin is not a tty, but also when
270 * there is noise on the modem control lines. In the latter case, the
271 * common course of action is (1) fix your cables (2) give the modem more
272 * time to properly reset after hanging up. SunOS users can achieve (2)
273 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
274 * 5 seconds seems to be a good value.
275 */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000276 ioctl_or_perror_and_die(0, TCGETS, tp, "%s: TCGETS", tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000277
278 /*
279 * It seems to be a terminal. Set proper protections and ownership. Mode
280 * 0622 is suitable for SYSV <4 because /bin/login does not change
281 * protections. SunOS 4 login will change the protections to 0620 (write
282 * access for group tty) after the login has succeeded.
283 */
284
285#ifdef DEBIAN
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000286#warning Debian /dev/vcs[a]NN hack is deprecated and will be removed
Robert Griebl1fca5582002-06-04 20:45:46 +0000287 {
288 /* tty to root.dialout 660 */
289 struct group *gr;
290 int id;
291
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000292 gr = getgrnam("dialout");
293 id = gr ? gr->gr_gid : 0;
Robert Griebl1fca5582002-06-04 20:45:46 +0000294 chown(tty, 0, id);
295 chmod(tty, 0660);
296
297 /* vcs,vcsa to root.sys 600 */
298 if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) {
299 char *vcs, *vcsa;
300
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000301 vcs = xstrdup(tty);
302 vcsa = xmalloc(strlen(tty) + 2);
Robert Griebl1fca5582002-06-04 20:45:46 +0000303 strcpy(vcs, "vcs");
304 strcpy(vcs + 3, tty + 3);
305 strcpy(vcsa, "vcsa");
306 strcpy(vcsa + 4, tty + 3);
307
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000308 gr = getgrnam("sys");
309 id = gr ? gr->gr_gid : 0;
Robert Griebl1fca5582002-06-04 20:45:46 +0000310 chown(vcs, 0, id);
311 chmod(vcs, 0600);
312 chown(vcsa, 0, id);
313 chmod(vcs, 0600);
314
315 free(vcs);
316 free(vcsa);
317 }
318 }
319#else
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000320 if (NOT_LONE_DASH(tty)) {
321 chown(tty, 0, 0); /* 0:0 */
322 chmod(tty, 0622); /* crw--w--w- */
323 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000324#endif
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000325 if (chdir_to_root)
326 xchdir("/");
Robert Griebl1fca5582002-06-04 20:45:46 +0000327}
328
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000329/* termios_init - initialize termios settings */
330static void termios_init(struct termios *tp, int speed, struct options *op)
Robert Griebl1fca5582002-06-04 20:45:46 +0000331{
Robert Griebl1fca5582002-06-04 20:45:46 +0000332 /*
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000333 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000334 * Special characters are set after we have read the login name; all
335 * reads will be done in raw mode anyway. Errors will be dealt with
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000336 * later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000337 */
338#ifdef __linux__
339 /* flush input and output queues, important for modems! */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000340 ioctl(0, TCFLSH, TCIOFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000341#endif
342
343 tp->c_cflag = CS8 | HUPCL | CREAD | speed;
344 if (op->flags & F_LOCAL) {
345 tp->c_cflag |= CLOCAL;
346 }
347
Rob Landleyf78ab532006-08-24 20:00:44 +0000348 tp->c_iflag = tp->c_lflag = tp->c_line = 0;
349 tp->c_oflag = OPOST | ONLCR;
Robert Griebl1fca5582002-06-04 20:45:46 +0000350 tp->c_cc[VMIN] = 1;
351 tp->c_cc[VTIME] = 0;
352
353 /* Optionally enable hardware flow control */
354
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000355#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000356 if (op->flags & F_RTSCTS)
357 tp->c_cflag |= CRTSCTS;
358#endif
359
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000360 ioctl(0, TCSETS, tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000361
362 /* go to blocking input even in local mode */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000363 ndelay_off(0);
Robert Griebl1fca5582002-06-04 20:45:46 +0000364
365 debug("term_io 2\n");
366}
367
368/* auto_baud - extract baud rate from modem status message */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000369static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000370{
371 int speed;
372 int vmin;
373 unsigned iflag;
Robert Griebl1fca5582002-06-04 20:45:46 +0000374 char *bp;
375 int nread;
376
377 /*
378 * This works only if the modem produces its status code AFTER raising
379 * the DCD line, and if the computer is fast enough to set the proper
380 * baud rate before the message has gone by. We expect a message of the
381 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000382 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000383 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000384 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000385 * The number is interpreted as the baud rate of the incoming call. If the
386 * modem does not tell us the baud rate within one second, we will keep
387 * using the current baud rate. It is advisable to enable BREAK
388 * processing (comma-separated list of baud rates) if the processing of
389 * modem status messages is enabled.
390 */
391
392 /*
393 * Use 7-bit characters, don't block if input queue is empty. Errors will
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000394 * be dealt with later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000395 */
396
397 iflag = tp->c_iflag;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000398 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
Robert Griebl1fca5582002-06-04 20:45:46 +0000399 vmin = tp->c_cc[VMIN];
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000400 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000401 ioctl(0, TCSETS, tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000402
403 /*
404 * Wait for a while, then read everything the modem has said so far and
405 * try to extract the speed of the dial-in call.
406 */
407
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000408 sleep(1);
Denis Vlasenko703aa132006-10-23 22:43:02 +0000409 nread = read(0, buf, size_buf - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000410 if (nread > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000411 buf[nread] = '\0';
412 for (bp = buf; bp < buf + nread; bp++) {
413 if (isascii(*bp) && isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000414 speed = bcode(bp);
415 if (speed) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000416 tp->c_cflag &= ~CBAUD;
417 tp->c_cflag |= speed;
418 }
419 break;
420 }
421 }
422 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000423 /* Restore terminal settings. Errors will be dealt with later on. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000424
425 tp->c_iflag = iflag;
426 tp->c_cc[VMIN] = vmin;
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000427 ioctl(0, TCSETS, tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000428}
429
Robert Griebl1fca5582002-06-04 20:45:46 +0000430/* next_speed - select next baud rate */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000431static void next_speed(struct termios *tp, struct options *op)
Robert Griebl1fca5582002-06-04 20:45:46 +0000432{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000433 static int baud_index = FIRST_SPEED; /* current speed index */
Robert Griebl1fca5582002-06-04 20:45:46 +0000434
435 baud_index = (baud_index + 1) % op->numspeed;
436 tp->c_cflag &= ~CBAUD;
437 tp->c_cflag |= op->speeds[baud_index];
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000438 ioctl(0, TCSETS, tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000439}
440
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000441
442/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000443static void do_prompt(struct options *op, struct termios *tp)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000444{
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000445#ifdef ISSUE
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000446 print_login_issue(op->issue, op->tty);
447#endif
448 print_login_prompt();
449}
450
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000451#ifdef HANDLE_ALLCAPS
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000452/* caps_lock - string contains upper case without lower case */
453/* returns 1 if true, 0 if false */
454static int caps_lock(const char *s)
455{
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000456 while (*s)
457 if (islower(*s++))
458 return 0;
459 return 1;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000460}
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000461#endif
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000462
Robert Griebl1fca5582002-06-04 20:45:46 +0000463/* get_logname - get user name, establish parity, speed, erase, kill, eol */
464/* return NULL on failure, logname on success */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000465static char *get_logname(char *logname, unsigned size_logname,
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000466 struct options *op, struct chardata *cp, struct termios *tp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000467{
Robert Griebl1fca5582002-06-04 20:45:46 +0000468 char *bp;
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000469 char c; /* input character, full eight bits */
470 char ascval; /* low 7 bits of input character */
471 int bits; /* # of "1" bits per character */
472 int mask; /* mask with 1 bit up */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000473 static const char erase[][3] = { /* backspace-space-backspace */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000474 "\010\040\010", /* space parity */
475 "\010\040\010", /* odd parity */
476 "\210\240\210", /* even parity */
477 "\210\240\210", /* no parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000478 };
479
480 /* Initialize kill, erase, parity etc. (also after switching speeds). */
481
482 *cp = init_chardata;
483
484 /* Flush pending input (esp. after parsing or switching the baud rate). */
485
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000486 sleep(1);
487 ioctl(0, TCFLSH, TCIFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000488
489 /* Prompt for and read a login name. */
490
Denis Vlasenko703aa132006-10-23 22:43:02 +0000491 logname[0] = '\0';
492 while (!logname[0]) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000493
494 /* Write issue file and prompt, with "parity" bit == 0. */
495
496 do_prompt(op, tp);
497
498 /* Read name, watch for break, parity, erase, kill, end-of-line. */
499
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000500 bp = logname;
Denis Vlasenko703aa132006-10-23 22:43:02 +0000501 cp->eol = '\0';
502 while (cp->eol == '\0') {
Robert Griebl1fca5582002-06-04 20:45:46 +0000503
504 /* Do not report trivial EINTR/EIO errors. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000505 if (read(0, &c, 1) < 1) {
506 if (errno == EINTR || errno == EIO)
507 exit(0);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000508 bb_perror_msg_and_die("%s: read", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000509 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000510
Denis Vlasenko703aa132006-10-23 22:43:02 +0000511 /* Do BREAK handling elsewhere. */
512 if (c == '\0' && op->numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000513 return NULL;
514
515 /* Do parity bit handling. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000516 ascval = c & 0177;
517 if (c != ascval) { /* "parity" bit on ? */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000518 bits = 1;
519 mask = 1;
520 while (mask & 0177) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000521 if (mask & ascval)
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000522 bits++; /* count "1" bits */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000523 mask <<= 1;
524 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000525 /* ... |= 2 - even, 1 - odd */
526 cp->parity |= 2 - (bits & 1);
Robert Griebl1fca5582002-06-04 20:45:46 +0000527 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000528
Denis Vlasenko703aa132006-10-23 22:43:02 +0000529 /* Do erase, kill and end-of-line processing. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000530 switch (ascval) {
531 case CR:
532 case NL:
Denis Vlasenko703aa132006-10-23 22:43:02 +0000533 *bp = '\0'; /* terminate logname */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000534 cp->eol = ascval; /* set end-of-line char */
Robert Griebl1fca5582002-06-04 20:45:46 +0000535 break;
536 case BS:
537 case DEL:
538 case '#':
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000539 cp->erase = ascval; /* set erase character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000540 if (bp > logname) {
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000541 write(1, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000542 bp--;
543 }
544 break;
545 case CTL('U'):
546 case '@':
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000547 cp->kill = ascval; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000548 while (bp > logname) {
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000549 write(1, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000550 bp--;
551 }
552 break;
553 case CTL('D'):
554 exit(0);
555 default:
556 if (!isascii(ascval) || !isprint(ascval)) {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000557 /* ignore garbage characters */
558 } else if (bp - logname >= size_logname - 1) {
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000559 bb_error_msg_and_die("%s: input overrun", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000560 } else {
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000561 write(1, &c, 1); /* echo the character */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000562 *bp++ = ascval; /* and store it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000563 }
564 break;
565 }
566 }
567 }
568 /* Handle names with upper case and no lower case. */
569
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000570#ifdef HANDLE_ALLCAPS
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000571 cp->capslock = caps_lock(logname);
572 if (cp->capslock) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000573 for (bp = logname; *bp; bp++)
574 if (isupper(*bp))
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000575 *bp = tolower(*bp); /* map name to lower case */
Robert Griebl1fca5582002-06-04 20:45:46 +0000576 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000577#endif
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000578 return logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000579}
580
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000581/* termios_final - set the final tty mode bits */
582static void termios_final(struct options *op, struct termios *tp, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000583{
584 /* General terminal-independent stuff. */
585
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000586 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
Robert Griebl1fca5582002-06-04 20:45:46 +0000587 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
588 /* no longer| ECHOCTL | ECHOPRT */
589 tp->c_oflag |= OPOST;
590 /* tp->c_cflag = 0; */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000591 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
592 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
593 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000594 tp->c_cc[VEOL] = DEF_EOL;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000595 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000596
597 /* Account for special characters seen in input. */
598
599 if (cp->eol == CR) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000600 tp->c_iflag |= ICRNL; /* map CR in input to NL */
601 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
Robert Griebl1fca5582002-06-04 20:45:46 +0000602 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000603 tp->c_cc[VERASE] = cp->erase; /* set erase character */
604 tp->c_cc[VKILL] = cp->kill; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000605
606 /* Account for the presence or absence of parity bits in input. */
607
608 switch (cp->parity) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000609 case 0: /* space (always 0) parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000610 break;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000611 case 1: /* odd parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000612 tp->c_cflag |= PARODD;
613 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000614 case 2: /* even parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000615 tp->c_cflag |= PARENB;
616 tp->c_iflag |= INPCK | ISTRIP;
617 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000618 case (1 | 2): /* no parity bit */
Robert Griebl1fca5582002-06-04 20:45:46 +0000619 tp->c_cflag &= ~CSIZE;
620 tp->c_cflag |= CS7;
621 break;
622 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000623
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000624 /* Account for upper case without lower case. */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000625#ifdef HANDLE_ALLCAPS
Robert Griebl1fca5582002-06-04 20:45:46 +0000626 if (cp->capslock) {
627 tp->c_iflag |= IUCLC;
628 tp->c_lflag |= XCASE;
629 tp->c_oflag |= OLCUC;
630 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000631#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000632 /* Optionally enable hardware flow control */
633
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000634#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000635 if (op->flags & F_RTSCTS)
636 tp->c_cflag |= CRTSCTS;
637#endif
638
639 /* Finally, make the new settings effective */
640
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000641 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000642}
643
Robert Griebl1fca5582002-06-04 20:45:46 +0000644
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000645#ifdef SYSV_STYLE
646#if ENABLE_FEATURE_UTMP
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000647/* update_utmp - update our utmp entry */
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000648static void update_utmp(const char *line)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000649{
650 struct utmp ut;
651 struct utmp *utp;
652 time_t t;
653 int mypid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000654
655 /*
656 * The utmp file holds miscellaneous information about things started by
657 * /sbin/init and other system-related events. Our purpose is to update
658 * the utmp entry for the current process, in particular the process type
659 * and the tty line we are listening to. Return successfully only if the
660 * utmp file can be opened for update, and if we are able to find our
661 * entry in the utmp file.
662 */
663 if (access(_PATH_UTMP, R_OK|W_OK) == -1) {
664 close(creat(_PATH_UTMP, 0664));
Robert Griebl1fca5582002-06-04 20:45:46 +0000665 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000666 utmpname(_PATH_UTMP);
667 setutent();
668 while ((utp = getutent())
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000669 && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid))
670 /* nothing */;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000671
672 if (utp) {
673 memcpy(&ut, utp, sizeof(ut));
674 } else {
675 /* some inits don't initialize utmp... */
676 memset(&ut, 0, sizeof(ut));
Rob Landleycf7577d2006-06-25 22:59:31 +0000677 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000678 }
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000679 /* endutent(); */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000680
Rob Landleycf7577d2006-06-25 22:59:31 +0000681 strcpy(ut.ut_user, "LOGIN");
682 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000683 if (fakehost)
Rob Landleycf7577d2006-06-25 22:59:31 +0000684 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000685 time(&t);
686 ut.ut_time = t;
687 ut.ut_type = LOGIN_PROCESS;
688 ut.ut_pid = mypid;
689
690 pututline(&ut);
691 endutent();
692
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000693#if ENABLE_FEATURE_WTMP
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000694 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1)
695 close(creat(bb_path_wtmp_file, 0664));
696 updwtmp(bb_path_wtmp_file, &ut);
697#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000698}
699
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000700#endif /* CONFIG_FEATURE_UTMP */
701#endif /* SYSV_STYLE */
Robert Griebl1fca5582002-06-04 20:45:46 +0000702
Robert Griebl1fca5582002-06-04 20:45:46 +0000703
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000704int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000705int getty_main(int argc, char **argv)
706{
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000707 int nullfd;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000708 char *logname = NULL; /* login name, given to /bin/login */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000709 /* Merging these into "struct local" may _seem_ to reduce
710 * parameter passing, but today's gcc will inline
711 * statics which are called once anyway, so don't do that */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000712 struct chardata chardata; /* set by get_logname() */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000713 struct termios termios; /* terminal mode bits */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000714 struct options options = {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000715 0, /* show /etc/issue (SYSV_STYLE) */
716 0, /* no timeout */
717 _PATH_LOGIN, /* default login program */
718 "tty1", /* default tty line */
719 "", /* modem init string */
720#ifdef ISSUE
721 ISSUE, /* default issue file */
Robert Griebl1fca5582002-06-04 20:45:46 +0000722#else
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000723 NULL,
724#endif
725 0, /* no baud rates known yet */
726 };
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000727
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000728 /* Already too late because of theoretical
729 * possibility of getty --help somehow triggered
730 * inadvertently before we reach this. Oh well. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000731 logmode = LOGMODE_NONE;
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000732 setsid();
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000733 nullfd = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000734 /* dup2(nullfd, 0); - no, because of possible "getty - 9600" */
735 /* open_tty() will take care of fd# 0 anyway */
736 dup2(nullfd, 1);
737 dup2(nullfd, 2);
738 while (nullfd > 2) close(nullfd--);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000739 /* We want special flavor of error_msg_and_die */
740 die_sleep = 10;
741 msg_eol = "\r\n";
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000742 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000743 logmode = LOGMODE_BOTH;
744
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000745#ifdef DEBUGGING
Rob Landleyd921b2e2006-08-03 15:41:12 +0000746 dbf = xfopen(DEBUGTERM, "w");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000747
748 {
749 int i;
750
751 for (i = 1; i < argc; i++) {
752 debug(argv[i]);
753 debug("\n");
754 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000755 }
756#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000757
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000758 /* Parse command-line arguments. */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000759 parse_args(argc, argv, &options);
760
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000761#ifdef SYSV_STYLE
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000762#if ENABLE_FEATURE_UTMP
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000763 /* Update the utmp file. */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000764 update_utmp(options.tty);
765#endif
766#endif
767
768 debug("calling open_tty\n");
769 /* Open the tty as standard { input, output, error }. */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000770 open_tty(options.tty, &termios, options.flags & F_LOCAL);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000771
772#ifdef __linux__
773 {
774 int iv;
775
776 iv = getpid();
777 ioctl(0, TIOCSPGRP, &iv);
778 }
779#endif
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000780 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
781 debug("calling termios_init\n");
782 termios_init(&termios, options.speeds[FIRST_SPEED], &options);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000783
784 /* write the modem init string and DON'T flush the buffers */
785 if (options.flags & F_INITSTRING) {
786 debug("writing init string\n");
787 write(1, options.initstring, strlen(options.initstring));
788 }
789
790 if (!(options.flags & F_LOCAL)) {
791 /* go to blocking write mode unless -L is specified */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000792 ndelay_off(1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000793 }
794
795 /* Optionally detect the baud rate from the modem status message. */
796 debug("before autobaud\n");
797 if (options.flags & F_PARSE)
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000798 auto_baud(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), &termios);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000799
800 /* Set the optional timer. */
801 if (options.timeout)
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000802 alarm(options.timeout);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000803
804 /* optionally wait for CR or LF before writing /etc/issue */
805 if (options.flags & F_WAITCRLF) {
806 char ch;
807
808 debug("waiting for cr-lf\n");
809 while (read(0, &ch, 1) == 1) {
810 ch &= 0x7f; /* strip "parity bit" */
811#ifdef DEBUGGING
812 fprintf(dbf, "read %c\n", ch);
813#endif
814 if (ch == '\n' || ch == '\r')
815 break;
816 }
817 }
818
819 chardata = init_chardata;
820 if (!(options.flags & F_NOPROMPT)) {
821 /* Read the login name. */
822 debug("reading login name\n");
Denis Vlasenko703aa132006-10-23 22:43:02 +0000823 logname = get_logname(bb_common_bufsiz1, sizeof(bb_common_bufsiz1),
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000824 &options, &chardata, &termios);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000825 while (logname == NULL)
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000826 next_speed(&termios, &options);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000827 }
828
829 /* Disable timer. */
830
831 if (options.timeout)
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000832 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000833
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000834 /* Finalize the termios settings. */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000835
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000836 termios_final(&options, &termios, &chardata);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000837
838 /* Now the newline character should be properly written. */
839
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000840 write(1, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000841
842 /* Let the login program take care of password validation. */
843
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000844 execl(options.login, options.login, "--", logname, (char *) 0);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000845 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000846}