init.c revision 1.7 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1986, 1987, 1992 Daniel D. Lanciani.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by
16 1.1 cgd * Daniel D. Lanciani.
17 1.1 cgd * 4. The name of the author may not
18 1.1 cgd * be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY Daniel D. Lanciani ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL Daniel D. Lanciani BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.7 cgd *
33 1.7 cgd * $Id: init.c,v 1.7 1993/06/10 01:03:02 cgd Exp $
34 1.1 cgd */
35 1.3 cgd
36 1.1 cgd #include <sys/types.h>
37 1.1 cgd #include <sys/errno.h>
38 1.1 cgd #include <sys/signal.h>
39 1.1 cgd #include <sys/wait.h>
40 1.1 cgd #include <setjmp.h>
41 1.1 cgd #include <ttyent.h>
42 1.1 cgd #include <unistd.h>
43 1.1 cgd
44 1.5 cgd #ifdef SECURE_CONSOLE
45 1.5 cgd #include <pwd.h>
46 1.5 cgd #endif
47 1.5 cgd
48 1.7 cgd #ifdef USE_DEVFS
49 1.7 cgd #include <sys/mount.h>
50 1.7 cgd #endif
51 1.7 cgd
52 1.1 cgd #define NTTY 32 /* max ttys */
53 1.1 cgd #define NARG 16 /* max args to login/getty */
54 1.1 cgd
55 1.1 cgd /* internal flags */
56 1.1 cgd #define TTY_SEEN 0x8000
57 1.1 cgd #define TTY_DIFF 0x4000
58 1.1 cgd #define TTY_LOGIN 0x2000
59 1.1 cgd
60 1.1 cgd /* non-standard tty_logout: rerun login/getty with -o switch to clean line */
61 1.1 cgd #ifndef TTY_LOGOUT
62 1.1 cgd #define TTY_LOGOUT 0x1000
63 1.1 cgd #endif
64 1.1 cgd
65 1.1 cgd /* non-standard tty_open: open device for login/getty */
66 1.1 cgd #ifndef TTY_OPEN
67 1.1 cgd #define TTY_OPEN 0x0800
68 1.1 cgd #endif
69 1.1 cgd
70 1.1 cgd #define isspace(c) ((c) == ' ' || (c) == '\t')
71 1.1 cgd
72 1.1 cgd struct ttytab {
73 1.1 cgd char *tt_name;
74 1.1 cgd char *tt_getty;
75 1.1 cgd char *tt_type;
76 1.1 cgd int tt_status;
77 1.1 cgd int tt_pid;
78 1.1 cgd } ttytab[NTTY], *ttytabend = ttytab;
79 1.1 cgd int drain, sflag;
80 1.1 cgd char arg[128], nam[64], term[64], *env[] = { term, 0 };
81 1.1 cgd jmp_buf single, reread;
82 1.1 cgd char *Reboot = "autoboot";
83 1.1 cgd
84 1.1 cgd char *newstring(), *malloc();
85 1.1 cgd extern int errno;
86 1.1 cgd
87 1.1 cgd /* signal state of child process */
88 1.1 cgd #define SIGNALSFORCHILD \
89 1.1 cgd signal(SIGHUP, SIG_DFL); signal(SIGINT, SIG_DFL); \
90 1.1 cgd signal(SIGTERM, SIG_DFL); signal(SIGALRM, SIG_DFL); \
91 1.1 cgd signal(SIGTSTP, SIG_DFL); signal(SIGCHLD, SIG_DFL); \
92 1.2 cgd signal(SIGTTIN, SIG_DFL); signal(SIGTTOU, SIG_DFL); \
93 1.2 cgd sigsetmask( 0); /* 04 Sep 92*/
94 1.1 cgd
95 1.1 cgd /* SIGHUP: reread /etc/ttys */
96 1.1 cgd void
97 1.1 cgd shup(sig)
98 1.1 cgd {
99 1.1 cgd longjmp(reread, 1);
100 1.1 cgd }
101 1.1 cgd
102 1.1 cgd /* SIGALRM: abort wait and go single user */
103 1.1 cgd void
104 1.1 cgd salrm(sig)
105 1.1 cgd {
106 1.1 cgd signal(SIGALRM, SIG_DFL);
107 1.1 cgd warn("process hung");
108 1.1 cgd longjmp(single, 1);
109 1.1 cgd }
110 1.1 cgd
111 1.1 cgd /* SIGTERM: go single user */
112 1.1 cgd void
113 1.1 cgd sterm(sig)
114 1.1 cgd {
115 1.1 cgd register struct ttytab *tt;
116 1.1 cgd
117 1.1 cgd if (!Reboot) {
118 1.1 cgd for(tt = ttytab; tt < ttytabend; tt++) {
119 1.1 cgd free(tt->tt_name);
120 1.1 cgd free(tt->tt_getty);
121 1.1 cgd free(tt->tt_type);
122 1.1 cgd }
123 1.1 cgd ttytabend = ttytab;
124 1.2 cgd /* give processes time to exit cleanly */ /* 15 Aug 92*/
125 1.2 cgd kill(-1, SIGTERM);
126 1.2 cgd sleep(10);
127 1.2 cgd /* Now murder them */
128 1.1 cgd kill(-1, SIGKILL);
129 1.1 cgd kill(-1, SIGCONT);
130 1.1 cgd signal(SIGALRM, salrm);
131 1.1 cgd alarm(30);
132 1.1 cgd while(wait((int *)0) > 0);
133 1.1 cgd alarm(0);
134 1.1 cgd signal(SIGALRM, SIG_DFL);
135 1.1 cgd longjmp(single, 1);
136 1.1 cgd }
137 1.1 cgd }
138 1.1 cgd
139 1.1 cgd /* SIGTSTP: drain system */
140 1.1 cgd void
141 1.1 cgd ststp(sig)
142 1.1 cgd {
143 1.1 cgd drain = 1;
144 1.1 cgd }
145 1.1 cgd
146 1.1 cgd /* init [-s] [-f] */
147 1.1 cgd
148 1.1 cgd main(argc, argv)
149 1.1 cgd char **argv;
150 1.1 cgd {
151 1.1 cgd register int pid;
152 1.1 cgd register struct ttytab *tt;
153 1.1 cgd struct ttyent *ty;
154 1.1 cgd int status;
155 1.1 cgd long mask = sigblock(sigmask(SIGHUP) | sigmask(SIGTERM));
156 1.1 cgd
157 1.1 cgd /* did some idiot try to run us? */
158 1.1 cgd if(getpid() != 1) {
159 1.1 cgd writes(2,"init: sorry, system daemon, runnable only by system\n");
160 1.1 cgd exit(0xff);
161 1.1 cgd }
162 1.1 cgd
163 1.1 cgd /* allocate a session for init */
164 1.1 cgd (void) setsid();
165 1.1 cgd
166 1.1 cgd /* protect against signals, listen for outside requests */
167 1.1 cgd signal(SIGHUP, shup);
168 1.1 cgd signal(SIGTSTP, ststp);
169 1.1 cgd
170 1.1 cgd signal (SIGTTIN, SIG_IGN);
171 1.1 cgd signal (SIGTTOU, SIG_IGN);
172 1.1 cgd signal (SIGCHLD, SIG_IGN);
173 1.1 cgd signal (SIGINT, SIG_IGN);
174 1.1 cgd
175 1.1 cgd /* handle arguments, if any */
176 1.1 cgd if(argc > 1)
177 1.1 cgd if(!strcmp(argv[1], "-s"))
178 1.1 cgd sflag++;
179 1.1 cgd else if(!strcmp(argv[1], "-f"))
180 1.1 cgd Reboot = 0;
181 1.7 cgd
182 1.7 cgd #ifdef USE_DEVFS
183 1.7 cgd if (mount(MOUNT_DEVFS, "/dev", 0, (caddr_t) 0) < 0) {
184 1.7 cgd writes(2, "init: couldn't mount /dev\n");
185 1.7 cgd perror( " mount");
186 1.7 cgd writes(2, " trying to continue...\n");
187 1.7 cgd }
188 1.7 cgd #endif
189 1.7 cgd
190 1.1 cgd top:
191 1.1 cgd /* Single user mode? */
192 1.1 cgd if(sflag) {
193 1.1 cgd sflag = 0;
194 1.1 cgd status = 1;
195 1.1 cgd } else {
196 1.1 cgd /* otherwise, execute /etc/rc */
197 1.1 cgd if (access("/etc/rc", F_OK) == 0) {
198 1.1 cgd
199 1.1 cgd signal(SIGTERM, SIG_IGN); /* XXX */
200 1.1 cgd if((pid = fork()) < 0)
201 1.1 cgd fatal("fork");
202 1.1 cgd else if(!pid) {
203 1.1 cgd /* signals, to default state */
204 1.1 cgd SIGNALSFORCHILD;
205 1.1 cgd
206 1.1 cgd /* clean off console */
207 1.1 cgd revoke("/dev/console");
208 1.1 cgd
209 1.1 cgd /* create a shell */
210 1.1 cgd login_tty(open("/dev/console", 2));
211 1.1 cgd execl("/bin/sh", "sh", "/etc/rc", Reboot, (char *)0);
212 1.1 cgd _exit(127);
213 1.1 cgd }
214 1.2 cgd Reboot = 0; /* 31 Jul 92*/
215 1.1 cgd while(wait(&status) != pid);
216 1.1 cgd
217 1.1 cgd /* if we are about to be rebooted, then wait for it */
218 1.1 cgd if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)
219 1.1 cgd pause();
220 1.1 cgd } else { status = 1; sflag = 1; goto top; }
221 1.1 cgd }
222 1.1 cgd signal(SIGTERM, sterm);
223 1.1 cgd Reboot = 0;
224 1.1 cgd
225 1.1 cgd /* do single user shell on console */
226 1.1 cgd if (setjmp(single) || status) {
227 1.5 cgd #ifdef SECURE_CONSOLE
228 1.5 cgd struct ttyent *ttyp;
229 1.5 cgd struct passwd *passp;
230 1.5 cgd char *pass;
231 1.5 cgd static const char banner[] =
232 1.5 cgd "Enter root password, or Control-D to go multi-user\n";
233 1.5 cgd #endif
234 1.5 cgd
235 1.1 cgd if((pid = fork()) < 0)
236 1.1 cgd fatal("fork");
237 1.1 cgd else if(!pid) {
238 1.1 cgd /* signals, to default state */
239 1.1 cgd SIGNALSFORCHILD;
240 1.1 cgd
241 1.1 cgd /* clean off console */
242 1.1 cgd revoke("/dev/console");
243 1.1 cgd
244 1.1 cgd /* do open and configuration of console */
245 1.1 cgd login_tty(open("/dev/console", 2));
246 1.5 cgd #ifdef SECURE_CONSOLE
247 1.5 cgd /* if the console isn't secure, check the root PW */
248 1.5 cgd ttyp = getttynam("console");
249 1.5 cgd if (!ttyp) {
250 1.5 cgd /* don't have an entry for "console", probably
251 1.5 cgd * have one for /dev/vga
252 1.5 cgd */
253 1.5 cgd ttyp = getttynam("vga");
254 1.5 cgd }
255 1.5 cgd passp = getpwnam("root");
256 1.5 cgd if (ttyp && ((ttyp->ty_status & TTY_SECURE) == 0) &&
257 1.5 cgd passp) {
258 1.5 cgd write(2, banner, sizeof(banner) - 1);
259 1.5 cgd do {
260 1.5 cgd pass = getpass("Password:");
261 1.5 cgd if ((pass == 0) || (*pass == '\0'))
262 1.5 cgd _exit(0); /* got control-d */
263 1.5 cgd pass = crypt(pass, passp->pw_passwd);
264 1.5 cgd } while (strcmp(pass, passp->pw_passwd) != 0);
265 1.5 cgd }
266 1.5 cgd #endif
267 1.1 cgd execl("/bin/sh", "-", (char *)0);
268 1.1 cgd _exit(127);
269 1.1 cgd }
270 1.2 cgd while(wait(&status) != pid);
271 1.2 cgd while(drain) /* 31 Jul 92*/
272 1.2 cgd pause();
273 1.1 cgd goto top;
274 1.1 cgd }
275 1.1 cgd
276 1.1 cgd /* multiuser mode, traipse through table */
277 1.1 cgd setttyent();
278 1.1 cgd for(tt = ttytab; (ty = getttyent()) && tt < &ttytab[NTTY]; tt++) {
279 1.1 cgd tt->tt_name = newstring(ty->ty_name);
280 1.1 cgd tt->tt_getty = newstring(ty->ty_getty);
281 1.1 cgd tt->tt_type = newstring(ty->ty_type);
282 1.1 cgd tt->tt_status = ty->ty_status;
283 1.1 cgd }
284 1.1 cgd ttytabend = tt;
285 1.1 cgd endttyent();
286 1.1 cgd for(tt = ttytab; tt < ttytabend; getty(tt++));
287 1.1 cgd
288 1.1 cgd /* if we receive a request to reread the table, come here */
289 1.1 cgd if(setjmp(reread)) {
290 1.1 cgd
291 1.1 cgd /* first pass. find and clean the entries that have changed */
292 1.1 cgd setttyent();
293 1.1 cgd while(ty = getttyent()) {
294 1.1 cgd for(tt = ttytab; tt < ttytabend; tt++)
295 1.1 cgd if(!strcmp(tt->tt_name, ty->ty_name)) {
296 1.1 cgd /* if a process present, mark */
297 1.1 cgd if((tt->tt_status & ~TTY_LOGIN) !=ty->ty_status)
298 1.1 cgd tt->tt_status = ty->ty_status |TTY_DIFF;
299 1.1 cgd if(strcmp(tt->tt_getty, ty->ty_getty)) {
300 1.1 cgd free(tt->tt_getty);
301 1.1 cgd tt->tt_getty = newstring(ty->ty_getty);
302 1.1 cgd tt->tt_status |= TTY_DIFF;
303 1.1 cgd }
304 1.1 cgd if(strcmp(tt->tt_type, ty->ty_type)) {
305 1.1 cgd free(tt->tt_type);
306 1.1 cgd tt->tt_type = newstring(ty->ty_type);
307 1.1 cgd tt->tt_status |= TTY_DIFF;
308 1.1 cgd }
309 1.1 cgd if(((tt->tt_status |= TTY_SEEN) & TTY_DIFF)
310 1.1 cgd && tt->tt_pid > 1)
311 1.1 cgd kill(tt->tt_pid, 9);
312 1.1 cgd break;
313 1.1 cgd }
314 1.1 cgd if(tt == ttytabend && tt < &ttytab[NTTY]) {
315 1.1 cgd tt->tt_name = newstring(ty->ty_name);
316 1.1 cgd tt->tt_getty = newstring(ty->ty_getty);
317 1.1 cgd tt->tt_type = newstring(ty->ty_type);
318 1.1 cgd tt->tt_status = ty->ty_status |
319 1.1 cgd TTY_SEEN | TTY_DIFF;
320 1.1 cgd ttytabend++;
321 1.1 cgd }
322 1.1 cgd }
323 1.1 cgd endttyent();
324 1.1 cgd /* second pass. offer gettys on previously cleaned entries,
325 1.1 cgd and garbage collect "dead" entries */
326 1.1 cgd for(tt = ttytab; tt < ttytabend; tt++)
327 1.1 cgd if(tt->tt_status & TTY_SEEN) {
328 1.1 cgd tt->tt_status &= ~TTY_SEEN;
329 1.1 cgd if(tt->tt_status & TTY_DIFF) {
330 1.1 cgd tt->tt_status &= ~TTY_DIFF;
331 1.1 cgd getty(tt);
332 1.1 cgd }
333 1.1 cgd }
334 1.1 cgd else {
335 1.1 cgd if(tt->tt_pid > 1)
336 1.1 cgd kill(tt->tt_pid, 9);
337 1.1 cgd free(tt->tt_name);
338 1.1 cgd free(tt->tt_getty);
339 1.1 cgd free(tt->tt_type);
340 1.1 cgd pid = tt - ttytab;
341 1.1 cgd for(tt++; tt < ttytabend; tt++)
342 1.1 cgd tt[-1] = *tt;
343 1.1 cgd ttytabend--;
344 1.1 cgd tt = &ttytab[pid];
345 1.1 cgd }
346 1.1 cgd }
347 1.1 cgd drain = 0;
348 1.1 cgd
349 1.1 cgd /* listen for terminating gettys and sessions, and process them */
350 1.1 cgd while(1) {
351 1.1 cgd sigsetmask(mask);
352 1.1 cgd pid = wait(&status);
353 1.1 cgd sigblock(sigmask(SIGHUP) | sigmask(SIGTERM));
354 1.1 cgd if(pid < 0) {
355 1.1 cgd sleep(5);
356 1.1 cgd continue;
357 1.1 cgd }
358 1.1 cgd for(tt = ttytab; tt < ttytabend; tt++)
359 1.1 cgd if(pid == tt->tt_pid) {
360 1.2 cgd /* 24 Jul 92*/ if (logout(tt->tt_name)) logwtmp(tt->tt_name,"","");
361 1.1 cgd if(drain && !(tt->tt_status & TTY_LOGIN)) {
362 1.1 cgd free(tt->tt_name);
363 1.1 cgd free(tt->tt_getty);
364 1.1 cgd free(tt->tt_type);
365 1.1 cgd for(tt++; tt < ttytabend; tt++)
366 1.1 cgd tt[-1] = *tt;
367 1.1 cgd ttytabend--;
368 1.1 cgd }
369 1.1 cgd else
370 1.1 cgd getty(tt);
371 1.1 cgd break;
372 1.1 cgd }
373 1.1 cgd }
374 1.1 cgd }
375 1.1 cgd
376 1.1 cgd /* process a getty for a "line". N.B. by having getty do open, init
377 1.1 cgd is not limited by filedescriptors for number of possible users */
378 1.1 cgd getty(tt)
379 1.1 cgd struct ttytab *tt;
380 1.1 cgd {
381 1.1 cgd char *sargv[NARG];
382 1.1 cgd register char *p = arg, **sp = sargv;
383 1.1 cgd
384 1.1 cgd if(!(tt->tt_status & TTY_ON)) {
385 1.1 cgd tt->tt_pid = -1;
386 1.1 cgd return;
387 1.1 cgd }
388 1.1 cgd if((tt->tt_pid = fork()) < 0)
389 1.1 cgd fatal("getty fork");
390 1.1 cgd else if(tt->tt_pid) {
391 1.1 cgd if(tt->tt_status & TTY_LOGOUT)
392 1.1 cgd tt->tt_status ^= TTY_LOGIN;
393 1.1 cgd return;
394 1.1 cgd }
395 1.1 cgd signal(SIGHUP, SIG_DFL);
396 1.1 cgd signal(SIGTERM, SIG_DFL);
397 1.1 cgd signal(SIGTSTP, SIG_DFL);
398 1.1 cgd sigsetmask(0);
399 1.1 cgd strcpy(p, tt->tt_getty);
400 1.1 cgd while(sp < &sargv[NARG - 2]) {
401 1.1 cgd while(isspace(*p))
402 1.1 cgd p++;
403 1.1 cgd if(!*p)
404 1.1 cgd break;
405 1.1 cgd *sp++ = p;
406 1.1 cgd while(!isspace(*p) && *p)
407 1.1 cgd p++;
408 1.1 cgd if(!*p)
409 1.1 cgd break;
410 1.1 cgd *p++ = 0;
411 1.1 cgd }
412 1.1 cgd strcpy(nam, tt->tt_name);
413 1.1 cgd *sp++ = nam;
414 1.1 cgd *sp = 0;
415 1.1 cgd p = *sargv;
416 1.1 cgd strcpy(term, "TERM=");
417 1.1 cgd strcat(term, tt->tt_type);
418 1.1 cgd execve(p, sargv, env);
419 1.1 cgd bad:
420 1.1 cgd sleep(30);
421 1.1 cgd fatal(tt->tt_name);
422 1.1 cgd }
423 1.1 cgd
424 1.1 cgd char *
425 1.1 cgd newstring(s)
426 1.1 cgd register char *s;
427 1.1 cgd {
428 1.1 cgd register char *n;
429 1.1 cgd
430 1.1 cgd if(!(n = malloc(strlen(s) + 1)))
431 1.1 cgd fatal("out of memory");
432 1.1 cgd strcpy(n, s);
433 1.1 cgd return(n);
434 1.1 cgd }
435 1.1 cgd
436 1.1 cgd warn(s)
437 1.1 cgd char *s;
438 1.1 cgd {
439 1.1 cgd register int pid;
440 1.1 cgd int fd;
441 1.1 cgd
442 1.1 cgd fd = open("/dev/console", 2);
443 1.1 cgd writes(fd, "init WARNING: ");
444 1.1 cgd writes(fd, s);
445 1.1 cgd write(fd, "\n", 1);
446 1.1 cgd close(fd);
447 1.1 cgd }
448 1.1 cgd
449 1.1 cgd fatal(s)
450 1.1 cgd char *s;
451 1.1 cgd {
452 1.1 cgd login_tty(open("/dev/console", 2));
453 1.1 cgd writes(2, "init FATAL error: ");
454 1.1 cgd perror(s);
455 1.2 cgd _exit(1); /* 04 Sep 92*/
456 1.1 cgd /* panic: init died */
457 1.1 cgd }
458 1.1 cgd
459 1.1 cgd writes(n, s)
460 1.1 cgd char *s;
461 1.1 cgd {
462 1.1 cgd write(n, s, strlen(s));
463 1.1 cgd }
464