Changeset 8223547 in nscp


Ignore:
Timestamp:
03/01/05 22:42:27 (8 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
2a94f3f
Parents:
36c340d
Message:
  • Improved thread safety in Core + Added exception handling and logging in PDH code
  • Various fixes all over the place + New module for check size of files/directories
  • Changed build to Dynamic link
  • Added regular expression support for EventLog? checker
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • NSClient++.cpp

    r36c340d r8223547  
    115115    LOG_DEBUG("Plugins unloaded..."); 
    116116  } catch(NSPluginException *e) { 
    117     LOG_ERROR_STD("Exception raised: " + e->error_ + " in module: " + e->file_); 
     117    std::cout << "Exception raised: " << e->error_ << " in module: " << e->file_ << std::endl;; 
    118118  } 
    119119  Settings::destroyInstance(); 
     
    159159 */ 
    160160void NSClientT::unloadPlugins() { 
    161   MutexLock lock(pluginMutex); 
     161  MutexLock lock(pluginMutex,20000); 
    162162  if (!lock.hasMutex()) { 
    163163    LOG_ERROR("FATAL ERROR: Could not get mutex."); 
     
    169169    std::cout << "Unloading plugin: " << (*it)->getName() << "..."; 
    170170#endif 
    171     (*it)->unload(); 
     171      (*it)->unload(); 
    172172#ifdef _DEBUG 
    173173    std::cout << "OK" << std::endl; 
    174174#endif 
    175175  } 
    176   messageHandlers_.clear(); 
     176  { 
     177    MutexLock lock2(messageMutex,20000); 
     178    if (!lock2.hasMutex()) { 
     179      LOG_ERROR("FATAL ERROR: Could not get mutex (we will now crash BTW)."); 
     180    } else { 
     181      messageHandlers_.clear(); 
     182    } 
     183  } 
    177184  commandHandlers_.clear(); 
    178185  for (it = plugins_.rbegin(); it != plugins_.rend(); ++it) { 
     
    215222std::string NSClientT::inject(const std::string buffer) { 
    216223  std::list<std::string> args = charEx::split(buffer.c_str(), '&'); 
     224  if (args.empty()) 
     225    return ""; 
    217226  std::string command = args.front(); args.pop_front(); 
    218227  LOG_MESSAGE_STD("Injecting: " + command); 
     
    244253  MutexLock lock(pluginMutex); 
    245254  if (!lock.hasMutex()) { 
    246     LOG_ERROR("FATAL ERROR: Could not get mutex."); 
    247     return "FATAL ERROR"; 
     255    LOG_ERROR("FATAL ERROR: Could not execute command with a reasonable timeframe. (could not get mutex so core is most likely locked down)."); 
     256    return "ERROR: Core was locked down"; 
    248257  } 
    249258  static unsigned int bufferSize = 0; 
     
    252261 
    253262  if (password != getPassword()) 
    254     return "INVALID PASSWORD"; 
    255  
    256   // Pack the argument as a char** buffer 
     263    return "ERROR: Authorization denied."; 
     264 
    257265  std::string ret; 
    258   int len = args.size(); 
    259   char **arguments = new char*[len]; 
    260   std::list<std::string>::iterator it = args.begin(); 
    261   for (int i=0;it!=args.end();++it,i++) { 
    262     int alen = (*it).size(); 
    263     arguments[i] = new char[alen+2]; 
    264     strncpy(arguments[i], (*it).c_str(), alen+1); 
    265   } 
     266  unsigned int len; 
     267  char **arguments = NSCHelper::list2arrayBuffer(args, len); 
    266268  // Allocate return buffer 
    267269  char* returnbuffer = new char[bufferSize+1]; 
     
    286288  // delete buffers 
    287289  delete [] returnbuffer; 
    288   for (int i=0;i<len;i++) { 
    289     delete [] arguments[i]; 
    290   } 
    291   delete [] arguments; 
     290  NSCHelper::destroyArrayBuffer(arguments, len); 
    292291  return ret; 
    293292} 
     
    303302  MutexLock lock(messageMutex); 
    304303  if (!lock.hasMutex()) { 
    305     LOG_ERROR("FATAL ERROR: Could not get mutex."); 
     304    std::cout << "Message was lost as the core was locked..." << std::endl; 
     305    std::cout << message << std::endl; 
    306306    return; 
    307307  } 
     
    324324    } catch(const NSPluginException& e) { 
    325325      // Here we are pretty much fucked! (as logging this might cause a loop :) 
     326      std::cout << "Caught: " << e.error_ << " when trying to log a message..." << std::endl; 
    326327      std::cout << "This is *really really* bad, now the world is about to end..." << std::endl; 
    327328    } 
  • NSClient++.vcproj

    r36c340d r8223547  
    457457      Filter=""> 
    458458      <File 
     459        RelativePath=".\changelog"> 
     460      </File> 
     461      <File 
    459462        RelativePath=".\Doxyfile"> 
    460463        <FileConfiguration 
  • changelog

    ra0528c4 r8223547  
     12005-03-01 MickeM 
     2 * Improved thread safety in Core 
     3 + Added exception handling and logging in PDH code 
     4 * Various fixes all over the place 
     5 + New module for check size of files/directories 
     6 * Changed build to Dynamic link 
     7 * Added regular expression support for EventLog checker 
     8 
     92005-02-22 MickeM 
     10 + Added documentation 
     11 
     122005-02-17 MickeM 
     13 * NSClient issues fixed 
     14 * Systray issues fixed 
     15 
    1162005-02-14 MickeM 
    2  * Initial sourceforge relase 
     17 * Initial sourceforge release 
  • config.h

    r167a5dd r8223547  
    77 
    88// Version 
    9 #define SZVERSION "0.1.0 2005-01-23" 
     9#define SZVERSION "0.0.1 RC4 2005-03-01" 
    1010 
    1111// internal name of the service 
     
    1818#define SZDEPENDENCIES       "" 
    1919 
    20  
    21 // PDH Check interval (Check every x second) 
    22 //#define CHECK_INTERVAL    1 
    23  
    24 // PDH CPU check backlog (x minutes) 
    25 //#define BACK_INTERVAL   2000   
    26  
    2720// Buffer size of incoming data (noteice this is the maximum request length!) 
    2821#define RECV_BUFFER_LEN   1024 
    2922 
    30  
    31  
    32 #define DEFAULT_TCP_PORT 1234 
     23#define DEFAULT_TCP_PORT 12489 
  • include/Mutex.h

    ra0528c4 r8223547  
    9999   * @param hMutex The mutex to use 
    100100   */ 
    101   MutexLock(HANDLE hMutex) : bHasMutex(false), hMutex_(hMutex) { 
     101  MutexLock(HANDLE hMutex, DWORD timeout = 5000L) : bHasMutex(false), hMutex_(hMutex) { 
    102102    assert(hMutex_ != NULL); 
    103     dwWaitResult = WaitForSingleObject(hMutex_, 5000L); 
     103    dwWaitResult = WaitForSingleObject(hMutex_, timeout); 
    104104    switch (dwWaitResult) { 
    105105      // The thread got mutex ownership. 
  • include/NSCHelper.cpp

    r36c340d r8223547  
    11#include "stdafx.h" 
    22#include <NSCHelper.h> 
     3#include <assert.h> 
    34 
    45#define BUFF_LEN 4096 
     
    1516 */ 
    1617int NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode /* = NSCAPI::success */) { 
    17   if (str.length() > bufLen) 
     18  if (str.length() >= bufLen) 
    1819    return NSCAPI::invalidBufferLen; 
    1920  strncpy(buffer, str.c_str(), bufLen); 
     
    2627 * @return Argument wrapped as a list 
    2728 */ 
    28 std::list<std::string> NSCHelper::makelist(const unsigned int argLen, char *argument[]) { 
     29std::list<std::string> NSCHelper::arrayBuffer2list(const unsigned int argLen, char *argument[]) { 
    2930  std::list<std::string> ret; 
    3031  int i=0; 
     
    3536  return ret; 
    3637} 
     38/** 
     39 * Create an arrayBuffer from a list. 
     40 * This is the reverse of arrayBuffer2list. 
     41 * <b>Notice</b> it is up to the caller to free the memory allocated in the returned buffer. 
     42 * 
     43 * @param lst A list to convert. 
     44 * @param &argLen Write the length to this argument. 
     45 * @return A pointer that is managed by the caller. 
     46 */ 
     47char ** NSCHelper::list2arrayBuffer(const std::list<std::string> lst, unsigned int &argLen) { 
     48  std::string ret; 
     49  argLen = static_cast<unsigned int>(lst.size()); 
     50  char **arrayBuffer = new char*[argLen]; 
     51  std::list<std::string>::const_iterator it = lst.begin(); 
     52  for (int i=0;it!=lst.end();++it,i++) { 
     53    std::string::size_type alen = (*it).size(); 
     54    arrayBuffer[i] = new char[alen+2]; 
     55    strncpy(arrayBuffer[i], (*it).c_str(), alen+1); 
     56  } 
     57  assert(i == argLen); 
     58  return arrayBuffer; 
     59} 
     60/** 
     61 * Destroy an arrayBuffer. 
     62 * The buffer should have been created with list2arrayBuffer. 
     63 * 
     64 * @param **argument  
     65 * @param argLen  
     66 */ 
     67void NSCHelper::destroyArrayBuffer(char **argument, const unsigned int argLen) { 
     68  for (unsigned int i=0;i<argLen;i++) { 
     69    delete [] argument[i]; 
     70  } 
     71  delete [] argument; 
     72} 
     73 
     74 
    3775/** 
    3876 * Translate a message type into a human readable string. 
     
    91129 * @param line Line where message was generated (__LINE__) 
    92130 * @param message Message in human readable format 
     131 * @throws NSCMHExcpetion When core pointer set is unavailable. 
    93132 */ 
    94133void NSCModuleHelper::Message(int msgType, std::string file, int line, std::string message) { 
    95134  if (!fNSAPIMessage) 
    96     throw "NSCore has not been initiated..."; 
     135    throw NSCMHExcpetion("NSCore has not been initiated..."); 
    97136  return fNSAPIMessage(msgType, file.c_str(), line, message.c_str()); 
    98137} 
     
    101140 * @param command Command to inject (password should not be included. 
    102141 * @return The result (if any) of the command. 
     142 * @throws NSCMHExcpetion When core pointer set is unavailable or an unknown inject error occures. 
    103143 */ 
    104144std::string NSCModuleHelper::InjectCommand(std::string command) { 
    105145  if (!fNSAPIInject) 
    106     return "NSCore has not been initiated..."; 
    107   char *buffer = new char[BUFF_LEN+1]; 
    108   if (fNSAPIInject(command.c_str(), buffer, BUFF_LEN) != NSCAPI::success) 
    109     throw "Application name could not be retrieved"; 
    110   std::string ret = buffer; 
     146    throw NSCMHExcpetion("NSCore has not been initiated..."); 
     147  char *buffer = new char[BUFF_LEN+1]; 
     148  std::string ret; 
     149  int err; 
     150  if ((err = fNSAPIInject(command.c_str(), buffer, BUFF_LEN)) != NSCAPI::success) { 
     151    if (err == NSCAPI::invalidBufferLen) 
     152      NSC_LOG_ERROR("Inject command resulted in an invalid buffer size."); 
     153    else 
     154      throw NSCMHExcpetion("Unknown inject error."); 
     155  } else { 
     156    ret = buffer; 
     157  } 
    111158  delete [] buffer; 
    112159  return ret; 
     
    128175 * @param defaultValue A default value (if no value is set in the settings file) 
    129176 * @return the current value or defaultValue if no value is set. 
     177 * @throws NSCMHExcpetion When core pointer set is unavailable or an error occurs. 
    130178 */ 
    131179std::string NSCModuleHelper::getSettingsString(std::string section, std::string key, std::string defaultValue) { 
    132180  if (!fNSAPIGetSettingsString) 
    133     throw "NSCore has not been initiated..."; 
    134   char *buffer = new char[BUFF_LEN+1]; 
    135   int x = fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, BUFF_LEN); 
     181    throw NSCMHExcpetion("NSCore has not been initiated..."); 
     182  char *buffer = new char[BUFF_LEN+1]; 
     183  if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, BUFF_LEN) != NSCAPI::success) { 
     184    delete [] buffer; 
     185    throw NSCMHExcpetion("Settings could not be retrieved."); 
     186  } 
    136187  std::string ret = buffer; 
    137188  delete [] buffer; 
     
    146197 * @param defaultValue A default value (if no value is set in the settings file) 
    147198 * @return the current value or defaultValue if no value is set. 
     199 * @throws NSCMHExcpetion When core pointer set is unavailable. 
    148200 */ 
    149201int NSCModuleHelper::getSettingsInt(std::string section, std::string key, int defaultValue) { 
    150202  if (!fNSAPIGetSettingsInt) 
    151     throw "NSCore has not been initiated..."; 
     203    throw NSCMHExcpetion("NSCore has not been initiated..."); 
    152204  return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue); 
    153205} 
    154206/** 
    155 * Retrieve the application name (in human readable format) from the core. 
    156 * @return A string representing the application name. 
    157 */ 
     207 * Retrieve the application name (in human readable format) from the core. 
     208 * @return A string representing the application name. 
     209 * @throws NSCMHExcpetion When core pointer set is unavailable or an unexpected error occurs. 
     210 */ 
    158211std::string NSCModuleHelper::getApplicationName() { 
    159212  if (!fNSAPIGetApplicationName) 
    160     return "NSCore has not been initiated..."; 
    161   char *buffer = new char[BUFF_LEN+1]; 
    162   if (fNSAPIGetApplicationName(buffer, BUFF_LEN) != NSCAPI::success) 
    163     throw "Application name could not be retrieved"; 
     213    throw NSCMHExcpetion("NSCore has not been initiated..."); 
     214  char *buffer = new char[BUFF_LEN+1]; 
     215  if (fNSAPIGetApplicationName(buffer, BUFF_LEN) != NSCAPI::success) { 
     216    delete [] buffer; 
     217    throw NSCMHExcpetion("Application name could not be retrieved"); 
     218  } 
    164219  std::string ret = buffer; 
    165220  delete [] buffer; 
     
    169224 * Retrieve the directory root of the application from the core. 
    170225 * @return A string representing the base path. 
     226 * @throws NSCMHExcpetion When core pointer set is unavailable or an unexpected error occurs. 
    171227 */ 
    172228std::string NSCModuleHelper::getBasePath() { 
    173229  if (!fNSAPIGetBasePath) 
    174     return "NSCore has not been initiated..."; 
    175   char *buffer = new char[BUFF_LEN+1]; 
    176   if (fNSAPIGetBasePath(buffer, BUFF_LEN) != NSCAPI::success) 
    177     throw "Base path could not be retrieved"; 
     230    throw NSCMHExcpetion("NSCore has not been initiated..."); 
     231  char *buffer = new char[BUFF_LEN+1]; 
     232  if (fNSAPIGetBasePath(buffer, BUFF_LEN) != NSCAPI::success) { 
     233    delete [] buffer; 
     234    throw NSCMHExcpetion("Base path could not be retrieved"); 
     235  } 
    178236  std::string ret = buffer; 
    179237  delete [] buffer; 
     
    183241 * Retrieve the application version as a string (in human readable format) from the core. 
    184242 * @return A string representing the application version. 
     243 * @throws NSCMHExcpetion When core pointer set is unavailable. 
    185244 */ 
    186245std::string NSCModuleHelper::getApplicationVersionString() { 
    187246  if (!fNSAPIGetApplicationVersionStr) 
    188     return "NSCore has not been initiated..."; 
     247    throw NSCMHExcpetion("NSCore has not been initiated..."); 
    189248  char *buffer = new char[BUFF_LEN+1]; 
    190249  int x = fNSAPIGetApplicationVersionStr(buffer, BUFF_LEN); 
  • include/NSCHelper.h

    r36c340d r8223547  
    88{ 
    99  int wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode = NSCAPI::success); 
    10   std::list<std::string> makelist(const unsigned int argLen, char **argument); 
     10 
     11  std::list<std::string> arrayBuffer2list(const unsigned int argLen, char **argument); 
     12  char ** list2arrayBuffer(const std::list<std::string> lst, unsigned int &argLen); 
     13  void destroyArrayBuffer(char **argument, const unsigned int argLen); 
     14 
    1115  std::string translateMessageType(NSCAPI::messageTypes msgType); 
    1216  std::string translateReturn(NSCAPI::returnCodes returnCode); 
     
    6670namespace NSCModuleHelper 
    6771{ 
     72  class NSCMHExcpetion { 
     73  public: 
     74    std::string msg_; 
     75    NSCMHExcpetion(std::string msg) : msg_(msg) {} 
     76  }; 
    6877  // Types for the Callbacks into the main program 
    6978  typedef int (*lpNSAPIGetBasePath)(char*,unsigned int); 
  • modules/CheckDisk/CheckDisk.cpp

    r36c340d r8223547  
    111111  std::string ret; 
    112112  NSCAPI::returnCodes returnCode = NSCAPI::returnOK; 
    113   std::list<std::string> args = NSCHelper::makelist(argLen, char_args); 
     113  std::list<std::string> args = NSCHelper::arrayBuffer2list(argLen, char_args); 
    114114  if (args.empty()) 
    115115    return "Missing argument(s)."; 
  • modules/CheckEventLog/CheckEventLog.cpp

    r36c340d r8223547  
    225225    try { 
    226226      std::string s = p.second; 
    227       NSC_DEBUG_MSG_STD("Attempting to make regexp from: " + s); 
    228227      item.eventSourceRegExp_ = s; 
    229       NSC_DEBUG_MSG_STD("success..."); 
    230228    } catch (const boost::bad_expression e) { 
    231229      item.eventSourceRegExp_ = ""; 
     
    250248  } else 
    251249    throw (std::string)"Invalid argument: " + p.first; 
     250   
    252251} 
    253252void addToQueryBundle(searchQuery::searchQueryBundle &bundle, std::string arg) { 
     
    301300    return ""; 
    302301  NSCAPI::returnCodes rCode = NSCAPI::returnOK; 
    303   std::list<std::string> args = NSCHelper::makelist(argLen, char_args); 
     302  std::list<std::string> args = NSCHelper::arrayBuffer2list(argLen, char_args); 
    304303  if (args.size() < 2) 
    305304    return "Missing argument"; 
  • modules/CheckEventLog/CheckEventLog.vcproj

    r36c340d r8223547  
    120120        Name="VCCLCompilerTool" 
    121121        AdditionalIncludeDirectories="../include;../../include" 
    122         PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_STLP_USE_STATIC_LIB;SYSTRAY_EXPORTS" 
    123         RuntimeLibrary="0" 
     122        PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;BOOST_REGEX_DYN_LINK" 
     123        RuntimeLibrary="2" 
    124124        UsePrecompiledHeader="3" 
    125125        WarningLevel="3" 
  • modules/NSClientCompat/NSClientCompat.cpp

    r36c340d r8223547  
    125125    case REQ_CPULOAD: 
    126126      { 
    127         stl_args = NSCHelper::makelist(argLen, args); 
     127        stl_args = NSCHelper::arrayBuffer2list(argLen, args); 
    128128        if (stl_args.empty()) 
    129129          return "ERROR: Missing argument exception."; 
     
    141141      } 
    142142    case REQ_SERVICESTATE: 
    143       return NSCommands::serviceState(NSCHelper::makelist(argLen, args)); 
     143      return NSCommands::serviceState(NSCHelper::arrayBuffer2list(argLen, args)); 
    144144 
    145145    case REQ_PROCSTATE: 
    146       return NSCommands::procState(NSCHelper::makelist(argLen, args)); 
     146      return NSCommands::procState(NSCHelper::arrayBuffer2list(argLen, args)); 
    147147 
    148148    case REQ_MEMUSE: 
     
    151151 
    152152    case REQ_USEDDISKSPACE: 
    153       return NSCommands::usedDiskSpace(NSCHelper::makelist(argLen, args)); 
     153      return NSCommands::usedDiskSpace(NSCHelper::arrayBuffer2list(argLen, args)); 
    154154  } 
    155155  return ""; 
Note: See TracChangeset for help on using the changeset viewer.