]>
git.saurik.com Git - apple/system_cmds.git/blob - getty.tproj/main.c
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 __unused
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 __unused
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>
77 #include <TargetConditionals.h>
82 #include "pathnames.h"
85 * Set the amount of running time that getty should accumulate
86 * before deciding that something is wrong and exit.
88 #define GETTY_TIMEOUT 60 /* seconds */
91 #define CTRL(x) (x&037)
93 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
95 #define PPP_FRAME 0x7e /* PPP Framing character */
96 #define PPP_STATION 0xff /* "All Station" character */
97 #define PPP_ESCAPE 0x7d /* Escape Character */
98 #define PPP_CONTROL 0x03 /* PPP Control Field */
99 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
100 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
101 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
103 /* original mode; flags've been reset using values from <sys/ttydefaults.h> */
104 struct termios omode
;
106 struct termios tmode
;
108 int crmod
, digit
, lower
, upper
;
110 char hostname
[MAXHOSTNAMELEN
];
111 char name
[MAXLOGNAME
*3];
112 char dev
[] = _PATH_DEV
;
116 #define TABBUFSIZ 512
123 0001,0201,0201,0001,0201,0001,0001,0201,
124 0202,0004,0003,0205,0005,0206,0201,0001,
125 0201,0001,0001,0201,0001,0201,0201,0001,
126 0001,0201,0201,0001,0201,0001,0001,0201,
127 0200,0000,0000,0200,0000,0200,0200,0000,
128 0000,0200,0200,0000,0200,0000,0000,0200,
129 0000,0200,0200,0000,0200,0000,0000,0200,
130 0200,0000,0000,0200,0000,0200,0200,0000,
131 0200,0000,0000,0200,0000,0200,0200,0000,
132 0000,0200,0200,0000,0200,0000,0000,0200,
133 0000,0200,0200,0000,0200,0000,0000,0200,
134 0200,0000,0000,0200,0000,0200,0200,0000,
135 0000,0200,0200,0000,0200,0000,0000,0200,
136 0200,0000,0000,0200,0000,0200,0200,0000,
137 0200,0000,0000,0200,0000,0200,0200,0000,
138 0000,0200,0200,0000,0200,0000,0000,0201
141 #define ERASE tmode.c_cc[VERASE]
142 #define KILL tmode.c_cc[VKILL]
143 #define EOT tmode.c_cc[VEOF]
147 static void defttymode(void);
148 static void dingdong(int);
149 static void dogettytab(void);
150 static int getname(void);
151 static void interrupt(int);
152 static void oflush(void);
153 static void prompt(void);
154 static void putchr(int);
155 static void putf(const char *);
156 static void putpad(const char *);
157 static void puts(const char *);
158 static void timeoverrun(int);
159 static char *getty_getline(int);
160 static void setttymode(int);
161 static int opentty(const char *, int);
166 dingdong(int signo __unused
)
175 interrupt(int signo __unused
)
181 * Action to take when getty is running too long.
184 timeoverrun(int signo __unused
)
187 syslog(LOG_ERR
, "getty exiting due to excessive running time");
192 main(int argc
, char *argv
[])
194 extern char **environ
;
195 int first_sleep
= 1, first_time
= 1;
199 int ttyopenmode
= O_RDWR
;
202 signal(SIGINT
, SIG_IGN
);
203 signal(SIGQUIT
, SIG_IGN
);
205 openlog("getty", LOG_ODELAY
|LOG_CONS
|LOG_PID
, LOG_AUTH
);
206 gethostname(hostname
, sizeof(hostname
) - 1);
207 hostname
[sizeof(hostname
) - 1] = '\0';
208 if (hostname
[0] == '\0')
209 strcpy(hostname
, "Amnesiac");
212 * Limit running time to deal with broken or dead lines.
214 (void)signal(SIGXCPU
, timeoverrun
);
215 limit
.rlim_max
= RLIM_INFINITY
;
216 limit
.rlim_cur
= GETTY_TIMEOUT
;
217 (void)setrlimit(RLIMIT_CPU
, &limit
);
226 * The following is a work around for vhangup interactions
227 * which cause great problems getting window systems started.
228 * If the tty line is "-", we do the old style getty presuming
229 * that the file descriptors are already set up for us.
230 * J. Gettys - MIT Project Athena.
232 if (argc
<= 2 || strcmp(argv
[2], "-") == 0)
235 // <rdar://problem/5178373>
236 char* n
= ttyname(STDIN_FILENO
);
238 strlcpy(ttyn
, n
, sizeof(ttyn
));
240 syslog(LOG_ERR
, "ttyname %m");
245 strcpy(ttyn
, ttyname(STDIN_FILENO
));
249 strncat(ttyn
, argv
[2], sizeof(ttyn
)-sizeof(dev
));
250 if (strcmp(argv
[0], "+") != 0) {
256 * Do the first scan through gettytab.
257 * Terminal mode parameters will be wrong until
258 * defttymode() called, but they're irrelevant for
259 * the initial setup of the terminal device.
263 #if defined(__APPLE__) && !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
264 if (strncmp(ttyn
, _PATH_CONSOLE
, sizeof(ttyn
)) == 0)
265 ttyopenmode
|= O_POPUP
;
268 * Init or answer modem sequence has been specified.
272 if (!opentty(ttyn
, ttyopenmode
))
274 if (!opentty(ttyn
, O_RDWR
|O_NONBLOCK
))
282 if (getty_chat(IC
, CT
, DC
) > 0) {
283 syslog(LOG_ERR
, "modem init problem on %s", ttyn
);
284 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
293 rfds
= 1 << 0; /* FD_SET */
296 i
= select(32, (fd_set
*)&rfds
, (fd_set
*)NULL
,
297 (fd_set
*)NULL
, RT
? &to
: NULL
);
299 syslog(LOG_ERR
, "select %s: %m", ttyn
);
301 syslog(LOG_NOTICE
, "recycle tty %s", ttyn
);
302 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
303 exit(0); /* recycle for init */
305 i
= getty_chat(AC
, CT
, DC
);
307 syslog(LOG_ERR
, "modem answer problem on %s", ttyn
);
308 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
311 } else { /* maybe blocking open */
313 if (!opentty(ttyn
, ttyopenmode
| (NC
? O_NONBLOCK
: 0 )))
315 if (!opentty(ttyn
, O_RDWR
| (NC
? O_NONBLOCK
: 0 )))
326 * if a delay was specified then sleep for that
327 * number of seconds before writing the initial prompt
329 if (first_sleep
&& DE
) {
331 /* remove any noise */
332 (void)tcflush(STDIN_FILENO
, TCIOFLUSH
);
343 tname
= portselector();
351 /* if this is the first time through this, and an
352 issue file has been given, then send it */
353 if (first_time
&& IF
) {
356 if ((fd
= open(IF
, O_RDONLY
)) != -1) {
359 while ((cp
= getty_getline(fd
)) != NULL
) {
367 if (IM
&& *IM
&& !(PL
&& PP
))
369 if (setjmp(timeout
)) {
370 cfsetispeed(&tmode
, B0
);
371 cfsetospeed(&tmode
, B0
);
372 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
376 signal(SIGALRM
, dingdong
);
385 while (*p
&& q
< &name
[sizeof name
- 1]) {
388 else if (islower(*p
))
390 else if (isdigit(*p
))
394 } else if (!(PL
&& PP
))
396 if (rval
== 2 || (PL
&& PP
)) {
399 limit
.rlim_max
= RLIM_INFINITY
;
400 limit
.rlim_cur
= RLIM_INFINITY
;
401 (void)setrlimit(RLIMIT_CPU
, &limit
);
402 execle(PP
, "ppplogin", ttyn
, (char *) 0, env
);
403 syslog(LOG_ERR
, "%s: %m", PP
);
405 } else if (rval
|| AL
) {
410 signal(SIGALRM
, SIG_DFL
);
413 if (name
[0] == '-') {
414 puts("user names may not start with '-'.");
417 if (!(upper
|| lower
|| digit
)) {
420 "invalid auto-login name: %s", AL
);
427 tmode
.c_iflag
|= ICRNL
;
428 tmode
.c_oflag
|= ONLCR
;
432 tmode
.sg_flags
|= LCASE
;
434 tmode
.sg_flags
&= ~LCASE
;
436 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
437 syslog(LOG_ERR
, "tcsetattr %s: %m", ttyn
);
440 signal(SIGINT
, SIG_DFL
);
441 for (i
= 0; environ
[i
] != (char *)0; i
++)
445 limit
.rlim_max
= RLIM_INFINITY
;
446 limit
.rlim_cur
= RLIM_INFINITY
;
447 (void)setrlimit(RLIMIT_CPU
, &limit
);
449 // <rdar://problem/3205179>
450 execle(LO
, "login", AL
? "-fp1" : "-p1", name
,
452 execle(LO
, "login", AL
? "-fp" : "-p", name
,
455 syslog(LOG_ERR
, "%s: %m", LO
);
459 signal(SIGALRM
, SIG_DFL
);
460 signal(SIGINT
, SIG_IGN
);
469 opentty(const char *tty
, int flags
)
472 int failopenlogged
= 0;
474 while ((i
= open(tty
, flags
)) == -1)
476 if (!failopenlogged
) {
477 syslog(LOG_ERR
, "open %s: %m", tty
);
482 if (login_tty(i
) < 0) {
484 if (daemon(0,0) < 0) {
485 syslog(LOG_ERR
,"daemon: %m");
490 if (login_tty(i
) < 0) {
491 syslog(LOG_ERR
, "login_tty %s: %m", tty
);
503 /* Start with default tty settings. */
504 if (tcgetattr(STDIN_FILENO
, &tmode
) < 0) {
505 syslog(LOG_ERR
, "tcgetattr %s: %m", ttyn
);
508 omode
= tmode
; /* fill c_cc for dogettytab() */
511 * Don't rely on the driver too much, and initialize crucial
512 * things according to <sys/ttydefaults.h>. Avoid clobbering
513 * the c_cc[] settings however, the console drivers might wish
514 * to leave their idea of the preferred VERASE key value
517 tmode
.c_iflag
= TTYDEF_IFLAG
;
518 tmode
.c_oflag
= TTYDEF_OFLAG
;
519 tmode
.c_lflag
= TTYDEF_LFLAG
;
520 tmode
.c_cflag
= TTYDEF_CFLAG
;
522 tmode
.c_cflag
|= CLOCAL
;
531 (void)tcflush(STDIN_FILENO
, TCIOFLUSH
); /* clear out the crap */
532 ioctl(STDIN_FILENO
, FIONBIO
, &off
); /* turn off non-blocking mode */
533 ioctl(STDIN_FILENO
, FIOASYNC
, &off
); /* ditto for async mode */
536 cfsetispeed(&tmode
, speed(IS
));
538 cfsetispeed(&tmode
, speed(SP
));
540 cfsetospeed(&tmode
, speed(OS
));
542 cfsetospeed(&tmode
, speed(SP
));
547 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
548 syslog(LOG_ERR
, "tcsetattr %s: %m", ttyn
);
561 int ppp_connection
= 0;
564 * Interrupt may happen if we use CBREAK mode
566 if (setjmp(intrupt
)) {
567 signal(SIGINT
, SIG_IGN
);
570 signal(SIGINT
, interrupt
);
578 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
579 syslog(LOG_ERR
, "%s: %m", ttyn
);
582 crmod
= digit
= lower
= upper
= 0;
586 if (read(STDIN_FILENO
, &cs
, 1) <= 0)
588 if ((c
= cs
&0177) == 0)
591 /* PPP detection state machine..
593 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
594 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
596 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
597 and Erik 'PPP' Olson, <eriko@wrq.com>
600 if (PP
&& (cs
== PPP_FRAME
)) {
602 } else if (ppp_state
== 1 && cs
== PPP_STATION
) {
604 } else if (ppp_state
== 2 && cs
== PPP_ESCAPE
) {
606 } else if ((ppp_state
== 2 && cs
== PPP_CONTROL
)
607 || (ppp_state
== 3 && cs
== PPP_CONTROL_ESCAPED
)) {
609 } else if (ppp_state
== 4 && cs
== PPP_LCP_HI
) {
611 } else if (ppp_state
== 5 && cs
== PPP_LCP_LOW
) {
618 if (c
== EOT
|| c
== CTRL('d'))
620 if (c
== '\r' || c
== '\n' || np
>= &name
[sizeof name
-1]) {
628 else if (c
== ERASE
|| c
== '\b' || c
== 0177) {
631 if (cfgetospeed(&tmode
) >= 1200)
637 } else if (c
== KILL
|| c
== CTRL('u')) {
639 if (cfgetospeed(&tmode
) < 1200)
641 /* this is the way they do it down under ... */
645 digit
= lower
= upper
= 0;
648 } else if (isdigit(c
))
650 if (IG
&& (c
<= ' ' || c
> 0176))
655 signal(SIGINT
, SIG_IGN
);
659 if ((upper
&& !lower
&& !LC
) || UC
)
660 for (np
= name
; *np
; np
++)
663 return (1 + ppp_connection
);
667 putpad(const char *s
)
670 speed_t ospeed
= cfgetospeed(&tmode
);
673 while (isdigit(*s
)) {
678 if (*s
== '.' && isdigit(s
[1])) {
686 * If no delay needed, or output speed is
687 * not comprehensible, then don't try to delay.
689 if (pad
== 0 || ospeed
<= 0)
693 * Round up by a half a character frame, and then do the delay.
694 * Too bad there are no user program accessible programmed delays.
695 * Transmitting pad characters slows many terminals down and also
698 pad
= (pad
* ospeed
+ 50000) / 100000;
710 char outbuf
[OBUFSIZ
];
720 c
|= partab
[c
&0177] & 0200;
725 outbuf
[obufcnt
++] = c
;
726 if (obufcnt
>= OBUFSIZ
)
729 write(STDOUT_FILENO
, &c
, 1);
736 write(STDOUT_FILENO
, outbuf
, obufcnt
);
751 getty_getline(int fd
)
754 static char linebuf
[512];
757 * This is certainly slow, but it avoids having to include
758 * stdio.h unnecessarily. Issue files should be small anyway.
760 while (i
< (sizeof linebuf
- 3) && read(fd
, linebuf
+i
, 1)==1) {
761 if (linebuf
[i
] == '\n') {
762 /* Don't rely on newline mode, assume raw */
771 return i
? linebuf
: 0;
777 extern char editedhost
[];
779 char *slash
, db
[100];
781 static struct utsname kerninfo
;
783 if (!*kerninfo
.sysname
)
794 slash
= strrchr(ttyn
, '/');
795 if (slash
== (char *) 0)
809 (void)setlocale(LC_TIME
, Lo
);
810 (void)strftime(db
, sizeof(db
), DF
, localtime(&t
));
815 puts(kerninfo
.sysname
);
819 puts(kerninfo
.machine
);
823 puts(kerninfo
.release
);
827 puts(kerninfo
.version
);
840 * Read a gettytab database entry and perform necessary quirks.
845 /* Read the database entry. */
849 * Avoid inheriting the parity values from the default entry
850 * if any of them is set in the current entry.
851 * Mixing different parity settings is unreasonable.
853 if (OPset
|| EPset
|| APset
|| NPset
)
854 OPset
= EPset
= APset
= NPset
= 1;
856 /* Fill in default values for unset capabilities. */