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