2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)from: main.c 8.1 (Berkeley) 6/20/93";
43 #include <sys/cdefs.h>
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>
69 #include "pathnames.h"
72 * Set the amount of running time that getty should accumulate
73 * before deciding that something is wrong and exit.
75 #define GETTY_TIMEOUT 60 /* seconds */
78 #define CTRL(x) (x&037)
80 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
82 #define PPP_FRAME 0x7e /* PPP Framing character */
83 #define PPP_STATION 0xff /* "All Station" character */
84 #define PPP_ESCAPE 0x7d /* Escape Character */
85 #define PPP_CONTROL 0x03 /* PPP Control Field */
86 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
87 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
88 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
90 /* original mode; flags've been reset using values from <sys/ttydefaults.h> */
95 static int crmod, digit, lower, upper;
97 char hostname[MAXHOSTNAMELEN];
98 static char name[MAXLOGNAME*3];
103 static const char *tname;
105 static char *env[128];
107 static char partab[] = {
108 0001,0201,0201,0001,0201,0001,0001,0201,
109 0202,0004,0003,0205,0005,0206,0201,0001,
110 0201,0001,0001,0201,0001,0201,0201,0001,
111 0001,0201,0201,0001,0201,0001,0001,0201,
112 0200,0000,0000,0200,0000,0200,0200,0000,
113 0000,0200,0200,0000,0200,0000,0000,0200,
114 0000,0200,0200,0000,0200,0000,0000,0200,
115 0200,0000,0000,0200,0000,0200,0200,0000,
116 0200,0000,0000,0200,0000,0200,0200,0000,
117 0000,0200,0200,0000,0200,0000,0000,0200,
118 0000,0200,0200,0000,0200,0000,0000,0200,
119 0200,0000,0000,0200,0000,0200,0200,0000,
120 0000,0200,0200,0000,0200,0000,0000,0200,
121 0200,0000,0000,0200,0000,0200,0200,0000,
122 0200,0000,0000,0200,0000,0200,0200,0000,
123 0000,0200,0200,0000,0200,0000,0000,0201
126 #define ERASE tmode.c_cc[VERASE]
127 #define KILL tmode.c_cc[VKILL]
128 #define EOT tmode.c_cc[VEOF]
132 static void defttymode(void);
133 static void dingdong(int);
134 static void dogettytab(void);
135 static int getname(void);
136 static void interrupt(int);
137 static void oflush(void);
138 static void prompt(void);
139 static void putchr(int);
140 static void putf(const char *);
141 static void putpad(const char *);
142 static void puts(const char *);
143 static void timeoverrun(int);
144 static char *get_line(int);
145 static void setttymode(int);
146 static int opentty(const char *, int);
148 static jmp_buf timeout;
151 dingdong(int signo __unused)
157 static jmp_buf intrupt;
160 interrupt(int signo __unused)
166 * Action to take when getty is running too long.
169 timeoverrun(int signo __unused)
172 syslog(LOG_ERR, "getty exiting due to excessive running time");
177 main(int argc, char *argv[])
179 int first_sleep = 1, first_time = 1;
183 signal(SIGINT, SIG_IGN);
184 signal(SIGQUIT, SIG_IGN);
186 openlog("getty", LOG_CONS|LOG_PID, LOG_AUTH);
187 gethostname(hostname, sizeof(hostname) - 1);
188 hostname[sizeof(hostname) - 1] = '\0';
189 if (hostname[0] == '\0')
190 snprintf(hostname, sizeof(hostname), "Amnesiac");
193 * Limit running time to deal with broken or dead lines.
195 (void)signal(SIGXCPU, timeoverrun);
196 limit.rlim_max = RLIM_INFINITY;
197 limit.rlim_cur = GETTY_TIMEOUT;
198 (void)setrlimit(RLIMIT_CPU, &limit);
207 * The following is a work around for vhangup interactions
208 * which cause great problems getting window systems started.
209 * If the tty line is "-", we do the old style getty presuming
210 * that the file descriptors are already set up for us.
211 * J. Gettys - MIT Project Athena.
213 if (argc <= 2 || strcmp(argv[2], "-") == 0) {
214 char *n = ttyname(STDIN_FILENO);
216 syslog(LOG_ERR, "ttyname: %m");
219 snprintf(ttyn, sizeof(ttyn), "%s", n);
221 snprintf(ttyn, sizeof(ttyn), "%s%s", _PATH_DEV, argv[2]);
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);
262 i = select(32, &rfds, NULL, NULL, RT ? &to : NULL);
264 syslog(LOG_ERR, "select %s: %m", ttyn);
266 syslog(LOG_NOTICE, "recycle tty %s", ttyn);
267 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
268 exit(0); /* recycle for init */
270 i = getty_chat(AC, CT, DC);
272 syslog(LOG_ERR, "modem answer problem on %s", ttyn);
273 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
276 } else { /* maybe blocking open */
277 if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
287 * if a delay was specified then sleep for that
288 * number of seconds before writing the initial prompt
290 if (first_sleep && DE) {
292 /* remove any noise */
293 (void)tcflush(STDIN_FILENO, TCIOFLUSH);
304 tname = portselector();
312 /* if this is the first time through this, and an
313 issue file has been given, then send it */
314 if (first_time && IF) {
317 if ((fd = open(IF, O_RDONLY)) != -1) {
320 while ((cp = get_line(fd)) != NULL) {
328 if (IMP && *IMP && !(PL && PP))
330 if (IM && *IM && !(PL && PP))
332 if (setjmp(timeout)) {
333 cfsetispeed(&tmode, B0);
334 cfsetospeed(&tmode, B0);
335 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
339 signal(SIGALRM, dingdong);
348 while (*p && q < &name[sizeof name - 1]) {
351 else if (islower(*p))
353 else if (isdigit(*p))
357 } else if (!(PL && PP))
359 if (rval == 2 || (PL && PP)) {
362 limit.rlim_max = RLIM_INFINITY;
363 limit.rlim_cur = RLIM_INFINITY;
364 (void)setrlimit(RLIMIT_CPU, &limit);
365 execle(PP, "ppplogin", ttyn, (char *) 0, env);
366 syslog(LOG_ERR, "%s: %m", PP);
368 } else if (rval || AL) {
373 signal(SIGALRM, SIG_DFL);
376 if (name[0] == '-') {
377 puts("user names may not start with '-'.");
380 if (!(upper || lower || digit)) {
383 "invalid auto-login name: %s", AL);
390 tmode.c_iflag |= ICRNL;
391 tmode.c_oflag |= ONLCR;
395 tmode.sg_flags |= LCASE;
397 tmode.sg_flags &= ~LCASE;
399 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
400 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
403 signal(SIGINT, SIG_DFL);
404 for (i = 0; environ[i] != (char *)0; i++)
408 limit.rlim_max = RLIM_INFINITY;
409 limit.rlim_cur = RLIM_INFINITY;
410 (void)setrlimit(RLIMIT_CPU, &limit);
411 execle(LO, "login", AL ? "-fp" : "-p", name,
413 syslog(LOG_ERR, "%s: %m", LO);
417 signal(SIGALRM, SIG_DFL);
418 signal(SIGINT, SIG_IGN);
427 opentty(const char *tty, int flags)
429 int failopenlogged = 0, i, saved_errno;
431 while ((i = open(tty, flags)) == -1)
434 if (!failopenlogged) {
435 syslog(LOG_ERR, "open %s: %m", tty);
438 if (saved_errno == ENOENT)
442 if (login_tty(i) < 0) {
443 if (daemon(0,0) < 0) {
444 syslog(LOG_ERR,"daemon: %m");
448 if (login_tty(i) < 0) {
449 syslog(LOG_ERR, "login_tty %s: %m", tty);
462 /* Start with default tty settings. */
463 if (tcgetattr(STDIN_FILENO, &tmode) < 0) {
464 syslog(LOG_ERR, "tcgetattr %s: %m", ttyn);
467 omode = tmode; /* fill c_cc for dogettytab() */
470 * Don't rely on the driver too much, and initialize crucial
471 * things according to <sys/ttydefaults.h>. Avoid clobbering
472 * the c_cc[] settings however, the console drivers might wish
473 * to leave their idea of the preferred VERASE key value
477 tmode.c_iflag = def.c_iflag;
478 tmode.c_oflag = def.c_oflag;
479 tmode.c_lflag = def.c_lflag;
480 tmode.c_cflag = def.c_cflag;
482 tmode.c_cflag |= CLOCAL;
491 (void)tcflush(STDIN_FILENO, TCIOFLUSH); /* clear out the crap */
492 ioctl(STDIN_FILENO, FIONBIO, &off); /* turn off non-blocking mode */
493 ioctl(STDIN_FILENO, FIOASYNC, &off); /* ditto for async mode */
496 cfsetispeed(&tmode, speed(IS));
498 cfsetispeed(&tmode, speed(SP));
500 cfsetospeed(&tmode, speed(OS));
502 cfsetospeed(&tmode, speed(SP));
507 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
508 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
521 int ppp_connection = 0;
524 * Interrupt may happen if we use CBREAK mode
526 if (setjmp(intrupt)) {
527 signal(SIGINT, SIG_IGN);
530 signal(SIGINT, interrupt);
538 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
539 syslog(LOG_ERR, "%s: %m", ttyn);
542 crmod = digit = lower = upper = 0;
546 if (read(STDIN_FILENO, &cs, 1) <= 0)
548 if ((c = cs&0177) == 0)
551 /* PPP detection state machine..
553 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
554 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
556 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
557 and Erik 'PPP' Olson, <eriko@wrq.com>
560 if (PP && (cs == PPP_FRAME)) {
562 } else if (ppp_state == 1 && cs == PPP_STATION) {
564 } else if (ppp_state == 2 && cs == PPP_ESCAPE) {
566 } else if ((ppp_state == 2 && cs == PPP_CONTROL)
567 || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
569 } else if (ppp_state == 4 && cs == PPP_LCP_HI) {
571 } else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
578 if (c == EOT || c == CTRL('d'))
580 if (c == '\r' || c == '\n' || np >= &name[sizeof name-1]) {
588 else if (c == ERASE || c == '\b' || c == 0177) {
591 if (cfgetospeed(&tmode) >= 1200)
597 } else if (c == KILL || c == CTRL('u')) {
599 if (cfgetospeed(&tmode) < 1200)
601 /* this is the way they do it down under ... */
605 digit = lower = upper = 0;
608 } else if (isdigit(c))
610 if (IG && (c <= ' ' || c > 0176))
615 signal(SIGINT, SIG_IGN);
619 if ((upper && !lower && !LC) || UC)
620 for (np = name; *np; np++)
623 return (1 + ppp_connection);
627 putpad(const char *s)
630 speed_t ospeed = cfgetospeed(&tmode);
633 while (isdigit(*s)) {
638 if (*s == '.' && isdigit(s[1])) {
646 * If no delay needed, or output speed is
647 * not comprehensible, then don't try to delay.
649 if (pad == 0 || ospeed <= 0)
653 * Round up by a half a character frame, and then do the delay.
654 * Too bad there are no user program accessible programmed delays.
655 * Transmitting pad characters slows many terminals down and also
658 pad = (pad * ospeed + 50000) / 100000;
670 static char outbuf[OBUFSIZ];
671 static int obufcnt = 0;
680 c |= partab[c&0177] & 0200;
685 outbuf[obufcnt++] = c;
686 if (obufcnt >= OBUFSIZ)
689 write(STDOUT_FILENO, &c, 1);
696 write(STDOUT_FILENO, outbuf, obufcnt);
714 static char linebuf[512];
717 * This is certainly slow, but it avoids having to include
718 * stdio.h unnecessarily. Issue files should be small anyway.
720 while (i < (sizeof linebuf - 3) && read(fd, linebuf+i, 1)==1) {
721 if (linebuf[i] == '\n') {
722 /* Don't rely on newline mode, assume raw */
731 return i ? linebuf : 0;
741 static struct utsname kerninfo;
743 if (!*kerninfo.sysname)
754 slash = strrchr(ttyn, '/');
755 if (slash == (char *) 0)
769 (void)setlocale(LC_TIME, Lo);
770 (void)strftime(db, sizeof(db), DF, localtime(&t));
775 puts(kerninfo.sysname);
779 puts(kerninfo.machine);
783 puts(kerninfo.release);
787 puts(kerninfo.version);
799 * Read a gettytab database entry and perform necessary quirks.
805 /* Read the database entry. */
809 * Avoid inheriting the parity values from the default entry
810 * if any of them is set in the current entry.
811 * Mixing different parity settings is unreasonable.
813 if (OPset || EPset || APset || NPset)
814 OPset = EPset = APset = NPset = 1;
816 /* Fill in default values for unset capabilities. */