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