00001
00002
00003
00004
00005
00006
00007 #include <iostream>
00008
00009 #include <unistd.h>
00010 #include <signal.h>
00011 #include <stdio.h>
00012 #include "SysPlusPlus/syscall.h"
00013
00014 #include "ComPlusPlus/Process.h"
00015 #include <sys/types.h>
00016 #include <errno.h>
00017 #include <sys/wait.h>
00018
00019
00023 compp::Process::Process() {
00024 SetAutoTerm(false);
00025 Pid = syspp::Call::Getpid();
00026 }
00027
00031 compp::Process::Process( std::string n) {
00032
00033 SetAutoTerm(false);
00034 SetName(n);
00035 Pid = syspp::Call::Getpid();
00036
00037 }
00045 compp::Process::~Process() {
00046 if ( TerminateInDestructor ) {
00047 GracefulTerminate();
00048 Terminate();
00049 }
00050 }
00054 void compp::Process::SetAutoTerm ( bool yn ) {
00055 TerminateInDestructor = yn;
00056 }
00060 void compp::Process::SetName( std::string s) {
00061 Name = s;
00062 }
00063
00067 bool compp::Process::GetName( std::string &s) const {
00068 s = Name ;
00069 return true;
00070 }
00071
00072
00076 bool compp::Process::GetPid ( int &p ) const{
00077
00078 p = Pid;
00079 return true;
00080 }
00081
00082
00086 int compp::Process::GetPid ( ) const{
00087 int p = Pid;
00088 return p;
00089 }
00090
00095 bool compp::Process::Suspend(){
00096 #ifdef SIGSTOP
00097 return SendSignal ( SIGSTOP );
00098 #endif
00099 }
00100
00105 bool compp::Process::Continue(){
00106 #ifdef SIGCONT
00107 return SendSignal ( SIGCONT );
00108 #endif
00109 }
00110
00115 bool compp::Process::Terminate(){
00116 #ifdef SIGKILL
00117 return SendSignal ( SIGKILL );
00118 #endif
00119 }
00120
00125 bool compp::Process::TerminateGroup(){
00126 #ifdef SIGKILL
00127 return killpg(Pid, SIGKILL);
00128
00129 #endif
00130 }
00131
00132
00137 bool compp::Process::GracefulTerminate(){
00138 #ifdef SIGTERM
00139 return SendSignal ( SIGTERM );
00140 #endif
00141 }
00142
00146 bool compp::Process::SendSignal ( int signo) {
00147 int i;
00148
00149 if ( signo < 0 ) {
00150
00151 }
00152
00153 i = syspp::Call::Kill ( Pid , signo ) ;
00154
00155 if ( i != 0 ) {
00156 return false;
00157 }
00158 return true;
00159 }
00160
00166 int compp::Process::Wait () {
00167
00168 int status=0, i=0 ;
00169 i = Pid;
00170 int ret=0;
00171 ret = syspp::Call::Waitpid(i, &status, 0);
00172
00173 std::cout << "Wait pid orig "<<Pid << "ret " << ret << "i=" << i <<"\n";
00174 std::cout << "Errno " << errno <<"\n";
00175 perror ( "wait");
00176
00177
00178 if ( WIFEXITED ( status ) ) {
00179 std::cout << "WIFEXITED " << WEXITSTATUS ( status ) << "\n";
00180 return WEXITSTATUS ( status );
00181 }
00182
00183
00184 if ( WEXITSTATUS ( status )) {
00185 std::cout << "WEXITSTATUS \n";
00186
00187 }
00188 if ( WIFSIGNALED ( status ) ) {
00189 std::cout << "WIFSIGNALED \n";
00190
00191 }
00192 if ( WTERMSIG ( status ) ) {
00193 std::cout << "WTERMSIG \n";
00194
00195 }
00196 if ( WCOREDUMP ( status ) ) {
00197 std::cout << "WCOREDUMP \n";
00198
00199 }
00200 if ( WIFSTOPPED ( status ) ) {
00201 std::cout << "WIFSTOPPED \n";
00202
00203 }
00204 if ( WSTOPSIG ( status ) ) {
00205 std::cout << "WSTOPSIG \n";
00206
00207 }
00208 if ( WIFCONTINUED ( status ) ) {
00209 std::cout << "WIFCONTINUED \n";
00210 return 1;
00211 }
00212
00213
00214 return status;
00215
00216 }
00217
00218 bool compp::Process::IsRunning () {
00219
00220 int status, i ;
00221 i = Pid;
00222 if ( i != syspp::Call::Waitpid(i, &status, WNOHANG)) {
00223 return false;
00224 }
00225
00226 return true;
00227 }
00228
00229
00230