2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)from: main.c 8.1 (Berkeley) 6/20/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
47 #include <sys/resource.h>
49 #include <sys/ttydefaults.h>
50 #include <sys/utsname.h>
68 #include "pathnames.h"
71 * Set the amount of running time that getty should accumulate
72 * before deciding that something is wrong and exit.
74 #define GETTY_TIMEOUT 60 /* seconds */
77 #define CTRL(x) (x&037)
79 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
81 #define PPP_FRAME 0x7e /* PPP Framing character */
82 #define PPP_STATION 0xff /* "All Station" character */
83 #define PPP_ESCAPE 0x7d /* Escape Character */
84 #define PPP_CONTROL 0x03 /* PPP Control Field */
85 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
86 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
87 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
89 /* original mode; flags've been reset using values from <sys/ttydefaults.h> */
94 int crmod, digit, lower, upper;
96 char hostname[MAXHOSTNAMELEN];
97 char name[MAXLOGNAME*3];
98 char dev[] = _PATH_DEV;
102 #define TABBUFSIZ 512
104 char defent[TABBUFSIZ];
105 char tabent[TABBUFSIZ];
111 0001,0201,0201,0001,0201,0001,0001,0201,
112 0202,0004,0003,0205,0005,0206,0201,0001,
113 0201,0001,0001,0201,0001,0201,0201,0001,
114 0001,0201,0201,0001,0201,0001,0001,0201,
115 0200,0000,0000,0200,0000,0200,0200,0000,
116 0000,0200,0200,0000,0200,0000,0000,0200,
117 0000,0200,0200,0000,0200,0000,0000,0200,
118 0200,0000,0000,0200,0000,0200,0200,0000,
119 0200,0000,0000,0200,0000,0200,0200,0000,
120 0000,0200,0200,0000,0200,0000,0000,0200,
121 0000,0200,0200,0000,0200,0000,0000,0200,
122 0200,0000,0000,0200,0000,0200,0200,0000,
123 0000,0200,0200,0000,0200,0000,0000,0200,
124 0200,0000,0000,0200,0000,0200,0200,0000,
125 0200,0000,0000,0200,0000,0200,0200,0000,
126 0000,0200,0200,0000,0200,0000,0000,0201
129 #define ERASE tmode.c_cc[VERASE]
130 #define KILL tmode.c_cc[VKILL]
131 #define EOT tmode.c_cc[VEOF]
135 static void defttymode(void);
136 static void dingdong(int);
137 static void dogettytab(void);
138 static int getname(void);
139 static void interrupt(int);
140 static void oflush(void);
141 static void prompt(void);
142 static void putchr(int);
143 static void putf(const char *);
144 static void putpad(const char *);
145 static void puts(const char *);
146 static void timeoverrun(int);
147 static char *getline(int);
148 static void setttymode(int);
149 static int opentty(const char *, int);
154 dingdong(int signo __unused)
163 interrupt(int signo __unused)
169 * Action to take when getty is running too long.
172 timeoverrun(int signo __unused)
175 syslog(LOG_ERR, "getty exiting due to excessive running time");
180 main(int argc, char *argv[])
182 extern char **environ;
183 int first_sleep = 1, first_time = 1;
187 signal(SIGINT, SIG_IGN);
188 signal(SIGQUIT, SIG_IGN);
190 openlog("getty", LOG_CONS|LOG_PID, LOG_AUTH);
191 gethostname(hostname, sizeof(hostname) - 1);
192 hostname[sizeof(hostname) - 1] = '\0';
193 if (hostname[0] == '\0')
194 strcpy(hostname, "Amnesiac");
197 * Limit running time to deal with broken or dead lines.
199 (void)signal(SIGXCPU, timeoverrun);
200 limit.rlim_max = RLIM_INFINITY;
201 limit.rlim_cur = GETTY_TIMEOUT;
202 (void)setrlimit(RLIMIT_CPU, &limit);
204 gettable("default", defent);
211 * The following is a work around for vhangup interactions
212 * which cause great problems getting window systems started.
213 * If the tty line is "-", we do the old style getty presuming
214 * that the file descriptors are already set up for us.
215 * J. Gettys - MIT Project Athena.
217 if (argc <= 2 || strcmp(argv[2], "-") == 0)
218 strcpy(ttyn, ttyname(STDIN_FILENO));
221 strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev));
222 if (strcmp(argv[0], "+") != 0) {
228 * Do the first scan through gettytab.
229 * Terminal mode parameters will be wrong until
230 * defttymode() called, but they're irrelevant for
231 * the initial setup of the terminal device.
236 * Init or answer modem sequence has been specified.
239 if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
246 if (getty_chat(IC, CT, DC) > 0) {
247 syslog(LOG_ERR, "modem init problem on %s", ttyn);
248 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
257 rfds = 1 << 0; /* FD_SET */
260 i = select(32, (fd_set*)&rfds, (fd_set*)NULL,
261 (fd_set*)NULL, RT ? &to : NULL);
263 syslog(LOG_ERR, "select %s: %m", ttyn);
265 syslog(LOG_NOTICE, "recycle tty %s", ttyn);
266 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
267 exit(0); /* recycle for init */
269 i = getty_chat(AC, CT, DC);
271 syslog(LOG_ERR, "modem answer problem on %s", ttyn);
272 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
275 } else { /* maybe blocking open */
276 if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
286 * if a delay was specified then sleep for that
287 * number of seconds before writing the initial prompt
289 if (first_sleep && DE) {
291 /* remove any noise */
292 (void)tcflush(STDIN_FILENO, TCIOFLUSH);
303 tname = portselector();
311 /* if this is the first time through this, and an
312 issue file has been given, then send it */
313 if (first_time && IF) {
316 if ((fd = open(IF, O_RDONLY)) != -1) {
319 while ((cp = getline(fd)) != NULL) {
327 if (IMP && *IMP && !(PL && PP))
329 if (IM && *IM && !(PL && PP))
331 if (setjmp(timeout)) {
332 cfsetispeed(&tmode, B0);
333 cfsetospeed(&tmode, B0);
334 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
338 signal(SIGALRM, dingdong);
347 while (*p && q < &name[sizeof name - 1]) {
350 else if (islower(*p))
352 else if (isdigit(*p))
356 } else if (!(PL && PP))
358 if (rval == 2 || (PL && PP)) {
361 limit.rlim_max = RLIM_INFINITY;
362 limit.rlim_cur = RLIM_INFINITY;
363 (void)setrlimit(RLIMIT_CPU, &limit);
364 execle(PP, "ppplogin", ttyn, (char *) 0, env);
365 syslog(LOG_ERR, "%s: %m", PP);
367 } else if (rval || AL) {
372 signal(SIGALRM, SIG_DFL);
375 if (name[0] == '-') {
376 puts("user names may not start with '-'.");
379 if (!(upper || lower || digit)) {
382 "invalid auto-login name: %s", AL);
389 tmode.c_iflag |= ICRNL;
390 tmode.c_oflag |= ONLCR;
394 tmode.sg_flags |= LCASE;
396 tmode.sg_flags &= ~LCASE;
398 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
399 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
402 signal(SIGINT, SIG_DFL);
403 for (i = 0; environ[i] != (char *)0; i++)
407 limit.rlim_max = RLIM_INFINITY;
408 limit.rlim_cur = RLIM_INFINITY;
409 (void)setrlimit(RLIMIT_CPU, &limit);
410 execle(LO, "login", AL ? "-fp" : "-p", name,
412 syslog(LOG_ERR, "%s: %m", LO);
416 signal(SIGALRM, SIG_DFL);
417 signal(SIGINT, SIG_IGN);
426 opentty(const char *tty, int flags)
429 int failopenlogged = 0;
431 while ((i = open(tty, flags)) == -1)
433 if (!failopenlogged) {
434 syslog(LOG_ERR, "open %s: %m", tty);
439 if (login_tty(i) < 0) {
440 if (daemon(0,0) < 0) {
441 syslog(LOG_ERR,"daemon: %m");
445 if (login_tty(i) < 0) {
446 syslog(LOG_ERR, "login_tty %s: %m", tty);
459 /* Start with default tty settings. */
460 if (tcgetattr(STDIN_FILENO, &tmode) < 0) {
461 syslog(LOG_ERR, "tcgetattr %s: %m", ttyn);
464 omode = tmode; /* fill c_cc for dogettytab() */
467 * Don't rely on the driver too much, and initialize crucial
468 * things according to <sys/ttydefaults.h>. Avoid clobbering
469 * the c_cc[] settings however, the console drivers might wish
470 * to leave their idea of the preferred VERASE key value
474 tmode.c_iflag = def.c_iflag;
475 tmode.c_oflag = def.c_oflag;
476 tmode.c_lflag = def.c_lflag;
477 tmode.c_cflag = def.c_cflag;
479 tmode.c_cflag |= CLOCAL;
488 (void)tcflush(STDIN_FILENO, TCIOFLUSH); /* clear out the crap */
489 ioctl(STDIN_FILENO, FIONBIO, &off); /* turn off non-blocking mode */
490 ioctl(STDIN_FILENO, FIOASYNC, &off); /* ditto for async mode */
493 cfsetispeed(&tmode, speed(IS));
495 cfsetispeed(&tmode, speed(SP));
497 cfsetospeed(&tmode, speed(OS));
499 cfsetospeed(&tmode, speed(SP));
504 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
505 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
518 int ppp_connection = 0;
521 * Interrupt may happen if we use CBREAK mode
523 if (setjmp(intrupt)) {
524 signal(SIGINT, SIG_IGN);
527 signal(SIGINT, interrupt);
535 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
536 syslog(LOG_ERR, "%s: %m", ttyn);
539 crmod = digit = lower = upper = 0;
543 if (read(STDIN_FILENO, &cs, 1) <= 0)
545 if ((c = cs&0177) == 0)
548 /* PPP detection state machine..
550 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
551 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
553 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
554 and Erik 'PPP' Olson, <eriko@wrq.com>
557 if (PP && (cs == PPP_FRAME)) {
559 } else if (ppp_state == 1 && cs == PPP_STATION) {
561 } else if (ppp_state == 2 && cs == PPP_ESCAPE) {
563 } else if ((ppp_state == 2 && cs == PPP_CONTROL)
564 || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
566 } else if (ppp_state == 4 && cs == PPP_LCP_HI) {
568 } else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
575 if (c == EOT || c == CTRL('d'))
577 if (c == '\r' || c == '\n' || np >= &name[sizeof name-1]) {
585 else if (c == ERASE || c == '\b' || c == 0177) {
588 if (cfgetospeed(&tmode) >= 1200)
594 } else if (c == KILL || c == CTRL('u')) {
596 if (cfgetospeed(&tmode) < 1200)
598 /* this is the way they do it down under ... */
602 digit = lower = upper = 0;
605 } else if (isdigit(c))
607 if (IG && (c <= ' ' || c > 0176))
612 signal(SIGINT, SIG_IGN);
616 if ((upper && !lower && !LC) || UC)
617 for (np = name; *np; np++)
620 return (1 + ppp_connection);
624 putpad(const char *s)
627 speed_t ospeed = cfgetospeed(&tmode);
630 while (isdigit(*s)) {
635 if (*s == '.' && isdigit(s[1])) {
643 * If no delay needed, or output speed is
644 * not comprehensible, then don't try to delay.
646 if (pad == 0 || ospeed <= 0)
650 * Round up by a half a character frame, and then do the delay.
651 * Too bad there are no user program accessible programmed delays.
652 * Transmitting pad characters slows many terminals down and also
655 pad = (pad * ospeed + 50000) / 100000;
667 char outbuf[OBUFSIZ];
677 c |= partab[c&0177] & 0200;
682 outbuf[obufcnt++] = c;
683 if (obufcnt >= OBUFSIZ)
686 write(STDOUT_FILENO, &c, 1);
693 write(STDOUT_FILENO, outbuf, obufcnt);
711 static char linebuf[512];
714 * This is certainly slow, but it avoids having to include
715 * stdio.h unnecessarily. Issue files should be small anyway.
717 while (i < (sizeof linebuf - 3) && read(fd, linebuf+i, 1)==1) {
718 if (linebuf[i] == '\n') {
719 /* Don't rely on newline mode, assume raw */
728 return i ? linebuf : 0;
734 extern char editedhost[];
736 char *slash, db[100];
738 static struct utsname kerninfo;
740 if (!*kerninfo.sysname)
751 slash = strrchr(ttyn, '/');
752 if (slash == (char *) 0)
766 (void)setlocale(LC_TIME, Lo);
767 (void)strftime(db, sizeof(db), DF, localtime(&t));
772 puts(kerninfo.sysname);
776 puts(kerninfo.machine);
780 puts(kerninfo.release);
784 puts(kerninfo.version);
797 * Read a gettytab database entry and perform necessary quirks.
803 /* Read the database entry. */
804 gettable(tname, tabent);
807 * Avoid inheriting the parity values from the default entry
808 * if any of them is set in the current entry.
809 * Mixing different parity settings is unreasonable.
811 if (OPset || EPset || APset || NPset)
812 OPset = EPset = APset = NPset = 1;
814 /* Fill in default values for unset capabilities. */