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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)from: main.c 8.1 (Berkeley) 6/20/93
35 * $FreeBSD: src/libexec/getty/main.c,v 1.28.2.4 2003/02/06 11:45:31 sobomax Exp $
38 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/ttydefaults.h>
43 #include <sys/utsname.h>
59 #include "pathnames.h"
63 * Set the amount of running time that getty should accumulate
64 * before deciding that something is wrong and exit.
66 #define GETTY_TIMEOUT 60 /* seconds */
69 #define CTRL(x) (x&037)
71 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
73 #define PPP_FRAME 0x7e /* PPP Framing character */
74 #define PPP_STATION 0xff /* "All Station" character */
75 #define PPP_ESCAPE 0x7d /* Escape Character */
76 #define PPP_CONTROL 0x03 /* PPP Control Field */
77 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
78 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
79 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
81 struct termios tmode, omode;
83 int crmod, digit, lower, upper;
85 char hostname[MAXHOSTNAMELEN];
86 char name[MAXLOGNAME*3];
87 char dev[] = _PATH_DEV;
93 char defent[TABBUFSIZ];
94 char tabent[TABBUFSIZ];
99 0001,0201,0201,0001,0201,0001,0001,0201,
100 0202,0004,0003,0205,0005,0206,0201,0001,
101 0201,0001,0001,0201,0001,0201,0201,0001,
102 0001,0201,0201,0001,0201,0001,0001,0201,
103 0200,0000,0000,0200,0000,0200,0200,0000,
104 0000,0200,0200,0000,0200,0000,0000,0200,
105 0000,0200,0200,0000,0200,0000,0000,0200,
106 0200,0000,0000,0200,0000,0200,0200,0000,
107 0200,0000,0000,0200,0000,0200,0200,0000,
108 0000,0200,0200,0000,0200,0000,0000,0200,
109 0000,0200,0200,0000,0200,0000,0000,0200,
110 0200,0000,0000,0200,0000,0200,0200,0000,
111 0000,0200,0200,0000,0200,0000,0000,0200,
112 0200,0000,0000,0200,0000,0200,0200,0000,
113 0200,0000,0000,0200,0000,0200,0200,0000,
114 0000,0200,0200,0000,0200,0000,0000,0201
117 #define ERASE tmode.c_cc[VERASE]
118 #define KILL tmode.c_cc[VKILL]
119 #define EOT tmode.c_cc[VEOF]
123 static void dingdong (int);
124 static int getname (void);
125 static void interrupt (int);
126 static void oflush (void);
127 static void prompt (void);
128 static void putchr (int);
129 static void putf (const char *);
130 static void putpad (const char *);
131 static void puts (const char *);
132 static void timeoverrun (int);
133 static char *getline (int);
134 static void setttymode (const char *, int);
135 static void setdefttymode (const char *);
136 static int opentty (const char *, int);
138 int main (int, char **);
158 * Action to take when getty is running too long.
161 timeoverrun(int signo)
163 syslog(LOG_ERR, "getty exiting due to excessive running time");
168 main(int argc, char **argv)
170 extern char **environ;
172 int first_sleep = 1, first_time = 1;
176 signal(SIGINT, SIG_IGN);
177 signal(SIGQUIT, SIG_IGN);
179 openlog("getty", LOG_ODELAY|LOG_CONS|LOG_PID, LOG_AUTH);
180 gethostname(hostname, sizeof(hostname) - 1);
181 hostname[sizeof(hostname) - 1] = '\0';
182 if (hostname[0] == '\0')
183 strcpy(hostname, "Amnesiac");
186 * Limit running time to deal with broken or dead lines.
188 (void)signal(SIGXCPU, timeoverrun);
189 limit.rlim_max = RLIM_INFINITY;
190 limit.rlim_cur = GETTY_TIMEOUT;
191 (void)setrlimit(RLIMIT_CPU, &limit);
193 gettable("default", defent);
200 * The following is a work around for vhangup interactions
201 * which cause great problems getting window systems started.
202 * If the tty line is "-", we do the old style getty presuming
203 * that the file descriptors are already set up for us.
204 * J. Gettys - MIT Project Athena.
206 if (argc <= 2 || strcmp(argv[2], "-") == 0)
207 strcpy(ttyn, ttyname(STDIN_FILENO));
210 strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev));
211 if (strcmp(argv[0], "+") != 0) {
216 gettable(tname, tabent);
218 /* Init modem sequence has been specified
221 if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
223 setdefttymode(tname);
224 if (getty_chat(IC, CT, DC) > 0) {
225 syslog(LOG_ERR, "modem init problem on %s", ttyn);
226 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
233 struct timeval timeout;
235 if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
237 setdefttymode(tname);
238 rfds = 1 << 0; /* FD_SET */
241 i = select(32, (fd_set*)&rfds, NULL,
242 NULL, RT ? &timeout : NULL);
244 syslog(LOG_ERR, "select %s: %m", ttyn);
246 syslog(LOG_NOTICE, "recycle tty %s", ttyn);
247 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
248 exit(0); /* recycle for init */
250 i = getty_chat(AC, CT, DC);
252 syslog(LOG_ERR, "modem answer problem on %s", ttyn);
253 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
256 } else { /* maybe blocking open */
257 if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
263 /* Start with default tty settings */
264 if (tcgetattr(STDIN_FILENO, &tmode) < 0) {
265 syslog(LOG_ERR, "tcgetattr %s: %m", ttyn);
269 * Don't rely on the driver too much, and initialize crucial
270 * things according to <sys/ttydefaults.h>. Avoid clobbering
271 * the c_cc[] settings however, the console drivers might wish
272 * to leave their idea of the preferred VERASE key value
275 tmode.c_iflag = TTYDEF_IFLAG;
276 tmode.c_oflag = TTYDEF_OFLAG;
277 tmode.c_lflag = TTYDEF_LFLAG;
278 tmode.c_cflag = TTYDEF_CFLAG;
279 tmode.c_cflag |= (NC ? CLOCAL : 0);
285 * if a delay was specified then sleep for that
286 * number of seconds before writing the initial prompt
288 if (first_sleep && DE) {
290 /* remove any noise */
291 (void)tcflush(STDIN_FILENO, TCIOFLUSH);
295 setttymode(tname, 0);
301 tname = portselector();
308 /* if this is the first time through this, and an
309 issue file has been given, then send it */
310 if (first_time && IF) {
313 if ((fd = open(IF, O_RDONLY)) != -1) {
316 while ((cp = getline(fd)) != NULL) {
324 if (IM && *IM && !(PL && PP))
326 if (setjmp(timeout)) {
327 cfsetispeed(&tmode, B0);
328 cfsetospeed(&tmode, B0);
329 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
333 signal(SIGALRM, dingdong);
340 while (*p && q < &name[sizeof name - 1]) {
343 else if (islower(*p))
345 else if (isdigit(*p))
349 } else if (!(PL && PP))
351 if (rval == 2 || (PL && PP)) {
354 limit.rlim_max = RLIM_INFINITY;
355 limit.rlim_cur = RLIM_INFINITY;
356 (void)setrlimit(RLIMIT_CPU, &limit);
357 execle(PP, "ppplogin", ttyn, NULL, env);
358 syslog(LOG_ERR, "%s: %m", PP);
360 } else if (rval || AL) {
365 signal(SIGALRM, SIG_DFL);
366 if (name[0] == '-') {
367 puts("user names may not start with '-'.");
370 if (!(upper || lower || digit))
374 tmode.c_iflag |= ICRNL;
375 tmode.c_oflag |= ONLCR;
379 tmode.sg_flags |= LCASE;
381 tmode.sg_flags &= ~LCASE;
383 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
384 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
387 signal(SIGINT, SIG_DFL);
388 for (i = 0; environ[i] != NULL; i++)
392 limit.rlim_max = RLIM_INFINITY;
393 limit.rlim_cur = RLIM_INFINITY;
394 (void)setrlimit(RLIMIT_CPU, &limit);
395 execle(LO, "login", AL ? "-fp" : "-p", name,
397 syslog(LOG_ERR, "%s: %m", LO);
401 signal(SIGALRM, SIG_DFL);
402 signal(SIGINT, SIG_IGN);
409 opentty(const char *ttyn, int flags)
412 int failopenlogged = 0;
414 while (j < 10 && (i = open(ttyn, flags)) == -1)
416 if (((j % 10) == 0) && (errno != ENXIO || !failopenlogged)) {
417 syslog(LOG_ERR, "open %s: %m", ttyn);
424 syslog(LOG_ERR, "open %s: %m", ttyn);
428 if (login_tty(i) < 0) {
429 if (daemon(0,0) < 0) {
430 syslog(LOG_ERR,"daemon: %m");
434 if (login_tty(i) < 0) {
435 syslog(LOG_ERR, "login_tty %s: %m", ttyn);
445 setdefttymode(const char *tname)
447 if (tcgetattr(STDIN_FILENO, &tmode) < 0) {
448 syslog(LOG_ERR, "tcgetattr %s: %m", ttyn);
451 tmode.c_iflag = TTYDEF_IFLAG;
452 tmode.c_oflag = TTYDEF_OFLAG;
453 tmode.c_lflag = TTYDEF_LFLAG;
454 tmode.c_cflag = TTYDEF_CFLAG;
456 setttymode(tname, 1);
460 setttymode(const char *tname, int raw)
464 gettable(tname, tabent);
465 if (OPset || EPset || APset)
466 APset++, OPset++, EPset++;
468 (void)tcflush(STDIN_FILENO, TCIOFLUSH); /* clear out the crap */
469 ioctl(STDIN_FILENO, FIONBIO, &off); /* turn off non-blocking mode */
470 ioctl(STDIN_FILENO, FIOASYNC, &off); /* ditto for async mode */
473 cfsetispeed(&tmode, speed(IS));
475 cfsetispeed(&tmode, speed(SP));
477 cfsetospeed(&tmode, speed(OS));
479 cfsetospeed(&tmode, speed(SP));
484 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
485 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
498 int ppp_connection = 0;
501 * Interrupt may happen if we use CBREAK mode
503 if (setjmp(intrupt)) {
504 signal(SIGINT, SIG_IGN);
507 signal(SIGINT, interrupt);
515 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
516 syslog(LOG_ERR, "%s: %m", ttyn);
519 crmod = digit = lower = upper = 0;
523 if (read(STDIN_FILENO, &cs, 1) <= 0)
525 if ((c = cs&0177) == 0)
528 /* PPP detection state machine..
530 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
531 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
533 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
534 and Erik 'PPP' Olson, <eriko@wrq.com>
537 if (PP && (cs == PPP_FRAME)) {
539 } else if (ppp_state == 1 && cs == PPP_STATION) {
541 } else if (ppp_state == 2 && cs == PPP_ESCAPE) {
543 } else if ((ppp_state == 2 && cs == PPP_CONTROL)
544 || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
546 } else if (ppp_state == 4 && cs == PPP_LCP_HI) {
548 } else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
555 if (c == EOT || c == CTRL('d'))
557 if (c == '\r' || c == '\n' || np >= &name[sizeof name-1]) {
565 else if (c == ERASE || c == '\b' || c == 0177) {
568 if (cfgetospeed(&tmode) >= 1200)
574 } else if (c == KILL || c == CTRL('u')) {
576 if (cfgetospeed(&tmode) < 1200)
578 /* this is the way they do it down under ... */
584 } else if (isdigit(c))
586 if (IG && (c <= ' ' || c > 0176))
591 signal(SIGINT, SIG_IGN);
595 if ((upper && !lower && !LC) || UC)
596 for (np = name; *np; np++)
599 return (1 + ppp_connection);
603 putpad(const char *s)
608 ospeed = cfgetospeed(&tmode);
611 while (isdigit(*s)) {
616 if (*s == '.' && isdigit(s[1])) {
624 * If no delay needed, or output speed is
625 * not comprehensible, then don't try to delay.
627 if (pad == 0 || ospeed <= 0)
631 * Round up by a half a character frame, and then do the delay.
632 * Too bad there are no user program accessible programmed delays.
633 * Transmitting pad characters slows many terminals down and also
636 pad = (pad * ospeed + 50000) / 100000;
648 char outbuf[OBUFSIZ];
658 c |= partab[c&0177] & 0200;
663 outbuf[obufcnt++] = c;
664 if (obufcnt >= OBUFSIZ)
667 write(STDOUT_FILENO, &c, 1);
674 write(STDOUT_FILENO, outbuf, obufcnt);
691 static char linebuf[512];
694 * This is certainly slow, but it avoids having to include
695 * stdio.h unnecessarily. Issue files should be small anyway.
697 while (i < (sizeof linebuf - 3) && read(fd, linebuf+i, 1)==1) {
698 if (linebuf[i] == '\n') {
699 /* Don't rely on newline mode, assume raw */
708 return i ? linebuf : 0;
714 extern char editedhost[];
716 char *slash, db[100];
718 static struct utsname kerninfo;
720 if (!*kerninfo.sysname)
731 slash = strrchr(ttyn, '/');
746 (void)setlocale(LC_TIME, Lo);
747 (void)strftime(db, sizeof(db), DF, localtime(&t));
752 puts(kerninfo.sysname);
756 puts(kerninfo.machine);
760 puts(kerninfo.release);
764 puts(kerninfo.version);