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