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