Changeset 01a278b in nscp for service


Ignore:
Timestamp:
11/22/09 16:23:49 (3 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
1e0bbec
Parents:
b3078b4
Message:

(re)added service support (both w32/*nix) so now it runs as a service again...

Location:
service
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • service/CMakeLists.txt

    rd05c3f0 r01a278b  
    99 
    1010SET(service_SRCS 
     11  StdAfx.cpp 
    1112  NSClient++.cpp 
    1213  NSCPlugin.cpp 
    1314  core_api.cpp 
    1415  settings_manager_impl.cpp 
     16 
    1517  ${NSCP_INCLUDE_PATH}/NSCHelper.cpp 
    1618  ${NSCP_INCLUDE_PATH}/arrayBuffer.cpp 
    1719  ${NSCP_INCLUDE_PATH}/simpleini/ConvertUTF.c 
    1820  ${NSCP_INCLUDE_PATH}/b64/b64.c 
     21 
    1922) 
    2023 
     
    2427  ) 
    2528  SET(service_SRCS ${service_SRCS} 
     29    StdAfx.h 
     30    NSClient++.h 
     31    NSCPlugin.h 
     32    core_api.h 
     33    settings_manager_impl.h 
     34    simple_client.hpp 
     35     
     36    commands.hpp 
     37    logger.hpp 
     38    settings_logger_impl.hpp 
     39    service_manager.hpp 
     40     
    2641    ${NSCP_INCLUDE_PATH}/settings/Settings.h 
    2742    ${NSCP_INCLUDE_PATH}/charEx.h 
     
    3853    ${NSCP_INCLUDE_PATH}/dll/impl_unix.hpp 
    3954    ${NSCP_INCLUDE_PATH}/dll/impl_w32.hpp 
    40     core_api.h 
    41     settings_manager_impl.h 
     55#   ${NSCP_INCLUDE_PATH}/nsclient_session.hpp 
     56 
     57    ${NSCP_INCLUDE_PATH}/service/system_service.hpp 
     58    ${NSCP_INCLUDE_PATH}/service/win32_service.hpp 
    4259  ) 
     60  SET(service_SRCS ${service_SRCS} 
     61    ${NSCP_INCLUDE_PATH}/service/unix_service.hpp 
     62  ) 
     63   
    4364  ADD_DEFINITIONS(-D_WIN32_DCOM) 
    4465 
  • service/NSCPlugin.cpp

    rb3078b4 r01a278b  
    2222#include "NSCPlugin.h" 
    2323#include "core_api.h" 
     24 
     25unsigned int NSCPlugin::last_plugin_id_ = 0; 
     26 
     27 
    2428/** 
    2529 * Default c-tor 
     
    316320 
    317321    try { 
    318       fModuleHelperInit(NSAPILoader); 
     322      fModuleHelperInit(get_id(), NSAPILoader); 
    319323    } catch (...) { 
    320324      throw NSPluginException(module_, _T("Unhandled exception in getDescription.")); 
     
    360364  } catch (dll::dll_exception &e) { 
    361365    throw NSPluginException(module_, _T("Unhandled exception when loading proces: ") + e.what()); 
     366  } catch (...) { 
     367    throw NSPluginException(module_, _T("Unhandled exception when loading proces: <UNKNOWN>")); 
    362368  } 
    363369 
  • service/NSCPlugin.h

    rb3078b4 r01a278b  
    111111  unsigned int plugin_id_; 
    112112 
    113   typedef int (*lpModuleHelperInit)(NSCModuleHelper::lpNSAPILoader f); 
     113  typedef int (*lpModuleHelperInit)(unsigned int, NSCModuleHelper::lpNSAPILoader f); 
    114114  typedef int (*lpLoadModule)(int); 
    115115  typedef int (*lpGetName)(wchar_t*,unsigned int); 
     
    198198}; 
    199199 
    200 unsigned int NSCPlugin::last_plugin_id_ = 0; 
    201  
    202  
    203  
     200 
     201 
     202 
  • service/NSClient++.cpp

    rb3078b4 r01a278b  
    3636#include <settings/macros.h> 
    3737#include <NSCHelper.h> 
     38#include "simple_client.hpp" 
     39#include "service_manager.hpp" 
    3840 
    3941NSClient mainClient(SZSERVICENAME); // Global core instance. 
    4042bool g_bConsoleLog = false; 
     43 
     44 
     45////////////////////////////////////////////////////////////////////////// 
     46// Log macros to simplify logging 
     47// Generally names are of the form LOG_<severity>[_STD]  
     48// Where _STD indicates that strings are force wrapped inside a std::wstring 
     49// 
     50#define LOG_ERROR_STD(msg) LOG_ERROR(((std::wstring)msg).c_str()) 
     51#define LOG_ERROR(msg) \ 
     52  NSAPIMessage(NSCAPI::error, __FILEW__, __LINE__, msg) 
     53 
     54#define LOG_CRITICAL_STD(msg) LOG_CRITICAL(((std::wstring)msg).c_str()) 
     55#define LOG_CRITICAL(msg) \ 
     56  NSAPIMessage(NSCAPI::critical, __FILEW__, __LINE__, msg) 
     57#define LOG_MESSAGE_STD(msg) LOG_MESSAGE(((std::wstring)msg).c_str()) 
     58#define LOG_MESSAGE(msg) \ 
     59  NSAPIMessage(NSCAPI::log, __FILEW__, __LINE__, msg) 
     60 
     61#define LOG_DEBUG_STD(msg) LOG_DEBUG(((std::wstring)msg).c_str()) 
     62#define LOG_DEBUG(msg) \ 
     63  NSAPIMessage(NSCAPI::debug, __FILEW__, __LINE__, msg) 
     64 
     65#define LOG_ERROR_CORE(msg) reportMessage(NSCAPI::error, __FILEW__, __LINE__, msg) 
     66#define LOG_ERROR_CORE_STD(msg) LOG_ERROR_CORE(std::wstring(msg)) 
     67 
    4168 
    4269#define SETTINGS_GET_BOOL_CORE(key) \ 
     
    241268  if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { 
    242269    if (false) { 
    243 #ifdef WIN32 
    244270    } if ( wcscasecmp( _T("install"), argv[1]+1 ) == 0 ) { 
    245       bool bGui = false; 
    246       bool bStart = false; 
    247       std::wstring service_name, service_description; 
    248       for (int i=2;i<argc;i++) { 
    249         if (wcscasecmp( _T("gui"), argv[i]) == 0) { 
    250           bGui = true; 
    251         } else if (wcscasecmp( _T("start"), argv[i]) == 0) { 
    252           bStart = true; 
    253         } else { 
    254           if (service_name.empty()) 
    255             service_name = argv[i]; 
    256           else { 
    257             if (!service_description.empty()) 
    258               service_description += _T(" "); 
    259             service_description += argv[i]; 
    260           } 
    261         } 
    262       } 
    263       if (service_name.empty()) 
    264         service_name = SZSERVICENAME; 
    265       if (service_description.empty()) 
    266         service_description = SZSERVICEDISPLAYNAME; 
    267       g_bConsoleLog = true; 
    268       try { 
    269         serviceControll::Install(service_name.c_str(), service_description.c_str(), SZDEPENDENCIES); 
    270         if (bStart) 
    271           serviceControll::Start(service_name); 
    272       } catch (const serviceControll::SCException& e) { 
    273         if (bGui) 
    274           display(_T("Error installing"), _T("Service installation failed; ") + e.error_); 
    275         LOG_ERROR_STD(_T("Service installation failed: ") + e.error_); 
    276         return -1; 
    277       } 
    278       try { 
    279         serviceControll::SetDescription(service_name, service_description); 
    280       } catch (const serviceControll::SCException& e) { 
    281         if (bGui) 
    282           display(_T("Error installing"), _T("Service installation failed; ") + e.error_); 
    283         LOG_MESSAGE_STD(_T("Couldn't set service description: ") + e.error_); 
    284       } 
    285       if (bGui) 
    286         display(_T("Service installed"), _T("Service installed successfully!")); 
    287       LOG_MESSAGE(_T("Service installed!")); 
    288       return 0; 
     271      nsclient::client::service_manager mgr(argc-1,&argv[1]); 
     272      return mgr.install(); 
    289273    } else if ( wcscasecmp( _T("uninstall"), argv[1]+1 ) == 0 ) { 
    290       bool bGui = false; 
    291       bool bStop = false; 
    292       std::wstring service_name; 
    293       for (int i=2;i<argc;i++) { 
    294         if (wcscasecmp( _T("gui"), argv[i]) == 0) { 
    295           bGui = true; 
    296         } else if (wcscasecmp( _T("stop"), argv[i]) == 0) { 
    297           bStop = true; 
    298         } else { 
    299           service_name = argv[i]; 
    300         } 
    301       } 
    302       if (service_name.empty()) 
    303         service_name = SZSERVICENAME; 
    304       g_bConsoleLog = true; 
    305       try { 
    306         if (bStop) 
    307           serviceControll::Stop(service_name); 
    308       } catch (const serviceControll::SCException& e) { 
    309         LOG_MESSAGE_STD(_T("Failed to stop service (") + service_name + _T(") failed; ") + e.error_); 
    310       } 
    311       try { 
    312         serviceControll::Uninstall(service_name); 
    313       } catch (const serviceControll::SCException& e) { 
    314         if (bGui) 
    315           display(_T("Error uninstalling"), _T("Service de-installation (") + service_name + _T(") failed; ") + e.error_ + _T("\nMaybe the service was not previously installed properly?")); 
    316         LOG_ERROR_STD(_T("Service deinstallation failed; ") + e.error_); 
    317         return 0; 
    318       } 
    319       if (bGui) 
    320         display(_T("Service uninstalled"), _T("Service uninstalled successfully!")); 
    321       LOG_MESSAGE(_T("Service uninstalled!")); 
    322       return 0; 
     274      nsclient::client::service_manager mgr(argc-1,&argv[1]); 
     275      return mgr.uninstall(); 
    323276    } else if ( wcscasecmp( _T("start"), argv[1]+1 ) == 0 ) { 
    324       g_bConsoleLog = true; 
    325       bool bGui = false; 
    326       std::wstring service_name; 
    327       for (int i=2;i<argc;i++) { 
    328         if (wcscasecmp( _T("gui"), argv[i]) == 0) { 
    329           bGui = true; 
    330         } else { 
    331           service_name = argv[i]; 
    332         } 
    333       } 
    334       if (service_name.empty()) 
    335         service_name = SZSERVICENAME; 
    336       try { 
    337         serviceControll::Start(service_name.c_str()); 
    338       } catch (const serviceControll::SCException& e) { 
    339         if (bGui) 
    340           display(_T("Service failed to start"), e.error_); 
    341         LOG_MESSAGE_STD(_T("Service failed to start: ") + e.error_); 
    342         return -1; 
    343       } 
     277      nsclient::client::service_manager mgr(argc-1,&argv[1]); 
     278      return mgr.start(); 
    344279    } else if ( wcscasecmp( _T("stop"), argv[1]+1 ) == 0 ) { 
    345       g_bConsoleLog = true; 
    346       bool bGui = false; 
    347       std::wstring service_name; 
    348       for (int i=2;i<argc;i++) { 
    349         if (wcscasecmp( _T("gui"), argv[i]) == 0) { 
    350           bGui = true; 
    351         } else { 
    352           service_name = argv[i]; 
    353         } 
    354       } 
    355       if (service_name.empty()) 
    356         service_name = SZSERVICENAME; 
    357       try { 
    358         serviceControll::Stop(service_name.c_str()); 
    359       } catch (const serviceControll::SCException& e) { 
    360         if (bGui) 
    361           display(_T("Service failed to stop"), e.error_); 
    362         LOG_MESSAGE_STD(_T("Service failed to stop: ") + e.error_); 
    363         return -1; 
    364       } 
     280      nsclient::client::service_manager mgr(argc-1,&argv[1]); 
     281      return mgr.stop(); 
    365282    } else if ( wcscasecmp( _T("svc"), argv[1]+1 ) == 0 ) { 
    366       g_bConsoleLog = true; 
    367       try { 
    368         std::wstring exe = serviceControll::get_exe_path(SZSERVICENAME); 
    369         LOG_MESSAGE_STD(_T("The Service uses: ") + exe); 
    370       } catch (const serviceControll::SCException& e) { 
    371         LOG_ERROR_STD(_T("Failed to find service: ") + e.error_); 
    372       } 
    373 #endif 
     283      nsclient::client::service_manager mgr(argc-1,&argv[1]); 
     284      return mgr.print_command(); 
    374285    } else if ( wcscasecmp( _T("encrypt"), argv[1]+1 ) == 0 ) { 
    375286      g_bConsoleLog = true; 
     
    477388          std::wcerr << "Service seems to be started, this is probably not a good idea..." << std::endl; 
    478389        } 
    479       } catch (const serviceControll::SCException& e) { 
    480         e;// Empty by design 
     390      } catch (...) { 
     391        // Empty by design 
    481392      } 
    482393#endif 
     394      nsclient::simple_client client(&mainClient); 
    483395      g_bConsoleLog = true; 
    484       mainClient.enableDebug(true); 
    485       if (!mainClient.initCore(true)) { 
    486         LOG_ERROR_STD(_T("Service *NOT* started!")); 
    487         return -1; 
    488       } 
    489       LOG_MESSAGE_STD(_T("Using settings from: ") + settings_manager::get_core()->get_settings_type_desc()); 
    490       LOG_MESSAGE(_T("Enter command to inject or exit to terminate...")); 
    491 /* 
    492       Settings::get_settings()->clear_cache(); 
    493       LOG_MESSAGE_STD( _T("test 001: ") + SETTINGS_GET_STRING(NSCLIENT_TEST1) ); 
    494       LOG_MESSAGE_STD( _T("test 002: ") + SETTINGS_GET_STRING(NSCLIENT_TEST2) ); 
    495       LOG_MESSAGE_STD( _T("test 003: ") + SETTINGS_GET_STRING(NSCLIENT_TEST3) ); 
    496       LOG_MESSAGE_STD( _T("test 004: ") + SETTINGS_GET_STRING(NSCLIENT_TEST4) ); 
    497  
    498       Settings::get_settings()->save_to(_T("test.ini")); 
    499 */ 
    500       std::wstring s = _T(""); 
    501       std::wstring buff = _T(""); 
    502       while (true) { 
    503         std::wcin >> s; 
    504         if (s == _T("exit")) { 
    505           std::wcout << _T("Exiting...") << std::endl; 
    506           break; 
    507         } else if (s == _T("plugins")) { 
    508           std::wcout << _T("Listing plugins...") << std::endl; 
    509           mainClient.listPlugins(); 
    510         } else if (s == _T("off") && buff == _T("debug ")) { 
    511           std::wcout << _T("Setting debug log off...") << std::endl; 
    512           mainClient.enableDebug(false); 
    513         } else if (s == _T("on") && buff == _T("debug ")) { 
    514           std::wcout << _T("Setting debug log on...") << std::endl; 
    515           mainClient.enableDebug(true); 
    516         } else if (s == _T("reattach")) { 
    517           std::wcout << _T("Reattaching to session 0") << std::endl; 
    518           mainClient.startTrayIcon(0); 
    519 //#ifdef DEBUG 
    520         } else if (s == _T("assert")) { 
    521           throw "test"; 
    522 //#endif 
    523         } else if (std::cin.peek() < 15) { 
    524           buff += s; 
    525           strEx::token t = strEx::getToken(buff, ' '); 
    526           std::wstring msg, perf; 
    527           NSCAPI::nagiosReturn ret = mainClient.inject(t.first, t.second, ' ', true, msg, perf); 
    528           if (ret == NSCAPI::returnIgnored) { 
    529             std::wcout << _T("No handler for command: ") << t.first << std::endl; 
    530           } else { 
    531             std::wcout << NSCHelper::translateReturn(ret) << _T(":"); 
    532             std::cout << strEx::wstring_to_string(msg); 
    533             if (!perf.empty()) 
    534               std::cout << "|" << strEx::wstring_to_string(perf); 
    535             std::wcout << std::endl; 
    536           } 
    537           buff = _T(""); 
    538         } else { 
    539           buff += s + _T(" "); 
    540         } 
    541       } 
    542       mainClient.exitCore(true); 
     396      client.start(); 
    543397      return 0; 
    544398    } else { 
     
    565419    return -1; 
    566420  } 
    567   std::wcout << _T("Running as service...") << std::endl; 
    568   if (!mainClient.StartServiceCtrlDispatcher()) { 
    569     LOG_MESSAGE(_T("We failed to start the service")); 
     421  try { 
     422    std::wcout << _T("Running as service...") << std::endl; 
     423    mainClient.enableDebug(true); 
     424    mainClient.start_and_wait(); 
     425  } catch (...) { 
     426    std::wcerr << _T("Unknown exception in service") << std::endl; 
    570427  } 
    571428  return nRetCode; 
     
    573430 
    574431void migrate() {} 
     432 
     433std::list<std::wstring> NSClientT::list_commands() { 
     434  return commands_.list(); 
     435} 
    575436 
    576437NSClientT::plugin_info_list NSClientT::get_all_plugins() { 
     
    655516    if (sarg == _T("migrate")) { 
    656517      if (argc == 0) { 
    657         LOG_ERROR_STD(_T("In correct syntax: nsclient++ -settings migrate <to>")); 
     518        LOG_ERROR_CORE_STD(_T("In correct syntax: nsclient++ -settings migrate <to>")); 
    658519        return; 
    659520      } 
     
    667528    } else if (sarg == _T("generate")) { 
    668529      if (argc == 0) { 
    669         LOG_ERROR_STD(_T("In correct syntax: nsclient++ -settings generate <what>")); 
    670         LOG_ERROR_STD(_T("     where <what> is one of ths following:")); 
    671         LOG_ERROR_STD(_T("      trac")); 
    672         LOG_ERROR_STD(_T("      default")); 
    673         LOG_ERROR_STD(_T("      <type>")); 
     530        LOG_ERROR_CORE_STD(_T("In correct syntax: nsclient++ -settings generate <what>")); 
     531        LOG_ERROR_CORE_STD(_T("     where <what> is one of ths following:")); 
     532        LOG_ERROR_CORE_STD(_T("      trac")); 
     533        LOG_ERROR_CORE_STD(_T("      default")); 
     534        LOG_ERROR_CORE_STD(_T("      <type>")); 
    674535        return; 
    675536      } 
     
    728589      } 
    729590    } else { 
    730       LOG_ERROR_STD(_T("In correct syntax: nsclient++ -settings <keyword>")); 
    731       LOG_ERROR_STD(_T(" <keyword> : ")); 
    732       LOG_ERROR_STD(_T("   migrate - migrate to a new setings subsystem")); 
    733       LOG_ERROR_STD(_T("   copy    - copy settings from one subsystem to another")); 
    734       LOG_ERROR_STD(_T("   set     - Set a setting system as the default store")); 
     591      LOG_ERROR_CORE_STD(_T("In correct syntax: nsclient++ -settings <keyword>")); 
     592      LOG_ERROR_CORE_STD(_T(" <keyword> : ")); 
     593      LOG_ERROR_CORE_STD(_T("   migrate - migrate to a new setings subsystem")); 
     594      LOG_ERROR_CORE_STD(_T("   copy    - copy settings from one subsystem to another")); 
     595      LOG_ERROR_CORE_STD(_T("   set     - Set a setting system as the default store")); 
    735596    } 
    736597 
     
    770631    return false; 
    771632  } 
     633  LOG_MESSAGE(_T("Got settings subsystem...")); 
    772634  try { 
    773635    if (debug_) 
     
    776638    enable_shared_session_ = SETTINGS_GET_BOOL_CORE(settings_def::SHARED_SESSION); 
    777639  } catch (SettingsException e) { 
    778     LOG_ERROR_STD(_T("Could not find settings: ") + e.getMessage()); 
     640    LOG_ERROR_CORE_STD(_T("Could not find settings: ") + e.getMessage()); 
    779641  } 
    780642 
     
    833695    com_helper_.initialize(); 
    834696  } catch (com_helper::com_exception e) { 
    835     LOG_ERROR_STD(_T("COM exception: ") + e.getMessage()); 
     697    LOG_ERROR_CORE_STD(_T("COM exception: ") + e.getMessage()); 
    836698    return false; 
    837699  } catch (...) { 
    838     LOG_ERROR_STD(_T("Unknown exception iniating COM...")); 
     700    LOG_ERROR_CORE(_T("Unknown exception iniating COM...")); 
    839701    return false; 
    840702  } 
     
    856718          loadPlugin(getBasePath() / boost::filesystem::wpath(_T("modules")) / boost::filesystem::wpath(*cit)); 
    857719        } catch(const NSPluginException& e) { 
    858           LOG_ERROR_STD(_T("Exception raised: '") + e.error_ + _T("' in module: ") + e.file_); 
     720          LOG_ERROR_CORE_STD(_T("Exception raised: '") + e.error_ + _T("' in module: ") + e.file_); 
    859721          //return false; 
    860722        } catch (std::exception e) { 
    861           LOG_ERROR_STD(_T("exception loading plugin: ") + (*cit) + strEx::string_to_wstring(e.what())); 
     723          LOG_ERROR_CORE_STD(_T("exception loading plugin: ") + (*cit) + strEx::string_to_wstring(e.what())); 
    862724          return false; 
    863725        } catch (...) { 
    864           LOG_ERROR_STD(_T("Unknown exception loading plugin: ") + (*cit)); 
     726          LOG_ERROR_CORE_STD(_T("Unknown exception loading plugin: ") + (*cit)); 
    865727          return false; 
    866728        } 
    867729      } 
    868730    } catch (SettingsException e) { 
    869       LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage()); 
     731      LOG_ERROR_CORE_STD(_T("Failed to set settings file") + e.getMessage()); 
    870732    } catch (...) { 
    871       LOG_ERROR_STD(_T("Unknown exception when loading plugins")); 
     733      LOG_ERROR_CORE_STD(_T("Unknown exception when loading plugins")); 
    872734      return false; 
    873735    } 
     
    875737      loadPlugins(boot?NSCAPI::normalStart:NSCAPI::dontStart); 
    876738    } catch (...) { 
    877       LOG_ERROR_STD(_T("Unknown exception loading plugins")); 
     739      LOG_ERROR_CORE_STD(_T("Unknown exception loading plugins")); 
    878740      return false; 
    879741    } 
     
    881743  } 
    882744  LOG_MESSAGE_STD(_T("NSCLient++ - ") SZVERSION _T(" Started!")); 
    883   return true; 
    884 } 
    885  
    886 /** 
    887  * Service control handler startup point. 
    888  * When the program is started as a service this will be the entry point. 
    889  */ 
    890 bool NSClientT::InitiateService() { 
    891   if (!initCore(true)) 
    892     return false; 
    893 /* 
    894   DWORD dwSessionId = remote_processes::getActiveSessionId(); 
    895   if (dwSessionId != 0xFFFFFFFF) 
    896     tray_starter::start(dwSessionId); 
    897   else 
    898     LOG_ERROR_STD(_T("Failed to start tray helper:" ) + error::lookup::last_error()); 
    899     */ 
    900745  return true; 
    901746} 
     
    940785      mainClient.unloadPlugins(false); 
    941786    } catch(NSPluginException e) { 
    942       LOG_ERROR_STD(_T("Exception raised when unloading non msg plguins: ") + e.error_ + _T(" in module: ") + e.file_); 
     787      LOG_ERROR_CORE_STD(_T("Exception raised when unloading non msg plguins: ") + e.error_ + _T(" in module: ") + e.file_); 
    943788    } catch(...) { 
    944       LOG_ERROR_STD(_T("Unknown exception raised when unloading non msg plugins")); 
     789      LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading non msg plugins")); 
    945790    } 
    946791  } 
     
    950795    com_helper_.unInitialize(); 
    951796  } catch (com_helper::com_exception e) { 
    952     LOG_ERROR_STD(_T("COM exception: ") + e.getMessage()); 
     797    LOG_ERROR_CORE_STD(_T("COM exception: ") + e.getMessage()); 
    953798  } catch (...) { 
    954     LOG_ERROR_STD(_T("Unknown exception uniniating COM...")); 
     799    LOG_ERROR_CORE_STD(_T("Unknown exception uniniating COM...")); 
    955800  } 
    956801#endif 
     
    985830//    } 
    986831  } catch(...) { 
    987     LOG_ERROR_STD(_T("UNknown exception raised: When closing shared session")); 
     832    LOG_ERROR_CORE_STD(_T("UNknown exception raised: When closing shared session")); 
    988833  } 
    989834  if (boot) { 
     
    992837      mainClient.unloadPlugins(true); 
    993838    } catch(NSPluginException e) { 
    994       LOG_ERROR_STD(_T("Exception raised when unloading msg plugins: ") + e.error_ + _T(" in module: ") + e.file_); 
     839      LOG_ERROR_CORE_STD(_T("Exception raised when unloading msg plugins: ") + e.error_ + _T(" in module: ") + e.file_); 
    995840    } catch(...) { 
    996       LOG_ERROR_STD(_T("UNknown exception raised: When stopping message plguins")); 
     841      LOG_ERROR_CORE_STD(_T("UNknown exception raised: When stopping message plguins")); 
    997842    } 
    998843  } 
    999844  LOG_MESSAGE_STD(_T("NSCLient++ - ") SZVERSION _T(" Stopped succcessfully")); 
    1000845  return true; 
    1001 } 
    1002 /** 
    1003  * Service control handler termination point. 
    1004  * When the program is stopped as a service this will be the "exit point". 
    1005  */ 
    1006 void NSClientT::TerminateService(void) { 
    1007   exitCore(true); 
    1008846} 
    1009847#ifdef WIN32x 
     
    1056894    boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(5)); 
    1057895    if (!readLock.owns_lock()) { 
    1058       LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     896      LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    1059897      return -1; 
    1060898    } 
     
    1070908            return p->commandLineExec(command, argLen, args); 
    1071909          } catch (NSPluginException e) { 
    1072             LOG_ERROR_STD(_T("Could not execute command: ") + e.error_ + _T(" in ") + e.file_); 
     910            LOG_ERROR_CORE_STD(_T("Could not execute command: ") + e.error_ + _T(" in ") + e.file_); 
    1073911            return -1; 
    1074912          } 
     
    1085923    LOG_MESSAGE_STD(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); 
    1086924  } 
    1087   LOG_ERROR_STD(_T("Module not found: ") + module + _T(" available modules are: ") + moduleList); 
     925  LOG_ERROR_CORE_STD(_T("Module not found: ") + std::wstring(module) + _T(" available modules are: ") + moduleList); 
    1088926  return 0; 
    1089927} 
     
    1096934  boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 
    1097935  if (!readLock.owns_lock()) { 
    1098     LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     936    LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    1099937    return; 
    1100938  } 
     
    1111949    boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 
    1112950    if (!writeLock.owns_lock()) { 
    1113       LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     951      LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    1114952      return; 
    1115953    } 
     
    1121959    boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 
    1122960    if (!readLock.owns_lock()) { 
    1123       LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     961      LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    1124962      return; 
    1125963    } 
     
    1136974        } 
    1137975      } catch(NSPluginException e) { 
    1138         LOG_ERROR_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 
     976        LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 
    1139977      } catch(...) { 
    1140         LOG_ERROR_STD(_T("Unknown exception raised when unloading plugin")); 
     978        LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading plugin")); 
    1141979      } 
    1142980    } 
     
    1145983    boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 
    1146984    if (!writeLock.owns_lock()) { 
    1147       LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     985      LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    1148986      return; 
    1149987    } 
     
    1158996        } 
    1159997      } catch(NSPluginException e) { 
    1160         LOG_ERROR_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 
     998        LOG_ERROR_CORE_STD(_T("Exception raised when unloading plugin: ") + e.error_ + _T(" in module: ") + e.file_); 
    1161999      } catch(...) { 
    1162         LOG_ERROR_STD(_T("Unknown exception raised when unloading plugin")); 
     1000        LOG_ERROR_CORE(_T("Unknown exception raised when unloading plugin")); 
    11631001      } 
    11641002      it++; 
     
    11721010    boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 
    11731011    if (!readLock.owns_lock()) { 
    1174       LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     1012      LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    11751013      return; 
    11761014    } 
     
    11801018        if (!(*it)->load_plugin(NSCAPI::normalStart)) { 
    11811019          it = plugins_.erase(it); 
    1182           LOG_ERROR_STD(_T("Plugin refused to load: ") + (*it)->getModule()); 
     1020          LOG_ERROR_CORE_STD(_T("Plugin refused to load: ") + (*it)->getModule()); 
    11831021        } 
    11841022        ++it; 
    11851023      } catch (NSPluginException e) { 
    11861024        it = plugins_.erase(it); 
    1187         LOG_ERROR_STD(_T("Could not load plugin: ") + e.file_ + _T(": ") + e.error_); 
     1025        LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + e.file_ + _T(": ") + e.error_); 
    11881026      } catch (...) { 
    11891027        it = plugins_.erase(it); 
    1190         LOG_ERROR_STD(_T("Could not load plugin: ") + (*it)->getModule()); 
     1028        LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + (*it)->getModule()); 
    11911029      } 
    11921030    } 
     
    11991037    } catch(NSPluginException e) { 
    12001038      it = plugins_.erase(it); 
    1201       LOG_ERROR_STD(_T("Exception raised when loading plugin: ") + e.error_ + _T(" in module: ") + e.file_ + _T(" plugin has been removed.")); 
     1039      LOG_ERROR_CORE_STD(_T("Exception raised when loading plugin: ") + e.error_ + _T(" in module: ") + e.file_ + _T(" plugin has been removed.")); 
    12021040    } catch(...) { 
    12031041      it = plugins_.erase(it); 
    1204       LOG_ERROR_STD(_T("Unknown exception raised when unloading plugin plugin has been removed")); 
     1042      LOG_ERROR_CORE(_T("Unknown exception raised when unloading plugin plugin has been removed")); 
    12051043    } 
    12061044  } 
     
    12241062    boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 
    12251063    if (!writeLock.owns_lock()) { 
    1226       LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     1064      LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    12271065      return plugin; 
    12281066    } 
     
    12571095      return setting_keys::settings_def::PAYLOAD_LEN_DEFAULT; 
    12581096    } catch (...) { 
    1259       LOG_ERROR(_T("Failed to get length: :(")); 
     1097      LOG_ERROR_CORE(_T("Failed to get length: :(")); 
    12601098      return setting_keys::settings_def::PAYLOAD_LEN_DEFAULT; 
    12611099    } 
     
    13301168    boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 
    13311169    if (!readLock.owns_lock()) { 
    1332       LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     1170      LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    13331171      return NSCAPI::returnUNKNOWN; 
    13341172    } 
     
    13381176        switch (c) { 
    13391177          case NSCAPI::returnInvalidBufferLen: 
    1340             LOG_ERROR(_T("UNKNOWN: Return buffer to small to handle this command.")); 
     1178            LOG_ERROR_CORE(_T("UNKNOWN: Return buffer to small to handle this command.")); 
    13411179            return c; 
    13421180          case NSCAPI::returnIgnored: 
     
    13501188            return c; 
    13511189          default: 
    1352             LOG_ERROR_STD(_T("Unknown error from handleCommand: ") + strEx::itos(c) + _T(" the injected command was: ") + (std::wstring)command); 
     1190            LOG_ERROR_CORE_STD(_T("Unknown error from handleCommand: ") + strEx::itos(c) + _T(" the injected command was: ") + (std::wstring)command); 
    13531191            return c; 
    13541192        } 
    13551193      } catch(const NSPluginException& e) { 
    1356         LOG_ERROR_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); 
     1194        LOG_ERROR_CORE_STD(_T("Exception raised: ") + e.error_ + _T(" in module: ") + e.file_); 
    13571195        return NSCAPI::returnCRIT; 
    13581196      } catch(...) { 
    1359         LOG_ERROR_STD(_T("Unknown exception raised in module")); 
     1197        LOG_ERROR_CORE(_T("Unknown exception raised in module")); 
    13601198        return NSCAPI::returnCRIT; 
    13611199      } 
     
    13691207  boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 
    13701208  if (!readLock.owns_lock()) { 
    1371     LOG_ERROR(_T("FATAL ERROR: Could not get read-mutex.")); 
     1209    LOG_ERROR_CORE(_T("FATAL ERROR: Could not get read-mutex.")); 
    13721210    return; 
    13731211  } 
     
    13801218      } 
    13811219    } catch (NSPluginException e) { 
    1382       LOG_ERROR_STD(_T("Could not load plugin: ") + e.file_ + _T(": ") + e.error_); 
     1220      LOG_ERROR_CORE_STD(_T("Could not load plugin: ") + e.file_ + _T(": ") + e.error_); 
    13831221    } 
    13841222  } 
     
    15071345  boost::unique_lock<boost::timed_mutex> lock(internalVariables, boost::get_system_time() + boost::posix_time::seconds(5)); 
    15081346  if (!lock.owns_lock()) { 
    1509     LOG_ERROR(_T("FATAL ERROR: Could not get mutex.")); 
     1347    LOG_ERROR_CORE(_T("FATAL ERROR: Could not get mutex.")); 
    15101348    return _T("FATAL ERROR"); 
    15111349  } 
     
    15261364    settings_manager::get_core()->set_base(basePath); 
    15271365  } catch (SettingsException e) { 
    1528     LOG_ERROR_STD(_T("Failed to set settings file: ") + e.getMessage()); 
     1366    LOG_ERROR_CORE_STD(_T("Failed to set settings file: ") + e.getMessage()); 
    15291367  } catch (...) { 
    1530     LOG_ERROR_STD(_T("Failed to set settings file")); 
     1368    LOG_ERROR_CORE_STD(_T("Failed to set settings file")); 
    15311369  } 
    15321370  return basePath; 
     
    15621400 
    15631401void NSClientT::nsclient_log_error(std::wstring file, int line, std::wstring error) { 
    1564   NSAPIMessage(NSCAPI::error, file.c_str(), line, error); 
    1565 } 
     1402  reportMessage(NSCAPI::error, file.c_str(), line, error); 
     1403} 
     1404 
     1405 
     1406 
     1407// Service API 
     1408NSClient* NSClientT::get_global_instance() { 
     1409  return &mainClient; 
     1410} 
     1411void NSClientT::handle_startup() { 
     1412  initCore(true); 
     1413/* 
     1414  DWORD dwSessionId = remote_processes::getActiveSessionId(); 
     1415  if (dwSessionId != 0xFFFFFFFF) 
     1416    tray_starter::start(dwSessionId); 
     1417  else 
     1418    LOG_ERROR_STD(_T("Failed to start tray helper:" ) + error::lookup::last_error()); 
     1419    */ 
     1420} 
     1421void NSClientT::handle_shutdown() { 
     1422  exitCore(true); 
     1423} 
     1424#ifdef _WIN32 
     1425void NSClientT::handle_session_change(unsigned long dwSessionId, bool logon) { 
     1426 
     1427} 
     1428#endif 
  • service/NSClient++.h

    rb3078b4 r01a278b  
    3333 
    3434//#include <nsclient_session.hpp> 
     35 
     36 
     37class NSClientT; 
     38typedef service_helper::impl<NSClientT>::system_service NSClient; 
    3539 
    3640/** 
     
    117121  bool plugins_loaded_; 
    118122  bool enable_shared_session_; 
    119  
    120123  nsclient::commands commands_; 
    121124 
     
    133136 
    134137  // Service helper functions 
    135   bool InitiateService(); 
    136   void TerminateService(void); 
    137138  bool initCore(bool boot); 
    138139  bool exitCore(bool boot); 
     
    148149  void nsclient_log_error(std::wstring file, int line, std::wstring error); 
    149150 
     151  // Service API 
     152  static NSClient* get_global_instance(); 
     153  void handle_error(unsigned int line, wchar_t *file, std::wstring message) { 
     154    reportMessage(NSCAPI::error, file, line, message); 
     155  } 
     156  void handle_startup(); 
     157  void handle_shutdown(); 
     158#ifdef _WIN32 
     159  void handle_session_change(unsigned long dwSessionId, bool logon); 
     160#endif 
     161 
    150162 
    151163  // Member functions 
    152   boost::filesystem::wpath  getBasePath(void); 
     164  boost::filesystem::wpath getBasePath(void); 
    153165  NSCAPI::nagiosReturn injectRAW(const wchar_t* command, const unsigned int argLen, wchar_t **argument, wchar_t *returnMessageBuffer, unsigned int returnMessageBufferLen, wchar_t *returnPerfBuffer, unsigned int returnPerfBufferLen); 
    154166  NSCAPI::nagiosReturn inject(std::wstring command, std::wstring arguments, wchar_t splitter, bool escape, std::wstring &msg, std::wstring & perf); 
     
    164176  std::wstring describeCommand(std::wstring command); 
    165177  std::list<std::wstring> getAllCommandNames(); 
    166   void registerCommand(std::wstring cmd, std::wstring desc); 
     178  void registerCommand(unsigned int id, std::wstring cmd, std::wstring desc); 
    167179  unsigned int getBufferLength(); 
    168180  void HandleSettingsCLI(wchar_t* arg, int argc, wchar_t* argv[]); 
     
    173185  void listPlugins(); 
    174186  plugin_info_list get_all_plugins(); 
     187  std::list<std::wstring> list_commands(); 
    175188 
    176189  // Shared session interface: 
     
    194207}; 
    195208 
    196 typedef service_helper::impl<NSClientT>::system_service NSClient; 
    197209 
    198210extern NSClient mainClient; // Global core instance forward declaration. 
     
    202214std::wstring Decrypt(std::wstring str, unsigned int algorithm = NSCAPI::encryption_xor); 
    203215 
    204 ////////////////////////////////////////////////////////////////////////// 
    205 // Log macros to simplify logging 
    206 // Generally names are of the form LOG_<severity>[_STD]  
    207 // Where _STD indicates that strings are force wrapped inside a std::wstring 
    208 // 
    209 #define LOG_ERROR_STD(msg) LOG_ERROR(((std::wstring)msg).c_str()) 
    210 #define LOG_ERROR(msg) \ 
    211   NSAPIMessage(NSCAPI::error, __FILEW__, __LINE__, msg) 
    212 #define LOG_CRITICAL_STD(msg) LOG_CRITICAL(((std::wstring)msg).c_str()) 
    213 #define LOG_CRITICAL(msg) \ 
    214   NSAPIMessage(NSCAPI::critical, __FILEW__, __LINE__, msg) 
    215 #define LOG_MESSAGE_STD(msg) LOG_MESSAGE(((std::wstring)msg).c_str()) 
    216 #define LOG_MESSAGE(msg) \ 
    217   NSAPIMessage(NSCAPI::log, __FILEW__, __LINE__, msg) 
    218  
    219 #define LOG_DEBUG_STD(msg) LOG_DEBUG(((std::wstring)msg).c_str()) 
    220 #define LOG_DEBUG(msg) \ 
    221   NSAPIMessage(NSCAPI::debug, __FILEW__, __LINE__, msg) 
    222216/* 
    223217#define LOG_DEBUG_STD(msg) 
  • service/StdAfx.cpp

    rf0eb62d r01a278b  
    1919*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
    2020***************************************************************************/ 
    21 #include "stdafx.h" 
     21#include "StdAfx.h" 
    2222#define COMPILE_NEWAPIS_STUBS  
  • service/commands.hpp

    rb3078b4 r01a278b  
    1010  public: 
    1111    class command_exception : public std::exception { 
     12      std::string what_; 
    1213    public: 
    13       command_exception(std::wstring error) : std::exception(to_string(error).c_str()) {} 
    14       command_exception(std::string error) : std::exception(error.c_str()) {} 
     14      command_exception(std::wstring error) throw() : what_(to_string(error).c_str()) {} 
     15      command_exception(std::string error) throw() : what_(error.c_str()) {} 
     16      virtual ~command_exception() throw() {}; 
     17 
     18      virtual const char* what() const throw() { 
     19        return what_.c_str(); 
     20      } 
    1521 
    1622    }; 
  • service/core_api.cpp

    rd05c3f0 r01a278b  
    2929#include <b64/b64.h> 
    3030#include <NSCHelper.h> 
    31  
     31#ifdef _WIN32 
     32#include <ServiceCmd.h> 
     33#endif 
     34 
     35#define LOG_ERROR_STD(msg) LOG_ERROR(((std::wstring)msg).c_str()) 
     36#define LOG_ERROR(msg) \ 
     37  NSAPIMessage(NSCAPI::error, __FILEW__, __LINE__, msg) 
     38 
     39#define LOG_CRITICAL_STD(msg) LOG_CRITICAL(((std::wstring)msg).c_str()) 
     40#define LOG_CRITICAL(msg) \ 
     41  NSAPIMessage(NSCAPI::critical, __FILEW__, __LINE__, msg) 
     42#define LOG_MESSAGE_STD(msg) LOG_MESSAGE(((std::wstring)msg).c_str()) 
     43#define LOG_MESSAGE(msg) \ 
     44  NSAPIMessage(NSCAPI::log, __FILEW__, __LINE__, msg) 
     45 
     46#define LOG_DEBUG_STD(msg) LOG_DEBUG(((std::wstring)msg).c_str()) 
     47#define LOG_DEBUG(msg) \ 
     48  NSAPIMessage(NSCAPI::debug, __FILEW__, __LINE__, msg) 
    3249 
    3350 
     
    250267  return NSCAPI::isSuccess; 
    251268} 
    252 NSCAPI::errorReturn NSAPIRegisterCommand(const wchar_t* cmd,const wchar_t* desc) { 
    253   mainClient.registerCommand(cmd, desc); 
     269NSCAPI::errorReturn NSAPIRegisterCommand(unsigned int id, const wchar_t* cmd,const wchar_t* desc) { 
     270  try { 
     271    mainClient.registerCommand(id, cmd, desc); 
     272  } catch (...) { 
     273    LOG_ERROR_STD(_T("Unknown exception registrying command: ") + std::wstring(cmd) + _T(", from: ") + to_wstring(id)); 
     274    return NSCAPI::isSuccess; 
     275 
     276  } 
    254277  return NSCAPI::isSuccess; 
    255278} 
  • service/core_api.h

    r818b54e r01a278b  
    5252NSCAPI::errorReturn NSAPIGetAllCommandNames(wchar_t***, unsigned int *); 
    5353NSCAPI::errorReturn NSAPIReleaseAllCommandNamessBuffer(wchar_t***, unsigned int *); 
    54 NSCAPI::errorReturn NSAPIRegisterCommand(const wchar_t*,const wchar_t*); 
     54NSCAPI::errorReturn NSAPIRegisterCommand(unsigned int, const wchar_t*,const wchar_t*); 
    5555NSCAPI::errorReturn NSAPISettingsRegKey(const wchar_t*, const wchar_t*, int, const wchar_t*, const wchar_t*, const wchar_t*, int); 
    5656NSCAPI::errorReturn NSAPISettingsRegPath(const wchar_t*, const wchar_t*, const wchar_t*, int); 
Note: See TracChangeset for help on using the changeset viewer.