1 /* getty - get tty speed Author: Fred van Kempen */
4 * GETTY - Initialize and serve a login-terminal for INIT.
5 * Also, select the correct speed. The STTY() code
6 * was taken from stty(1).c; which was written by
9 * Usage: getty [-c filename] [-h] [-k] [-t] line [speed]
11 * Version: 3.4 02/17/90
13 * Author: F. van Kempen, MicroWalt Corporation
16 * All the good stuff removed to get a minimal getty, because
17 * many modems don't like all that fancy speed detection stuff.
18 * 03/03/91 Kees J. Bot (kjb@cs.vu.nl)
20 * Uname(), termios. More nonsense removed. (The result has
21 * only 10% of the original functionality, but a 10x chance of
23 * 12/12/92 Kees J. Bot
25 * Customizable login banner.
26 * 11/13/95 Kees J. Bot
28 * Suspend/resume signals removed.
29 * 2001-04-04 Kees J. Bot
32 #include <sys/types.h>
40 #include <sys/utsname.h>
42 char LOGIN
[] = "/usr/bin/login";
43 char SHELL
[] = "/bin/sh";
45 char *tty_name
; /* name of the line */
47 /* Crude indication of a tty being physically secure: */
48 #define securetty(dev) ((unsigned) ((dev) - 0x0400) < (unsigned) 8)
52 write(1, s
, strlen(s
));
55 /* Read one character from stdin.
62 /* read character from TTY */
63 st
= read(0, &ch1
, 1);
71 std_out(": read error\n");
78 /* Handle the process of a GETTY.
80 void do_getty(char *name
, size_t len
, char **args
, char *ttyname
)
82 register char *np
, *s
, *s0
;
84 struct utsname utsname
;
86 static char *def_banner
[] = { "%s Release %r Version %v (%t)\n\n%n login: ", 0 };
88 /* Clean up tty name. */
89 if((t
= strrchr(ttyname
, '/'))) ttyname
= t
+ 1;
92 if (args
[0] == NULL
) args
= def_banner
;
98 /* Get data about this machine. */
101 /* Print the banner. */
102 for (banner
= args
; *banner
!= NULL
; banner
++) {
103 std_out(banner
== args
? "\n" : " ");
105 for (s
= *banner
; *s
!= 0; s
++) {
110 case 'n': std_out("\n"); break;
111 case 's': std_out(" "); break;
112 case 't': std_out("\t"); break;
121 case 's': std_out(utsname
.sysname
); break;
122 case 'n': std_out(utsname
.nodename
); break;
123 case 'r': std_out(utsname
.release
); break;
124 case 'v': std_out(utsname
.version
); break;
125 case 'm': std_out(utsname
.machine
); break;
126 case 'p': std_out(utsname
.arch
); break;
127 case 't': std_out(ttyname
); break;
129 case 'k': std_out(utsname
.kernel
); break;
130 case 'h': std_out(utsname
.hostname
); break;
131 case 'b': std_out(utsname
.bus
); break;
143 while ((ch
= readch()) != '\n') {
144 if (np
< name
+ len
) *np
++ = ch
;
147 if (*name
== '\0') ch
= ' '; /* blank line typed! */
152 /* Execute the login(1) command with the current
153 * username as its argument. It will reply to the
154 * calling user by typing "Password: "...
156 void do_login(char *name
)
160 execl(LOGIN
, LOGIN
, name
, (char *) NULL
);
161 /* Failed to exec login. Impossible, but true. Try a shell, but only if
162 * the terminal is more or less secure, because it will be a root shell.
164 std_out("getty: can't exec ");
167 if (fstat(0, &st
) == 0 && S_ISCHR(st
.st_mode
) && securetty(st
.st_rdev
)) {
168 execl(SHELL
, SHELL
, (char *) NULL
);
173 int main(int argc
, char **argv
)
179 /* Don't let QUIT dump core. */
180 sigemptyset(&sa
.sa_mask
);
182 sa
.sa_handler
= exit
;
183 sigaction(SIGQUIT
, &sa
, NULL
);
185 tty_name
= ttyname(0);
186 if (tty_name
== NULL
) {
187 std_out("getty: tty name unknown\n");
192 chown(tty_name
, 0, 0); /* set owner of TTY to root */
193 chmod(tty_name
, 0600); /* mode to max secure */
195 do_getty(name
, sizeof(name
), argv
+1, tty_name
); /* handle getty() */
196 name
[29] = '\0'; /* make sure the name fits! */
198 do_login(name
); /* and call login(1) if OK */
200 return(1); /* never executed */