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