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