Home | History | Annotate | Line # | Download | only in init
init.c revision 1.2
      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 
     75 struct 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;
     82 int drain, sflag;
     83 char arg[128], nam[64], term[64], *env[] = { term, 0 };
     84 jmp_buf single, reread;
     85 char *Reboot = "autoboot";
     86 
     87 char *newstring(), *malloc();
     88 extern 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 */
     99 void
    100 shup(sig)
    101 {
    102 	longjmp(reread, 1);
    103 }
    104 
    105 /* SIGALRM: abort wait and go single user */
    106 void
    107 salrm(sig)
    108 {
    109 	signal(SIGALRM, SIG_DFL);
    110 	warn("process hung");
    111 	longjmp(single, 1);
    112 }
    113 
    114 /* SIGTERM: go single user */
    115 void
    116 sterm(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 */
    143 void
    144 ststp(sig)
    145 {
    146 	drain = 1;
    147 }
    148 
    149 /* init [-s] [-f] */
    150 
    151 main(argc, argv)
    152 char **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;
    184 top:
    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();
    214 		} else	{ status = 1;  sflag = 1; goto top; }
    215 	}
    216 	signal(SIGTERM, sterm);
    217 	Reboot = 0;
    218 
    219 	/* do single user shell on console */
    220 	if (setjmp(single) || status) {
    221 		if((pid = fork()) < 0)
    222 			fatal("fork");
    223 		else if(!pid) {
    224 			/* signals, to default state */
    225 			SIGNALSFORCHILD;
    226 
    227 			/* clean off console */
    228 			revoke("/dev/console");
    229 
    230 			/* do open and configuration of console */
    231 			login_tty(open("/dev/console", 2));
    232 			execl("/bin/sh", "-", (char *)0);
    233 			_exit(127);
    234 		}
    235 		while(wait(&status) != pid);
    236 		while(drain)				/* 31 Jul 92*/
    237 			pause();
    238 		goto top;
    239 	}
    240 
    241 	/* multiuser mode, traipse through table */
    242 	setttyent();
    243 	for(tt = ttytab; (ty = getttyent()) && tt < &ttytab[NTTY]; tt++) {
    244 		tt->tt_name = newstring(ty->ty_name);
    245 		tt->tt_getty = newstring(ty->ty_getty);
    246 		tt->tt_type = newstring(ty->ty_type);
    247 		tt->tt_status = ty->ty_status;
    248 	}
    249 	ttytabend = tt;
    250 	endttyent();
    251 	for(tt = ttytab; tt < ttytabend; getty(tt++));
    252 
    253 	/* if we receive a request to reread the table, come here */
    254 	if(setjmp(reread)) {
    255 
    256 		/* first pass. find and clean the entries that have changed */
    257 		setttyent();
    258 		while(ty = getttyent()) {
    259 			for(tt = ttytab; tt < ttytabend; tt++)
    260 			if(!strcmp(tt->tt_name, ty->ty_name)) {
    261 				/* if a process present, mark */
    262 				if((tt->tt_status & ~TTY_LOGIN) !=ty->ty_status)
    263 					tt->tt_status = ty->ty_status |TTY_DIFF;
    264 				if(strcmp(tt->tt_getty, ty->ty_getty)) {
    265 					free(tt->tt_getty);
    266 					tt->tt_getty = newstring(ty->ty_getty);
    267 					tt->tt_status |= TTY_DIFF;
    268 				}
    269 				if(strcmp(tt->tt_type, ty->ty_type)) {
    270 					free(tt->tt_type);
    271 					tt->tt_type = newstring(ty->ty_type);
    272 					tt->tt_status |= TTY_DIFF;
    273 				}
    274 				if(((tt->tt_status |= TTY_SEEN) & TTY_DIFF)
    275 					&& tt->tt_pid > 1)
    276 					kill(tt->tt_pid, 9);
    277 				break;
    278 			}
    279 			if(tt == ttytabend && tt < &ttytab[NTTY]) {
    280 				tt->tt_name = newstring(ty->ty_name);
    281 				tt->tt_getty = newstring(ty->ty_getty);
    282 				tt->tt_type = newstring(ty->ty_type);
    283 				tt->tt_status = ty->ty_status |
    284 					TTY_SEEN | TTY_DIFF;
    285 				ttytabend++;
    286 			}
    287 		}
    288 		endttyent();
    289 		/* second pass. offer gettys on previously cleaned entries,
    290 		   and garbage collect "dead" entries */
    291 		for(tt = ttytab; tt < ttytabend; tt++)
    292 			if(tt->tt_status & TTY_SEEN) {
    293 				tt->tt_status &= ~TTY_SEEN;
    294 				if(tt->tt_status & TTY_DIFF) {
    295 					tt->tt_status &= ~TTY_DIFF;
    296 					getty(tt);
    297 				}
    298 			}
    299 			else {
    300 				if(tt->tt_pid > 1)
    301 					kill(tt->tt_pid, 9);
    302 				free(tt->tt_name);
    303 				free(tt->tt_getty);
    304 				free(tt->tt_type);
    305 				pid = tt - ttytab;
    306 				for(tt++; tt < ttytabend; tt++)
    307 					tt[-1] = *tt;
    308 				ttytabend--;
    309 				tt = &ttytab[pid];
    310 			}
    311 	}
    312 	drain = 0;
    313 
    314 	/* listen for terminating gettys and sessions, and process them */
    315 	while(1) {
    316 		sigsetmask(mask);
    317 		pid = wait(&status);
    318 		sigblock(sigmask(SIGHUP) | sigmask(SIGTERM));
    319 		if(pid < 0) {
    320 			sleep(5);
    321 			continue;
    322 		}
    323 		for(tt = ttytab; tt < ttytabend; tt++)
    324 			if(pid == tt->tt_pid) {
    325 /* 24 Jul 92*/			if (logout(tt->tt_name)) logwtmp(tt->tt_name,"","");
    326 				if(drain && !(tt->tt_status & TTY_LOGIN)) {
    327 					free(tt->tt_name);
    328 					free(tt->tt_getty);
    329 					free(tt->tt_type);
    330 					for(tt++; tt < ttytabend; tt++)
    331 						tt[-1] = *tt;
    332 					ttytabend--;
    333 				}
    334 				else
    335 					getty(tt);
    336 				break;
    337 			}
    338 	}
    339 }
    340 
    341 /* process a getty for a "line". N.B. by having getty do open, init
    342    is not limited by filedescriptors for number of possible users */
    343 getty(tt)
    344 struct ttytab *tt;
    345 {
    346 	char *sargv[NARG];
    347 	register char *p = arg, **sp = sargv;
    348 
    349 	if(!(tt->tt_status & TTY_ON)) {
    350 		tt->tt_pid = -1;
    351 		return;
    352 	}
    353 	if((tt->tt_pid = fork()) < 0)
    354 		fatal("getty fork");
    355 	else if(tt->tt_pid) {
    356 		if(tt->tt_status & TTY_LOGOUT)
    357 			tt->tt_status ^= TTY_LOGIN;
    358 		return;
    359 	}
    360 	signal(SIGHUP, SIG_DFL);
    361 	signal(SIGTERM, SIG_DFL);
    362 	signal(SIGTSTP, SIG_DFL);
    363 	sigsetmask(0);
    364 	strcpy(p, tt->tt_getty);
    365 	while(sp < &sargv[NARG - 2]) {
    366 		while(isspace(*p))
    367 			p++;
    368 		if(!*p)
    369 			break;
    370 		*sp++ = p;
    371 		while(!isspace(*p) && *p)
    372 			p++;
    373 		if(!*p)
    374 			break;
    375 		*p++ = 0;
    376 	}
    377 	strcpy(nam, tt->tt_name);
    378 	*sp++ = nam;
    379 	*sp = 0;
    380 	p = *sargv;
    381 	strcpy(term, "TERM=");
    382 	strcat(term, tt->tt_type);
    383 	execve(p, sargv, env);
    384 bad:
    385 	sleep(30);
    386 	fatal(tt->tt_name);
    387 }
    388 
    389 char *
    390 newstring(s)
    391 register char *s;
    392 {
    393 	register char *n;
    394 
    395 	if(!(n = malloc(strlen(s) + 1)))
    396 		fatal("out of memory");
    397 	strcpy(n, s);
    398 	return(n);
    399 }
    400 
    401 warn(s)
    402 char *s;
    403 {
    404 	register int pid;
    405 	int fd;
    406 
    407 	fd = open("/dev/console", 2);
    408 	writes(fd, "init WARNING: ");
    409 	writes(fd, s);
    410 	write(fd, "\n", 1);
    411 	close(fd);
    412 }
    413 
    414 fatal(s)
    415 char *s;
    416 {
    417 	login_tty(open("/dev/console", 2));
    418 	writes(2, "init FATAL error: ");
    419 	perror(s);
    420 	_exit(1);				/* 04 Sep 92*/
    421 	/* panic: init died */
    422 }
    423 
    424 writes(n, s)
    425 char *s;
    426 {
    427 	write(n, s, strlen(s));
    428 }
    429