#include #include #include #include #define TTYS "/etc/inittab" #define RC "/etc/rc" #define SH "/bin/sh" #define DIAG "/dev/console" #define ERRACT "/bin/sh < /dev/console > /dev/console 2>&1" #define INIT 1 #define NOPROC 0 #define NOCMD -1 #define NOIND -1 #define CHILD 0 #define FORKERR -1 #define ASIZE 256 #define TABSIZ 100 #define RCLIM 300 #define STx 1 #define ST1 4 #define ST2 5 #define ST3 6 #define ST4 7 #define ST5 8 #define ST6 10 #define ST7 11 #define ST8 12 #define ST9 13 struct initbl { int in_proc; char in_id[2]; int in_cmd; }; struct initbl itab[TABSIZ]; char state = '1'; char a0[] = "INIT "; char lstate = '0'; int statecnt [10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int ST [10] = {0, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9}; int chst(), rckill(); int sigs [4] = {SIGINT, SIGQUIT, SIGALRM, SIGTERM}; int rdstate = 1; int xstate = 0; int idlim = 0; int rcid; unsigned csize = ASIZE; char *start, *malloc(), *realloc(); unsigned nextcmd = 0; /* offset from start */ main(argc, argv) int argc; char **argv; { int i; if(argc > 1) { if((*argv[1] > '9') || (*argv[1] < '1')) { fputs("init: invalid state\n", stderr); exit(2); } if(kill(INIT, ST[*argv[1]-'0'])) { fputs("init: permission denied\n", stderr); exit(2); } exit(0); } if(getpid() != INIT) { fputs("usage: init state\n", stderr); exit(2); } signal(STx, chst); signal(SIGPWR, chst); ignsigs(); for(i=0;i<=10;i++) close(i); while ((start=malloc(csize)) == NULL) err(2, "no memory, malloc failed", ""); for(;;) { a0[5] = state; strcpy(*argv, a0); xstate = 0; while(rdstate) { rdstate = 0; readttys(); statecnt[state-'0']++; runrc(); xstate = 0; } invttys(); waitttys(); } } fit(str) char *str; { /* returns offset from start where string stored. */ int len, rstr; char *ostart; len = strlen(str) + 1; if((len+nextcmd) > csize) { csize += ASIZE; ostart = start; if ((start=realloc(start, csize)) == NULL) { start = ostart; csize -= ASIZE; err(0, "no memory, malloc failed", ""); return(NOCMD); } } strcpy(start+nextcmd, str); rstr = nextcmd; nextcmd += len; return(rstr); } invttys() { int i; for(i=0;i NOPROC) { /* killpgrp */ kill(-(itab[itp].in_proc), 15); tset=1; } break; case 'k': if(itab[itp].in_proc > NOPROC) { /* killpgrp */ if(tset) sleep(1); kill(-(itab[itp].in_proc), 9); } break; } } fclose(tfile); } lookproc(proc) int proc; { int i; for(i=0;i 0); if(sign < 0) s[i++] = '-'; s[i] = '\0'; for(i=0, j=strlen(s)-1;i