home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / server / src / sighandlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  4.2 KB  |  192 lines

  1. #include "headers.h"
  2.  
  3. /* this file has the signal handlers for timeouts, gen. signals, etc. */
  4.  
  5. /* FIX - MUST BE REENTRANT.. OR "SAFE"..                 */
  6. /*     - this probably includes functions it calls too.. */
  7.  
  8. /* called for things such as SIGINT, SIGTERM, etc. */
  9. RETSIGTYPE sighandler(int signum)
  10. {
  11.    int res;    /* results of various functions */
  12.    int status; /* used with waitpid()          */
  13.  
  14.    register int i;
  15.  
  16.    if (signum == SIGUSR1)
  17.    {
  18.       stopped = 0;
  19.       return;
  20.    }
  21.  
  22.    else if (signum == SIGPIPE)
  23.    {
  24.       error("SIGPIPE received, killing myself...\n\n");
  25.       longjmp(doquit, ERROR);
  26.    }
  27.  
  28.    else if (signum == SIGCHLD)
  29.    {
  30.       /* FIX - NOT SAFE.. BLOCK SIGNALS */
  31.       signal(SIGCHLD, sighandler); /* restart it */
  32.       
  33.       for (i = 0; i < MAXCONNS; i++)
  34.       {
  35.          if (clients[i].free == 1) continue;
  36.  
  37.          errno = 0;
  38.          res = waitpid((clients[i]).pidstats[0].pid, &status, WNOHANG);
  39.  
  40.          if (res == 0) continue;
  41.          else if ((res == ERROR) || (WIFEXITED(status)))
  42.          {
  43.             if (errno <= 0)
  44.             {
  45.                if (clients[i].pidstats[0].pid > 0)
  46.                {
  47.                   debug("child (pid %d) has exited..\n\n",
  48.                         clients[i].pidstats[0].pid);
  49.  
  50.                   memset(&clients[i], 0, sizeof(struct client));
  51.  
  52.                   (clients[i]).free = 1;
  53.                   (clients[i]).ID = ERROR;
  54.                   (clients[i]).pidstats[0].pid = ERROR;
  55.                   (clients[i]).pidstats[1].pid = ERROR;
  56.  
  57.                   nofree = 0;
  58.  
  59.                   /* continue; */
  60.                   break;
  61.                }
  62.             }
  63.  
  64.             else
  65.             {
  66.                if (errno != ECHILD)
  67.                   error("waitpid() result: %s\n\n", strerror(errno));
  68.  
  69.                if (clients[i].pidstats[0].pid > 0)
  70.                {
  71.                   debug("child (pid %d) has exited..\n\n",
  72.                         clients[i].pidstats[0].pid);
  73.  
  74.                   memset(&clients[i], 0, sizeof(struct client));
  75.  
  76.                   (clients[i]).free = 1;
  77.  
  78.                   (clients[i]).ID = ERROR;
  79.                   (clients[i]).pidstats[0].pid = ERROR;
  80.                   (clients[i]).pidstats[1].pid = ERROR;
  81.  
  82.                   nofree = 0;
  83.  
  84.                   /* continue; */
  85.                   break;
  86.                }
  87.             }
  88.          }
  89.       }
  90.    }
  91.  
  92.    else 
  93.    {
  94.       error("received a signal to abort in sighandler.. signal was %d\n\n",
  95.             signum);
  96.  
  97.       /* FIX - NOT SAFE */
  98.       longjmp(doquit, ABORT);
  99.    }
  100. }
  101.  
  102.  
  103. /* ---------------------------- */
  104.  
  105.  
  106. /* called when we time out waiting for ID */
  107. RETSIGTYPE IDtimeout(int signum)
  108. {
  109.    error("timedout while waiting for ID\n\n");
  110.    timeout = 1;
  111. }
  112.  
  113.  
  114. /* ---------------------------- */
  115.  
  116.  
  117. /* called when we time out auth'ing ID */
  118. RETSIGTYPE authIDtimeout(int signum)
  119. {
  120.    error("timeout while authenticating ID\n\n");
  121.    timeout = 1;
  122. }
  123.  
  124.  
  125. /* ------------------------- */
  126.  
  127.  
  128. /* called when we time out waiting for an instruction */
  129. RETSIGTYPE InsTimeout(int signum)
  130. {
  131.    error("timeout while waiting for an instruction.. exiting\n\n");
  132.    longjmp(doquit, ERROR);
  133. }
  134.  
  135.  
  136. /* ------------------------- */
  137.  
  138.  
  139. /* when we time out waiting for or sending "OKAY */
  140. RETSIGTYPE okayTimeout(int sig)
  141. {
  142.    error("timed out waiting or sending OKAY\n\n");
  143.    timeout = 1;
  144. }
  145.  
  146.  
  147. /* ------------------------- */
  148.  
  149.  
  150. /* called when we timeout waiting for pong */
  151. RETSIGTYPE pingTimeout(int signum)
  152. {
  153.    error("timed out waiting for ping response.. exiting\n\n");
  154.    longjmp(doquit, ERROR);
  155. }
  156.  
  157.  
  158. /* ------------------------- */
  159.  
  160.  
  161. /* sighandler when we get OK from child to restart */
  162. RETSIGTYPE startUp(int signum)
  163. {
  164.    if (debugging == 1)
  165.    {
  166.       char *error = "got SIGUSR2 from child (free to continue)\n";
  167.       (void)write(STDOUT, error, strlen(error));
  168.    }
  169.  
  170.    clients[curClient].running = 1; /* set us back running */
  171.    return;
  172. }
  173.  
  174.  
  175. /* ------------------------- */
  176.  
  177.  
  178. /* sighandler when we are notified of new data from parent */
  179. RETSIGTYPE gotNewData(int signum)
  180. {
  181.    if (debugging == 1)
  182.    {
  183.       char *error = "got SIGUSR1 from parent (new data available)\n";
  184.       (void)write(STDOUT, error, strlen(error));
  185.    }
  186.  
  187.    signal(SIGUSR1, gotNewData); /* this has to always be up */
  188.    clients[curClient].newData = 1; /* set us back running */
  189.  
  190.    return;
  191. }
  192.