home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / backups / quietize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-31  |  2.9 KB  |  168 lines

  1. static char rcsid[] = "$Header: quietize.c,v 1.7 86/10/14 01:34:03 scooter Exp $";
  2.  
  3. /*
  4.  * quietize
  5.  *
  6.  * This module controls all of the procedures necessary to bring
  7.  * the system to a quiescent state.  Basically this can be summarized
  8.  * by the following:
  9.  *
  10.  *    1. Do a shutdown to tell the users goodbye (/etc/shutdown)
  11.  *    2. Get a listing of all the processes running (/bin/ps)
  12.  *    3. Send a SIGHUP to all user processes and handle special shutdown
  13.  *    4. Get another listing
  14.  *    5. Send a SIGSTOP to everything but gettys and inits
  15.  *
  16.  * $Log:    quietize.c,v $
  17.  * Revision 1.7  86/10/14  01:34:03  scooter
  18.  * Added process specific startup/shutdown stuff
  19.  * 
  20.  *
  21.  */
  22.  
  23. #include    "backup.h"
  24. #include    "ulist.h"
  25. #include    <stdio.h>
  26. #include    <signal.h>
  27.  
  28. struct    ulist     *u_list,*proclist(),*proc;
  29. int    our_pid;
  30. int    our_ppid;
  31.  
  32. quietize(minutes)
  33. int minutes;
  34. {
  35.     int fixlog(),procont();
  36.  
  37. /*
  38.  * Begin by going through all of the warning stuff
  39.  */
  40.  
  41.     fprintf(stderr,"\n*** Warning users ...");
  42.     if (minutes <= 0)minutes = 5;
  43.  
  44.     signal(SIGINT,fixlog);
  45.  
  46. #ifdef    DEBUG
  47.     if (x_opt == NO)
  48.         shutdown(minutes);
  49. #else    DEBUG
  50.     shutdown(minutes);
  51. #endif    DEBUG
  52.  
  53. /*
  54.  * Get our pid
  55.  */
  56.  
  57.     our_pid = getpid();
  58.  
  59. /*
  60.  * Get the list of processes on the system
  61.  */
  62.  
  63.     fprintf(stderr,"\n*** Finding processes ...");
  64.     u_list = proclist();
  65.  
  66. /*
  67.  * Blow them away
  68.  */
  69.  
  70.     fprintf(stderr,"\n*** Hanging up processes ...");
  71.     for (proc = u_list ; proc != NULL ; proc = proc->next)
  72.     {
  73.         if (proc->pid == our_pid)our_ppid = proc->ppid;
  74.     }
  75.  
  76. #ifdef    DEBUG
  77.     if (x_opt)
  78.         printf("Our pid = %d, our ppid = %d\n",our_pid,our_ppid);
  79. #endif    DEBUG
  80.  
  81.     for (proc = u_list ; proc != NULL ; proc = proc->next)
  82.     {
  83.         if (proc->pid == our_pid ||
  84.             proc->pid == our_ppid ||
  85.             proc->special_flag)continue;
  86. #ifdef    DEBUG
  87.     if (x_opt)
  88.         printf("Hanging up proces %s (%d)\n",proc->cmd,proc->pid);
  89.     else
  90. #endif    DEBUG
  91.         kill(proc->pid,SIGHUP);
  92.     }
  93.  
  94.     /*
  95.      * Make an attempt to be clean by freeing up the memory
  96.      */
  97.  
  98.     for (proc = u_list ; proc != NULL ; )
  99.     {
  100.         free(proc->cmd);
  101.         u_list = proc;
  102.         proc = proc->next;
  103.         free(u_list);
  104.     }
  105.  
  106.     /*
  107.      * Get the left-over list
  108.      */
  109.  
  110.     u_list = proclist();
  111.  
  112.     /*
  113.      * Stop them
  114.      */
  115.  
  116.     fprintf(stderr,"\n*** Stopping up processes ...");
  117.  
  118.     signal(SIGINT,procont);
  119.  
  120.     for (proc = u_list ; proc != NULL ; proc = proc->next)
  121.     {
  122.         if ( proc->pid == our_pid ||
  123.             proc->pid == our_ppid)continue;
  124.  
  125. #ifdef    DEBUG
  126.         if (x_opt)
  127.         {
  128.             if (proc->special_flag)
  129.                 printf("Special shutdown of %s (%d) with %s\n",
  130.                     proc->cmd,proc->pid,proc->proc_shutdn);
  131.             else
  132.                 printf("Stopping up process %s (%d)\n",proc->cmd,
  133.                     proc->pid);
  134.         } else
  135. #endif    DEBUG
  136.         if (proc->special_flag)
  137.             system(proc->proc_shutdn);
  138.         else
  139.             kill(proc->pid,SIGSTOP);
  140.     }
  141.  
  142.     /*
  143.      * Now that things should be quiet, sync the disks
  144.      */
  145.  
  146.     sync();
  147.     sleep(1);
  148.     sync();
  149.     sleep(5);
  150.  
  151. }
  152.  
  153. fixlog()
  154. {
  155.     unlink("/etc/nologin");
  156.     fprintf(stderr,"*** Backup aborted ***\n");
  157.     exit(0);
  158. }
  159.  
  160. procont()
  161. {
  162.     int restart();
  163.  
  164.     restart();
  165.     fprintf(stderr,"*** Backup aborted ***\n");
  166.     exit(0);
  167. }
  168.