source: MondoRescue/branches/2.2.5/mindi-busybox/loginutils/getty.c@ 1765

Last change on this file since 1765 was 1765, checked in by Bruno Cornec, 16 years ago

Update to busybox 1.7.2

File size: 23.2 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/* agetty.c - another getty program for Linux. By W. Z. Venema 1989
[1765]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 *
9 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
10 * - added Native Language Support
[821]11
[1765]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.
16 *
17 */
[821]18
[1765]19#include "libbb.h"
20#include <syslog.h>
[821]21
[1765]22#if ENABLE_FEATURE_UTMP
[821]23#include <utmp.h>
24#endif
25
[1765]26/*
27 * Some heuristics to find out what environment we are in: if it is not
28 * System V, assume it is SunOS 4.
29 */
[821]30#ifdef LOGIN_PROCESS /* defined in System V utmp.h */
31#define SYSV_STYLE /* select System V style getty */
[1765]32#include <sys/utsname.h>
33#include <time.h>
34#if ENABLE_FEATURE_WTMP
[821]35extern void updwtmp(const char *filename, const struct utmp *ut);
[1765]36static void update_utmp(const char *line);
[821]37#endif
38#endif /* LOGIN_PROCESS */
39
[1765]40/*
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 */
[821]47
[1765]48/* I doubt there are systems which still need this */
49#undef HANDLE_ALLCAPS
[821]50
[1765]51#define _PATH_LOGIN "/bin/login"
52
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 */
[821]57#define ISSUE "/etc/issue" /* displayed before the login prompt */
58
59/* Some shorthands for control characters. */
60#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 */
65
66/* Defaults for line-editing etc. characters; you may want to change this. */
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 */
74
[1765]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 */
[821]79#define FIRST_SPEED 0
80
81/* Storage for command-line options. */
82
83#define MAX_SPEED 10 /* max. nr. of baud rates */
84
85struct options {
86 int flags; /* toggle switches, see below */
[1765]87 unsigned timeout; /* time-out period */
88 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 */
[821]92 int numspeed; /* number of baud rates to try */
93 int speeds[MAX_SPEED]; /* baud rates to be tried */
94};
95
[1765]96static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
[821]97#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! */
108
109/* Storage for things detected while the login name was read. */
110struct chardata {
[1765]111 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
[821]118};
119
120/* Initial values for the above. */
[1765]121static const struct chardata init_chardata = {
[821]122 DEF_ERASE, /* default erase character */
123 DEF_KILL, /* default kill character */
124 13, /* default eol char */
125 0, /* space parity */
[1765]126#ifdef HANDLE_ALLCAPS
[821]127 0, /* no capslock */
128#endif
129};
130
131/* 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"
[1765]140static FILE *dbf;
[821]141#else
[1765]142#define debug(s) /* nothing */
[821]143#endif
144
145
146/* bcode - convert speed string to speed code; return 0 on failure */
147static int bcode(const char *s)
148{
149 int r;
[1765]150 unsigned value = bb_strtou(s, NULL, 10);
151 if (errno) {
[821]152 return -1;
153 }
[1765]154 r = tty_value_to_baud(value);
155 if (r > 0) {
[821]156 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, ",")) {
169 if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0)
[1765]170 bb_error_msg_and_die("bad speed: %s", cp);
[821]171 if (op->numspeed > MAX_SPEED)
[1765]172 bb_error_msg_and_die("too many alternate speeds");
[821]173 }
174 debug("exiting parsespeeds\n");
175}
176
177
[1765]178/* parse_args - parse command-line arguments */
[821]179static void parse_args(int argc, char **argv, struct options *op)
180{
181 char *ts;
182
[1765]183 op->flags = getopt32(argv, opt_string,
[821]184 &(op->initstring), &fakehost, &(op->issue),
185 &(op->login), &ts);
[1765]186 if (op->flags & F_INITSTRING) {
[821]187 const char *p = op->initstring;
188 char *q;
189
[1765]190 op->initstring = q = xstrdup(op->initstring);
[821]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++;
199 }
200 }
201 *q = '\0';
202 }
203 op->flags ^= F_ISSUE; /* revert flag show /etc/issue */
[1765]204 if (op->flags & F_TIMEOUT) {
205 op->timeout = xatoul_range(ts, 1, INT_MAX);
[821]206 }
[1765]207 argv += optind;
208 argc -= optind;
[821]209 debug("after getopt loop\n");
[1765]210 if (argc < 2) /* check parameter count */
[821]211 bb_show_usage();
212
213 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
[1765]214 if (isdigit(argv[0][0])) {
[821]215 /* a number first, assume it's a speed (BSD style) */
[1765]216 parse_speeds(op, argv[0]); /* baud rate(s) */
217 op->tty = argv[1]; /* tty name */
[821]218 } else {
[1765]219 op->tty = argv[0]; /* tty name */
220 parse_speeds(op, argv[1]); /* baud rate(s) */
[821]221 }
222
[1765]223 if (argv[2])
224 setenv("TERM", argv[2], 1);
[821]225
226 debug("exiting parseargs\n");
227}
228
229/* open_tty - set up tty as standard { input, output, error } */
[1765]230static void open_tty(const char *tty, struct termios *tp, int local)
[821]231{
232 int chdir_to_root = 0;
233
234 /* Set up new standard input, unless we are given an already opened port. */
[1765]235 if (NOT_LONE_DASH(tty)) {
[821]236 struct stat st;
237 int fd;
238
239 /* Sanity checks... */
[1765]240 xchdir("/dev");
[821]241 chdir_to_root = 1;
[1765]242 xstat(tty, &st);
[821]243 if ((st.st_mode & S_IFMT) != S_IFCHR)
[1765]244 bb_error_msg_and_die("%s: not a character device", tty);
[821]245
246 /* Open the tty as standard input. */
247 debug("open(2)\n");
[1765]248 fd = xopen(tty, O_RDWR | O_NONBLOCK);
249 xdup2(fd, 0);
250 while (fd > 2)
251 close(fd--);
[821]252 } else {
253 /*
254 * Standard input should already be connected to an open port. Make
255 * sure it is open for read/write.
256 */
[1765]257 if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR)
258 bb_error_msg_and_die("stdin is not open for read/write");
[821]259 }
260
261 /* Replace current standard output/error fd's with new ones */
262 debug("duping\n");
[1765]263 xdup2(0, 1);
264 xdup2(0, 2);
[821]265
266 /*
267 * The following ioctl will fail if stdin is not a tty, but also when
268 * there is noise on the modem control lines. In the latter case, the
269 * common course of action is (1) fix your cables (2) give the modem more
270 * time to properly reset after hanging up. SunOS users can achieve (2)
271 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
272 * 5 seconds seems to be a good value.
273 */
[1765]274 ioctl_or_perror_and_die(0, TCGETS, tp, "%s: TCGETS", tty);
[821]275
276 /*
277 * It seems to be a terminal. Set proper protections and ownership. Mode
278 * 0622 is suitable for SYSV <4 because /bin/login does not change
279 * protections. SunOS 4 login will change the protections to 0620 (write
280 * access for group tty) after the login has succeeded.
281 */
282
283#ifdef DEBIAN
[1765]284#warning Debian /dev/vcs[a]NN hack is deprecated and will be removed
[821]285 {
286 /* tty to root.dialout 660 */
287 struct group *gr;
288 int id;
289
[1765]290 gr = getgrnam("dialout");
291 id = gr ? gr->gr_gid : 0;
[821]292 chown(tty, 0, id);
293 chmod(tty, 0660);
294
295 /* vcs,vcsa to root.sys 600 */
296 if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) {
297 char *vcs, *vcsa;
298
[1765]299 vcs = xstrdup(tty);
300 vcsa = xmalloc(strlen(tty) + 2);
[821]301 strcpy(vcs, "vcs");
302 strcpy(vcs + 3, tty + 3);
303 strcpy(vcsa, "vcsa");
304 strcpy(vcsa + 4, tty + 3);
305
[1765]306 gr = getgrnam("sys");
307 id = gr ? gr->gr_gid : 0;
[821]308 chown(vcs, 0, id);
309 chmod(vcs, 0600);
310 chown(vcsa, 0, id);
311 chmod(vcs, 0600);
312
313 free(vcs);
314 free(vcsa);
315 }
316 }
317#else
[1765]318 if (NOT_LONE_DASH(tty)) {
319 chown(tty, 0, 0); /* 0:0 */
320 chmod(tty, 0622); /* crw--w--w- */
321 }
[821]322#endif
[1765]323 if (chdir_to_root)
324 xchdir("/");
[821]325}
326
[1765]327/* termios_init - initialize termios settings */
328static void termios_init(struct termios *tp, int speed, struct options *op)
[821]329{
330 /*
[1765]331 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
[821]332 * Special characters are set after we have read the login name; all
333 * reads will be done in raw mode anyway. Errors will be dealt with
[1765]334 * later on.
[821]335 */
336#ifdef __linux__
337 /* flush input and output queues, important for modems! */
[1765]338 ioctl(0, TCFLSH, TCIOFLUSH);
[821]339#endif
340
341 tp->c_cflag = CS8 | HUPCL | CREAD | speed;
342 if (op->flags & F_LOCAL) {
343 tp->c_cflag |= CLOCAL;
344 }
345
[902]346 tp->c_iflag = tp->c_lflag = tp->c_line = 0;
347 tp->c_oflag = OPOST | ONLCR;
[821]348 tp->c_cc[VMIN] = 1;
349 tp->c_cc[VTIME] = 0;
350
351 /* Optionally enable hardware flow control */
352
353#ifdef CRTSCTS
354 if (op->flags & F_RTSCTS)
355 tp->c_cflag |= CRTSCTS;
356#endif
357
[1765]358 ioctl(0, TCSETS, tp);
[821]359
360 /* go to blocking input even in local mode */
[1765]361 ndelay_off(0);
[821]362
363 debug("term_io 2\n");
364}
365
366/* auto_baud - extract baud rate from modem status message */
[1765]367static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
[821]368{
369 int speed;
370 int vmin;
371 unsigned iflag;
372 char *bp;
373 int nread;
374
375 /*
376 * This works only if the modem produces its status code AFTER raising
377 * the DCD line, and if the computer is fast enough to set the proper
378 * baud rate before the message has gone by. We expect a message of the
379 * following format:
380 *
381 * <junk><number><junk>
382 *
383 * The number is interpreted as the baud rate of the incoming call. If the
384 * modem does not tell us the baud rate within one second, we will keep
385 * using the current baud rate. It is advisable to enable BREAK
386 * processing (comma-separated list of baud rates) if the processing of
387 * modem status messages is enabled.
388 */
389
390 /*
391 * Use 7-bit characters, don't block if input queue is empty. Errors will
[1765]392 * be dealt with later on.
[821]393 */
394
395 iflag = tp->c_iflag;
396 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
397 vmin = tp->c_cc[VMIN];
398 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
[1765]399 ioctl(0, TCSETS, tp);
[821]400
401 /*
402 * Wait for a while, then read everything the modem has said so far and
403 * try to extract the speed of the dial-in call.
404 */
405
[1765]406 sleep(1);
407 nread = read(0, buf, size_buf - 1);
408 if (nread > 0) {
[821]409 buf[nread] = '\0';
410 for (bp = buf; bp < buf + nread; bp++) {
411 if (isascii(*bp) && isdigit(*bp)) {
[1765]412 speed = bcode(bp);
413 if (speed) {
[821]414 tp->c_cflag &= ~CBAUD;
415 tp->c_cflag |= speed;
416 }
417 break;
418 }
419 }
420 }
[1765]421 /* Restore terminal settings. Errors will be dealt with later on. */
[821]422
423 tp->c_iflag = iflag;
424 tp->c_cc[VMIN] = vmin;
[1765]425 ioctl(0, TCSETS, tp);
[821]426}
427
428/* next_speed - select next baud rate */
[1765]429static void next_speed(struct termios *tp, struct options *op)
[821]430{
431 static int baud_index = FIRST_SPEED; /* current speed index */
432
433 baud_index = (baud_index + 1) % op->numspeed;
434 tp->c_cflag &= ~CBAUD;
435 tp->c_cflag |= op->speeds[baud_index];
[1765]436 ioctl(0, TCSETS, tp);
[821]437}
438
439
440/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
[1765]441static void do_prompt(struct options *op, struct termios *tp)
[821]442{
[1765]443#ifdef ISSUE
[821]444 print_login_issue(op->issue, op->tty);
445#endif
446 print_login_prompt();
447}
448
[1765]449#ifdef HANDLE_ALLCAPS
[821]450/* caps_lock - string contains upper case without lower case */
451/* returns 1 if true, 0 if false */
452static int caps_lock(const char *s)
453{
[1765]454 while (*s)
455 if (islower(*s++))
456 return 0;
457 return 1;
[821]458}
[1765]459#endif
[821]460
461/* get_logname - get user name, establish parity, speed, erase, kill, eol */
462/* return NULL on failure, logname on success */
[1765]463static char *get_logname(char *logname, unsigned size_logname,
464 struct options *op, struct chardata *cp, struct termios *tp)
[821]465{
466 char *bp;
467 char c; /* input character, full eight bits */
468 char ascval; /* low 7 bits of input character */
469 int bits; /* # of "1" bits per character */
470 int mask; /* mask with 1 bit up */
[1765]471 static const char erase[][3] = { /* backspace-space-backspace */
[821]472 "\010\040\010", /* space parity */
473 "\010\040\010", /* odd parity */
474 "\210\240\210", /* even parity */
475 "\210\240\210", /* no parity */
476 };
477
478 /* Initialize kill, erase, parity etc. (also after switching speeds). */
479
480 *cp = init_chardata;
481
482 /* Flush pending input (esp. after parsing or switching the baud rate). */
483
[1765]484 sleep(1);
485 ioctl(0, TCFLSH, TCIFLUSH);
[821]486
487 /* Prompt for and read a login name. */
488
[1765]489 logname[0] = '\0';
490 while (!logname[0]) {
[821]491
492 /* Write issue file and prompt, with "parity" bit == 0. */
493
494 do_prompt(op, tp);
495
496 /* Read name, watch for break, parity, erase, kill, end-of-line. */
497
[1765]498 bp = logname;
499 cp->eol = '\0';
500 while (cp->eol == '\0') {
[821]501
502 /* Do not report trivial EINTR/EIO errors. */
503 if (read(0, &c, 1) < 1) {
504 if (errno == EINTR || errno == EIO)
505 exit(0);
[1765]506 bb_perror_msg_and_die("%s: read", op->tty);
[821]507 }
[1765]508
[821]509 /* Do BREAK handling elsewhere. */
[1765]510 if (c == '\0' && op->numspeed > 1)
[821]511 return NULL;
512
513 /* Do parity bit handling. */
[1765]514 ascval = c & 0177;
515 if (c != ascval) { /* "parity" bit on ? */
516 bits = 1;
517 mask = 1;
518 while (mask & 0177) {
[821]519 if (mask & ascval)
520 bits++; /* count "1" bits */
[1765]521 mask <<= 1;
522 }
523 /* ... |= 2 - even, 1 - odd */
524 cp->parity |= 2 - (bits & 1);
[821]525 }
[1765]526
[821]527 /* Do erase, kill and end-of-line processing. */
528 switch (ascval) {
529 case CR:
530 case NL:
[1765]531 *bp = '\0'; /* terminate logname */
[821]532 cp->eol = ascval; /* set end-of-line char */
533 break;
534 case BS:
535 case DEL:
536 case '#':
537 cp->erase = ascval; /* set erase character */
538 if (bp > logname) {
[1765]539 write(1, erase[cp->parity], 3);
[821]540 bp--;
541 }
542 break;
543 case CTL('U'):
544 case '@':
545 cp->kill = ascval; /* set kill character */
546 while (bp > logname) {
[1765]547 write(1, erase[cp->parity], 3);
[821]548 bp--;
549 }
550 break;
551 case CTL('D'):
552 exit(0);
553 default:
554 if (!isascii(ascval) || !isprint(ascval)) {
[1765]555 /* ignore garbage characters */
556 } else if (bp - logname >= size_logname - 1) {
557 bb_error_msg_and_die("%s: input overrun", op->tty);
[821]558 } else {
[1765]559 write(1, &c, 1); /* echo the character */
[821]560 *bp++ = ascval; /* and store it */
561 }
562 break;
563 }
564 }
565 }
566 /* Handle names with upper case and no lower case. */
567
[1765]568#ifdef HANDLE_ALLCAPS
569 cp->capslock = caps_lock(logname);
570 if (cp->capslock) {
[821]571 for (bp = logname; *bp; bp++)
572 if (isupper(*bp))
573 *bp = tolower(*bp); /* map name to lower case */
574 }
[1765]575#endif
576 return logname;
[821]577}
578
[1765]579/* termios_final - set the final tty mode bits */
580static void termios_final(struct options *op, struct termios *tp, struct chardata *cp)
[821]581{
582 /* General terminal-independent stuff. */
583
584 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
585 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
586 /* no longer| ECHOCTL | ECHOPRT */
587 tp->c_oflag |= OPOST;
588 /* tp->c_cflag = 0; */
589 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
590 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
591 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
592 tp->c_cc[VEOL] = DEF_EOL;
593 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
594
595 /* Account for special characters seen in input. */
596
597 if (cp->eol == CR) {
598 tp->c_iflag |= ICRNL; /* map CR in input to NL */
599 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
600 }
601 tp->c_cc[VERASE] = cp->erase; /* set erase character */
602 tp->c_cc[VKILL] = cp->kill; /* set kill character */
603
604 /* Account for the presence or absence of parity bits in input. */
605
606 switch (cp->parity) {
607 case 0: /* space (always 0) parity */
608 break;
609 case 1: /* odd parity */
610 tp->c_cflag |= PARODD;
611 /* FALLTHROUGH */
612 case 2: /* even parity */
613 tp->c_cflag |= PARENB;
614 tp->c_iflag |= INPCK | ISTRIP;
615 /* FALLTHROUGH */
616 case (1 | 2): /* no parity bit */
617 tp->c_cflag &= ~CSIZE;
618 tp->c_cflag |= CS7;
619 break;
620 }
[1765]621
[821]622 /* Account for upper case without lower case. */
[1765]623#ifdef HANDLE_ALLCAPS
[821]624 if (cp->capslock) {
625 tp->c_iflag |= IUCLC;
626 tp->c_lflag |= XCASE;
627 tp->c_oflag |= OLCUC;
628 }
[1765]629#endif
[821]630 /* Optionally enable hardware flow control */
631
632#ifdef CRTSCTS
633 if (op->flags & F_RTSCTS)
634 tp->c_cflag |= CRTSCTS;
635#endif
636
637 /* Finally, make the new settings effective */
638
[1765]639 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty);
[821]640}
641
642
[1765]643#ifdef SYSV_STYLE
644#if ENABLE_FEATURE_UTMP
[821]645/* update_utmp - update our utmp entry */
[1765]646static void update_utmp(const char *line)
[821]647{
648 struct utmp ut;
649 struct utmp *utp;
650 time_t t;
651 int mypid = getpid();
652
653 /*
654 * The utmp file holds miscellaneous information about things started by
655 * /sbin/init and other system-related events. Our purpose is to update
656 * the utmp entry for the current process, in particular the process type
657 * and the tty line we are listening to. Return successfully only if the
658 * utmp file can be opened for update, and if we are able to find our
659 * entry in the utmp file.
660 */
661 if (access(_PATH_UTMP, R_OK|W_OK) == -1) {
662 close(creat(_PATH_UTMP, 0664));
663 }
664 utmpname(_PATH_UTMP);
665 setutent();
666 while ((utp = getutent())
[1765]667 && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid))
668 /* nothing */;
[821]669
670 if (utp) {
671 memcpy(&ut, utp, sizeof(ut));
672 } else {
673 /* some inits don't initialize utmp... */
674 memset(&ut, 0, sizeof(ut));
675 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
676 }
[1765]677 /* endutent(); */
[821]678
679 strcpy(ut.ut_user, "LOGIN");
680 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
681 if (fakehost)
682 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
683 time(&t);
684 ut.ut_time = t;
685 ut.ut_type = LOGIN_PROCESS;
686 ut.ut_pid = mypid;
687
688 pututline(&ut);
689 endutent();
690
[1765]691#if ENABLE_FEATURE_WTMP
[821]692 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1)
693 close(creat(bb_path_wtmp_file, 0664));
694 updwtmp(bb_path_wtmp_file, &ut);
695#endif
696}
697
698#endif /* CONFIG_FEATURE_UTMP */
699#endif /* SYSV_STYLE */
700
701
[1765]702int getty_main(int argc, char **argv);
[821]703int getty_main(int argc, char **argv)
704{
[1765]705 int nullfd;
[821]706 char *logname = NULL; /* login name, given to /bin/login */
[1765]707 /* Merging these into "struct local" may _seem_ to reduce
708 * parameter passing, but today's gcc will inline
709 * statics which are called once anyway, so don't do that */
[821]710 struct chardata chardata; /* set by get_logname() */
[1765]711 struct termios termios; /* terminal mode bits */
712 struct options options = {
[821]713 0, /* show /etc/issue (SYSV_STYLE) */
714 0, /* no timeout */
715 _PATH_LOGIN, /* default login program */
716 "tty1", /* default tty line */
717 "", /* modem init string */
718#ifdef ISSUE
719 ISSUE, /* default issue file */
720#else
721 NULL,
722#endif
723 0, /* no baud rates known yet */
724 };
725
[1765]726 /* Already too late because of theoretical
727 * possibility of getty --help somehow triggered
728 * inadvertently before we reach this. Oh well. */
729 logmode = LOGMODE_NONE;
730 setsid();
731 nullfd = xopen(bb_dev_null, O_RDWR);
732 /* dup2(nullfd, 0); - no, because of possible "getty - 9600" */
733 /* open_tty() will take care of fd# 0 anyway */
734 dup2(nullfd, 1);
735 dup2(nullfd, 2);
736 while (nullfd > 2) close(nullfd--);
737 /* We want special flavor of error_msg_and_die */
738 die_sleep = 10;
739 msg_eol = "\r\n";
740 openlog(applet_name, LOG_PID, LOG_AUTH);
741 logmode = LOGMODE_BOTH;
742
[821]743#ifdef DEBUGGING
[1765]744 dbf = xfopen(DEBUGTERM, "w");
[821]745
746 {
747 int i;
748
749 for (i = 1; i < argc; i++) {
750 debug(argv[i]);
751 debug("\n");
752 }
753 }
754#endif
755
756 /* Parse command-line arguments. */
757 parse_args(argc, argv, &options);
758
[1765]759#ifdef SYSV_STYLE
760#if ENABLE_FEATURE_UTMP
[821]761 /* Update the utmp file. */
762 update_utmp(options.tty);
763#endif
764#endif
765
766 debug("calling open_tty\n");
767 /* Open the tty as standard { input, output, error }. */
[1765]768 open_tty(options.tty, &termios, options.flags & F_LOCAL);
[821]769
770#ifdef __linux__
771 {
772 int iv;
773
774 iv = getpid();
775 ioctl(0, TIOCSPGRP, &iv);
776 }
777#endif
[1765]778 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
779 debug("calling termios_init\n");
780 termios_init(&termios, options.speeds[FIRST_SPEED], &options);
[821]781
782 /* write the modem init string and DON'T flush the buffers */
783 if (options.flags & F_INITSTRING) {
784 debug("writing init string\n");
785 write(1, options.initstring, strlen(options.initstring));
786 }
787
788 if (!(options.flags & F_LOCAL)) {
789 /* go to blocking write mode unless -L is specified */
[1765]790 ndelay_off(1);
[821]791 }
792
793 /* Optionally detect the baud rate from the modem status message. */
794 debug("before autobaud\n");
795 if (options.flags & F_PARSE)
[1765]796 auto_baud(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), &termios);
[821]797
798 /* Set the optional timer. */
799 if (options.timeout)
[1765]800 alarm(options.timeout);
[821]801
802 /* optionally wait for CR or LF before writing /etc/issue */
803 if (options.flags & F_WAITCRLF) {
804 char ch;
805
806 debug("waiting for cr-lf\n");
807 while (read(0, &ch, 1) == 1) {
808 ch &= 0x7f; /* strip "parity bit" */
809#ifdef DEBUGGING
810 fprintf(dbf, "read %c\n", ch);
811#endif
812 if (ch == '\n' || ch == '\r')
813 break;
814 }
815 }
816
817 chardata = init_chardata;
818 if (!(options.flags & F_NOPROMPT)) {
819 /* Read the login name. */
820 debug("reading login name\n");
[1765]821 logname = get_logname(bb_common_bufsiz1, sizeof(bb_common_bufsiz1),
822 &options, &chardata, &termios);
823 while (logname == NULL)
824 next_speed(&termios, &options);
[821]825 }
826
827 /* Disable timer. */
828
829 if (options.timeout)
[1765]830 alarm(0);
[821]831
[1765]832 /* Finalize the termios settings. */
[821]833
[1765]834 termios_final(&options, &termios, &chardata);
[821]835
836 /* Now the newline character should be properly written. */
837
[1765]838 write(1, "\n", 1);
[821]839
840 /* Let the login program take care of password validation. */
841
[1765]842 execl(options.login, options.login, "--", logname, (char *) 0);
843 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);
[821]844}
Note: See TracBrowser for help on using the repository browser.