struct ProcessInfo *getProcessInfoByID(pid_t pid) { struct ProcessInfo *p = findProcess(pid); if (!p) { topUpdate(); p = findProcess(pid); } return p; }
// --------------------------------------------------------------------------- // 2nd phase constructor. // --------------------------------------------------------------------------- // void CWatchdog::ConstructL() { PRINT(_L("CWatchdog::ConstructL()")); TFullName name; // find Harvester Server TFindProcess findProcess( KHarvesterServerProcess ); if ( findProcess.Next(name) == KErrNone ) { PRINT(_L("CWatchdog::ConstructL() - found server, start listening")); User::LeaveIfError( iProcess.Open(name) ); iState = ERunning; // logon to get termination signal iProcess.Logon(iStatus); SetActive(); } else { PRINT(_L("CWatchdog::ConstructL() - start Harvester server")); // start new Harvester Start(); } PRINT(_L("CWatchdog::ConstructL() - create observer")); iShutdownObserver = CWDShutdownObserver::NewL( *this ); iSelfShutdownObserver = CWDSelfShutdownObserver::NewL( *this ); }
void CTestCalInterimApiSuiteStepBase::WaitForAgendaServerShutdown() { // the name of the agenda server process includes its uid like this [10003a5b] _LIT(KAgendaServerUIDMatch, "*[10003a5b]*"); TFindProcess findProcess(KAgendaServerUIDMatch); TFullName fullName; TInt findProcessResult(KErrNone); findProcessResult = findProcess.Next(fullName); if (findProcessResult == KErrNone) { // find the latest agenda server process while (findProcessResult == KErrNone) { findProcessResult = findProcess.Next(fullName); } // The agenda server process is running so wait RProcess process; if (process.Open(fullName) == KErrNone) { TRequestStatus processStatus; process.Logon(processStatus); // ask for a callback when the process ends // Don't wait for the server to close for longer than 7 seconds RTimer timeOutTimer; timeOutTimer.CreateLocal(); TRequestStatus timeOutStatus; timeOutTimer.After(timeOutStatus, 7000000); // Wait for either the agenda server to close // or the time out timer to time out. User::WaitForRequest(processStatus, timeOutStatus); if (timeOutStatus.Int() == KRequestPending) { timeOutTimer.Cancel(); User::WaitForRequest(timeOutStatus); } else { if (timeOutStatus.Int() == KErrNone) { // Agenda server shutdown request has timed out ERR_PRINTF1(KErrAgendaServerShutdownTimedOut); SetTestStepResult(EFail); } } timeOutTimer.Close(); process.LogonCancel(processStatus); process.Close(); } } }
int JdbcUtil::addProcess(pid_t pid) { if (!attachMemory()) return -1; if (!lockSemaphore(SEM_MEMORY_ACCESS,true)) return -1; int rc = -1; thisProcessId = pid; int idx = findProcess(thisProcessId); if (idx>=0) { setError("Cannot add existing process to table"); } else { idx = findProcess(0); if (idx<0) { if (lockSemaphore(SEM_MEMORY_ACCESS, false)) { if (cleanup()) { if (lockSemaphore(SEM_MEMORY_ACCESS, true)) { idx = findProcess(0); if (idx<0) { setError("Too many processes"); } } } } } if (idx>=0) { sharedMemory->processTable[idx].processId = thisProcessId; sharedMemory->processTable[idx].cmd = JDBCUTIL_CMD_NOP; sharedMemory->processTable[idx].pending = false; rc = idx; memoryIdx = idx; } } if (!lockSemaphore(SEM_MEMORY_ACCESS, false)) rc = -1; return rc; }
BOOL Injector::getTargetProcess(std::wstring processName) { DWORD pid = 0; pid = findProcess(processName); this->_proc = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, pid); if (this->_proc == NULL) return (FALSE); return (TRUE); }
bool ProcessServer::removeProcess( dword processId ) { AutoLock lock( &m_Lock ); int pi = findProcess( processId ); if ( pi >= 0 ) { m_ProcessList.remove( pi ); saveProcessList(); } return pi >= 0; }
int read_proc(char *buf,char **start,off_t offset,int count,int *eof,void *data ) { char outputBuffer[1001]; int len = 0; struct processInfo* curProcess; struct threadInfo* curThread; if(DEBUG) printk("Entering read_proc\n"); if (offset>0){ return 0; } //Find if current process exists; if not create it curProcess = findProcess(current); // if (DEBUG) curProcess = headProcess; // if (DEBUG) current->pid = headProcess->headThread->pid; if (curProcess==NULL){ if(DEBUG) printk("current process does not exist\n"); curProcess=addProcess(current); curProcess->seed = A_PRNG; if(DEBUG) printk("numThreads:%lu", curProcess->numThreads); }else{ printk("Process already exists. Process not being reseeded"); } //Find if current thread exists; if not create it curThread = findThread(curProcess,current); if (curThread==NULL){ if(DEBUG) printk("creating curret thread\n"); curThread=addThread(curProcess,current); } len = sprintf(outputBuffer, "%ld\n", curThread->nextRandom); if(DEBUG) printk("nextRandom: %lu\n",curThread->nextRandom); curThread->nextRandom = nextRandomGen(curThread->nextRandom, curProcess->numThreads); if (DEBUG) printk("nextnextRandom %lu\n", curThread->nextRandom); if (DEBUG) printk("thread %d\n", curThread->pid); memcpy(buf, outputBuffer, len); /*if()){*/ /*if(DEBUG) printk("copy to user failed\n");*/ /*return -EFAULT; */ /*}*/ *eof = 1; if(DEBUG) printk("Leaving read_proc\n"); return len; }
/** @SYMTestCaseID SYSLIB-LOGENG-CT-1021 @SYMTestCaseDesc Tests for killing the server Tests for RProcess::Kill(),RThread::Kill() function @SYMTestPriority High @SYMTestActions If EKA2 find the process and kill it, for EKA1 find thread and kill it @SYMTestExpectedResults Test must not fail @SYMREQ REQ0000 */ LOCAL_C void TestKillServerL() { TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-LOGENG-CT-1021 Before Killing Server ")); User::After(2000000); // for EKA2 find process and Kill // Note: this needs CAPABILITY PowerMgmt TFindProcess findProcess(SERVER_NAME); TFullName result; LEAVE_IF_ERROR( findProcess.Next(result) ); RProcess server; LEAVE_IF_ERROR( server.Open(findProcess, EOwnerProcess) ); server.Kill(0); TheTest.Next(_L("Server is Killed")); User::After(2000000); }
/* * Start the accessories server * @return err Symbian error code. KErrNone if successful */ void CT_DevSoundServer::DoCmdutilStartApp() { RProcess process; TInt err(KErrNone); TFullName processName; TFindProcess findProcess(KPROCESSNAME); findProcess.Next(processName); if (processName != KNullDesC) { return; } err = process.Create(KApplicationName,KCmdName); // start the process running! Don't forget this. if (err==KErrNone) { process.Resume(); //start .exe application } process.Close(); // Closes the handle, not the process. }
int signal(struct pcb* process, int pid, int sig_no){ struct pcb* target_process; // kprintf("in signal\n"); // kprintf("pid = %d, sig num = %d\n",pid,sig_no); target_process = findProcess(pid); if(0>target_process){ process->return_code = -18; return -18; //invalid PID, process not found } if(sig_no < 0 || sig_no > SIG_TABLE_SIZE-1){ process->return_code = -3; return -3; //invalid signal number } //check if target signal is installed if(!target_process->sig_handler_table[sig_no]){ process->return_code = -3; return -3; //invalid signal number } //register signal to be waiting target_process->sig_waiting_mask = (1 << sig_no) | target_process->sig_waiting_mask; //kprintf("wait mask = %b\n",target_process->sig_waiting_mask); //check if process is blocked if(target_process->state!=RUNNING || target_process->state != READY){ target_process->return_code = -128; ready(target_process);//puts it back on ready queue } process->return_code = 0; return 0; }
void CPerformanceFunctionalityBase::WaitForServerToExitL() { /** hardware testing is performed using techview, which always has a session open to cntsrv, hence cntsrv never shutsdown when performing HW testing. */ #ifdef __WINSCW__ // The name of the CntSrv process includes its uid like this CNTSRV.EXE[10003a73]001 TInt findProcessResult = KErrNone; TFindProcess findProcess( KServerUid ); TFullName fullNameLastProcess; TFullName fullNameNextProcess; //TFullName findProcessResult = findProcess.Next(fullNameNextProcess); for ( TInt iteration = 1; findProcessResult == KErrNone; ++iteration ) { fullNameLastProcess = fullNameNextProcess; RProcess process; const TInt OpenError = process.Open(fullNameLastProcess); if ( OpenError == KErrNone) { TExitCategoryName category = process.ExitCategory(); //if this handle to server is still open, then wait for this handle to close if( 0 == category.Length() ) { CloseProcessL( fullNameLastProcess, iteration ); } } findProcessResult = findProcess.Next(fullNameNextProcess); } #else KServerUid();//removes unused variable warning #endif }
void CWatchdog::Start() { PRINT(_L("CWatchdog::Start()")); // Double check that harvester server is not already running TFullName name; TInt res( KErrNone ); // find really Harvester Server, using TFindServer to avoid logon a dead process TFindServer findServer( KHarvesterServerProcess ); if ( findServer.Next(name) == KErrNone ) { TFindProcess findProcess( KHarvesterServerProcess ); if ( findProcess.Next(name) == KErrNone ) { PRINT(_L("CWatchdog::Start() - server already running, start listening")); iProcess.Close(); res = iProcess.Open(name); if ( res != KErrNone ) { PRINT(_L("CWatchdog::Start() - error in starting listening ")); return; } iState = EWaitingRendezvous; SetActive(); return; } } // close the panic process TFindProcess findProcess( KHarvesterServerProcess ); if ( findProcess.Next(name) == KErrNone ) { iProcess.Close(); } // Create the server process // KNullDesC param causes server's E32Main() to be run res = iProcess.Create( KHarvesterServerExe, KNullDesC ); if ( res != KErrNone ) { PRINT(_L("CWatchdog::ConstructL() - error in server creation")); return; } // start process and wait until it is constructed iProcess.Rendezvous(iStatus); if( iStatus != KRequestPending ) { iProcess.Kill( 0 ); // abort startup } else { iProcess.Resume(); // logon OK - start the server iState = EWaitingRendezvous; SetActive(); } PRINT(_L("CWatchdog::ConstructL() - Start end")); }
int send(struct pcb* this_process, int dest_pid, void *buffer, int buffer_len ){ struct pcb* dest_process = findProcess(dest_pid); struct pcb* expected_sender; int buffer_limit; if(dest_process==-1) return -1; //no matching pid if(this_process->pid==dest_process->pid) return -2; //sending to itself expected_sender = dest_process->msg_queue; //kprintf("sending %d to %d\n",this_process->pid,dest_pid); while(expected_sender){ //matching receiver found if(expected_sender->pid==this_process->pid && dest_process->state==RECV_BLOCKED){ //kprintf("match again!\n"); if(dest_process->buffer.size < buffer_len) buffer_limit = dest_process->buffer.size; else buffer_limit = buffer_len; //copy data blkcopy(dest_process->buffer.addr,buffer,buffer_limit); //dequeue expected sender from sender queue dest_process->msg_queue = dequeue(dest_process->msg_queue,expected_sender); //unblock destination process ready(dest_process); //return bytes return buffer_limit; } expected_sender = expected_sender->next; } //no match, receiver is not receiving yet if(dest_process->buffer.ipc_pid==-1){ //destination is in receive from all mode if(dest_process->buffer.size < buffer_len) buffer_limit = dest_process->buffer.size; else buffer_limit = buffer_len; //copy data blkcopy(dest_process->buffer.addr,buffer,buffer_limit); //unblock destination process ready(dest_process); //return bytes return buffer_limit; } this_process->buffer.ipc_pid = dest_process->pid; this_process->buffer.addr = buffer; this_process->buffer.size = buffer_len; this_process->state = SEND_BLOCKED; //add this process to receiver's sender queue dest_process->msg_queue = addtoqueue(dest_process->msg_queue,this_process); //remember to set this_process->next to NULL after calling next() return -3; //destination process not yet receiving }
int recv(struct pcb* this_process, int *from_pid, void *buffer, int buffer_len ){ struct pcb* src_process; int buffer_limit; //kprintf("in receive\n"); if(*from_pid==0){ if(this_process->msg_queue){ kprintf("send queue is not empty!\n"); *from_pid = this_process->msg_queue->pid; } kprintf("pid not specified, receiving from anyone\n"); } if(*from_pid!=0){ src_process = findProcess(*from_pid); kprintf("receiving from pid %d to %d\n",*from_pid,this_process->pid); } if(src_process==-1) return -1; //no matching pid if(this_process->pid==src_process->pid) return -2; //sending to itself //receiving from a process that's receiving from you if(src_process){ if(src_process->state==RECV_BLOCKED){ if(src_process->buffer.ipc_pid=this_process->pid) return -4; } } if(src_process->state==SEND_BLOCKED && src_process->buffer.ipc_pid==this_process->pid && *from_pid!=0){ //match found, proceed with data transfer if(src_process->buffer.size > buffer_len) buffer_limit = buffer_len; else buffer_limit = src_process->buffer.size; //copy data blkcopy(buffer,src_process->buffer.addr,buffer_limit); printQueue(this_process->msg_queue); //dequeue src process from sender list this_process->msg_queue = dequeue(this_process->msg_queue,src_process); //update the pointer to from pid //blkcopy(*from_pid,&(src_process->pid),sizeof(int)); printQueue(this_process->msg_queue); //while(1); //unblock sending process, put it back on ready queue ready(src_process); //return received bytes return buffer_limit; }else{ if(*from_pid!=0){ //add source process to send queue this_process->msg_queue = addtoqueue(this_process->msg_queue,src_process); //printQueue(this_process->msg_queue); this_process->buffer.ipc_pid = src_process->pid; }else{ kprintf("no senders\n"); this_process->buffer.ipc_pid = -1; } this_process->buffer.addr = buffer; this_process->buffer.size = buffer_len; this_process->state = RECV_BLOCKED; return -3; //source process not yet sending } }
LOCAL_C void MainL() { // delay so that cenrep has time to read new config TN_DEBUG1( "MainL() after" ); OstTrace0( TRACE_NORMAL, _MAINL, "::MainL - after" ); User::After(5000000); TInt res( KErrNone ); RProcess process; TFullName name; // find and terminate Thumb AG Daemon TN_DEBUG1( "MainL() KTAGDaemonProcess find" ); OstTrace0( TRACE_NORMAL, DUP1__MAINL, "::MainL - find" ); TFindProcess findProcess( KTAGDaemonProcess ); if ( findProcess.Next(name) == KErrNone ) { res = process.Open(name); TN_DEBUG2( "MainL() KTAGDaemonProcess open res = %d", res ); OstTrace1( TRACE_NORMAL, DUP2__MAINL, "::MainL;res=%d - open res", res ); // logon to get termination signal TRequestStatus status; TN_DEBUG1( "MainL() KTAGDaemonProcess logon" ); OstTrace0( TRACE_NORMAL, DUP3__MAINL, "::MainL - logon" ); process.Logon(status); // shutdown using P&S key TN_DEBUG2( "MainL() KTAGDaemonProcess Set = %d", res ); OstTrace1( TRACE_NORMAL, DUP4__MAINL, "::MainL - Set = res;res=%d", res ); res = RProperty::Set(KTAGDPSNotification,KShutdown,1); // blocks here until thread is terminated TN_DEBUG1( "MainL() KTAGDaemonProcess wait" ); OstTrace0( TRACE_NORMAL, DUP5__MAINL, "::MainL - KTAGDaemonProcess wait" ); User::WaitForRequest(status); // reset key TN_DEBUG1( "MainL() KTAGDaemonProcess reset" ); OstTrace0( TRACE_NORMAL, DUP6__MAINL, "::MainL - KTAGDaemonProcess reset" ); res = RProperty::Set(KTAGDPSNotification, KShutdown, 0); TN_DEBUG1( "MainL() KTAGDaemonProcess close" ); OstTrace0( TRACE_NORMAL, DUP7__MAINL, "::MainL - close" ); process.Close(); } // find and terminate Thumbnail Server TFindProcess findProcess2( KThumbnailServerProcess ); TN_DEBUG1( "MainL() KThumbnailServerProcess find" ); OstTrace0( TRACE_NORMAL, DUP8__MAINL, "::MainL - find" ); if ( findProcess2.Next(name) == KErrNone ) { res = process.Open(name); TN_DEBUG2( "MainL() KThumbnailServerProcess open res = %d", res ); OstTrace1( TRACE_NORMAL, DUP9__MAINL, "::MainL - KThumbnailServerProcess open;res=%d", res ); // logon to get termination signal TRequestStatus status; TN_DEBUG1( "MainL() KThumbnailServerProcess logon" ); OstTrace0( TRACE_NORMAL, DUP10__MAINL, "::MainL - KThumbnailServerProcess logon" ); process.Logon(status); // shutdown using P&S key TN_DEBUG2( "MainL() KThumbnailServerProcess Set res = %d", res ); OstTrace1( TRACE_NORMAL, DUP11__MAINL, "::MainL - KThumbnailServerProcess Set;res=%d", res ); res = RProperty::Set(KTMPSNotification,KShutdown,1); // blocks here until thread is terminated TN_DEBUG1( "MainL() KThumbnailServerProcess wait" ); OstTrace0( TRACE_NORMAL, DUP12__MAINL, "::MainL - KThumbnailServerProcess wait" ); User::WaitForRequest(status); // reset key TN_DEBUG2( "MainL() KThumbnailServerProcess reset res = %d", res ); OstTrace1( TRACE_NORMAL, DUP13__MAINL, "::MainL;res=%d - KThumbnailServerProcess reset", res ); res = RProperty::Set(KTMPSNotification, KShutdown, 0); TN_DEBUG1( "MainL() KThumbnailServerProcess close" ); OstTrace0( TRACE_NORMAL, DUP14__MAINL, "::MainL - close" ); process.Close(); } // delay just in case old daemon hasn't died yet TN_DEBUG1( "MainL() after" ); User::After(2500000); // start new ThumbAGDaemon RProcess server; TInt retryCount = 0; // Create the server process // KNullDesC param causes server's E32Main() to be run TN_DEBUG2( "MainL() KTAGDaemonExe create res = %d", res ); OstTrace1( TRACE_NORMAL, DUP15__MAINL, "::MainL - KTAGDaemonExe create res;res=%d", res ); res = server.Create( KTAGDaemonExe, KNullDesC ); // try again 3 times if fails while ( res != KErrNone) { TN_DEBUG1( "MainL() KTAGDaemonExe retry" ); OstTrace0( TRACE_NORMAL, DUP16__MAINL, "::MainL- KTAGDaemonExe retry" ); if (retryCount > 2) { TN_DEBUG1( "MainL() KTAGDaemonExe retry > 2" ); OstTrace0( TRACE_NORMAL, DUP17__MAINL, "::MainL - KTAGDaemonExe retry > 2" ); return; } TN_DEBUG1( "MainL() after" ); OstTrace0( TRACE_NORMAL, DUP18__MAINL, "::MainL - after" ); User::After(2500000); TN_DEBUG2( "MainL() KTAGDaemonExe create res = %d", res ); OstTrace1( TRACE_NORMAL, DUP19__MAINL, "::MainL - KTAGDaemonExe create;res=%d", res ); res = server.Create( KTAGDaemonExe, KNullDesC ); retryCount++; } // Process created successfully TRequestStatus status; TN_DEBUG1( "MainL() Rendezvous" ); OstTrace0( TRACE_NORMAL, DUP20__MAINL, "::MainL - Rendezvous" ); server.Rendezvous( status ); TN_DEBUG1( "MainL() Resume" ); OstTrace0( TRACE_NORMAL, DUP21__MAINL, "::MainL - resume" ); server.Resume(); // start it going // Wait until the completion of the server creation TN_DEBUG1( "MainL() wait" ); OstTrace0( TRACE_NORMAL, DUP22__MAINL, "::MainL - wait" ); User::WaitForRequest( status ); TN_DEBUG1( "MainL() wait done" ); OstTrace0( TRACE_NORMAL, DUP23__MAINL, "::MainL - wait done" ); // Server created successfully server.Close(); // we're no longer interested in the other process }
int write_proc(struct file *file,const char *buf,int count,void *data ) { char bufCopy[1001]; long seed = 0; /*int numThreads = 0;*/ /*int len = 0;*/ struct processInfo* curProcess; struct threadInfo* curThread; struct task_struct* taskNext; //Nothing to write if (count==0){ return 0; } //Set upper limit on size if(count > MAX_PROC_SIZE) count = MAX_PROC_SIZE; //Copy from user to kernal space if(copy_from_user(bufCopy,buf, count)) return -EFAULT; //Set terminating character /*bufCopy[count] = '\0';*/ //Pull out numbers from user sscanf(bufCopy, "%lu",&seed); //If no seed chosen if (seed==0){ seed = A_PRNG; } printk("proccess id: %d", current->tgid); //Find if current process exists; if not create it curProcess = findProcess(current); if (curProcess==NULL){ curProcess=addProcess(current); curProcess->seed = seed; }else{ printk("Process already exists. Process not being reseeded."); curProcess->numThreads++; printk("threads:%d\n", curProcess->numThreads); /* taskNext = next_task(current); while (taskNext&&taskNext->pid!=current->pid){ curProcess->numThreads++; printk("threads:\n",curProcess->numThreads ); taskNext = next_task(taskNext); }*/ } //Find if current thread exists; if not create it curThread = findThread(curProcess,current); if (curThread==NULL){ curThread=addThread(curProcess,current); } if(DEBUG) printk("Exiting write proc\n"); memcpy(proc_data,bufCopy,count); if(DEBUG) printk("Exiting write_proc seed:%lu s\n",seed); return count; }
void ProcessServer::onReceive( dword client, byte message, const InStream & input ) { //LOG_STATUS( "ProcessServer","onReceive, client = %u (%s), message = 0x%x", client, clientAddress(client), message ); switch( message ) { case ProcessClient::SERVER_LOGIN: { dword job; input >> job; CharString uid; input >> uid; CharString md5; input >> md5; bool result = false; MetaClient::Profile profile; if ( m_MetaClient.loginByProxy( uid, md5, profile ) > 0 ) { LOG_STATUS( "ProcessServer", CharString().format("Login %s, client %u (%s)", profile.name.cstr(), client, clientAddress(client)) ); result = (profile.flags & (MetaClient::ADMINISTRATOR|MetaClient::SERVER)) != 0; } else LOG_STATUS( "ProcessServer", CharString().format("Login failed, client %u (%s)", client, clientAddress(client)) ); AutoLock lock( &m_Lock ); m_ClientValid[ client ] = result; send( client, ProcessClient::CLIENT_JOB_DONE ) << job << result; } break; case ProcessClient::SERVER_SESSION_LOGIN: { dword job; input >> job; dword sessionId; input >> sessionId; bool result = false; MetaClient::Profile profile; if ( m_MetaClient.loginByProxy( sessionId, profile ) > 0 ) { LOG_STATUS( "ProcessServer", CharString().format("Login %s, client %u (%s)", profile.name.cstr(), client, clientAddress(client)) ); result = (profile.flags & MetaClient::ADMINISTRATOR) != 0; } else LOG_STATUS( "ProcessServer", CharString().format("Login failed, client %u (%s)", client, clientAddress(client)) ); AutoLock lock( &m_Lock ); m_ClientValid[ client ] = result; send( client, ProcessClient::CLIENT_JOB_DONE ) << job << result; } break; case ProcessClient::SERVER_SEND_PROCESS_LIST: if ( validateClient( client ) ) { dword job; input >> job; // send process list to the server AutoLock lock( &m_Lock ); send( client, ProcessClient::CLIENT_RECV_PROCESS_LIST ) << job << m_ProcessList; } break; case ProcessClient::SERVER_SEND_CONFIG: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; CharString config; CharString configFile; if ( processId != 0 ) { Process proc; if ( findProcess( processId, proc ) ) configFile = proc.config; } else configFile = m_Context.config; LOG_STATUS( "ProcessServer", CharString().format("Send Config, client %u (%s), configFile = %s", client, clientAddress(client), configFile.cstr()) ); // attempt to load the configuration file char * pConfig = FileDisk::loadTextFile( configFile ); if ( pConfig != NULL ) { config = pConfig; delete [] pConfig; } send( client, ProcessClient::CLIENT_RECV_CONFIG ) << job << config; } break; case ProcessClient::SERVER_RECV_CONFIG: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; CharString config; input >> config; bool jobDone = false; CharString configFile; if ( processId != 0 ) { Process proc; if ( findProcess( processId, proc ) ) configFile = proc.config; } else configFile = m_Context.config; LOG_STATUS( "ProcessServer", "Recv Config, client %u (%s), configFile = %s", client, clientAddress(client), configFile.cstr() ); // save the new file jobDone = FileDisk::saveTextFile( configFile, CharString( config ) ); send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_SEND_LOG: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; CharString log; CharString logFile; if ( processId != 0 ) { // send log of one of our processes Process proc; if ( findProcess( processId, proc ) ) logFile = proc.log; } else logFile = m_Context.logFile; FileDisk file; if ( file.open( logFile ) ) { dword size = file.size(); if ( size > MAX_LOG_SIZE ) { file.setPosition( size - MAX_LOG_SIZE ); size = MAX_LOG_SIZE; } char * pLog = new char[ size + 1]; pLog[ size ] = 0; file.read( pLog, size ); file.close(); // save to string log = pLog; // release allocated memory delete [] pLog; } else log = "Failed to open log file!"; send( client, ProcessClient::CLIENT_RECV_LOG ) << job << log; } break; case ProcessClient::SERVER_ADD_PROCESS: if ( validateClient( client ) ) { dword job; input >> job; Process proc; input >> proc; AutoLock lock( &m_Lock ); proc.processId = m_NextProcessId++; m_ProcessList.push( proc ); LOG_STATUS( "ProcessServer", "Add Process, client = %u (%s), processId = %u, name = %s, exec = %s", client, clientAddress(client), proc.processId, proc.name.cstr(), proc.executable.cstr() ); saveProcessList(); send( client, ProcessClient::CLIENT_JOB_DONE ) << job << true; } break; case ProcessClient::SERVER_SET_PROCESS: if ( validateClient( client ) ) { dword job; input >> job; Process proc; input >> proc; bool jobDone = false; AutoLock lock( &m_Lock ); int pi = findProcess( proc.processId ); if ( pi >= 0 ) { m_ProcessList[pi] = proc; jobDone = true; LOG_STATUS( "ProcessServer", "Set Process, client = %u (%s), processId = %u, name = %s, exec = %s", client, clientAddress(client), proc.processId, proc.name.cstr(), proc.executable.cstr() ); saveProcessList(); } send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_DEL_PROCESS: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; bool jobDone = false; AutoLock lock( &m_Lock ); int pi = findProcess( processId ); if ( pi >= 0 ) { LOG_STATUS( "ProcessServer", "Delete Process, name = %s, client = %u (%s), processId = %u", m_ProcessList[pi].name.cstr(), client, clientAddress(client), processId ); // stop the actual process if any if ( m_ProcessInfo.find( processId ).valid() ) { ::Process::stop( m_ProcessInfo[ processId ].m_pHandle ); m_ProcessInfo.remove( processId ); } // remove from the list m_ProcessList.remove( pi ); jobDone = true; saveProcessList(); } send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_STOP_PROCESS: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; bool jobDone = false; AutoLock lock( &m_Lock ); int pi = findProcess( processId ); if ( pi >= 0 ) { m_ProcessList[ pi ].flags |= ProcessClient::PF_DISABLED; jobDone = true; LOG_STATUS( "ProcessServer", "Stop Process, name = %s, client = %u (%s), processId = %u", m_ProcessList[pi].name.cstr(), client, clientAddress(client), processId ); saveProcessList(); } send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_START_PROCESS: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; bool jobDone = false; AutoLock lock( &m_Lock ); int pi = findProcess( processId ); if ( pi >= 0 ) { m_ProcessList[ pi ].flags &= ~ProcessClient::PF_DISABLED; jobDone = true; LOG_STATUS( "ProcessServer", "Start Process, name = %s, client = %u (%s), processId = %u", m_ProcessList[pi].name.cstr(), client, clientAddress(client), processId ); saveProcessList(); } send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_RESTART_PROCESS: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; bool jobDone = false; AutoLock lock( &m_Lock ); Process proc; if ( findProcess( processId, proc ) ) { if ( m_ProcessInfo.find( processId ).valid() ) { jobDone = stopProcess( processId ); LOG_STATUS( "ProcessServer", "Restart Process, name = %s, client = %u (%s), processId = %u,", proc.name.cstr(), client, clientAddress(client), processId ); } } send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_SEND_STATUS: if ( validateClient( client ) ) { dword job; input >> job; AutoLock lock( &m_Lock ); ProcessClient::Status status; status.processGroup = m_Context.processGroup; status.networkGroup = m_Context.networkGroup; status.cpuUsage = cpuUsage(); status.memoryUsage = memoryUsage(); status.processCount = 0; for(int i=0;i<m_ProcessList.size();i++) if ( (m_ProcessList[i].flags & ProcessClient::PF_RUNNING) != 0 ) status.processCount++; send( client, ProcessClient::CLIENT_RECV_STATUS ) << job << status; } break; case ProcessClient::SERVER_STOP_ALL: if ( validateClient( client ) ) { dword job; input >> job; bool jobDone = false; AutoLock lock( &m_Lock ); for(int i=0;i<m_ProcessList.size();i++) m_ProcessList[i].flags |= ProcessClient::PF_DISABLED; saveProcessList(); jobDone = true; LOG_STATUS( "ProcessServer", CharString().format("Stop All, client = %u (%s)", client, clientAddress(client)) ); send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_START_ALL: if ( validateClient( client ) ) { dword job; input >> job; bool jobDone = false; AutoLock lock( &m_Lock ); for(int i=0;i<m_ProcessList.size();i++) m_ProcessList[i].flags &= ~ProcessClient::PF_DISABLED; saveProcessList(); jobDone = true; LOG_STATUS( "ProcessServer", CharString().format("Start All, client = %u (%s)", client, clientAddress(client)) ); send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_RESTART_ALL: if ( validateClient( client ) ) { dword job; input >> job; bool jobDone = false; AutoLock lock( &m_Lock ); for(int i=0;i<m_ProcessList.size();i++) stopProcess( m_ProcessList[i].processId ); jobDone = true; LOG_STATUS( "ProcessServer", CharString().format("Restart All, client = %u (%s)", client, clientAddress(client)) ); send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_REBOOT: if ( validateClient( client ) ) { dword job; input >> job; bool jobDone = reboot(); if ( jobDone ) LOG_STATUS( "ProcessServer", CharString().format("Server Rebooting, client = %u (%s)", client, clientAddress(client)) ); send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_EXIT: if ( validateClient( client ) ) { dword job; input >> job; // signal all running processes to stop bool jobDone = shutdown(); if ( jobDone ) LOG_STATUS( "ProcessServer", CharString().format("Server Exiting, client = %u (%s)", client, clientAddress(client)) ); send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_TERMINATE_PROCESS: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; bool jobDone = false; AutoLock lock( &m_Lock ); Process proc; if ( findProcess( processId, proc ) ) { // just terminate the process if ( m_ProcessInfo.find( processId ).valid() ) { ::Process::stop( m_ProcessInfo[ processId ].m_pHandle ); jobDone = true; LOG_STATUS( "ProcessServer", "Terminated Process, name = %s, client = %u (%s), processId = %u,", proc.name.cstr(), client, clientAddress(client), processId ); } } send( client, ProcessClient::CLIENT_JOB_DONE ) << job << jobDone; } break; case ProcessClient::SERVER_OPEN_LOG: if ( validateClient( client ) ) { dword job; input >> job; dword processId; input >> processId; CharString logFile; if ( processId != 0 ) { // send log of one of our processes Process proc; if ( findProcess( processId, proc ) ) logFile = proc.log; } else logFile = m_Context.logFile; dword logId = m_NextLogId++; AutoLock lock( &m_Lock ); FileDisk & file = m_LogFile[ logId ]; if ( file.open( logFile ) ) { LOG_STATUS( "ProcessServer", "Open Log, logFile = %s, logId = %u, clientId = %u", logFile.cstr(), logId, client ); m_ActiveLog.push( logId ); m_LogClient[ logId ] = client; m_ClientLog[ client ].push( logId ); } else { LOG_STATUS( "ProcessServer", "Open Log Failed, logFile = %s, clientId = %u", logFile.cstr(), client ); // failed to open file m_LogFile.remove( logId ); // set id to 0 for error logId = 0; } send( client, ProcessClient::CLIENT_RECV_LOG_ID ) << job << logId; } break; case ProcessClient::SERVER_CLOSE_LOG: if ( validateClient( client ) ) { dword logId; input >> logId; AutoLock lock( &m_Lock ); LOG_STATUS( "ProcessServer", CharString().format("Close Log, logId = %u, client = %u", logId, client) ); m_ActiveLog.removeSearch( logId ); m_LogFile.remove( logId ); m_LogClient.remove( logId ); m_ClientLog[ client ].removeSearch( logId ); } break; case ProcessClient::SERVER_SEARCH_LOGS: if ( validateClient( client ) ) { dword job; input >> job; ProcessClient::SearchLogRequest req; input >> req; LOG_STATUS( "ProcessServer", CharString().format("Search Log, clientId = %u", client) ); CharString result; if( req.filemask.find('/') >= 0 || req.filemask.find('\\') >= 0 ) { // this should never happen, unless the user has a hacked client LOG_STATUS( "ProcessServer", CharString().format("Search Log, invalid filemask received from clientId = %u", client) ); result = "Failed"; } else { result = searchLogFiles( req.filemask, req.searchString, req.isRegExp, req.searchLevel, req.resolveClientId ); } send( client, ProcessClient::CLIENT_RECV_SEARCHRESULT ) << job << result; } break; case ProcessClient::PING: send( client, ProcessClient::PONG ); break; case ProcessClient::PONG: break; default: { LOG_ERROR( "ProcessServer", CharString().format("Bad Message, client = %u (%s), message = %d", client, clientAddress(client), message) ); removeClient( client ); } break; } }