rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Public Member Functions | Private Member Functions | Private Attributes | List of all members
rlPcontrol Class Reference

#include <rlpcontrol.h>

Collaboration diagram for rlPcontrol:
Collaboration graph
[legend]

Public Member Functions

 rlPcontrol ()
 
virtual ~rlPcontrol ()
 
void setStartupCommand (const char *command, const char *process_name)
 
int start ()
 
int sigterm ()
 
int sigkill ()
 
int isAlive ()
 
const char * startupCommand ()
 
const char * processName ()
 
rlTimeprocessTime ()
 
void setPID (long pid)
 
long pid ()
 
rlPcontrolgetNext ()
 
rlPcontroladdNew ()
 
void setInput (const char *input)
 
void setOutput (const char *output)
 
void setError (const char *error)
 
void setPriority (int priority)
 
int priority ()
 

Private Member Functions

int rlstrlen (const char *str)
 

Private Attributes

char * startup_command
 
char * process_name
 
char * m_input
 
char * m_output
 
char * m_error
 
long m_dwProcessId
 
long m_pid
 
rlTime process_time
 
rlPcontrolnext
 
int prio
 

Detailed Description

A class for starting/stoping/monitoring other processes.

Definition at line 24 of file rlpcontrol.h.

Constructor & Destructor Documentation

rlPcontrol::rlPcontrol ( )
   construct a process control object
   

Definition at line 45 of file rlpcontrol.cpp.

{
m_pid = -1;
next = NULL;
#ifdef __VMS
m_input = m_output = m_error = NULL;
prio = 4; // normal user priority
#endif
#ifdef RLWIN32
prio = 1; // normal user priority
if(rlSIGTERM == 0) rlSIGTERM = RegisterWindowMessage("rlSIGTERM");
#endif
#ifdef RLUNIX
prio = 0;
#endif
}
rlPcontrol::~rlPcontrol ( )
virtual
   deconstruct the process control object
   

Definition at line 64 of file rlpcontrol.cpp.

{
if(startup_command != NULL) delete [] startup_command;
if(next != NULL) delete next;
#ifdef RLWIN32
if(m_pid != -1) CloseHandle((HANDLE) m_pid);
#endif
#ifdef __VMS
if(m_input != NULL) delete [] m_input;
if(m_output != NULL) delete [] m_output;
if(m_error != NULL) delete [] m_error;
#endif
}

Member Function Documentation

rlPcontrol * rlPcontrol::addNew ( )
   add and return a new rlPcontrol object
   

Definition at line 346 of file rlpcontrol.cpp.

{
rlPcontrol *item;
item = this;
while(item->next != NULL) item = item->next;
item->next = new rlPcontrol();
return item->next;
}
rlPcontrol * rlPcontrol::getNext ( )
   get next object in list
   

Definition at line 341 of file rlpcontrol.cpp.

{
return next;
}
int rlPcontrol::isAlive ( )
   test if process is alive
   

Definition at line 300 of file rlpcontrol.cpp.

{
#ifdef __VMS
long ret,code,mypid;
if(m_pid == -1) return 0;
mypid = m_pid;
code = JPI$_STATE;
ret = lib$getjpi(&code,&mypid,0,0,0,0);
if(ret != SS$_NORMAL)
{
//printf("lib$getjpi terminated abnormal\n");
return 0;
}
if(mypid == m_pid) return 1;
return 0;
#endif
#ifdef RLUNIX
int ret,status;
if(m_pid == -1) return 0;
ret = waitpid(m_pid, &status, WNOHANG);
//printf("isAlive pid=%ld\n",m_pid);
if(ret == 0) return 1;
return 0;
#endif
#ifdef RLWIN32
long status;
if(m_pid == -1) return 0;
if(GetExitCodeProcess((HANDLE) m_pid, (unsigned long *) &status) != 0) // success
{
if(status == STILL_ACTIVE) return 1;
else return 0;
}
return 0; // failure
#endif
}
long rlPcontrol::pid ( )
   get process id
   

Definition at line 109 of file rlpcontrol.cpp.

{
#ifdef RLWIN32
if(m_dwProcessId == -1) return 0;
if(m_dwProcessId == 0) return 0;
return m_dwProcessId;
#else
if(m_pid == -1) return 0;
if(m_pid == 0) return 0;
return m_pid;
#endif
}
int rlPcontrol::priority ( )
   get priority
   

Definition at line 392 of file rlpcontrol.cpp.

{
return prio;
}
const char * rlPcontrol::processName ( )
   get process name
   

Definition at line 83 of file rlpcontrol.cpp.

{
return process_name;
}
rlTime * rlPcontrol::processTime ( )
   get process time start/stop
   

Definition at line 88 of file rlpcontrol.cpp.

{
return &process_time;
}
int rlPcontrol::rlstrlen ( const char *  str)
private

Definition at line 133 of file rlpcontrol.cpp.

{
if(str == NULL) return -1;
else return strlen(str);
}
void rlPcontrol::setError ( const char *  error)
   set error filename or logical
   

Definition at line 371 of file rlpcontrol.cpp.

{
if(m_error != NULL) delete [] m_error;
m_error = new char [strlen(error)+1];
strcpy(m_error,error);
}
void rlPcontrol::setInput ( const char *  input)
   set input filename or logical
   

Definition at line 357 of file rlpcontrol.cpp.

{
if(m_input != NULL) delete [] m_input;
m_input = new char [strlen(input)+1];
strcpy(m_input,input);
}
void rlPcontrol::setOutput ( const char *  output)
   set output filename or logical
   

Definition at line 364 of file rlpcontrol.cpp.

{
if(m_output != NULL) delete [] m_output;
m_output = new char [strlen(output)+1];
strcpy(m_output,output);
}
void rlPcontrol::setPID ( long  pid)
   set process id
   

Definition at line 93 of file rlpcontrol.cpp.

{
if(_pid <= 0)
{
m_pid = -1;
return;
}
#ifdef RLWIN32
if(m_pid != -1) CloseHandle((HANDLE) m_pid);
m_dwProcessId = _pid;
m_pid = (long) OpenProcess(PROCESS_ALL_ACCESS,TRUE,m_dwProcessId);
#else
m_pid = _pid;
#endif
}
void rlPcontrol::setPriority ( int  priority)
   set priority default=8
   

Definition at line 379 of file rlpcontrol.cpp.

{
#ifdef __VMS
if(pri < 0) pri = 0;
if(pri > 15) pri = 15;
#endif
#ifdef RLWIN32
if(pri < 0) pri = 0;
if(pri > 3) pri = 3;
#endif
prio = pri;
}
void rlPcontrol::setStartupCommand ( const char *  command,
const char *  process_name 
)
   set the startup command and the process_name
   

Definition at line 122 of file rlpcontrol.cpp.

{
if(startup_command != NULL) delete [] startup_command;
startup_command = new char[strlen(command)+1];
strcpy(startup_command,command);
if(process_name != NULL) delete [] process_name;
process_name = new char[strlen(processname)+1];
strcpy(process_name,processname);
}
int rlPcontrol::sigkill ( )
   stop the process with SIGKILL
   

Definition at line 276 of file rlpcontrol.cpp.

{
if(m_pid == -1) return -1;
if(m_pid == 0) return -1;
#ifdef RLUNIX
kill(m_pid,SIGKILL);
//printf("kill pid=%ld\n",m_pid);
#endif
#ifdef __VMS
kill(m_pid,SIGKILL);
#endif
#ifdef RLWIN32
TerminateProcess((HANDLE) m_pid, 0);
WaitForSingleObject((HANDLE) m_pid, 60000);
CloseHandle((HANDLE) m_pid);
#endif
m_pid = -1;
return 0;
}
int rlPcontrol::sigterm ( )
   stop the process with SIGTERM
   

Definition at line 231 of file rlpcontrol.cpp.

{
if(m_pid == -1) return -1;
if(m_pid == 0) return -1;
#ifdef RLUNIX
kill(m_pid,SIGTERM);
//printf("kill pid=%ld\n",m_pid);
#endif
#ifdef __VMS
kill(m_pid,SIGTERM);
#endif
#ifdef RLWIN32
#ifdef USE_WINKILL
// does anybody know HOWTO send SIGTERM under windows ?
// for the moment i just kill the process
TerminateProcess((HANDLE) m_pid, 0);
WaitForSingleObject((HANDLE) m_pid, 60000);
CloseHandle((HANDLE) m_pid);
#else
// broadcast rlSIGTERM
// Other prozesses haveto:
// UINT RegisterWindowMessage("rlSIGTERM");
// and evaluate their pid from the message
if(rlSIGTERM != 0)
{
PostMessage(HWND_BROADCAST,rlSIGTERM,-1,m_dwProcessId);
/*
BroadcastSystemMessage(
BSF_IGNORECURRENTTASK, // do not send message to this process
BSM_ALLCOMPONENTS, // BSM_APPLICATIONS, // broadcast only to applications
rlSIGTERM, // PM_MYMSG, // registered private message
-1, // message-specific value
m_dwProcessId); // message-specific value
*/
}
#endif
#endif
m_pid = -1;
return 0;
}
int rlPcontrol::start ( )
   start the process
   

Definition at line 139 of file rlpcontrol.cpp.

{
if(startup_command == NULL) return -1;
if(isAlive()) return -1;
#ifdef RLUNIX
if((m_pid = ::fork()) == 0)
{
::exit(0);
}
// printf("start pid=%ld\n",m_pid);
return 0;
#endif
#ifdef __VMS
int ret;
struct dsc$descriptor_s image,prcnam,input,output,error,*inputptr,*outputptr,*errorptr;
image.dsc$w_length = rlstrlen(startup_command);
image.dsc$b_dtype = DSC$K_DTYPE_T;
image.dsc$b_class = DSC$K_CLASS_S;
image.dsc$a_pointer = startup_command;
prcnam.dsc$w_length = rlstrlen(process_name);
prcnam.dsc$b_dtype = DSC$K_DTYPE_T;
prcnam.dsc$b_class = DSC$K_CLASS_S;
prcnam.dsc$a_pointer = process_name;
input.dsc$w_length = rlstrlen(m_input);
input.dsc$b_dtype = DSC$K_DTYPE_T;
input.dsc$b_class = DSC$K_CLASS_S;
input.dsc$a_pointer = m_input;
output.dsc$w_length = rlstrlen(m_output);
output.dsc$b_dtype = DSC$K_DTYPE_T;
output.dsc$b_class = DSC$K_CLASS_S;
output.dsc$a_pointer = m_output;
error.dsc$w_length = rlstrlen(m_error);
error.dsc$b_dtype = DSC$K_DTYPE_T;
error.dsc$b_class = DSC$K_CLASS_S;
error.dsc$a_pointer = m_error;
inputptr = outputptr = errorptr = 0;
if( input.dsc$w_length > 0) inputptr = &input;
if(output.dsc$w_length > 0) outputptr = &output;
if( error.dsc$w_length > 0) errorptr = &error;
if( inputptr != 0 && inputptr->dsc$a_pointer == NULL) inputptr = 0;
if(outputptr != 0 && outputptr->dsc$a_pointer == NULL) outputptr = 0;
if( errorptr != 0 && errorptr->dsc$a_pointer == NULL) errorptr = 0;
/*
printf("sys$creprc(\n");
printf(" image=%s\n",image.dsc$a_pointer);
printf(" inputptr=%s\n" ,inputptr->dsc$a_pointer);
printf(" outputptr=%s\n",outputptr->dsc$a_pointer);
printf(" errorptr=%s\n" ,errorptr->dsc$a_pointer);
printf(" prcnam=%s\n",prcnam.dsc$a_pointer);
printf(" priority=%d\n",prio);
printf(")\n");
*/
ret = sys$creprc(&m_pid,&image,inputptr,outputptr,errorptr,0,0,&prcnam,prio,0,0,0,0,0);
if(ret != SS$_NORMAL) return -1;
return 0;
#endif
#ifdef RLWIN32
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
DWORD dwCreationFlags;
if(m_pid != -1) CloseHandle((HANDLE) m_pid);
dwCreationFlags = CREATE_NO_WINDOW;
if (prio == 0) dwCreationFlags |= IDLE_PRIORITY_CLASS;
else if(prio == 1) dwCreationFlags |= NORMAL_PRIORITY_CLASS;
else if(prio == 2) dwCreationFlags |= HIGH_PRIORITY_CLASS;
else if(prio == 3) dwCreationFlags |= REALTIME_PRIORITY_CLASS;
int ret = (int) CreateProcess( NULL, startup_command
, NULL, NULL
, FALSE, dwCreationFlags
, NULL, NULL
, &si, &pi);
m_pid = (int) pi.hProcess;
m_dwProcessId = pi.dwProcessId;
CloseHandle(pi.hThread);
return ret;
#endif
}
const char * rlPcontrol::startupCommand ( )
   get startup command
   

Definition at line 78 of file rlpcontrol.cpp.

{
}

Member Data Documentation

long rlPcontrol::m_dwProcessId
private

Definition at line 132 of file rlpcontrol.h.

char * rlPcontrol::m_error
private

Definition at line 128 of file rlpcontrol.h.

char* rlPcontrol::m_input
private

Definition at line 128 of file rlpcontrol.h.

char * rlPcontrol::m_output
private

Definition at line 128 of file rlpcontrol.h.

long rlPcontrol::m_pid
private

Definition at line 135 of file rlpcontrol.h.

rlPcontrol* rlPcontrol::next
private

Definition at line 137 of file rlpcontrol.h.

int rlPcontrol::prio
private

Definition at line 138 of file rlpcontrol.h.

char * rlPcontrol::process_name
private

Definition at line 125 of file rlpcontrol.h.

rlTime rlPcontrol::process_time
private

Definition at line 136 of file rlpcontrol.h.

char* rlPcontrol::startup_command
private

Definition at line 125 of file rlpcontrol.h.


The documentation for this class was generated from the following files: