kernel: scheduling fix for ARM
[minix.git] / commands / getty / getty.c
blob14356c6b3d7d1b67d23676d997b4d86187d8cdd8
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 [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
31 * Removed unused custom banner and returned speed option
32 * functionality (by simply calling stty).
33 * 2012-09-24 T. Veerman
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <sys/utsname.h>
45 #include <stdio.h>
47 char LOGIN[] = "/usr/bin/login";
48 char SHELL[] = "/bin/sh";
50 char *tty_name; /* name of the line */
52 /* Crude indication of a tty being physically secure: */
53 #define securetty(dev) ((unsigned) ((dev) - 0x0400) < (unsigned) 8)
55 void std_out(const char *s)
57 write(1, s, strlen(s));
60 /* Read one character from stdin.
62 int readch(void)
64 int st;
65 char ch1;
67 /* read character from TTY */
68 st = read(0, &ch1, 1);
69 if (st == 0) {
70 std_out("\n");
71 exit(0);
73 if (st < 0) {
74 std_out("getty: ");
75 std_out(tty_name);
76 std_out(": read error\n");
77 exit(1);
79 return(ch1 & 0xFF);
83 /* Handle the process of a GETTY.
85 void do_getty(char *name, size_t len, char **args, const char *ttyname)
87 register char *np, *s, *s0;
88 int ch;
89 struct utsname utsname;
90 char **banner, *t;
91 static char *def_banner[] = { "%s Release %r Version %v (%t)\n\n%n login: ", 0 };
93 /* Clean up tty name. */
94 if((t = strrchr(ttyname, '/'))) ttyname = t + 1;
96 if (args[0] != NULL) {
97 char cmd[5+6+1]; /* "stty " + "115200" + '\0' */
98 int speed;
99 speed = atoi(args[0]);
100 snprintf(cmd, sizeof(cmd), "stty %d\n", speed);
101 system(cmd);
104 /* Display prompt. */
105 ch = ' ';
106 *name = '\0';
107 while (ch != '\n') {
108 /* Get data about this machine. */
109 uname(&utsname);
111 /* Print the banner. */
112 for (banner = def_banner; *banner != NULL; banner++) {
113 std_out(banner == args ? "\n" : " ");
114 s0 = *banner;
115 for (s = *banner; *s != 0; s++) {
116 if (*s == '\\') {
117 write(1, s0, s-s0);
118 s0 = s+2;
119 switch (*++s) {
120 case 'n': std_out("\n"); break;
121 case 's': std_out(" "); break;
122 case 't': std_out("\t"); break;
123 case 0: goto leave;
124 default: s0 = s;
126 } else
127 if (*s == '%') {
128 write(1, s0, s-s0);
129 s0 = s+2;
130 switch (*++s) {
131 case 's': std_out(utsname.sysname); break;
132 case 'n': std_out(utsname.nodename); break;
133 case 'r': std_out(utsname.release); break;
134 case 'v': std_out(utsname.version); break;
135 case 'm': std_out(utsname.machine); break;
136 case 'p': std_out(utsname.arch); break;
137 case 't': std_out(ttyname); break;
138 #if __minix_vmd
139 case 'k': std_out(utsname.kernel); break;
140 case 'h': std_out(utsname.hostname); break;
141 case 'b': std_out(utsname.bus); break;
142 #endif
143 case 0: goto leave;
144 default: s0 = s-1;
148 leave:
149 write(1, s0, s-s0);
152 np = name;
153 while ((ch = readch()) != '\n') {
154 if (np < name + len) *np++ = ch;
156 *np = '\0';
157 if (*name == '\0') ch = ' '; /* blank line typed! */
162 /* Execute the login(1) command with the current
163 * username as its argument. It will reply to the
164 * calling user by typing "Password: "...
166 void do_login(char *name)
168 struct stat st;
170 execl(LOGIN, LOGIN, name, (char *) NULL);
171 /* Failed to exec login. Impossible, but true. Try a shell, but only if
172 * the terminal is more or less secure, because it will be a root shell.
174 std_out("getty: can't exec ");
175 std_out(LOGIN);
176 std_out("\n");
177 if (fstat(0, &st) == 0 && S_ISCHR(st.st_mode) && securetty(st.st_rdev)) {
178 execl(SHELL, SHELL, (char *) NULL);
183 int main(int argc, char **argv)
185 char name[30];
186 struct sigaction sa;
188 /* Don't let QUIT dump core. */
189 sigemptyset(&sa.sa_mask);
190 sa.sa_flags = 0;
191 sa.sa_handler = exit;
192 sigaction(SIGQUIT, &sa, NULL);
194 tty_name = ttyname(0);
195 if (tty_name == NULL) {
196 std_out("getty: tty name unknown\n");
197 pause();
198 return(1);
201 chown(tty_name, 0, 0); /* set owner of TTY to root */
202 chmod(tty_name, 0600); /* mode to max secure */
204 do_getty(name, sizeof(name), argv+1, tty_name); /* handle getty() */
205 name[29] = '\0'; /* make sure the name fits! */
207 do_login(name); /* and call login(1) if OK */
209 return(1); /* never executed */