00001 #include <sys/wait.h> 00002 00003 #include <fcntl.h> 00004 #include <unistd.h> 00005 00006 #include "procctl.h" 00007 00008 ProcContainer *ProcCtl::m_last = 0; 00009 ProcCtl* ProcCtl::m_self = 0; 00010 00011 ProcCtl::ProcCtl() { 00012 signal( SIGCHLD, signal_handler ); 00013 } 00014 ProcCtl::~ProcCtl() { 00015 } 00016 ProcCtl* ProcCtl::self() { 00017 if (!m_self ) { 00018 m_self = new ProcCtl; 00019 } 00020 return m_self; 00021 } 00022 void ProcCtl::add(pid_t pi, int fd ) { 00023 ProcContainer * con = new ProcContainer; 00024 //memset(con, 0, sizeof(con) ); 00025 con->pid = pi; 00026 con->fd = fd; 00027 con->status = 0; 00028 con->prev = m_last; 00029 00030 m_last = con; 00031 00032 } 00033 void ProcCtl::remove( pid_t pi ) { 00034 /* 00035 * We first check if the last item 00036 * is equal to pi the we 00037 * 00038 */ 00039 ProcContainer* con; 00040 if (m_last->pid == pi ) { 00041 con = m_last; 00042 m_last = con->prev; 00043 delete con; 00044 return; 00045 } 00046 00047 con = m_last; 00048 ProcContainer* forw = 0l; 00049 while (con ) { 00050 /* remove it */ 00051 if ( pi == con->pid ) { 00052 forw->prev = con->prev; 00053 delete con; 00054 return; 00055 } 00056 00057 forw = con; 00058 con = con->prev; 00059 } 00060 00061 } 00062 void ProcCtl::remove( ProcContainer con ) { 00063 remove( con.pid ); 00064 } 00065 int ProcCtl::status(pid_t pid )const{ 00066 ProcContainer *con = m_last; 00067 while (con) { 00068 if (con->pid == pid ) 00069 return con->status; 00070 con = con->prev; 00071 } 00072 return -1; 00073 } 00074 void ProcCtl::signal_handler(int) { 00075 int status; 00076 signal( SIGCHLD, signal_handler ); 00077 pid_t pi = waitpid( -1, &status, WNOHANG ); 00078 00079 /* 00080 * find the container for pid 00081 * 00082 */ 00083 if ( pi < 0 ) { 00084 return; 00085 } 00086 00087 ProcContainer* con = m_last; 00088 while (con) { 00089 if ( con->pid == pi ) { 00090 con->status = status; 00091 char result = 1; 00092 /* give a 'signal' */ 00093 ::write(con->fd, &result, 1 ); 00094 } 00095 con = con->prev; 00096 } 00097 }
1.4.2