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