home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / source / s1 / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1975-05-13  |  3.0 KB  |  220 lines

  1. #define    tabsize    20
  2. #define    all    p = &itab[0]; p < &itab[20]; p++
  3. #define    ever    ;;
  4. #define    single    0173030
  5. #define    reboot    0173040
  6. char    shell[]    "/bin/sh";
  7. char    minus[]    "-";
  8. char    runc[]    "/etc/rc";
  9. char    init[]    "/etc/init";
  10. char    ifile[]    "/etc/ttys";
  11. char    utmp[]    "/etc/utmp";
  12. char    wtmpf[]    "/usr/adm/wtmp";
  13. char    ctty[]    "/dev/tty8";
  14. int    fi;
  15. struct
  16. {
  17.     int    flag;
  18.     int    line;
  19.     char    coms[2];
  20. } line;
  21. struct    tab
  22. {
  23.     int    pid;
  24.     int    line;
  25.     int    comn;
  26. } itab[tabsize];
  27.  
  28. struct {
  29.     char    name[8];
  30.     char    tty;
  31.     char    fill;
  32.     int    time[2];
  33.     int    wfill;
  34. } wtmp;
  35.  
  36. main()
  37. {
  38.     register i;
  39.     register struct tab *p, *q;
  40.     int reset();
  41.  
  42.     /*
  43.      * if not single user,
  44.      * run shell sequence
  45.      */
  46.  
  47.     if(getcsw() != single) {
  48.         i = fork();
  49.         if(i == 0) {
  50.             open("/", 0);
  51.             dup(0);
  52.             dup(0);
  53.             execl(shell, shell, runc, 0);
  54.             exit();
  55.         }
  56.         while(wait() != i);
  57.         close(creat(utmp, 0644));
  58.         if ((i = open(wtmpf, 1)) >= 0) {
  59.             seek(i, 0, 2);
  60.             wtmp.tty = '~';
  61.             time(wtmp.time);
  62.             write(i, &wtmp, 16);
  63.             close(i);
  64.         }
  65.     }
  66.  
  67.     /*
  68.      * main loop for hangup signal
  69.      * close all files and
  70.      * check switches for magic values
  71.      */
  72.  
  73.     setexit();
  74.     signal(1, reset);
  75.     for(i=0; i<10; i++)
  76.         close(i);
  77.     switch(getcsw()) {
  78.  
  79.     case single:
  80.     error:
  81.         termall();
  82.         i = fork();
  83.         if(i == 0) {
  84.             open(ctty, 2);
  85.             dup(0);
  86.             execl(shell, minus, 0);
  87.             exit();
  88.         }
  89.         while(wait() != i);
  90.  
  91.     case reboot:
  92.         termall();
  93.         execl(init, minus, 0);
  94.         reset();
  95.     }
  96.  
  97.     /*
  98.      * open and merge in init file
  99.      */
  100.  
  101.     fi = open(ifile, 0);
  102.     q = &itab[0];
  103.     while(rline()) {
  104.         if(line.flag == '0')
  105.             continue;
  106.         for(all)
  107.             if(p->line==line.line || p->line==0) {
  108.                 if(p >= q) {
  109.                     i = p->pid;
  110.                     p->pid = q->pid;
  111.                     q->pid = i;
  112.                     p->line = q->line;
  113.                     p->comn = q->comn;
  114.                     q->line = line.line;
  115.                     q->coms[0] = line.comn;
  116.                     q++;
  117.                 }
  118.                 break;
  119.             }
  120.     }
  121.     close(fi);
  122.     if(q == &itab[0])
  123.         goto error;
  124.     for(; q < &itab[tabsize]; q++)
  125.         term(q);
  126.     for(all)
  127.         if(p->line != 0 && p->pid == 0)
  128.             dfork(p);
  129.     for(ever) {
  130.         i = wait();
  131.         for(all)
  132.             if(p->pid == i) {
  133.                 rmut(p);
  134.                 dfork(p);
  135.             }
  136.     }
  137. }
  138.  
  139. termall()
  140. {
  141.     register struct tab *p;
  142.  
  143.     for(all)
  144.         term(p);
  145. }
  146.  
  147. term(ap)
  148. struct tab *ap;
  149. {
  150.     register struct tab *p;
  151.  
  152.     p = ap;
  153.     if(p->pid != 0) {
  154.         rmut(p);
  155.         kill(p->pid, 9);
  156.     }
  157.     p->pid = 0;
  158.     p->line = 0;
  159. }
  160.  
  161. rline()
  162. {
  163.     static char c[4];
  164.  
  165.     if(read(fi, c, 4) != 4 || c[3] != '\n')
  166.         return(0);
  167.     line.flag = c[0];
  168.     line.line = c[1];
  169.     line.comn = c[2];
  170.     return(1);
  171. }
  172.  
  173. dfork(ap)
  174. struct tab *ap;
  175. {
  176.     register i;
  177.     register char *tty;
  178.     register struct tab *p;
  179.  
  180.     p = ap;
  181.     i = fork();
  182.     if(i == 0) {
  183.         signal(1, 0);
  184.         tty = "/dev/ttyx";
  185.         tty[8] = p->line;
  186.         chown(tty, 0);
  187.         chmod(tty, 0622);
  188.         open(tty, 2);
  189.         dup(0);
  190.         execl("etc/getty", minus, p->coms, 0);
  191.         exit();
  192.     }
  193.     p->pid = i;
  194. }
  195.  
  196. rmut(p)
  197. struct tab *p;
  198. {
  199.     register i, f;
  200.     static char zero[16];
  201.  
  202.     f = open(utmp, 1);
  203.     if(f >= 0) {
  204.         i = p->line;
  205.         if(i >= 'a')
  206.             i =+ '0' + 10 - 'a';
  207.         seek(f, (i-'0')*16, 0);
  208.         write(f, zero, 16);
  209.         close(f);
  210.     }
  211.     f = open(wtmpf, 1);
  212.     if (f >= 0) {
  213.         wtmp.tty = p->line;
  214.         time(wtmp.time);
  215.         seek(f, 0, 2);
  216.         write(f, &wtmp, 16);
  217.         close(f);
  218.     }
  219. }
  220.