From 69acb356f9e96246b21a91d19d90366aecc2820b Mon Sep 17 00:00:00 2001 From: Evgeny Gagua Date: Tue, 3 Sep 2024 17:56:34 +0300 Subject: [PATCH] Unicness to a Mojo process added --- Projects/watchDogDaemon/kernel.cpp | 9 +++- lib/App/mojo/mojoBackend.cpp | 18 +++++-- lib/App/mojo/mojoKernel.cpp | 23 ++++----- lib/components/base/basicFunctionality.cpp | 56 ++++++++++++++++++++++ lib/components/base/basicFunctionality.h | 3 +- 5 files changed, 90 insertions(+), 19 deletions(-) diff --git a/Projects/watchDogDaemon/kernel.cpp b/Projects/watchDogDaemon/kernel.cpp index 51b584de..b209a5ce 100644 --- a/Projects/watchDogDaemon/kernel.cpp +++ b/Projects/watchDogDaemon/kernel.cpp @@ -239,7 +239,14 @@ void *SP_KERNEL::sw_proc( void *ptr ){ //sprintf( jB, "Server: I recieved [%d] %s from client!", retVal, tB ); //mR->master->m_pLog->Log( "K_T", mR->master, jB ); if( CPID ){ - uint64_t nMS = atoll(tB); + int64_t nMS = atoll(tB); + //Stop WD and exit + if ( nMS < 0 ) { + shutdown( mR->client_socket, 2 ); + close( mR->client_socket ); + mR->client_socket = -1; + break; + }; //sprintf( jB, "Will Wait next [%d] milliseconds", nMS ); //mR->master->m_pLog->Log( "K_T", mR->master, jB ); tMS = cMS + nMS; diff --git a/lib/App/mojo/mojoBackend.cpp b/lib/App/mojo/mojoBackend.cpp index a07a526f..33852003 100644 --- a/lib/App/mojo/mojoBackend.cpp +++ b/lib/App/mojo/mojoBackend.cpp @@ -158,10 +158,20 @@ int main( int argc, char *argv[] ){ #endif ; - if( !isProcessUniq( argv[0] ) ){ - printf( "The Process [%s] Already is Runing\n", argv[0] ); - return 33; - }; + //if( !isProcessUniq( argv[0] ) ){ + if( fileExist( gsCFG_PID_FILE ) ){ + FILE * fd = fopen( gsCFG_PID_FILE, "r" ); + if ( fd ) { + char Kaka[64]; + readLine( fd, Kaka, 64 ); + pid_t tPID = ( pid_t ) atol( Kaka ); + if ( kill( tPID, 0 ) == 0 ) { + printf("Can not Start!\nThe Process [%s] Already is Runing[PID:%lld]\n", argv[0], tPID ); + return 33; + }; + }; + }; + //}; // Reading Config if( argc > 1 ){ diff --git a/lib/App/mojo/mojoKernel.cpp b/lib/App/mojo/mojoKernel.cpp index edaac002..88260005 100644 --- a/lib/App/mojo/mojoKernel.cpp +++ b/lib/App/mojo/mojoKernel.cpp @@ -115,6 +115,16 @@ mojoKernel::~mojoKernel(){ // LOG( "BopBop!" ); // SLEEP(1000); //}; +#ifdef LINUX_KERNEL_WATCHDOG + LOG("Disable WD!"); + if( m_iWatchdogFd > -1 ) { + char tB[64]; + sprintf( tB, "-1\n" ); + //ToDo: Make Error Handling here! + write( m_iWatchdogFd, &tB, strlen( tB ) ); + }; + LOG("WatchDog turned Off!"); +#endif _instance = NULL; }; @@ -327,22 +337,9 @@ void mojoKernel::m_fThreadProc( void ){ #ifdef LINUX_KERNEL_WATCHDOG if( m_iWatchdogFd > -1 ) { char tB[64]; - char ttB[128]; sprintf( tB, "2000\n" ); - //int ret = xioctl( m_iWatchdogFd, WDIOC_KEEPALIVE, NULL ); - //if( ret < 0 ){ - // char tB[200]; - // sprintf( tB, "Could not pat watchdog: %s\n", strerror(errno)); - // LOG_ERROR( tB ); - //}; - //LOG("Write WD!"); //ToDo: Make Error Handling here! write( m_iWatchdogFd, &tB, strlen( tB ) ); - //LOG("Read WD!"); - //read( m_iWatchdogFd, &tB, 64 ); - //LOG("Done WD!"); - //sprintf( ttB, "Client: I recieved %s from server!", tB ); - //LOG( ttB ); }; #endif m_bDebugMode = m_pDBC->getAppDebugMode(); diff --git a/lib/components/base/basicFunctionality.cpp b/lib/components/base/basicFunctionality.cpp index 2ab47225..10d100e0 100644 --- a/lib/components/base/basicFunctionality.cpp +++ b/lib/components/base/basicFunctionality.cpp @@ -933,12 +933,68 @@ ssize_t readLine(int fd, char *buffer, size_t n) { return totRead; }; +ssize_t readLine( FILE *fd, char *buffer, size_t n) { + ssize_t numRead; /* # of bytes fetched by last read() */ + size_t totRead; /* Total bytes read so far */ + char *buf; + char ch; + + if (n <= 0 || buffer == NULL) { + errno = EINVAL; + return -1; + } + + buf = buffer; /* No pointer arithmetic on "void *" */ + + totRead = 0; + for (;;) { + numRead = fread(&ch, 1, 1, fd); + + if (numRead == -1) { + if (errno == EINTR) /* Interrupted --> restart read() */ + continue; + else + return -1; /* Some other error */ + + } else if (numRead == 0) { /* EOF */ + if (totRead == 0) /* No bytes read; return 0 */ + return 0; + else /* Some bytes read; add '\0' */ + break; + + } else { /* 'numRead' must be 1 if we get here */ + if (totRead < n - 1) { /* Discard > (n - 1) bytes */ + totRead++; + *buf++ = ch; + } + + if (ch == '\n') + break; + } + } + + *buf = '\0'; + return totRead; +}; + localIpAddresses g_vLocalIpAdressess; bool g_bAdressesAreDiscovered = false; #ifdef __linux__ bool isProcessUniq( CSTR pName ){ + char tB[256]; + FILE *fd; + sprintf( tB, "pidof %s", pName ); + fd = popen( tB, "r" ); + if ( fd ) { + int rs = readLine( fd, tB, 256 ); + if ( rs > 2 ) { + fclose( fd ); + return false; + }; + fclose( fd ); + }; return true; }; diff --git a/lib/components/base/basicFunctionality.h b/lib/components/base/basicFunctionality.h index 571a3158..8083ad56 100644 --- a/lib/components/base/basicFunctionality.h +++ b/lib/components/base/basicFunctionality.h @@ -69,7 +69,8 @@ CSTR strReplace( CSTR orig, CSTR rep, CSTR with ); int checkCmdExists( CSTR cmd ); -ssize_t readLine(int fd, char *buffer, size_t n ); +ssize_t readLine( int fd, char *buffer, size_t n ); +ssize_t readLine( FILE *fd, char *buffer, size_t n); //#define EPOCH_DIFF 11644473600LL #define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000