121135Sdist /* 228801Skarels * Copyright (c) 1980,1986 Regents of the University of California. 321135Sdist * All rights reserved. The Berkeley software License Agreement 421135Sdist * specifies the terms and conditions for redistribution. 521135Sdist */ 621135Sdist 712682Ssam #ifndef lint 8*43633Skarels static char sccsid[] = "@(#)init.c 5.17 (Berkeley) 06/24/90"; 921135Sdist #endif not lint 1012682Ssam 111029Sbill #include <sys/types.h> 1237284Sbostic #include <sys/file.h> 1337284Sbostic #include <sys/signal.h> 1437284Sbostic #include <sys/reboot.h> 1537284Sbostic #include <sys/syslog.h> 1637284Sbostic #include <sys/stat.h> 1742407Smarc #include <sys/ioctl.h> 181029Sbill #include <setjmp.h> 1935606Sbostic #include <utmp.h> 202821Swnj #include <errno.h> 2116452Sroot #include <ttyent.h> 2237284Sbostic #include "pathnames.h" 231029Sbill 2424494Skarels #define CMDSIZ 200 /* max string length for getty or window command*/ 2523147Sbloom #define ALL p = itab; p ; p = p->next 261029Sbill #define EVER ;; 271029Sbill #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 281029Sbill #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 291029Sbill 3037284Sbostic char shell[] = _PATH_BSHELL; 311029Sbill char minus[] = "-"; 3237284Sbostic char runc[] = _PATH_RC; 3337284Sbostic char ctty[] = _PATH_CONSOLE; 341029Sbill 351029Sbill struct tab 361029Sbill { 3735670Sbostic char line[UT_LINESIZE]; 3818542Sralph char comn[CMDSIZ]; 391029Sbill char xflag; 401029Sbill int pid; 4118542Sralph int wpid; /* window system pid for SIGHUP */ 4218542Sralph char wcmd[CMDSIZ]; /* command to start window system process */ 435971Sroot time_t gettytime; 445971Sroot int gettycnt; 4522181Skarels time_t windtime; 4622181Skarels int windcnt; 4723147Sbloom struct tab *next; 4823147Sbloom } *itab; 491029Sbill 501029Sbill int fi; 511029Sbill int mergflag; 521029Sbill char tty[20]; 531429Sbill jmp_buf sjbuf, shutpass; 541029Sbill 551029Sbill char *strcpy(), *strcat(); 561029Sbill long lseek(); 5742407Smarc void idle(), merge(), reset(); 581029Sbill 5918542Sralph struct sigvec rvec = { reset, sigmask(SIGHUP), 0 }; 6013021Ssam 61*43633Skarels main(argc, argv) 62*43633Skarels char **argv; 631029Sbill { 6429836Ssam #if defined(tahoe) 6529836Ssam register int r12; /* make sure r11 gets bootflags */ 6629836Ssam #endif 67*43633Skarels #if defined(vax) || defined(tahoe) || defined(hp300) 681403Sbill register int r11; /* passed thru from boot */ 6913021Ssam #endif 7042408Smckusick #ifdef __GNUC__ 7142408Smckusick /* insure proper semantics for setjmp/longjmp */ 7242408Smckusick static 7342408Smckusick #endif 74*43633Skarels int howto, oldhowto, started = 0; 751403Sbill 76*43633Skarels #if defined(vax) || defined(tahoe) || defined(hp300) 77*43633Skarels /* howto passed in high-order register XXX */ 7842408Smckusick #ifdef __GNUC__ 7942408Smckusick #ifdef hp300 8042408Smckusick asm("movl d7,%0" : "=rm" (howto)); 8142408Smckusick #else 8242408Smckusick asm("movl r11,%0" : "=rm" (howto)); 8342408Smckusick #endif 8442408Smckusick #else 851403Sbill howto = r11; 86*43633Skarels #endif /* __GNUC__ */ 87*43633Skarels #else /* defined(vax) || defined(tahoe) || defined(hp300) */ 88*43633Skarels /* howto passed as argument */ 899869Spugs if (argc > 1 && argv[1][0] == '-') { 909869Spugs char *cp; 919869Spugs 9242408Smckusick howto = 0; 939869Spugs cp = &argv[1][1]; 949869Spugs while (*cp) switch (*cp++) { 959869Spugs case 'a': 969869Spugs howto |= RB_ASKNAME; 979869Spugs break; 989869Spugs case 's': 999869Spugs howto |= RB_SINGLE; 1009869Spugs break; 1019869Spugs } 102*43633Skarels } else 1039869Spugs howto = RB_SINGLE; 10413021Ssam #endif 10542407Smarc if (getuid() != 0) 10642407Smarc exit(1); 10724854Seric openlog("init", LOG_CONS|LOG_ODELAY, LOG_AUTH); 10842407Smarc if (setsid() < 0) 10942407Smarc syslog(LOG_ERR, "setsid failed (initial) %m"); 11013021Ssam sigvec(SIGTERM, &rvec, (struct sigvec *)0); 1112821Swnj signal(SIGTSTP, idle); 1121029Sbill signal(SIGSTOP, SIG_IGN); 1131029Sbill signal(SIGTTIN, SIG_IGN); 1141029Sbill signal(SIGTTOU, SIG_IGN); 11513021Ssam (void) setjmp(sjbuf); 116*43633Skarels for (; ; ) { 1171403Sbill oldhowto = howto; 1181403Sbill howto = RB_SINGLE; 11942407Smarc if (started && setjmp(shutpass) == 0) 1201429Sbill shutdown(); 12142407Smarc started = 1; 1221403Sbill if (oldhowto & RB_SINGLE) 1231403Sbill single(); 1241403Sbill if (runcom(oldhowto) == 0) 1251403Sbill continue; 1261029Sbill merge(); 1271029Sbill multiple(); 1281029Sbill } 1291029Sbill } 1301029Sbill 13142407Smarc void shutreset(); 1321429Sbill 1331029Sbill shutdown() 1341029Sbill { 1351029Sbill register i; 13623147Sbloom register struct tab *p, *p1; 1371029Sbill 1381029Sbill signal(SIGHUP, SIG_IGN); 13923147Sbloom for (p = itab; p ; ) { 1401029Sbill term(p); 14123147Sbloom p1 = p->next; 14223147Sbloom free(p); 14323147Sbloom p = p1; 1441029Sbill } 14523147Sbloom itab = (struct tab *)0; 1461429Sbill signal(SIGALRM, shutreset); 14728801Skarels (void) kill(-1, SIGTERM); /* one chance to catch it */ 14828801Skarels sleep(5); 1491429Sbill alarm(30); 15013021Ssam for (i = 0; i < 5; i++) 1511029Sbill kill(-1, SIGKILL); 15213021Ssam while (wait((int *)0) != -1) 1531029Sbill ; 1541029Sbill alarm(0); 1551429Sbill shutend(); 1561429Sbill } 1571429Sbill 15842407Smarc char shutfailm[] = "init: WARNING: something is hung (won't die); ps axl advised\n"; 1591429Sbill 16042407Smarc void 1611429Sbill shutreset() 1621429Sbill { 1631429Sbill int status; 1641429Sbill 1651429Sbill if (fork() == 0) { 1661429Sbill int ct = open(ctty, 1); 1671429Sbill write(ct, shutfailm, sizeof (shutfailm)); 1681429Sbill sleep(5); 1691429Sbill exit(1); 1701429Sbill } 1711429Sbill sleep(5); 1721429Sbill shutend(); 1731429Sbill longjmp(shutpass, 1); 1741429Sbill } 1751429Sbill 1761429Sbill shutend() 1771429Sbill { 17842408Smckusick register i; 1791429Sbill 1802821Swnj acct(0); 1811029Sbill signal(SIGALRM, SIG_DFL); 18213021Ssam for (i = 0; i < 10; i++) 1831029Sbill close(i); 18435606Sbostic logwtmp("~", "shutdown", ""); 1851029Sbill } 1861029Sbill 1871029Sbill single() 1881029Sbill { 1891029Sbill register pid; 1902821Swnj register xpid; 191*43633Skarels int fd; 19235606Sbostic extern int errno; 1931029Sbill 19413021Ssam do { 19513021Ssam pid = fork(); 19613021Ssam if (pid == 0) { 19713021Ssam signal(SIGTERM, SIG_DFL); 19813021Ssam signal(SIGHUP, SIG_DFL); 19913021Ssam signal(SIGALRM, SIG_DFL); 20016452Sroot signal(SIGTSTP, SIG_IGN); 20142407Smarc if (setsid() < 0) 20242407Smarc syslog(LOG_ERR, "setsid failed (single): %m"); 203*43633Skarels (void) revoke(ctty); 204*43633Skarels if ((fd = open(ctty, O_RDWR)) < 0) { 205*43633Skarels syslog(LOG_ERR, "open %s: %m", ctty); 206*43633Skarels exit(1); 207*43633Skarels } 208*43633Skarels if (ioctl(fd, TIOCSCTTY, 0) < 0) 20942407Smarc syslog(LOG_ERR, "TIOCSCTTY failed: %m"); 210*43633Skarels dup2(fd, 0); 211*43633Skarels dup2(fd, 1); 212*43633Skarels dup2(fd, 2); 213*43633Skarels if (fd > 2) 214*43633Skarels close(fd); 21513021Ssam execl(shell, minus, (char *)0); 21630516Sbostic perror(shell); 21713021Ssam exit(0); 21813021Ssam } 21913021Ssam while ((xpid = wait((int *)0)) != pid) 22013021Ssam if (xpid == -1 && errno == ECHILD) 22113021Ssam break; 22213021Ssam } while (xpid == -1); 2231029Sbill } 2241029Sbill 2251403Sbill runcom(oldhowto) 2261403Sbill int oldhowto; 2271029Sbill { 22842408Smckusick register pid; 2291403Sbill int status; 2301029Sbill 2311029Sbill pid = fork(); 23213021Ssam if (pid == 0) { 23342407Smarc (void) open(ctty, O_RDONLY); 23413021Ssam dup2(0, 1); 23513021Ssam dup2(0, 2); 23642407Smarc if (setsid() < 0) 23742407Smarc syslog(LOG_ERR, "setsid failed (runcom) %m"); 23842407Smarc if (ioctl(0, TIOCSCTTY, 0) < 0) 23942407Smarc syslog(LOG_ERR, "TIOCSCTTY failed (runcom) %m"); 2401403Sbill if (oldhowto & RB_SINGLE) 2411403Sbill execl(shell, shell, runc, (char *)0); 2421403Sbill else 2431403Sbill execl(shell, shell, runc, "autoboot", (char *)0); 2441403Sbill exit(1); 2451029Sbill } 24613021Ssam while (wait(&status) != pid) 2471029Sbill ; 24813021Ssam if (status) 24913021Ssam return (0); 25035606Sbostic logwtmp("~", "reboot", ""); 25113021Ssam return (1); 2521029Sbill } 2531029Sbill 25418542Sralph struct sigvec mvec = { merge, sigmask(SIGTERM), 0 }; 25513021Ssam /* 25613021Ssam * Multi-user. Listen for users leaving, SIGHUP's 25713021Ssam * which indicate ttys has changed, and SIGTERM's which 25813021Ssam * are used to shutdown the system. 25913021Ssam */ 2601029Sbill multiple() 2611029Sbill { 26242407Smarc extern int errno; 2631029Sbill register struct tab *p; 2641029Sbill register pid; 26523147Sbloom int omask; 2661029Sbill 26713021Ssam sigvec(SIGHUP, &mvec, (struct sigvec *)0); 26813021Ssam for (EVER) { 2691029Sbill pid = wait((int *)0); 27042407Smarc /* SHOULD FIX THIS IN THE KERNEL */ 27142407Smarc if (pid == -1 && errno != EINTR) 2721029Sbill return; 27330516Sbostic omask = sigblock(sigmask(SIGHUP)); 27418542Sralph for (ALL) { 27518542Sralph /* must restart window system BEFORE emulator */ 27618542Sralph if (p->wpid == pid || p->wpid == -1) 27718542Sralph wstart(p); 27813021Ssam if (p->pid == pid || p->pid == -1) { 27918542Sralph /* disown the window system */ 28018542Sralph if (p->wpid) 28118542Sralph kill(p->wpid, SIGHUP); 28235445Sbostic cleanutmp(p); 2831029Sbill dfork(p); 2841029Sbill } 28518542Sralph } 28623147Sbloom sigsetmask(omask); 2871029Sbill } 2881029Sbill } 2891029Sbill 29013021Ssam /* 29113021Ssam * Merge current contents of ttys file 29213021Ssam * into in-core table of configured tty lines. 29313021Ssam * Entered as signal handler for SIGHUP. 29413021Ssam */ 29513021Ssam #define FOUND 1 29613021Ssam #define CHANGE 2 29718542Sralph #define WCHANGE 4 29813021Ssam 29942407Smarc void 30013021Ssam merge() 30113021Ssam { 30213021Ssam register struct tab *p; 30316452Sroot register struct ttyent *t; 30423147Sbloom register struct tab *p1; 30513021Ssam 30613021Ssam for (ALL) 30713021Ssam p->xflag = 0; 30816452Sroot setttyent(); 30916452Sroot while (t = getttyent()) { 31016452Sroot if ((t->ty_status & TTY_ON) == 0) 31116452Sroot continue; 31213021Ssam for (ALL) { 31316452Sroot if (SCMPN(p->line, t->ty_name)) 31413021Ssam continue; 31513021Ssam p->xflag |= FOUND; 31616452Sroot if (SCMPN(p->comn, t->ty_getty)) { 31713021Ssam p->xflag |= CHANGE; 31816452Sroot SCPYN(p->comn, t->ty_getty); 31913021Ssam } 32030516Sbostic if (SCMPN(p->wcmd, t->ty_window ? t->ty_window : "")) { 32118542Sralph p->xflag |= WCHANGE|CHANGE; 32218542Sralph SCPYN(p->wcmd, t->ty_window); 32318542Sralph } 32413021Ssam goto contin1; 32513021Ssam } 32618542Sralph 32723147Sbloom /* 32823147Sbloom * Make space for a new one 32923147Sbloom */ 33023147Sbloom p1 = (struct tab *)calloc(1, sizeof(*p1)); 33123147Sbloom if (!p1) { 33223147Sbloom syslog(LOG_ERR, "no space for '%s' !?!", t->ty_name); 33313021Ssam goto contin1; 33413021Ssam } 33523147Sbloom /* 33623147Sbloom * Put new terminal at the end of the linked list. 33723147Sbloom */ 33823147Sbloom if (itab) { 33923147Sbloom for (p = itab; p->next ; p = p->next) 34023147Sbloom ; 34123147Sbloom p->next = p1; 34223147Sbloom } else 34323147Sbloom itab = p1; 34423147Sbloom 34523147Sbloom p = p1; 34623147Sbloom SCPYN(p->line, t->ty_name); 34723147Sbloom p->xflag |= FOUND|CHANGE; 34823147Sbloom SCPYN(p->comn, t->ty_getty); 34930516Sbostic if (t->ty_window && strcmp(t->ty_window, "") != 0) { 35023147Sbloom p->xflag |= WCHANGE; 35123147Sbloom SCPYN(p->wcmd, t->ty_window); 35223147Sbloom } 35313021Ssam contin1: 35413021Ssam ; 35513021Ssam } 35616452Sroot endttyent(); 35723147Sbloom p1 = (struct tab *)0; 35813021Ssam for (ALL) { 35913021Ssam if ((p->xflag&FOUND) == 0) { 36013021Ssam term(p); 36118542Sralph wterm(p); 36223147Sbloom if (p1) 36323147Sbloom p1->next = p->next; 36423147Sbloom else 36523147Sbloom itab = p->next; 36623147Sbloom free(p); 36723147Sbloom p = p1 ? p1 : itab; 36823147Sbloom } else { 36923147Sbloom /* window system should be started first */ 37023147Sbloom if (p->xflag&WCHANGE) { 37123147Sbloom wterm(p); 37223147Sbloom wstart(p); 37323147Sbloom } 37423147Sbloom if (p->xflag&CHANGE) { 37523147Sbloom term(p); 37623147Sbloom dfork(p); 37723147Sbloom } 37813021Ssam } 37923147Sbloom p1 = p; 38013021Ssam } 38113021Ssam } 38213021Ssam 3831029Sbill term(p) 38413021Ssam register struct tab *p; 3851029Sbill { 3861029Sbill 38713021Ssam if (p->pid != 0) { 38835445Sbostic cleanutmp(p); 3891029Sbill kill(p->pid, SIGKILL); 3901029Sbill } 3911029Sbill p->pid = 0; 39218542Sralph /* send SIGHUP to get rid of connections */ 39318542Sralph if (p->wpid > 0) 39418542Sralph kill(p->wpid, SIGHUP); 3951029Sbill } 3961029Sbill 3971029Sbill dfork(p) 39813021Ssam struct tab *p; 3991029Sbill { 4001029Sbill register pid; 4015971Sroot time_t t; 4025971Sroot int dowait = 0; 4031029Sbill 4045971Sroot time(&t); 4055971Sroot p->gettycnt++; 4065971Sroot if ((t - p->gettytime) >= 60) { 4075971Sroot p->gettytime = t; 4085971Sroot p->gettycnt = 1; 40918542Sralph } else if (p->gettycnt >= 5) { 41018542Sralph dowait = 1; 41118542Sralph p->gettytime = t; 41218542Sralph p->gettycnt = 1; 4135971Sroot } 4141029Sbill pid = fork(); 41513021Ssam if (pid == 0) { 4166816Ssam signal(SIGTERM, SIG_DFL); 4176816Ssam signal(SIGHUP, SIG_IGN); 41822181Skarels sigsetmask(0); /* since can be called from masked code */ 4195971Sroot if (dowait) { 42018542Sralph syslog(LOG_ERR, "'%s %s' failing, sleeping", p->comn, p->line); 42118542Sralph closelog(); 4225971Sroot sleep(30); 4235971Sroot } 42442407Smarc if (setsid() < 0) 42542407Smarc syslog(LOG_ERR, "setsid failed(dfork) %m"); 42618542Sralph execit(p->comn, p->line); 4271029Sbill exit(0); 4281029Sbill } 4291029Sbill p->pid = pid; 4301029Sbill } 4311029Sbill 43235445Sbostic cleanutmp(p) 43313021Ssam register struct tab *p; 4341029Sbill { 43535445Sbostic if (logout(p->line)) { 43642407Smarc logwtmp(p->line, "", ""); 43717582Ssam /* 43817582Ssam * After a proper login force reset 43917582Ssam * of error detection code in dfork. 44017582Ssam */ 44117582Ssam p->gettytime = 0; 44222181Skarels p->windtime = 0; 4431029Sbill } 4441029Sbill } 4451029Sbill 44642407Smarc void 4471029Sbill reset() 4481029Sbill { 4491029Sbill longjmp(sjbuf, 1); 4501029Sbill } 4512821Swnj 45213021Ssam jmp_buf idlebuf; 45313021Ssam 45442407Smarc void 45513021Ssam idlehup() 45613021Ssam { 45713021Ssam longjmp(idlebuf, 1); 45813021Ssam } 45913021Ssam 46042407Smarc void 4612821Swnj idle() 4622821Swnj { 4632821Swnj register struct tab *p; 4642821Swnj register pid; 4652821Swnj 46613021Ssam signal(SIGHUP, idlehup); 46718542Sralph for (EVER) { 46813021Ssam if (setjmp(idlebuf)) 46913021Ssam return; 4702821Swnj pid = wait((int *) 0); 47113021Ssam if (pid == -1) { 47213021Ssam sigpause(0); 47313021Ssam continue; 4742821Swnj } 47518542Sralph for (ALL) { 47618542Sralph /* if window system dies, mark it for restart */ 47718542Sralph if (p->wpid == pid) 47818542Sralph p->wpid = -1; 47913021Ssam if (p->pid == pid) { 48035445Sbostic cleanutmp(p); 48113021Ssam p->pid = -1; 48213021Ssam } 48318542Sralph } 4842821Swnj } 4852821Swnj } 48618542Sralph 48718542Sralph wterm(p) 48818542Sralph register struct tab *p; 48918542Sralph { 49018542Sralph if (p->wpid != 0) { 49118542Sralph kill(p->wpid, SIGKILL); 49218542Sralph } 49318542Sralph p->wpid = 0; 49418542Sralph } 49518542Sralph 49618542Sralph wstart(p) 49718542Sralph register struct tab *p; 49818542Sralph { 49922181Skarels register pid; 50022181Skarels time_t t; 50122181Skarels int dowait = 0; 50218542Sralph 50322181Skarels time(&t); 50422181Skarels p->windcnt++; 50522181Skarels if ((t - p->windtime) >= 60) { 50622181Skarels p->windtime = t; 50722181Skarels p->windcnt = 1; 50822181Skarels } else if (p->windcnt >= 5) { 50922181Skarels dowait = 1; 51022181Skarels p->windtime = t; 51122181Skarels p->windcnt = 1; 51222181Skarels } 51322181Skarels 51422181Skarels pid = fork(); 51522181Skarels 51622181Skarels if (pid == 0) { 51718542Sralph signal(SIGTERM, SIG_DFL); 51822181Skarels signal(SIGHUP, SIG_IGN); 51922181Skarels sigsetmask(0); /* since can be called from masked code */ 52022181Skarels if (dowait) { 52122181Skarels syslog(LOG_ERR, "'%s %s' failing, sleeping", p->wcmd, p->line); 52222181Skarels closelog(); 52322181Skarels sleep(30); 52422181Skarels } 52542407Smarc if (setsid() < 0) 52642407Smarc syslog(LOG_ERR, "setsid failed (window) %m"); 52718542Sralph execit(p->wcmd, p->line); 52818542Sralph exit(0); 52918542Sralph } 53022181Skarels p->wpid = pid; 53118542Sralph } 53218542Sralph 53322181Skarels #define NARGS 20 /* must be at least 4 */ 53418542Sralph #define ARGLEN 512 /* total size for all the argument strings */ 53518542Sralph 53618542Sralph execit(s, arg) 53718542Sralph char *s; 53818542Sralph char *arg; /* last argument on line */ 53918542Sralph { 54018542Sralph char *argv[NARGS], args[ARGLEN], *envp[1]; 54118542Sralph register char *sp = s; 54218542Sralph register char *ap = args; 54318542Sralph register char c; 54418542Sralph register int i; 54518542Sralph 54618542Sralph /* 54718542Sralph * First we have to set up the argument vector. 54818542Sralph * "prog arg1 arg2" maps to exec("prog", "-", "arg1", "arg2"). 54918542Sralph */ 55018542Sralph for (i = 1; i < NARGS - 2; i++) { 55118542Sralph argv[i] = ap; 55218542Sralph for (EVER) { 55318542Sralph if ((c = *sp++) == '\0' || ap >= &args[ARGLEN-1]) { 55418542Sralph *ap = '\0'; 55518542Sralph goto done; 55618542Sralph } 55718542Sralph if (c == ' ') { 55818542Sralph *ap++ = '\0'; 55918542Sralph while (*sp == ' ') 56018542Sralph sp++; 56118542Sralph if (*sp == '\0') 56218542Sralph goto done; 56318542Sralph break; 56418542Sralph } 56518542Sralph *ap++ = c; 56618542Sralph } 56718542Sralph } 56818542Sralph done: 56918542Sralph argv[0] = argv[1]; 57018542Sralph argv[1] = "-"; 57118542Sralph argv[i+1] = arg; 57218542Sralph argv[i+2] = 0; 57318542Sralph envp[0] = 0; 57418542Sralph execve(argv[0], &argv[1], envp); 57518542Sralph /* report failure of exec */ 57618542Sralph syslog(LOG_ERR, "%s: %m", argv[0]); 57718542Sralph closelog(); 57818542Sralph sleep(10); /* prevent failures from eating machine */ 57918542Sralph } 580