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