]>
git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/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>
80 #include <libiosexec.h>
84 #include "pathnames.h"
87 * Set the amount of running time that getty should accumulate
88 * before deciding that something is wrong and exit.
90 #define GETTY_TIMEOUT 60 /* seconds */
93 #define CTRL(x) (x&037)
95 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
97 #define PPP_FRAME 0x7e /* PPP Framing character */
98 #define PPP_STATION 0xff /* "All Station" character */
99 #define PPP_ESCAPE 0x7d /* Escape Character */
100 #define PPP_CONTROL 0x03 /* PPP Control Field */
101 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
102 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
103 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
105 /* original mode; flags've been reset using values from <sys/ttydefaults.h> */
106 struct termios omode
;
108 struct termios tmode
;
110 int crmod
, digit
, lower
, upper
;
112 char hostname
[MAXHOSTNAMELEN
];
113 char name
[MAXLOGNAME
*3];
114 char dev
[] = _PATH_DEV
;
118 #define TABBUFSIZ 512
125 0001,0201,0201,0001,0201,0001,0001,0201,
126 0202,0004,0003,0205,0005,0206,0201,0001,
127 0201,0001,0001,0201,0001,0201,0201,0001,
128 0001,0201,0201,0001,0201,0001,0001,0201,
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 0200,0000,0000,0200,0000,0200,0200,0000,
134 0000,0200,0200,0000,0200,0000,0000,0200,
135 0000,0200,0200,0000,0200,0000,0000,0200,
136 0200,0000,0000,0200,0000,0200,0200,0000,
137 0000,0200,0200,0000,0200,0000,0000,0200,
138 0200,0000,0000,0200,0000,0200,0200,0000,
139 0200,0000,0000,0200,0000,0200,0200,0000,
140 0000,0200,0200,0000,0200,0000,0000,0201
143 #define ERASE tmode.c_cc[VERASE]
144 #define KILL tmode.c_cc[VKILL]
145 #define EOT tmode.c_cc[VEOF]
149 static void defttymode(void);
150 static void dingdong(int);
151 static void dogettytab(void);
152 static int getname(void);
153 static void interrupt(int);
154 static void oflush(void);
155 static void prompt(void);
156 static void putchr(int);
157 static void putf(const char *);
158 static void putpad(const char *);
159 static void puts(const char *);
160 static void timeoverrun(int);
161 static char *getty_getline(int);
162 static void setttymode(int);
163 static int opentty(const char *, int);
168 dingdong(int signo __unused
)
177 interrupt(int signo __unused
)
183 * Action to take when getty is running too long.
186 timeoverrun(int signo __unused
)
189 syslog(LOG_ERR
, "getty exiting due to excessive running time");
194 main(int argc
, char *argv
[])
196 extern char **environ
;
197 int first_sleep
= 1, first_time
= 1;
201 int ttyopenmode
= O_RDWR
;
204 signal(SIGINT
, SIG_IGN
);
205 signal(SIGQUIT
, SIG_IGN
);
207 openlog("getty", LOG_ODELAY
|LOG_CONS
|LOG_PID
, LOG_AUTH
);
208 gethostname(hostname
, sizeof(hostname
) - 1);
209 hostname
[sizeof(hostname
) - 1] = '\0';
210 if (hostname
[0] == '\0')
211 strcpy(hostname
, "Amnesiac");
214 * Limit running time to deal with broken or dead lines.
216 (void)signal(SIGXCPU
, timeoverrun
);
217 limit
.rlim_max
= RLIM_INFINITY
;
218 limit
.rlim_cur
= GETTY_TIMEOUT
;
219 (void)setrlimit(RLIMIT_CPU
, &limit
);
228 * The following is a work around for vhangup interactions
229 * which cause great problems getting window systems started.
230 * If the tty line is "-", we do the old style getty presuming
231 * that the file descriptors are already set up for us.
232 * J. Gettys - MIT Project Athena.
234 if (argc
<= 2 || strcmp(argv
[2], "-") == 0)
237 // <rdar://problem/5178373>
238 char* n
= ttyname(STDIN_FILENO
);
240 strlcpy(ttyn
, n
, sizeof(ttyn
));
242 syslog(LOG_ERR
, "ttyname %m");
247 strcpy(ttyn
, ttyname(STDIN_FILENO
));
251 strncat(ttyn
, argv
[2], sizeof(ttyn
)-sizeof(dev
));
252 if (strcmp(argv
[0], "+") != 0) {
258 * Do the first scan through gettytab.
259 * Terminal mode parameters will be wrong until
260 * defttymode() called, but they're irrelevant for
261 * the initial setup of the terminal device.
265 #if defined(__APPLE__) && !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
266 if (strncmp(ttyn
, _PATH_CONSOLE
, sizeof(ttyn
)) == 0)
267 ttyopenmode
|= O_POPUP
;
270 * Init or answer modem sequence has been specified.
274 if (!opentty(ttyn
, ttyopenmode
))
276 if (!opentty(ttyn
, O_RDWR
|O_NONBLOCK
))
284 if (getty_chat(IC
, CT
, DC
) > 0) {
285 syslog(LOG_ERR
, "modem init problem on %s", ttyn
);
286 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
295 rfds
= 1 << 0; /* FD_SET */
298 i
= select(32, (fd_set
*)&rfds
, (fd_set
*)NULL
,
299 (fd_set
*)NULL
, RT
? &to
: NULL
);
301 syslog(LOG_ERR
, "select %s: %m", ttyn
);
303 syslog(LOG_NOTICE
, "recycle tty %s", ttyn
);
304 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
305 exit(0); /* recycle for init */
307 i
= getty_chat(AC
, CT
, DC
);
309 syslog(LOG_ERR
, "modem answer problem on %s", ttyn
);
310 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
313 } else { /* maybe blocking open */
315 if (!opentty(ttyn
, ttyopenmode
| (NC
? O_NONBLOCK
: 0 )))
317 if (!opentty(ttyn
, O_RDWR
| (NC
? O_NONBLOCK
: 0 )))
328 * if a delay was specified then sleep for that
329 * number of seconds before writing the initial prompt
331 if (first_sleep
&& DE
) {
333 /* remove any noise */
334 (void)tcflush(STDIN_FILENO
, TCIOFLUSH
);
345 tname
= portselector();
353 /* if this is the first time through this, and an
354 issue file has been given, then send it */
355 if (first_time
&& IF
) {
358 if ((fd
= open(IF
, O_RDONLY
)) != -1) {
361 while ((cp
= getty_getline(fd
)) != NULL
) {
369 if (IM
&& *IM
&& !(PL
&& PP
))
371 if (setjmp(timeout
)) {
372 cfsetispeed(&tmode
, B0
);
373 cfsetospeed(&tmode
, B0
);
374 (void)tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
);
378 signal(SIGALRM
, dingdong
);
387 while (*p
&& q
< &name
[sizeof name
- 1]) {
390 else if (islower(*p
))
392 else if (isdigit(*p
))
396 } else if (!(PL
&& PP
))
398 if (rval
== 2 || (PL
&& PP
)) {
401 limit
.rlim_max
= RLIM_INFINITY
;
402 limit
.rlim_cur
= RLIM_INFINITY
;
403 (void)setrlimit(RLIMIT_CPU
, &limit
);
404 execle(PP
, "ppplogin", ttyn
, (char *) 0, env
);
405 syslog(LOG_ERR
, "%s: %m", PP
);
407 } else if (rval
|| AL
) {
412 signal(SIGALRM
, SIG_DFL
);
415 if (name
[0] == '-') {
416 puts("user names may not start with '-'.");
419 if (!(upper
|| lower
|| digit
)) {
422 "invalid auto-login name: %s", AL
);
429 tmode
.c_iflag
|= ICRNL
;
430 tmode
.c_oflag
|= ONLCR
;
434 tmode
.sg_flags
|= LCASE
;
436 tmode
.sg_flags
&= ~LCASE
;
438 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
439 syslog(LOG_ERR
, "tcsetattr %s: %m", ttyn
);
442 signal(SIGINT
, SIG_DFL
);
443 for (i
= 0; environ
[i
] != (char *)0; i
++)
447 limit
.rlim_max
= RLIM_INFINITY
;
448 limit
.rlim_cur
= RLIM_INFINITY
;
449 (void)setrlimit(RLIMIT_CPU
, &limit
);
451 // <rdar://problem/3205179>
452 execle(LO
, "login", AL
? "-fp1" : "-p1", name
,
454 execle(LO
, "login", AL
? "-fp" : "-p", name
,
457 syslog(LOG_ERR
, "%s: %m", LO
);
461 signal(SIGALRM
, SIG_DFL
);
462 signal(SIGINT
, SIG_IGN
);
471 opentty(const char *tty
, int flags
)
474 int failopenlogged
= 0;
476 while ((i
= open(tty
, flags
)) == -1)
478 if (!failopenlogged
) {
479 syslog(LOG_ERR
, "open %s: %m", tty
);
484 if (login_tty(i
) < 0) {
486 if (daemon(0,0) < 0) {
487 syslog(LOG_ERR
,"daemon: %m");
492 if (login_tty(i
) < 0) {
493 syslog(LOG_ERR
, "login_tty %s: %m", tty
);
505 /* Start with default tty settings. */
506 if (tcgetattr(STDIN_FILENO
, &tmode
) < 0) {
507 syslog(LOG_ERR
, "tcgetattr %s: %m", ttyn
);
510 omode
= tmode
; /* fill c_cc for dogettytab() */
513 * Don't rely on the driver too much, and initialize crucial
514 * things according to <sys/ttydefaults.h>. Avoid clobbering
515 * the c_cc[] settings however, the console drivers might wish
516 * to leave their idea of the preferred VERASE key value
519 tmode
.c_iflag
= TTYDEF_IFLAG
;
520 tmode
.c_oflag
= TTYDEF_OFLAG
;
521 tmode
.c_lflag
= TTYDEF_LFLAG
;
522 tmode
.c_cflag
= TTYDEF_CFLAG
;
524 tmode
.c_cflag
|= CLOCAL
;
533 (void)tcflush(STDIN_FILENO
, TCIOFLUSH
); /* clear out the crap */
534 ioctl(STDIN_FILENO
, FIONBIO
, &off
); /* turn off non-blocking mode */
535 ioctl(STDIN_FILENO
, FIOASYNC
, &off
); /* ditto for async mode */
538 cfsetispeed(&tmode
, speed(IS
));
540 cfsetispeed(&tmode
, speed(SP
));
542 cfsetospeed(&tmode
, speed(OS
));
544 cfsetospeed(&tmode
, speed(SP
));
549 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
550 syslog(LOG_ERR
, "tcsetattr %s: %m", ttyn
);
563 int ppp_connection
= 0;
566 * Interrupt may happen if we use CBREAK mode
568 if (setjmp(intrupt
)) {
569 signal(SIGINT
, SIG_IGN
);
572 signal(SIGINT
, interrupt
);
580 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
581 syslog(LOG_ERR
, "%s: %m", ttyn
);
584 crmod
= digit
= lower
= upper
= 0;
588 if (read(STDIN_FILENO
, &cs
, 1) <= 0)
590 if ((c
= cs
&0177) == 0)
593 /* PPP detection state machine..
595 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
596 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
598 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
599 and Erik 'PPP' Olson, <eriko@wrq.com>
602 if (PP
&& (cs
== PPP_FRAME
)) {
604 } else if (ppp_state
== 1 && cs
== PPP_STATION
) {
606 } else if (ppp_state
== 2 && cs
== PPP_ESCAPE
) {
608 } else if ((ppp_state
== 2 && cs
== PPP_CONTROL
)
609 || (ppp_state
== 3 && cs
== PPP_CONTROL_ESCAPED
)) {
611 } else if (ppp_state
== 4 && cs
== PPP_LCP_HI
) {
613 } else if (ppp_state
== 5 && cs
== PPP_LCP_LOW
) {
620 if (c
== EOT
|| c
== CTRL('d'))
622 if (c
== '\r' || c
== '\n' || np
>= &name
[sizeof name
-1]) {
630 else if (c
== ERASE
|| c
== '\b' || c
== 0177) {
633 if (cfgetospeed(&tmode
) >= 1200)
639 } else if (c
== KILL
|| c
== CTRL('u')) {
641 if (cfgetospeed(&tmode
) < 1200)
643 /* this is the way they do it down under ... */
647 digit
= lower
= upper
= 0;
650 } else if (isdigit(c
))
652 if (IG
&& (c
<= ' ' || c
> 0176))
657 signal(SIGINT
, SIG_IGN
);
661 if ((upper
&& !lower
&& !LC
) || UC
)
662 for (np
= name
; *np
; np
++)
665 return (1 + ppp_connection
);
669 putpad(const char *s
)
672 speed_t ospeed
= cfgetospeed(&tmode
);
675 while (isdigit(*s
)) {
680 if (*s
== '.' && isdigit(s
[1])) {
688 * If no delay needed, or output speed is
689 * not comprehensible, then don't try to delay.
691 if (pad
== 0 || ospeed
<= 0)
695 * Round up by a half a character frame, and then do the delay.
696 * Too bad there are no user program accessible programmed delays.
697 * Transmitting pad characters slows many terminals down and also
700 pad
= (pad
* ospeed
+ 50000) / 100000;
712 char outbuf
[OBUFSIZ
];
722 c
|= partab
[c
&0177] & 0200;
727 outbuf
[obufcnt
++] = c
;
728 if (obufcnt
>= OBUFSIZ
)
731 write(STDOUT_FILENO
, &c
, 1);
738 write(STDOUT_FILENO
, outbuf
, obufcnt
);
753 getty_getline(int fd
)
756 static char linebuf
[512];
759 * This is certainly slow, but it avoids having to include
760 * stdio.h unnecessarily. Issue files should be small anyway.
762 while (i
< (sizeof linebuf
- 3) && read(fd
, linebuf
+i
, 1)==1) {
763 if (linebuf
[i
] == '\n') {
764 /* Don't rely on newline mode, assume raw */
773 return i
? linebuf
: 0;
779 extern char editedhost
[];
781 char *slash
, db
[100];
783 static struct utsname kerninfo
;
785 if (!*kerninfo
.sysname
)
796 slash
= strrchr(ttyn
, '/');
797 if (slash
== (char *) 0)
811 (void)setlocale(LC_TIME
, Lo
);
812 (void)strftime(db
, sizeof(db
), DF
, localtime(&t
));
817 puts(kerninfo
.sysname
);
821 puts(kerninfo
.machine
);
825 puts(kerninfo
.release
);
829 puts(kerninfo
.version
);
842 * Read a gettytab database entry and perform necessary quirks.
847 /* Read the database entry. */
851 * Avoid inheriting the parity values from the default entry
852 * if any of them is set in the current entry.
853 * Mixing different parity settings is unreasonable.
855 if (OPset
|| EPset
|| APset
|| NPset
)
856 OPset
= EPset
= APset
= NPset
= 1;
858 /* Fill in default values for unset capabilities. */