]>
git.saurik.com Git - apple/system_cmds.git/blob - getty.tproj/main.c
c25373c3639bdc17673ac36baaf10c65906471d6
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Portions copyright (c) 2007 Apple Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 static const char copyright
[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
44 static char sccsid
[] = "@(#)from: main.c 8.1 (Berkeley) 6/20/93";
46 static const char rcsid
[] =
47 "$FreeBSD: src/libexec/getty/main.c,v 1.47 2005/04/06 17:42:24 stefanf Exp $";
50 #include <sys/param.h>
51 #include <sys/ioctl.h>
53 #include <sys/resource.h>
55 #include <sys/ttydefaults.h>
56 #include <sys/utsname.h>
73 #include <TargetConditionals.h>
78 #include "pathnames.h"
81 * Set the amount of running time that getty should accumulate
82 * before deciding that something is wrong and exit.
84 #define GETTY_TIMEOUT 60 /* seconds */
87 #define CTRL(x) (x&037)
89 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
91 #define PPP_FRAME 0x7e /* PPP Framing character */
92 #define PPP_STATION 0xff /* "All Station" character */
93 #define PPP_ESCAPE 0x7d /* Escape Character */
94 #define PPP_CONTROL 0x03 /* PPP Control Field */
95 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
96 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
97 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
99 /* original mode; flags've been reset using values from <sys/ttydefaults.h> */
100 struct termios omode
;
102 struct termios tmode
;
104 int crmod
, digit
, lower
, upper
;
106 char hostname
[MAXHOSTNAMELEN
];
107 char name
[MAXLOGNAME
*3];
108 char dev
[] = _PATH_DEV
;
112 #define TABBUFSIZ 512
114 char defent
[TABBUFSIZ
];
115 char tabent
[TABBUFSIZ
];
121 0001,0201,0201,0001,0201,0001,0001,0201,
122 0202,0004,0003,0205,0005,0206,0201,0001,
123 0201,0001,0001,0201,0001,0201,0201,0001,
124 0001,0201,0201,0001,0201,0001,0001,0201,
125 0200,0000,0000,0200,0000,0200,0200,0000,
126 0000,0200,0200,0000,0200,0000,0000,0200,
127 0000,0200,0200,0000,0200,0000,0000,0200,
128 0200,0000,0000,0200,0000,0200,0200,0000,
129 0200,0000,0000,0200,0000,0200,0200,0000,
130 0000,0200,0200,0000,0200,0000,0000,0200,
131 0000,0200,0200,0000,0200,0000,0000,0200,
132 0200,0000,0000,0200,0000,0200,0200,0000,
133 0000,0200,0200,0000,0200,0000,0000,0200,
134 0200,0000,0000,0200,0000,0200,0200,0000,
135 0200,0000,0000,0200,0000,0200,0200,0000,
136 0000,0200,0200,0000,0200,0000,0000,0201
139 #define ERASE tmode.c_cc[VERASE]
140 #define KILL tmode.c_cc[VKILL]
141 #define EOT tmode.c_cc[VEOF]
145 static void defttymode(void);
146 static void dingdong(int);
147 static void dogettytab(void);
148 static int getname(void);
149 static void interrupt(int);
150 static void oflush(void);
151 static void prompt(void);
152 static void putchr(int);
153 static void putf(const char *);
154 static void putpad(const char *);
155 static void puts(const char *);
156 static void timeoverrun(int);
157 static char *getline(int);
158 static void setttymode(int);
159 static int opentty(const char *, int);
164 dingdong(int signo __unused
)
173 interrupt(int signo __unused
)
179 * Action to take when getty is running too long.
182 timeoverrun(int signo __unused
)
185 syslog(LOG_ERR
, "getty exiting due to excessive running time");
190 main(int argc
, char *argv
[])
192 extern char **environ
;
193 int first_sleep
= 1, first_time
= 1;
197 int ttyopenmode
= O_RDWR
;
200 signal(SIGINT
, SIG_IGN
);
201 signal(SIGQUIT
, SIG_IGN
);
203 openlog("getty", LOG_ODELAY
|LOG_CONS
|LOG_PID
, LOG_AUTH
);
204 gethostname(hostname
, sizeof(hostname
) - 1);
205 hostname
[sizeof(hostname
) - 1] = '\0';
206 if (hostname
[0] == '\0')
207 strcpy(hostname
, "Amnesiac");
210 * Limit running time to deal with broken or dead lines.
212 (void)signal(SIGXCPU
, timeoverrun
);
213 limit
.rlim_max
= RLIM_INFINITY
;
214 limit
.rlim_cur
= GETTY_TIMEOUT
;
215 (void)setrlimit(RLIMIT_CPU
, &limit
);
217 gettable("default", defent
);
224 * The following is a work around for vhangup interactions
225 * which cause great problems getting window systems started.
226 * If the tty line is "-", we do the old style getty presuming
227 * that the file descriptors are already set up for us.
228 * J. Gettys - MIT Project Athena.
230 if (argc
<= 2 || strcmp(argv
[2], "-") == 0)
233 // <rdar://problem/5178373>
234 char* n
= ttyname(STDIN_FILENO
);
236 strlcpy(ttyn
, n
, sizeof(ttyn
));
238 syslog(LOG_ERR
, "ttyname %m");
243 strcpy(ttyn
, ttyname(STDIN_FILENO
));
247 strncat(ttyn
, argv
[2], sizeof(ttyn
)-sizeof(dev
));
248 if (strcmp(argv
[0], "+") != 0) {
254 * Do the first scan through gettytab.
255 * Terminal mode parameters will be wrong until
256 * defttymode() called, but they're irrelevant for
257 * the initial setup of the terminal device.
261 #if defined(__APPLE__) && !TARGET_OS_EMBEDDED
262 if (strncmp(ttyn
, _PATH_CONSOLE
, sizeof(ttyn
)) == 0)
263 ttyopenmode
|= O_POPUP
;
266 * Init or answer modem sequence has been specified.
270 if (!opentty(ttyn
, ttyopenmode
))
272 if (!opentty(ttyn
, O_RDWR
|O_NONBLOCK
))
280 if (getty_chat(IC
, CT
, DC
) > 0) {
281 syslog(LOG_ERR
, "modem init problem on %s", ttyn
);
282 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
291 rfds
= 1 << 0; /* FD_SET */
294 i
= select(32, (fd_set
*)&rfds
, (fd_set
*)NULL
,
295 (fd_set
*)NULL
, RT
? &to
: NULL
);
297 syslog(LOG_ERR
, "select %s: %m", ttyn
);
299 syslog(LOG_NOTICE
, "recycle tty %s", ttyn
);
300 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
301 exit(0); /* recycle for init */
303 i
= getty_chat(AC
, CT
, DC
);
305 syslog(LOG_ERR
, "modem answer problem on %s", ttyn
);
306 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
309 } else { /* maybe blocking open */
311 if (!opentty(ttyn
, ttyopenmode
| (NC
? O_NONBLOCK
: 0 )))
313 if (!opentty(ttyn
, O_RDWR
| (NC
? O_NONBLOCK
: 0 )))
324 * if a delay was specified then sleep for that
325 * number of seconds before writing the initial prompt
327 if (first_sleep
&& DE
) {
329 /* remove any noise */
330 (void)tcflush(STDIN_FILENO
, TCIOFLUSH
);
341 tname
= portselector();
349 /* if this is the first time through this, and an
350 issue file has been given, then send it */
351 if (first_time
&& IF
) {
354 if ((fd
= open(IF
, O_RDONLY
)) != -1) {
357 while ((cp
= getline(fd
)) != NULL
) {
365 if (IM
&& *IM
&& !(PL
&& PP
))
367 if (setjmp(timeout
)) {
368 cfsetispeed(&tmode
, B0
);
369 cfsetospeed(&tmode
, B0
);
370 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
374 signal(SIGALRM
, dingdong
);
383 while (*p
&& q
< &name
[sizeof name
- 1]) {
386 else if (islower(*p
))
388 else if (isdigit(*p
))
392 } else if (!(PL
&& PP
))
394 if (rval
== 2 || (PL
&& PP
)) {
397 limit
.rlim_max
= RLIM_INFINITY
;
398 limit
.rlim_cur
= RLIM_INFINITY
;
399 (void)setrlimit(RLIMIT_CPU
, &limit
);
400 execle(PP
, "ppplogin", ttyn
, (char *) 0, env
);
401 syslog(LOG_ERR
, "%s: %m", PP
);
403 } else if (rval
|| AL
) {
408 signal(SIGALRM
, SIG_DFL
);
411 if (name
[0] == '-') {
412 puts("user names may not start with '-'.");
415 if (!(upper
|| lower
|| digit
)) {
418 "invalid auto-login name: %s", AL
);
425 tmode
.c_iflag
|= ICRNL
;
426 tmode
.c_oflag
|= ONLCR
;
430 tmode
.sg_flags
|= LCASE
;
432 tmode
.sg_flags
&= ~LCASE
;
434 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
435 syslog(LOG_ERR
, "tcsetattr %s: %m", ttyn
);
438 signal(SIGINT
, SIG_DFL
);
439 for (i
= 0; environ
[i
] != (char *)0; i
++)
443 limit
.rlim_max
= RLIM_INFINITY
;
444 limit
.rlim_cur
= RLIM_INFINITY
;
445 (void)setrlimit(RLIMIT_CPU
, &limit
);
447 // <rdar://problem/3205179>
448 execle(LO
, "login", AL
? "-fp1" : "-p1", name
,
450 execle(LO
, "login", AL
? "-fp" : "-p", name
,
453 syslog(LOG_ERR
, "%s: %m", LO
);
457 signal(SIGALRM
, SIG_DFL
);
458 signal(SIGINT
, SIG_IGN
);
467 opentty(const char *tty
, int flags
)
470 int failopenlogged
= 0;
472 while ((i
= open(tty
, flags
)) == -1)
474 if (!failopenlogged
) {
475 syslog(LOG_ERR
, "open %s: %m", tty
);
480 if (login_tty(i
) < 0) {
482 if (daemon(0,0) < 0) {
483 syslog(LOG_ERR
,"daemon: %m");
488 if (login_tty(i
) < 0) {
489 syslog(LOG_ERR
, "login_tty %s: %m", tty
);
501 /* Start with default tty settings. */
502 if (tcgetattr(STDIN_FILENO
, &tmode
) < 0) {
503 syslog(LOG_ERR
, "tcgetattr %s: %m", ttyn
);
506 omode
= tmode
; /* fill c_cc for dogettytab() */
509 * Don't rely on the driver too much, and initialize crucial
510 * things according to <sys/ttydefaults.h>. Avoid clobbering
511 * the c_cc[] settings however, the console drivers might wish
512 * to leave their idea of the preferred VERASE key value
515 tmode
.c_iflag
= TTYDEF_IFLAG
;
516 tmode
.c_oflag
= TTYDEF_OFLAG
;
517 tmode
.c_lflag
= TTYDEF_LFLAG
;
518 tmode
.c_cflag
= TTYDEF_CFLAG
;
520 tmode
.c_cflag
|= CLOCAL
;
529 (void)tcflush(STDIN_FILENO
, TCIOFLUSH
); /* clear out the crap */
530 ioctl(STDIN_FILENO
, FIONBIO
, &off
); /* turn off non-blocking mode */
531 ioctl(STDIN_FILENO
, FIOASYNC
, &off
); /* ditto for async mode */
534 cfsetispeed(&tmode
, speed(IS
));
536 cfsetispeed(&tmode
, speed(SP
));
538 cfsetospeed(&tmode
, speed(OS
));
540 cfsetospeed(&tmode
, speed(SP
));
545 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
546 syslog(LOG_ERR
, "tcsetattr %s: %m", ttyn
);
559 int ppp_connection
= 0;
562 * Interrupt may happen if we use CBREAK mode
564 if (setjmp(intrupt
)) {
565 signal(SIGINT
, SIG_IGN
);
568 signal(SIGINT
, interrupt
);
576 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
577 syslog(LOG_ERR
, "%s: %m", ttyn
);
580 crmod
= digit
= lower
= upper
= 0;
584 if (read(STDIN_FILENO
, &cs
, 1) <= 0)
586 if ((c
= cs
&0177) == 0)
589 /* PPP detection state machine..
591 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
592 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
594 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
595 and Erik 'PPP' Olson, <eriko@wrq.com>
598 if (PP
&& (cs
== PPP_FRAME
)) {
600 } else if (ppp_state
== 1 && cs
== PPP_STATION
) {
602 } else if (ppp_state
== 2 && cs
== PPP_ESCAPE
) {
604 } else if ((ppp_state
== 2 && cs
== PPP_CONTROL
)
605 || (ppp_state
== 3 && cs
== PPP_CONTROL_ESCAPED
)) {
607 } else if (ppp_state
== 4 && cs
== PPP_LCP_HI
) {
609 } else if (ppp_state
== 5 && cs
== PPP_LCP_LOW
) {
616 if (c
== EOT
|| c
== CTRL('d'))
618 if (c
== '\r' || c
== '\n' || np
>= &name
[sizeof name
-1]) {
626 else if (c
== ERASE
|| c
== '\b' || c
== 0177) {
629 if (cfgetospeed(&tmode
) >= 1200)
635 } else if (c
== KILL
|| c
== CTRL('u')) {
637 if (cfgetospeed(&tmode
) < 1200)
639 /* this is the way they do it down under ... */
643 digit
= lower
= upper
= 0;
646 } else if (isdigit(c
))
648 if (IG
&& (c
<= ' ' || c
> 0176))
653 signal(SIGINT
, SIG_IGN
);
657 if ((upper
&& !lower
&& !LC
) || UC
)
658 for (np
= name
; *np
; np
++)
661 return (1 + ppp_connection
);
665 putpad(const char *s
)
668 speed_t ospeed
= cfgetospeed(&tmode
);
671 while (isdigit(*s
)) {
676 if (*s
== '.' && isdigit(s
[1])) {
684 * If no delay needed, or output speed is
685 * not comprehensible, then don't try to delay.
687 if (pad
== 0 || ospeed
<= 0)
691 * Round up by a half a character frame, and then do the delay.
692 * Too bad there are no user program accessible programmed delays.
693 * Transmitting pad characters slows many terminals down and also
696 pad
= (pad
* ospeed
+ 50000) / 100000;
708 char outbuf
[OBUFSIZ
];
718 c
|= partab
[c
&0177] & 0200;
723 outbuf
[obufcnt
++] = c
;
724 if (obufcnt
>= OBUFSIZ
)
727 write(STDOUT_FILENO
, &c
, 1);
734 write(STDOUT_FILENO
, outbuf
, obufcnt
);
752 static char linebuf
[512];
755 * This is certainly slow, but it avoids having to include
756 * stdio.h unnecessarily. Issue files should be small anyway.
758 while (i
< (sizeof linebuf
- 3) && read(fd
, linebuf
+i
, 1)==1) {
759 if (linebuf
[i
] == '\n') {
760 /* Don't rely on newline mode, assume raw */
769 return i
? linebuf
: 0;
775 extern char editedhost
[];
777 char *slash
, db
[100];
779 static struct utsname kerninfo
;
781 if (!*kerninfo
.sysname
)
792 slash
= strrchr(ttyn
, '/');
793 if (slash
== (char *) 0)
807 (void)setlocale(LC_TIME
, Lo
);
808 (void)strftime(db
, sizeof(db
), DF
, localtime(&t
));
813 puts(kerninfo
.sysname
);
817 puts(kerninfo
.machine
);
821 puts(kerninfo
.release
);
825 puts(kerninfo
.version
);
838 * Read a gettytab database entry and perform necessary quirks.
844 /* Read the database entry. */
845 gettable(tname
, tabent
);
848 * Avoid inheriting the parity values from the default entry
849 * if any of them is set in the current entry.
850 * Mixing different parity settings is unreasonable.
852 if (OPset
|| EPset
|| APset
|| NPset
)
853 OPset
= EPset
= APset
= NPset
= 1;
855 /* Fill in default values for unset capabilities. */