forget difference between big and small commands - obsolete with vm.
[minix.git] / commands / simple / getty.c
blob42d931f35561d4a8f0289c5c12d26f5d96585224
1 /* getty - get tty speed Author: Fred van Kempen */
3 /*
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
7 * Andrew S. Tanenbaum.
9 * Usage: getty [-c filename] [-h] [-k] [-t] line [speed]
11 * Version: 3.4 02/17/90
13 * Author: F. van Kempen, MicroWalt Corporation
15 * Modifications:
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
22 * working.)
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>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <errno.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)
50 void std_out(char *s)
52 write(1, s, strlen(s));
55 /* Read one character from stdin.
57 int readch(void)
59 int st;
60 char ch1;
62 /* read character from TTY */
63 st = read(0, &ch1, 1);
64 if (st == 0) {
65 std_out("\n");
66 exit(0);
68 if (st < 0) {
69 std_out("getty: ");
70 std_out(tty_name);
71 std_out(": read error\n");
72 exit(1);
74 return(ch1 & 0xFF);
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;
83 int ch;
84 struct utsname utsname;
85 char **banner, *t;
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;
91 /* Default banner? */
92 if (args[0] == NULL) args = def_banner;
94 /* Display prompt. */
95 ch = ' ';
96 *name = '\0';
97 while (ch != '\n') {
98 /* Get data about this machine. */
99 uname(&utsname);
101 /* Print the banner. */
102 for (banner = args; *banner != NULL; banner++) {
103 std_out(banner == args ? "\n" : " ");
104 s0 = *banner;
105 for (s = *banner; *s != 0; s++) {
106 if (*s == '\\') {
107 write(1, s0, s-s0);
108 s0 = s+2;
109 switch (*++s) {
110 case 'n': std_out("\n"); break;
111 case 's': std_out(" "); break;
112 case 't': std_out("\t"); break;
113 case 0: goto leave;
114 default: s0 = s;
116 } else
117 if (*s == '%') {
118 write(1, s0, s-s0);
119 s0 = s+2;
120 switch (*++s) {
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;
128 #if __minix_vmd
129 case 'k': std_out(utsname.kernel); break;
130 case 'h': std_out(utsname.hostname); break;
131 case 'b': std_out(utsname.bus); break;
132 #endif
133 case 0: goto leave;
134 default: s0 = s-1;
138 leave:
139 write(1, s0, s-s0);
142 np = name;
143 while ((ch = readch()) != '\n') {
144 if (np < name + len) *np++ = ch;
146 *np = '\0';
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)
158 struct stat st;
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 ");
165 std_out(LOGIN);
166 std_out("\n");
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)
175 register char *s;
176 char name[30];
177 struct sigaction sa;
179 /* Don't let QUIT dump core. */
180 sigemptyset(&sa.sa_mask);
181 sa.sa_flags = 0;
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");
188 pause();
189 return(1);
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 */