home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / sredird / telnetcpcd-1.09.tar.gz / telnetcpcd-1.09.tar / wait_child.c < prev   
C/C++ Source or Header  |  2003-08-12  |  2KB  |  71 lines

  1. /*
  2.     wait_child.c
  3.  
  4.     Copyright (c) 2002,2003 Thomas J Pinkl <tom@pinkl.com>
  5.  
  6.     reap the exit status of a child process 
  7.  
  8.     Version 1.00    12/03/2001
  9. */
  10.  
  11. #include "telnetcpcd.h"
  12.  
  13. pid_t wait_child(pid_t pid,int *statloc)
  14. {
  15.     extern int errno;
  16.     pid_t ret;
  17.     int *status;
  18.     int wstatus;
  19.  
  20.     if (statloc != (int *) NULL) {
  21.         status = statloc;
  22.     } else {
  23.         status = &wstatus;
  24.     }
  25.     ret = waitpid(pid,status,WNOHANG);
  26.     if ((ret == (pid_t) 0) && (pid != (pid_t) -1)) {
  27.         /*
  28.             pid == -1 means caller is polling.  don't retry in this case.
  29.             status not yet available for specified pid. 
  30.             we'll wait a second and try again 
  31.         */
  32.         syslog(LOG_DEBUG,"waitpid() returned 0; will try again");
  33.         sleep(1);
  34.         ret = waitpid(pid,status,WNOHANG);
  35.     }
  36.     if (ret == (pid_t) -1) {
  37.         if (errno == ECHILD) {
  38.             ;
  39.             /* syslog(LOG_DEBUG,"waitpid(): no more child processes"); */
  40.         } else {
  41.             syslog_perror("waitpid() error");
  42.         }
  43.     } else {
  44.         if (ret > 0) {
  45.             log_termination_status(ret,*status);
  46.         }
  47.     }
  48.     return(ret);
  49. }
  50.  
  51. /*
  52.     interpret a child's exit status and send the 
  53.     result to the syslog
  54. */
  55. void log_termination_status(pid_t pid,int status)
  56. {
  57.     char *str;
  58.  
  59.     str = "child PID";
  60.     if (WIFEXITED(status)) {
  61.         syslog(LOG_ERR,"%s %d terminated normally by calling exit(%d)",
  62.             str,pid,WEXITSTATUS(status));
  63.     } else if (WIFSIGNALED(status)) {
  64.         syslog(LOG_ERR,"%s %d terminated by %s signal (number %d)",
  65.             str,pid,signame(WTERMSIG(status)),WTERMSIG(status));
  66.     } else {
  67.         syslog(LOG_ERR,"%s %d unknown termination status, value: %d",
  68.             str,pid,status);
  69.     }
  70. }
  71.