- Timestamp:
- 11/22/09 16:23:49 (3 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 1e0bbec
- Parents:
- b3078b4
- Location:
- service
- Files:
-
- 9 edited
-
CMakeLists.txt (modified) (3 diffs)
-
NSCPlugin.cpp (modified) (3 diffs)
-
NSCPlugin.h (modified) (2 diffs)
-
NSClient++.cpp (modified) (40 diffs)
-
NSClient++.h (modified) (8 diffs)
-
StdAfx.cpp (modified) (1 diff)
-
commands.hpp (modified) (1 diff)
-
core_api.cpp (modified) (2 diffs)
-
core_api.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
service/CMakeLists.txt
rd05c3f0 r01a278b 9 9 10 10 SET(service_SRCS 11 StdAfx.cpp 11 12 NSClient++.cpp 12 13 NSCPlugin.cpp 13 14 core_api.cpp 14 15 settings_manager_impl.cpp 16 15 17 ${NSCP_INCLUDE_PATH}/NSCHelper.cpp 16 18 ${NSCP_INCLUDE_PATH}/arrayBuffer.cpp 17 19 ${NSCP_INCLUDE_PATH}/simpleini/ConvertUTF.c 18 20 ${NSCP_INCLUDE_PATH}/b64/b64.c 21 19 22 ) 20 23 … … 24 27 ) 25 28 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 26 41 ${NSCP_INCLUDE_PATH}/settings/Settings.h 27 42 ${NSCP_INCLUDE_PATH}/charEx.h … … 38 53 ${NSCP_INCLUDE_PATH}/dll/impl_unix.hpp 39 54 ${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 42 59 ) 60 SET(service_SRCS ${service_SRCS} 61 ${NSCP_INCLUDE_PATH}/service/unix_service.hpp 62 ) 63 43 64 ADD_DEFINITIONS(-D_WIN32_DCOM) 44 65 -
service/NSCPlugin.cpp
rb3078b4 r01a278b 22 22 #include "NSCPlugin.h" 23 23 #include "core_api.h" 24 25 unsigned int NSCPlugin::last_plugin_id_ = 0; 26 27 24 28 /** 25 29 * Default c-tor … … 316 320 317 321 try { 318 fModuleHelperInit( NSAPILoader);322 fModuleHelperInit(get_id(), NSAPILoader); 319 323 } catch (...) { 320 324 throw NSPluginException(module_, _T("Unhandled exception in getDescription.")); … … 360 364 } catch (dll::dll_exception &e) { 361 365 throw NSPluginException(module_, _T("Unhandled exception when loading proces: ") + e.what()); 366 } catch (...) { 367 throw NSPluginException(module_, _T("Unhandled exception when loading proces: <UNKNOWN>")); 362 368 } 363 369 -
service/NSCPlugin.h
rb3078b4 r01a278b 111 111 unsigned int plugin_id_; 112 112 113 typedef int (*lpModuleHelperInit)( NSCModuleHelper::lpNSAPILoader f);113 typedef int (*lpModuleHelperInit)(unsigned int, NSCModuleHelper::lpNSAPILoader f); 114 114 typedef int (*lpLoadModule)(int); 115 115 typedef int (*lpGetName)(wchar_t*,unsigned int); … … 198 198 }; 199 199 200 unsigned int NSCPlugin::last_plugin_id_ = 0; 201 202 203 200 201 202 -
service/NSClient++.cpp
rb3078b4 r01a278b 36 36 #include <settings/macros.h> 37 37 #include <NSCHelper.h> 38 #include "simple_client.hpp" 39 #include "service_manager.hpp" 38 40 39 41 NSClient mainClient(SZSERVICENAME); // Global core instance. 40 42 bool 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 41 68 42 69 #define SETTINGS_GET_BOOL_CORE(key) \ … … 241 268 if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { 242 269 if (false) { 243 #ifdef WIN32244 270 } 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(); 289 273 } 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(); 323 276 } 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(); 344 279 } 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(); 365 282 } 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(); 374 285 } else if ( wcscasecmp( _T("encrypt"), argv[1]+1 ) == 0 ) { 375 286 g_bConsoleLog = true; … … 477 388 std::wcerr << "Service seems to be started, this is probably not a good idea..." << std::endl; 478 389 } 479 } catch ( const serviceControll::SCException& e) {480 e;// Empty by design390 } catch (...) { 391 // Empty by design 481 392 } 482 393 #endif 394 nsclient::simple_client client(&mainClient); 483 395 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(); 543 397 return 0; 544 398 } else { … … 565 419 return -1; 566 420 } 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; 570 427 } 571 428 return nRetCode; … … 573 430 574 431 void migrate() {} 432 433 std::list<std::wstring> NSClientT::list_commands() { 434 return commands_.list(); 435 } 575 436 576 437 NSClientT::plugin_info_list NSClientT::get_all_plugins() { … … 655 516 if (sarg == _T("migrate")) { 656 517 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>")); 658 519 return; 659 520 } … … 667 528 } else if (sarg == _T("generate")) { 668 529 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>")); 674 535 return; 675 536 } … … 728 589 } 729 590 } 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")); 735 596 } 736 597 … … 770 631 return false; 771 632 } 633 LOG_MESSAGE(_T("Got settings subsystem...")); 772 634 try { 773 635 if (debug_) … … 776 638 enable_shared_session_ = SETTINGS_GET_BOOL_CORE(settings_def::SHARED_SESSION); 777 639 } 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()); 779 641 } 780 642 … … 833 695 com_helper_.initialize(); 834 696 } 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()); 836 698 return false; 837 699 } catch (...) { 838 LOG_ERROR_ STD(_T("Unknown exception iniating COM..."));700 LOG_ERROR_CORE(_T("Unknown exception iniating COM...")); 839 701 return false; 840 702 } … … 856 718 loadPlugin(getBasePath() / boost::filesystem::wpath(_T("modules")) / boost::filesystem::wpath(*cit)); 857 719 } 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_); 859 721 //return false; 860 722 } 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())); 862 724 return false; 863 725 } catch (...) { 864 LOG_ERROR_ STD(_T("Unknown exception loading plugin: ") + (*cit));726 LOG_ERROR_CORE_STD(_T("Unknown exception loading plugin: ") + (*cit)); 865 727 return false; 866 728 } 867 729 } 868 730 } 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()); 870 732 } catch (...) { 871 LOG_ERROR_ STD(_T("Unknown exception when loading plugins"));733 LOG_ERROR_CORE_STD(_T("Unknown exception when loading plugins")); 872 734 return false; 873 735 } … … 875 737 loadPlugins(boot?NSCAPI::normalStart:NSCAPI::dontStart); 876 738 } catch (...) { 877 LOG_ERROR_ STD(_T("Unknown exception loading plugins"));739 LOG_ERROR_CORE_STD(_T("Unknown exception loading plugins")); 878 740 return false; 879 741 } … … 881 743 } 882 744 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 else898 LOG_ERROR_STD(_T("Failed to start tray helper:" ) + error::lookup::last_error());899 */900 745 return true; 901 746 } … … 940 785 mainClient.unloadPlugins(false); 941 786 } 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_); 943 788 } 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")); 945 790 } 946 791 } … … 950 795 com_helper_.unInitialize(); 951 796 } 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()); 953 798 } catch (...) { 954 LOG_ERROR_ STD(_T("Unknown exception uniniating COM..."));799 LOG_ERROR_CORE_STD(_T("Unknown exception uniniating COM...")); 955 800 } 956 801 #endif … … 985 830 // } 986 831 } 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")); 988 833 } 989 834 if (boot) { … … 992 837 mainClient.unloadPlugins(true); 993 838 } 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_); 995 840 } 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")); 997 842 } 998 843 } 999 844 LOG_MESSAGE_STD(_T("NSCLient++ - ") SZVERSION _T(" Stopped succcessfully")); 1000 845 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);1008 846 } 1009 847 #ifdef WIN32x … … 1056 894 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(5)); 1057 895 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.")); 1059 897 return -1; 1060 898 } … … 1070 908 return p->commandLineExec(command, argLen, args); 1071 909 } 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_); 1073 911 return -1; 1074 912 } … … 1085 923 LOG_MESSAGE_STD(_T("Module (") + e.file_ + _T(") was not found: ") + e.error_); 1086 924 } 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); 1088 926 return 0; 1089 927 } … … 1096 934 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 1097 935 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.")); 1099 937 return; 1100 938 } … … 1111 949 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 1112 950 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.")); 1114 952 return; 1115 953 } … … 1121 959 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1122 960 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.")); 1124 962 return; 1125 963 } … … 1136 974 } 1137 975 } 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_); 1139 977 } catch(...) { 1140 LOG_ERROR_ STD(_T("Unknown exception raised when unloading plugin"));978 LOG_ERROR_CORE_STD(_T("Unknown exception raised when unloading plugin")); 1141 979 } 1142 980 } … … 1145 983 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 1146 984 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.")); 1148 986 return; 1149 987 } … … 1158 996 } 1159 997 } 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_); 1161 999 } catch(...) { 1162 LOG_ERROR_ STD(_T("Unknown exception raised when unloading plugin"));1000 LOG_ERROR_CORE(_T("Unknown exception raised when unloading plugin")); 1163 1001 } 1164 1002 it++; … … 1172 1010 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1173 1011 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.")); 1175 1013 return; 1176 1014 } … … 1180 1018 if (!(*it)->load_plugin(NSCAPI::normalStart)) { 1181 1019 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()); 1183 1021 } 1184 1022 ++it; 1185 1023 } catch (NSPluginException e) { 1186 1024 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_); 1188 1026 } catch (...) { 1189 1027 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()); 1191 1029 } 1192 1030 } … … 1199 1037 } catch(NSPluginException e) { 1200 1038 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.")); 1202 1040 } catch(...) { 1203 1041 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")); 1205 1043 } 1206 1044 } … … 1224 1062 boost::unique_lock<boost::shared_mutex> writeLock(m_mutexRW, boost::get_system_time() + boost::posix_time::seconds(10)); 1225 1063 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.")); 1227 1065 return plugin; 1228 1066 } … … 1257 1095 return setting_keys::settings_def::PAYLOAD_LEN_DEFAULT; 1258 1096 } catch (...) { 1259 LOG_ERROR (_T("Failed to get length: :("));1097 LOG_ERROR_CORE(_T("Failed to get length: :(")); 1260 1098 return setting_keys::settings_def::PAYLOAD_LEN_DEFAULT; 1261 1099 } … … 1330 1168 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1331 1169 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.")); 1333 1171 return NSCAPI::returnUNKNOWN; 1334 1172 } … … 1338 1176 switch (c) { 1339 1177 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.")); 1341 1179 return c; 1342 1180 case NSCAPI::returnIgnored: … … 1350 1188 return c; 1351 1189 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); 1353 1191 return c; 1354 1192 } 1355 1193 } 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_); 1357 1195 return NSCAPI::returnCRIT; 1358 1196 } catch(...) { 1359 LOG_ERROR_ STD(_T("Unknown exception raised in module"));1197 LOG_ERROR_CORE(_T("Unknown exception raised in module")); 1360 1198 return NSCAPI::returnCRIT; 1361 1199 } … … 1369 1207 boost::shared_lock<boost::shared_mutex> readLock(m_mutexRW, boost::get_system_time() + boost::posix_time::milliseconds(5000)); 1370 1208 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.")); 1372 1210 return; 1373 1211 } … … 1380 1218 } 1381 1219 } 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_); 1383 1221 } 1384 1222 } … … 1507 1345 boost::unique_lock<boost::timed_mutex> lock(internalVariables, boost::get_system_time() + boost::posix_time::seconds(5)); 1508 1346 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.")); 1510 1348 return _T("FATAL ERROR"); 1511 1349 } … … 1526 1364 settings_manager::get_core()->set_base(basePath); 1527 1365 } 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()); 1529 1367 } catch (...) { 1530 LOG_ERROR_ STD(_T("Failed to set settings file"));1368 LOG_ERROR_CORE_STD(_T("Failed to set settings file")); 1531 1369 } 1532 1370 return basePath; … … 1562 1400 1563 1401 void 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 1408 NSClient* NSClientT::get_global_instance() { 1409 return &mainClient; 1410 } 1411 void 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 } 1421 void NSClientT::handle_shutdown() { 1422 exitCore(true); 1423 } 1424 #ifdef _WIN32 1425 void NSClientT::handle_session_change(unsigned long dwSessionId, bool logon) { 1426 1427 } 1428 #endif -
service/NSClient++.h
rb3078b4 r01a278b 33 33 34 34 //#include <nsclient_session.hpp> 35 36 37 class NSClientT; 38 typedef service_helper::impl<NSClientT>::system_service NSClient; 35 39 36 40 /** … … 117 121 bool plugins_loaded_; 118 122 bool enable_shared_session_; 119 120 123 nsclient::commands commands_; 121 124 … … 133 136 134 137 // Service helper functions 135 bool InitiateService();136 void TerminateService(void);137 138 bool initCore(bool boot); 138 139 bool exitCore(bool boot); … … 148 149 void nsclient_log_error(std::wstring file, int line, std::wstring error); 149 150 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 150 162 151 163 // Member functions 152 boost::filesystem::wpath getBasePath(void);164 boost::filesystem::wpath getBasePath(void); 153 165 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); 154 166 NSCAPI::nagiosReturn inject(std::wstring command, std::wstring arguments, wchar_t splitter, bool escape, std::wstring &msg, std::wstring & perf); … … 164 176 std::wstring describeCommand(std::wstring command); 165 177 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); 167 179 unsigned int getBufferLength(); 168 180 void HandleSettingsCLI(wchar_t* arg, int argc, wchar_t* argv[]); … … 173 185 void listPlugins(); 174 186 plugin_info_list get_all_plugins(); 187 std::list<std::wstring> list_commands(); 175 188 176 189 // Shared session interface: … … 194 207 }; 195 208 196 typedef service_helper::impl<NSClientT>::system_service NSClient;197 209 198 210 extern NSClient mainClient; // Global core instance forward declaration. … … 202 214 std::wstring Decrypt(std::wstring str, unsigned int algorithm = NSCAPI::encryption_xor); 203 215 204 //////////////////////////////////////////////////////////////////////////205 // Log macros to simplify logging206 // Generally names are of the form LOG_<severity>[_STD]207 // Where _STD indicates that strings are force wrapped inside a std::wstring208 //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)222 216 /* 223 217 #define LOG_DEBUG_STD(msg) -
service/StdAfx.cpp
rf0eb62d r01a278b 19 19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 20 20 ***************************************************************************/ 21 #include " stdafx.h"21 #include "StdAfx.h" 22 22 #define COMPILE_NEWAPIS_STUBS -
service/commands.hpp
rb3078b4 r01a278b 10 10 public: 11 11 class command_exception : public std::exception { 12 std::string what_; 12 13 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 } 15 21 16 22 }; -
service/core_api.cpp
rd05c3f0 r01a278b 29 29 #include <b64/b64.h> 30 30 #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) 32 49 33 50 … … 250 267 return NSCAPI::isSuccess; 251 268 } 252 NSCAPI::errorReturn NSAPIRegisterCommand(const wchar_t* cmd,const wchar_t* desc) { 253 mainClient.registerCommand(cmd, desc); 269 NSCAPI::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 } 254 277 return NSCAPI::isSuccess; 255 278 } -
service/core_api.h
r818b54e r01a278b 52 52 NSCAPI::errorReturn NSAPIGetAllCommandNames(wchar_t***, unsigned int *); 53 53 NSCAPI::errorReturn NSAPIReleaseAllCommandNamessBuffer(wchar_t***, unsigned int *); 54 NSCAPI::errorReturn NSAPIRegisterCommand( const wchar_t*,const wchar_t*);54 NSCAPI::errorReturn NSAPIRegisterCommand(unsigned int, const wchar_t*,const wchar_t*); 55 55 NSCAPI::errorReturn NSAPISettingsRegKey(const wchar_t*, const wchar_t*, int, const wchar_t*, const wchar_t*, const wchar_t*, int); 56 56 NSCAPI::errorReturn NSAPISettingsRegPath(const wchar_t*, const wchar_t*, const wchar_t*, int);
Note: See TracChangeset
for help on using the changeset viewer.








