You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
9.9 KiB

​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
2 years ago
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
44 minutes ago
2 years ago
​
​
​
​
​
2 years ago
2 years ago
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
44 minutes ago
​
2 years ago
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
44 minutes ago
​
2 years ago
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
44 minutes ago
2 years ago
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
2 years ago
​
​
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
44 minutes ago
2 years ago
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
44 minutes ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaKernel.h"
  2. #include <sys/types.h>
  3. #include <sys/sysinfo.h>
  4. #include <sys/statvfs.h>
  5. #define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
  6. #ifdef DEBUG
  7. #define LOG_ERROR( X ) {\
  8. char _tBfrErrMSG234[256];\
  9. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  10. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  11. }
  12. #else
  13. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  14. #endif
  15. bool servaKernel::cleanUpMess(){
  16. LOG("Cleanup");
  17. //Get Not transfered Items
  18. DBC_UINT64_LIST *tList = m_pDBC->listMediaQueueItems( 0 );
  19. if ( tList ) {
  20. LOG("1");
  21. for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  22. LOG("*");
  23. DBC_RPC_MEDIA_INFO_EX *tData = m_pDBC->getRemoteMediaInfoEx( *idx );
  24. if ( tData ) {
  25. LOG("2");
  26. CSTR tRB = m_pSM->getReportDir( m_pDBC->getSessionTrackId( tData->session_id ), tData->ts );
  27. if ( tRB ) {
  28. LOG("3");
  29. char tName[MAX_PATH];
  30. sprintf( tName, "%s/%s.%s", tRB, tData->uuid, tData->fmt );
  31. LOG( tName );
  32. free( tRB );
  33. unlink( tName );
  34. };
  35. LOG("f");
  36. free( tData );
  37. };
  38. m_pDBC->removeRemoteMediaInfo( *idx );
  39. };
  40. LOG("-");
  41. tList->clear();
  42. delete( tList );
  43. };
  44. LOG("LVL2");
  45. //Get Not Converted Items
  46. tList = m_pDBC->listMediaQueueItems( 2 );
  47. if ( tList ) {
  48. LOG("1");
  49. for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) {
  50. LOG("*");
  51. DBC_RPC_MEDIA_INFO_EX *tData = m_pDBC->getRemoteMediaInfoEx( *idx );
  52. if ( tData ) {
  53. LOG("2");
  54. CSTR tRB = m_pSM->getReportDir( m_pDBC->getSessionTrackId( tData->session_id ), tData->ts );
  55. LOG("2a");
  56. if ( tRB ) {
  57. LOG("3");
  58. char tName[MAX_PATH];
  59. sprintf( tName, "%s/%s.%s", tRB, tData->uuid, "mp4" );
  60. free( tRB );
  61. LOG( tName );
  62. unlink( tName );
  63. };
  64. LOG("f");
  65. free( tData );
  66. };
  67. m_pDBC->updateEncodeStatus( *idx, 1, 0 );
  68. };
  69. tList->clear();
  70. delete( tList );
  71. };
  72. LOG("Close all Staled Sessions");
  73. LOG("Done");
  74. return true;
  75. };
  76. bool servaKernel::registerServA(){
  77. DBC_SERVA_CONFIG *tData = m_pDBC->getServaConfig();
  78. if ( tData ) {
  79. memcpy(&m_sServaConfig, tData, sizeof(DBC_SERVA_CONFIG));
  80. free( tData ); // was leaked
  81. } else {
  82. memset(&m_sServaConfig, 0, sizeof(DBC_SERVA_CONFIG));
  83. m_sServaConfig.encoderLimit = 1;
  84. };
  85. m_pDBC->setServerStatValue( SERVER_VERSION, _BUILD_STRING_ );
  86. m_pDBC->setSystemProp("version_string", _BUILD_STRING_ );
  87. #ifndef NO_EXEC_EXTERNALS
  88. char OS_INFO[128]; // was 120 while fgets() wrote up to 127 bytes and [127] was set: stack overflow
  89. OS_INFO[0] = (char)0;
  90. FILE *fd = popen("/bin/uname -s -r -v -m","r");
  91. if( fd!= NULL ) {
  92. if ( fgets( OS_INFO, sizeof( OS_INFO ), fd ) == NULL ) OS_INFO[0] = (char)0;
  93. OS_INFO[sizeof( OS_INFO ) - 1] = (char)0;
  94. trim( OS_INFO );
  95. pclose( fd ); // popen() streams must be closed with pclose() (fclose leaves a zombie process)
  96. fd = NULL;
  97. m_pDBC->setServerStatValue( SERVER_OS, OS_INFO );
  98. m_pDBC->setSystemProp("os_info", (CSTR) OS_INFO );
  99. } else {
  100. m_pDBC->setServerStatValue( SERVER_OS, "Linux" );
  101. m_pDBC->setSystemProp("os_info", "Linux" );
  102. };
  103. #else
  104. FILE *fd = NULL;
  105. m_pDBC->setServerStatValue( SERVER_OS, "Linux" );
  106. m_pDBC->setSystemProp("os_info", "Linux" );
  107. #endif
  108. char HW_INFO[128];
  109. fd = fopen("/proc/cpuinfo","r");
  110. if ( fd!= NULL ) {
  111. char tBB[1024];
  112. bool bARM = false;
  113. while( !feof( fd ) ) {
  114. fgets( tBB, 1024, fd );
  115. trim( tBB );
  116. if( strcasestr(tBB, "model") != NULL ) {
  117. if( strcasestr(tBB, "model name") != NULL ) {
  118. if ( !bARM ) {
  119. if( strcasestr( tBB, "ARM" ) != NULL){
  120. bARM = true;
  121. } else {
  122. CArray *tA = explode(':', tBB );
  123. if ( tA ) {
  124. strncpy( HW_INFO, tA->at(1), 127 );
  125. delete tA;
  126. };
  127. break;
  128. };
  129. };
  130. } else {
  131. if( bARM ) {
  132. CArray *tA = explode(':', tBB);
  133. if ( tA ) {
  134. strncpy( HW_INFO, tA->at(1), 127 );
  135. delete tA;
  136. };
  137. break;
  138. };
  139. };
  140. };
  141. };
  142. HW_INFO[127] = (char)0;
  143. trim( HW_INFO );
  144. fclose( fd );
  145. fd = NULL;
  146. };
  147. m_pDBC->setSystemProp("hw_info", (CSTR) HW_INFO );
  148. m_pDBC->setServerStatValue( SERVER_HW, (CSTR) HW_INFO );
  149. m_pDBC->setServerStatValue( SERVER_START_TS, (uint32_t) time(NULL) );
  150. return true;
  151. };
  152. bool servaKernel::updateStatus(){
  153. struct sysinfo SI;
  154. struct statvfs DI;
  155. DBC_SYS_LOAD_INFO info = { 0 };
  156. info.CPU_CORES = get_nprocs();
  157. info.C_TS = (uint32_t) time(NULL);
  158. if( sysinfo( &SI ) == 0 ){
  159. float f_load = 100.f / (float)( info.CPU_CORES * ( 1 << SI_LOAD_SHIFT ) );
  160. info.cpu1 = SI.loads[0] * f_load;
  161. info.cpu5 = SI.loads[1] * f_load;
  162. info.cpu15 = SI.loads[2] * f_load;
  163. info.T_MEM = SI.totalram;
  164. info.C_MEM = SI.totalram - SI.freeram;
  165. info.mem = 100.0f * (float)info.C_MEM / (float)info.T_MEM;
  166. } else {
  167. LOG_ERROR("Error while getting System Info!");
  168. };
  169. //const uint32_t MB = 1 << 10;//(1024 * 1024);// * 1024;
  170. if( statvfs("/", &DI) == 0 ){
  171. info.T_DISK = ( DI.f_blocks * (unsigned long long) DI.f_frsize ) >> 10;// / MB;
  172. //cDISK = ( DI.f_bavail * (unsigned long long) DI.f_bsize ) >> 10;// / MB;
  173. info.C_DISK = info.T_DISK - ( ( DI.f_bfree * (unsigned long long) DI.f_bsize ) >> 10 );
  174. info.disk = 100.0f * (float)info.C_DISK / (float)info.T_DISK;
  175. } else {
  176. LOG_ERROR("Error while getting Disk Info!");
  177. };
  178. //Check if need to erase some reports
  179. if ( m_sServaConfig.historyAutoDeleteLimit ) {
  180. #ifdef DEBUG_EXTRA
  181. LOG("History Auto Delete is set!");
  182. #endif
  183. if (m_sServaConfig.historyPreserveFreeSpacePerc || m_sServaConfig.historyPreserveFreeSpaceMB) {
  184. #ifdef DEBUG_EXTRA
  185. LOG("History Size is set!");
  186. #endif
  187. uint64_t tid = 0;
  188. if ( m_sServaConfig.historyPreserveFreeSpacePerc > ( 100.0f - info.disk ) ) {
  189. tid = m_pDBC->getSessionOldest( m_sServaConfig.historyAutoDeleteLimit );
  190. } else if ( m_sServaConfig.historyPreserveFreeSpaceMB > ( info.T_DISK - info.C_DISK ) ) {
  191. tid = m_pDBC->getSessionOldest( m_sServaConfig.historyAutoDeleteLimit );
  192. };
  193. if ( tid ) {
  194. #ifdef DEBUG_EXTRA
  195. LOG("Need to Delete some Sessions!");
  196. #endif
  197. m_pSM->eraseSession( tid );
  198. #ifdef DEBUG_EXTRA
  199. LOG("Delete Session Done!");
  200. #endif
  201. };
  202. #ifdef DEBUG_EXTRA
  203. } else {
  204. LOG("No History Size is set!");
  205. #endif
  206. };
  207. if ( m_sServaConfig.historyDaysLimit ) {
  208. #ifdef DEBUG_EXTRA
  209. LOG("History Time is set!");
  210. #endif
  211. uint32_t cts = time( NULL ) - (m_sServaConfig.historyDaysLimit * 86400);
  212. uint64_t tid = m_pDBC->getSessionOlderThan( cts, m_sServaConfig.historyAutoDeleteLimit );
  213. if ( tid ) {
  214. #ifdef DEBUG_EXTRA
  215. LOG("Need to Delete some Sessions!");
  216. #endif
  217. m_pSM->eraseSession( tid );
  218. #ifdef DEBUG_EXTRA
  219. LOG("Delete Session Done!");
  220. #endif
  221. };
  222. #ifdef DEBUG_EXTRA
  223. } else {
  224. LOG("No History Time Limit is set!");
  225. #endif
  226. };
  227. #ifdef DEBUG_EXTRA
  228. } else {
  229. LOG("No History Auto Delete!"); // every second: ~86400 log lines (and DB log rows) a day in DEBUG builds
  230. #endif
  231. };
  232. //Temperature reading
  233. info.temperature = -1;
  234. if( fileExist( "/sys/class/thermal" ) ) {
  235. char tB[128];
  236. // Check first 8 zones to find CPU one
  237. for ( uint8_t i = 0; i < 8; i++ ) {
  238. sprintf( tB, "/sys/class/thermal/thermal_zone%d", i );
  239. if ( fileExist( tB ) ) {
  240. sprintf( tB, "/sys/class/thermal/thermal_zone%d/type", i );
  241. if ( fileExist( tB ) ) {
  242. FILE *fd = fopen( tB, "r" );
  243. readLine( fd, tB, 128 );
  244. fclose(fd);
  245. if ( strcasestr(tB,"cpu")!=NULL || strcasestr(tB,"x86_pkg")!=NULL || strcasestr(tB,"acpitz")!=NULL ) {
  246. sprintf( tB, "/sys/class/thermal/thermal_zone%d/temp", i );
  247. FILE *fd = fopen( tB, "r" );
  248. readLine( fd, tB, 128 );
  249. fclose(fd);
  250. info.temperature = ((float) atoll( tB )) / 1000.0f;
  251. break;
  252. };
  253. } else continue;
  254. } else break;
  255. };
  256. };
  257. return m_pDBC->updateServerUtilisation( &info );
  258. };
  259. #undef LOG
  260. #undef LOG_ERROR