Changeset 2d69ab6 in nscp
- Timestamp:
- 03/26/11 21:10:52 (2 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- c391984
- Parents:
- 7443b58
- Files:
-
- 13 edited
-
helpers/installer-dlls/main_dll/main_dll.cpp (modified) (10 diffs)
-
helpers/installers/installer/Product.wxs (modified) (2 diffs)
-
include/ServiceCmd.cpp (modified) (4 diffs)
-
include/ServiceCmd.h (modified) (1 diff)
-
include/service/win32_service.hpp (modified) (3 diffs)
-
modules/CheckNSCP/CheckNSCP.h (modified) (1 diff)
-
service/NSClient++.cpp (modified) (15 diffs)
-
service/NSClient++.h (modified) (5 diffs)
-
service/cli_parser.hpp (modified) (4 diffs)
-
service/core_api.cpp (modified) (2 diffs)
-
service/logger.hpp (modified) (2 diffs)
-
service/service_manager.hpp (modified) (6 diffs)
-
service/simple_client.hpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
helpers/installer-dlls/main_dll/main_dll.cpp
r197b263 r2d69ab6 13 13 14 14 LPCWSTR vcsServiceQuery = 15 L"SELECT `ShortName`, `LongName`, `Description`, `Program`, ` Attributes`, `Component_` FROM `Services`";16 enum eServiceQuery { feqShortName = 1, feqLongName, feqDesc, feqProgram, feq Attributes, feqComponent };15 L"SELECT `ShortName`, `LongName`, `Description`, `Program`, `Dependencies`, `Attributes`, `Component_` FROM `Services`"; 16 enum eServiceQuery { feqShortName = 1, feqLongName, feqDesc, feqProgram, feqDeps, feqAttributes, feqComponent }; 17 17 enum eFirewallExceptionAttributes { feaIgnoreFailures = 1 }; 18 18 19 bool install(msi_helper &h, std::wstring exe, std::wstring service_short_name, std::wstring service_long_name, std::wstring service_description );19 bool install(msi_helper &h, std::wstring exe, std::wstring service_short_name, std::wstring service_long_name, std::wstring service_description, std::wstring service_deps); 20 20 bool uninstall(msi_helper &h, std::wstring service_name); 21 21 UINT SchedServiceMgmt(__in MSIHANDLE hInstall, msi_helper::WCA_TODO todoSched); … … 53 53 std::wstring longname = data.get_next_string(); 54 54 std::wstring desc = data.get_next_string(); 55 std::wstring deps = data.get_next_string(); 55 56 int attr = data.get_next_int(); 56 57 BOOL fIgnoreFailures = feaIgnoreFailures == (attr & feaIgnoreFailures); … … 60 61 case msi_helper::WCA_TODO_REINSTALL: 61 62 h.logMessage(_T("Installing service install: ") + shortname + _T(", ") + file); 62 install(h, file, shortname, longname, desc );63 install(h, file, shortname, longname, desc, deps); 63 64 break; 64 65 … … 109 110 std::wstring longname = h.get_record_formatted_string(hRec, feqLongName); 110 111 std::wstring desc = h.get_record_formatted_string(hRec, feqDesc); 112 std::wstring deps = h.get_record_formatted_string(hRec, feqDeps); 111 113 std::wstring program = h.get_record_formatted_string(hRec, feqProgram); 112 114 int attributes = h.get_record_integer(hRec, feqAttributes); … … 127 129 custom_data.write_string(longname); 128 130 custom_data.write_string(desc); 131 custom_data.write_string(deps); 129 132 custom_data.write_int(attributes); 130 133 //custom_data.write_int(fetApplication); … … 274 277 275 278 276 bool install(msi_helper &h, std::wstring exe, std::wstring service_short_name, std::wstring service_long_name, std::wstring service_desc ription) {279 bool install(msi_helper &h, std::wstring exe, std::wstring service_short_name, std::wstring service_long_name, std::wstring service_desc, std::wstring service_deps) { 277 280 h.updateProgress(_T("Preparing to install service"), service_short_name); 278 281 try { … … 286 289 } 287 290 h.updateProgress(_T("Installing service"), service_short_name); 288 serviceControll::Install(service_short_name, service_long_name, SZDEPENDENCIES, SERVICE_WIN32_OWN_PROCESS, exe);291 serviceControll::Install(service_short_name, service_long_name, service_deps, SERVICE_WIN32_OWN_PROCESS, exe); 289 292 } catch (const serviceControll::SCException& e) { 290 293 h.errorMessage(_T("Failed to install service: ") + e.error_); … … 292 295 } 293 296 try { 294 serviceControll::SetDescription(service_short_name, service_desc ription);297 serviceControll::SetDescription(service_short_name, service_desc); 295 298 } catch (const serviceControll::SCException& e) { 296 299 h.errorMessage(_T("Failed to set description of service: ") + e.error_); … … 453 456 454 457 455 bool start(msi_helper &h, std::wstring service_name = _T("")) { 456 if (service_name.empty()) 457 service_name = SZSERVICENAME; 458 bool start(msi_helper &h, std::wstring service_name) { 458 459 if (!serviceControll::isInstalled(service_name)) { 459 460 h.logMessage(_T("Service was not installed (so we cannot start it): ")+ service_name); … … 474 475 } 475 476 476 bool stop(msi_helper &h, std::wstring service_name = _T("")) { 477 if (service_name.empty()) 478 service_name = SZSERVICENAME; 477 bool stop(msi_helper &h, std::wstring service_name) { 479 478 h.updateProgress(_T("Preparing to stopp service"), service_name); 480 479 try { -
helpers/installers/installer/Product.wxs
r197b263 r2d69ab6 266 266 <Column Id="Program" Category="Formatted" PrimaryKey="no" Type="string" Width="255" Modularize="Property" Nullable="yes" 267 267 Description="Exception for a program (formatted path name)."/> 268 <Column Id="Dependencies" Category="Formatted" PrimaryKey="no" Type="string" Width="255" Modularize="Property" Nullable="yes" 269 Description="Service Dependencies"/> 268 270 <Column Id="Attributes" Category="Integer" PrimaryKey="no" Type="int" Width="4" Nullable="yes" MinValue="0" MaxValue="65536" 269 271 Description="Vital=1"/> … … 277 279 <Data Column="Component_">ServiceRegistration</Data> 278 280 <Data Column="Program">[#NSClientEXE]</Data> 281 <Data Column="Dependencies"></Data> 279 282 </Row> 280 283 </CustomTable> -
include/ServiceCmd.cpp
rb1ac2fa r2d69ab6 36 36 * 37 37 */ 38 void Install(std::wstring szName, std::wstring szDisplayName, LPCTSTR szDependencies, DWORD dwServiceType, std::wstring exe) {38 void Install(std::wstring szName, std::wstring szDisplayName, std::wstring szDependencies, DWORD dwServiceType, std::wstring args, std::wstring exe) { 39 39 SC_HANDLE schService; 40 40 SC_HANDLE schSCManager; … … 47 47 } 48 48 49 std::wstring bin = _T("\"") + exe + _T("\""); 50 if (!args.empty()) 51 bin += _T(" ") + args; 49 52 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); 50 53 if (!schSCManager) … … 58 61 SERVICE_AUTO_START, // start type 59 62 SERVICE_ERROR_NORMAL, // error control type 60 exe.c_str(),// service's binary63 bin.c_str(), // service's binary 61 64 NULL, // no load ordering group 62 65 NULL, // no tag identifier 63 szDependencies ,// dependencies66 szDependencies.c_str(), // dependencies 64 67 NULL, // LocalSystem account 65 68 NULL); // no password … … 73 76 throw SCException(_T("Unable to install service.") + error::lookup::last_error(err)); 74 77 } 75 std::wcout << _T("Service ") << szName << _T(" installed...") << std::endl;;78 std::wcout << _T("Service ") << szName << _T(" (") << bin << _T(") installed...") << std::endl;; 76 79 CloseServiceHandle(schService); 77 80 CloseServiceHandle(schSCManager); -
include/ServiceCmd.h
rb1ac2fa r2d69ab6 36 36 } 37 37 }; 38 void Install(std::wstring,std::wstring, LPCTSTR,DWORD=SERVICE_WIN32_OWN_PROCESS, std::wstring exe=_T(""));38 void Install(std::wstring,std::wstring,std::wstring,DWORD=SERVICE_WIN32_OWN_PROCESS, std::wstring args = _T(""), std::wstring exe=_T("")); 39 39 void ModifyServiceType(LPCTSTR szName, DWORD dwServiceType); 40 40 void Uninstall(std::wstring); -
include/service/win32_service.hpp
r3080680 r2d69ab6 113 113 wchar_t *serviceName_; 114 114 public: 115 win32_service(std::wstring name) : dispatchTable(NULL), name_(name), dwControlsAccepted(SERVICE_ACCEPT_STOP) { 115 win32_service() : dispatchTable(NULL), serviceName_(NULL), name_(), dwControlsAccepted(SERVICE_ACCEPT_STOP) { 116 } 117 virtual ~win32_service() { 118 delete [] dispatchTable; 119 delete [] serviceName_; 120 } 121 122 void create_dispatch_table(std::wstring name) { 116 123 serviceName_ = new wchar_t[name.length()+2]; 117 124 wcsncpy(serviceName_, name.c_str(), name.length()); … … 122 129 dispatchTable[1].lpServiceProc = NULL; 123 130 } 124 virtual ~win32_service() { 125 delete [] dispatchTable; 126 delete [] serviceName_; 127 } 128 129 void start_and_wait() { 131 132 void start_and_wait(std::wstring name) { 133 name_ = name; 134 print_debug(_T("Starting: ") + name); 135 create_dispatch_table(name); 130 136 StartServiceCtrlDispatcher(); 131 137 } … … 312 318 } 313 319 314 TBase::handle_startup( );320 TBase::handle_startup(name_); 315 321 316 322 stop_mutex_.lock(); 317 323 318 324 319 print_debug(_T("Shutting down ..."));320 TBase::handle_shutdown( );325 print_debug(_T("Shutting down: ") + name_); 326 TBase::handle_shutdown(name_); 321 327 } 322 328 -
modules/CheckNSCP/CheckNSCP.h
r7443b58 r2d69ab6 45 45 } 46 46 std::wstring getModuleDescription() { 47 return _T("Checkes the state of " SZAPPNAME);47 return _T("Checkes the state of the agent"); 48 48 } 49 49 -
service/NSClient++.cpp
rc760fc9 r2d69ab6 50 50 #endif 51 51 52 NSClient mainClient (SZSERVICENAME); // Global core instance.52 NSClient mainClient; // Global core instance. 53 53 54 54 #define LOG_CRITICAL_CORE(msg) { std::string s = nsclient::logger_helper::create_error(__FILE__, __LINE__, msg); mainClient.reportMessage(s); } … … 241 241 cli_parser parser(&mainClient); 242 242 243 if (argc > 0) { 244 LOG_INFO_CORE(_T("Got arguments on command line")); 245 for (int i=0;i<argc;i++) { 246 LOG_INFO_CORE((std::wstring)_T("arg: ") + argv[i]); 247 } 248 } else { 249 LOG_INFO_CORE(_T("Got NO arguments on command line")); 250 } 243 251 parser.parse(argc, argv); 244 252 … … 248 256 if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { 249 257 if (false) { 250 } if ( wcscasecmp( _T("install"), argv[1]+1 ) == 0 ) {251 nsclient::client::service_manager mgr(argc-1,&argv[1]);252 return mgr.install();253 } else if ( wcscasecmp( _T("uninstall"), argv[1]+1 ) == 0 ) {254 nsclient::client::service_manager mgr(argc-1,&argv[1]);255 return mgr.uninstall();256 } else if ( wcscasecmp( _T("start"), argv[1]+1 ) == 0 ) {257 nsclient::client::service_manager mgr(argc-1,&argv[1]);258 return mgr.start();259 } else if ( wcscasecmp( _T("stop"), argv[1]+1 ) == 0 ) {260 nsclient::client::service_manager mgr(argc-1,&argv[1]);261 return mgr.stop();262 } else if ( wcscasecmp( _T("svc"), argv[1]+1 ) == 0 ) {263 nsclient::client::service_manager mgr(argc-1,&argv[1]);264 return mgr.print_command();265 258 } else if ( wcscasecmp( _T("encrypt"), argv[1]+1 ) == 0 ) { 266 259 std::wstring password; … … 281 274 try { 282 275 unsigned int next_plugin_id = 0; 283 LOG_INFO_CORE( SZAPPNAME _T(" (C) Michael Medin - michael<at>medin<dot>name"));284 LOG_INFO_CORE(_T("Version: ") SZVERSION);276 LOG_INFO_CORE(APPLICATION_NAME _T(" (C) Michael Medin - michael<at>medin<dot>name")); 277 LOG_INFO_CORE(_T("Version: ") CURRENT_SERVICE_VERSION); 285 278 LOG_INFO_CORE(_T("Architecture: ") SZARCH); 286 279 … … 325 318 return false; 326 319 } else if ( wcscasecmp( _T("version"), argv[1]+1 ) == 0 ) { 327 LOG_INFO_CORE( SZAPPNAME _T(" Version: ") SZVERSION _T(", Plattform: ") SZARCH);320 LOG_INFO_CORE(APPLICATION_NAME _T(", Version: ") CURRENT_SERVICE_VERSION _T(", Platform: ") SZARCH); 328 321 } else if ( wcscasecmp( _T("d"), argv[1]+1 ) == 0 ) { 329 322 // Run command from command line (like NRPE) but with debug enabled … … 352 345 mainClient.exitCore(true); 353 346 return nRetCode; 354 } else if ( wcscasecmp( _T("test"), argv[1]+1 ) == 0 ) {355 bool server = false;356 if (argc > 2 && wcscasecmp( _T("server"), argv[2] ) == 0 ) {357 server = true;358 }359 std::wcout << "Launching test mode - " << (server?_T("server mode"):_T("client mode")) << std::endl;360 LOG_INFO_CORE(_T("Booting: ") SZSERVICEDISPLAYNAME );361 #ifdef WIN32362 try {363 if (serviceControll::isStarted(SZSERVICENAME)) {364 std::wcerr << "Service seems to be started, this is probably not a good idea..." << std::endl;365 }366 } catch (...) {367 // Empty by design368 }369 #endif370 nsclient::simple_client client(&mainClient);371 client.start();372 return 0;373 347 } else { 374 348 std::wcerr << _T("Usage: -version, -about, -install, -uninstall, -start, -stop, -encrypt -settings") << std::endl; … … 386 360 mainClient.exitCore(true); 387 361 return nRetCode; 388 } else if (argc > 1) {389 mainClient.enableDebug(true);390 std::wcerr << _T("Invalid command line argument: ") << argv[1] << std::endl;391 std::wcerr << _T("Usage: -version, -about, -install, -uninstall, -start, -stop, -encrypt") << std::endl;392 std::wcerr << _T("Usage: [-noboot] <ModuleName> <commnd> [arguments]") << std::endl;393 return -1;394 }395 try {396 std::wcout << _T("Running as service...") << std::endl;397 mainClient.enableDebug(true);398 mainClient.start_and_wait();399 } catch (...) {400 std::wcerr << _T("Unknown exception in service") << std::endl;401 362 } 402 363 return nRetCode; … … 554 515 */ 555 516 bool NSClientT::initCore(bool boot) { 556 LOG_INFO_CORE(_T("Attempting to start NSCLient++ - ") SZVERSION);517 LOG_INFO_CORE(_T("Attempting to start")); 557 518 558 519 if (!settings_manager::init_settings(context_)) { … … 595 556 _T("RESTART"), _T("Submit crash reports to nsclient.org (or your configured submission server)")) 596 557 597 (_T("restart target"), sh::wstring_key(&crash_target, SZSERVICENAME),558 (_T("restart target"), sh::wstring_key(&crash_target, get_service_control().get_service_name()), 598 559 _T("RESTART SERVICE NAME"), _T("The url to submit crash reports to")) 599 560 … … 617 578 g_exception_manager = new ExceptionManager(false); 618 579 619 g_exception_manager->setup_app(to_wstring( SZSERVICENAME), to_wstring(STRPRODUCTVER), to_wstring(STRPRODUCTDATE));580 g_exception_manager->setup_app(to_wstring(APPLICATION_NAME), to_wstring(STRPRODUCTVER), to_wstring(STRPRODUCTDATE)); 620 581 621 582 if (crash_restart) { … … 728 689 return false; 729 690 } 730 LOG_DEBUG_CORE_STD( _T("NSCLient++ - ") SZVERSION _T(" Started!"));691 LOG_DEBUG_CORE_STD(APPLICATION_NAME _T(" - ") CURRENT_SERVICE_VERSION _T(" Started!")); 731 692 return true; 732 693 } … … 764 725 765 726 bool NSClientT::exitCore(bool boot) { 766 LOG_DEBUG_CORE(_T("Attempting to stop NSCLient++ - ") SZVERSION);727 LOG_DEBUG_CORE(_T("Attempting to stop")); 767 728 if (boot) { 768 729 try { … … 827 788 } 828 789 } 829 LOG_INFO_CORE(_T(" NSCLient++ - ") SZVERSION _T("Stopped succcessfully"));790 LOG_INFO_CORE(_T("Stopped succcessfully")); 830 791 logger_master_.stop_slave(); 831 792 return true; 832 793 } 833 #ifdef WIN32x834 /**835 * Forward this to the main service dispatcher helper class836 * @param dwArgc837 * @param *lpszArgv838 */839 void WINAPI NSClientT::service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv) {840 try {841 //WTF!!! mainClient.service_main(dwArgc, lpszArgv);842 } catch (service_helper::service_exception e) {843 LOG_ERROR_STD(_T("Unknown service error: ") + e.what());844 } catch (...) {845 LOG_ERROR_STD(_T("Unknown service error!"));846 }847 }848 DWORD WINAPI NSClientT::service_ctrl_dispatch_ex(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) {849 return mainClient.service_ctrl_ex(dwControl, dwEventType, lpEventData, lpContext);850 }851 /**852 * Forward this to the main service dispatcher helper class853 * @param dwCtrlCode854 */855 void WINAPI NSClientT::service_ctrl_dispatch(unsigned long dwCtrlCode) {856 mainClient.service_ctrl_ex(dwCtrlCode, NULL, NULL, NULL);857 }858 #endif859 860 794 void NSClientT::service_on_session_changed(unsigned long dwSessionId, bool logon, unsigned long dwEventType) { 861 795 // if (shared_server_.get() == NULL) { … … 1321 1255 return &mainClient; 1322 1256 } 1323 void NSClientT::handle_startup() { 1257 void NSClientT::handle_startup(std::wstring service_name) { 1258 service_name_ = service_name; 1324 1259 initCore(true); 1325 1260 /* … … 1331 1266 */ 1332 1267 } 1333 void NSClientT::handle_shutdown( ) {1268 void NSClientT::handle_shutdown(std::wstring service_name) { 1334 1269 exitCore(true); 1270 } 1271 1272 NSClientT::service_controller NSClientT::get_service_control() { 1273 return service_controller(service_name_); 1274 } 1275 1276 void NSClientT::service_controller::stop() { 1277 #ifdef WIN32 1278 serviceControll::StopNoWait(get_service_name()); 1279 #endif 1280 } 1281 void NSClientT::service_controller::start() { 1282 #ifdef WIN32 1283 serviceControll::Start(get_service_name()); 1284 #endif 1285 } 1286 bool NSClientT::service_controller::is_started() { 1287 #ifdef WIN32 1288 try { 1289 if (serviceControll::isStarted(get_service_name())) { 1290 return true; 1291 } 1292 } catch (...) { 1293 return false; 1294 } 1295 #endif 1296 return false; 1335 1297 } 1336 1298 -
service/NSClient++.h
rc760fc9 r2d69ab6 111 111 nsclient::channels channels_; 112 112 unsigned int next_plugin_id_; 113 std::wstring service_name_; 113 114 114 115 … … 116 117 typedef std::multimap<std::wstring,std::wstring> plugin_alias_list_type; 117 118 // c-tor, d-tor 118 NSClientT(void) : debug_(log_unknown), enable_shared_session_(false), commands_(this), channels_(this), next_plugin_id_(0) {119 NSClientT(void) : debug_(log_unknown), enable_shared_session_(false), commands_(this), channels_(this), next_plugin_id_(0), service_name_(DEFAULT_SERVICE_NAME) { 119 120 logger_master_.start_slave(); 120 121 } … … 148 149 reportMessage(s.c_str()); 149 150 } 150 void handle_startup( );151 void handle_shutdown( );151 void handle_startup(std::wstring service_name); 152 void handle_shutdown(std::wstring service_name); 152 153 #ifdef _WIN32 153 154 void handle_session_change(unsigned long dwSessionId, bool logon); … … 165 166 int commandLineExec(const wchar_t* module, const unsigned int argLen, wchar_t** args); 166 167 168 struct service_controller { 169 std::wstring service; 170 service_controller(std::wstring service) : service(service) {} 171 service_controller(const service_controller & other) : service(other.service) {} 172 service_controller& operator=(const service_controller & other) { 173 service = other.service; 174 return *this; 175 } 176 void stop(); 177 void start(); 178 std::wstring get_service_name() { 179 return service; 180 } 181 bool is_started(); 182 }; 183 184 service_controller get_service_control(); 185 167 186 //plugin_type loadPlugin(const boost::filesystem::wpath plugin, std::wstring alias); 168 187 void loadPlugins(NSCAPI::moduleLoadMode mode); … … 190 209 return 0; // TODO: Readd this!!! inject(command, arguments, splitter, escape, msg, perf); 191 210 } 192 std::pair<std::wstring,std::wstring> session_get_name() {193 return std::pair<std::wstring,std::wstring>(SZAPPNAME,SZVERSION);194 }211 // std::pair<std::wstring,std::wstring> session_get_name() { 212 // return std::pair<std::wstring,std::wstring>(SZAPPNAME,SZVERSION); 213 // } 195 214 196 215 std::wstring expand_path(std::wstring file); -
service/cli_parser.hpp
r1ecd26f r2d69ab6 44 44 45 45 service.add_options() 46 ("install", po::value<std::wstring>(), "Install service") 46 ("install", "Install service") 47 ("uninstall", "Uninstall service") 48 ("start", "Start service") 49 ("stop", "Stop service") 50 ("info", "Show information about service") 51 ("run", "Run as a service") 52 ("name", po::value<std::wstring>(), "Name of service") 53 ("description", po::value<std::wstring>()->default_value(_T("")), "Description of service") 47 54 ; 48 55 … … 70 77 mainClient.set_console_log(); 71 78 return parse_settings(argc, argv); 79 } 80 if (vm.count("service")) { 81 //mainClient.set_console_log(); 82 return parse_service(argc, argv); 72 83 } 73 84 if (vm.count("test")) { … … 107 118 // std::wcout << "Launching test mode - " << (server?_T("server mode"):_T("client mode")) << std::endl; 108 119 // LOG_MESSAGE_STD(_T("Booting: ") SZSERVICEDISPLAYNAME ); 109 #ifdef WIN32110 try {111 if (serviceControll::isStarted(SZSERVICENAME)) {112 std::wcerr << "Service seems to be started, this is probably not a good idea..." << std::endl;113 }114 } catch (...) {115 // Empty by design116 }117 #endif118 120 nsclient::simple_client client(core_); 119 121 client.start(); … … 175 177 } 176 178 179 180 int parse_service(int argc, wchar_t* argv[]) { 181 try { 182 po::options_description all("Allowed options (settings)"); 183 all.add(desc).add(service); 184 185 po::variables_map vm; 186 po::store(po::parse_command_line(argc, argv, all), vm); 187 po::notify(vm); 188 189 if (vm.count("help")) { 190 std::cout << all << "\n"; 191 return 1; 192 } 193 bool debug = false; 194 if (vm.count("debug")) { 195 std::wcout << _T("----"); 196 debug = true; 197 } 198 std::wstring name; 199 if (vm.count("name")) { 200 name = vm["name"].as<std::wstring>(); 201 } else { 202 std::wcout << _T("TODO retrieve name from service here") << std::endl; 203 } 204 std::wstring desc; 205 if (vm.count("description")) { 206 desc = vm["description"].as<std::wstring>(); 207 } else { 208 std::wcout << _T("TODO retrieve name from service here") << std::endl; 209 } 210 if (debug) { 211 std::wcout << _T("Service name: ") << name << std::endl; 212 std::wcout << _T("Service description: ") << desc << std::endl; 213 } 214 215 std::wstringstream ss; 216 ss << _T("run: ") << vm.count("run"); 217 ss << _T(", name: ") << vm.count("name"); 218 ss << _T(", info: ") << vm.count("info"); 219 220 std::wstring s = ss.str(); 221 OutputDebugString(s.c_str()); 222 std::wcout << s << std::endl; 223 224 if (vm.count("run")) { 225 try { 226 mainClient.enableDebug(true); 227 mainClient.start_and_wait(name); 228 } catch (...) { 229 std::wcerr << _T("Unknown exception in service") << std::endl; 230 } 231 } else { 232 mainClient.set_console_log(); 233 nsclient::client::service_manager service_manager(name); 234 235 if (vm.count("install")) { 236 service_manager.install(desc); 237 } else if (vm.count("uninstall")) { 238 service_manager.uninstall(); 239 } else if (vm.count("start")) { 240 service_manager.start(); 241 } else if (vm.count("stop")) { 242 service_manager.stop(); 243 } else if (vm.count("info")) { 244 service_manager.info(); 245 } else { 246 std::cerr << "Missing argument" << std::endl; 247 return 1; 248 } 249 } 250 return 0; 251 } catch(std::exception & e) { 252 std::cerr << "Unable to parse command line (settings): " << e.what() << std::endl; 253 return 1; 254 } 255 } 177 256 }; 178 257 -
service/core_api.cpp
rc760fc9 r2d69ab6 78 78 } 79 79 NSCAPI::errorReturn NSAPIGetApplicationName(wchar_t*buffer, unsigned int bufLen) { 80 return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, SZAPPNAME, NSCAPI::isSuccess);80 return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, APPLICATION_NAME, NSCAPI::isSuccess); 81 81 } 82 82 NSCAPI::errorReturn NSAPIGetApplicationVersionStr(wchar_t*buffer, unsigned int bufLen) { 83 return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, SZVERSION, NSCAPI::isSuccess);83 return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, CURRENT_SERVICE_VERSION, NSCAPI::isSuccess); 84 84 } 85 85 void NSAPIMessage(const char* data, unsigned int count) { … … 87 87 } 88 88 void NSAPIStopServer(void) { 89 #ifdef WIN32 90 serviceControll::StopNoWait(SZSERVICENAME); 91 #endif 89 mainClient.get_service_control().stop(); 92 90 } 93 91 NSCAPI::nagiosReturn NSAPIInject(const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) { -
service/logger.hpp
r7e54a5f r2d69ab6 281 281 282 282 void log(std::string data) { 283 OutputDebugString(strEx::string_to_wstring(strEx::strip_hex(data)).c_str()); 283 284 if (!mq_) { 284 285 log_fatal_error("Failed to send to logging queue: " + data); … … 297 298 298 299 void log_fatal_error(std::string message) { 300 #ifdef WIN32 301 OutputDebugString(strEx::string_to_wstring(message).c_str()); 302 #endif 299 303 std::cout << "TODO: " << message << std::endl; 300 304 } -
service/service_manager.hpp
r858ecc1 r2d69ab6 9 9 namespace client { 10 10 #ifdef _WIN32 11 namespace po = boost::program_options;12 11 class service_manager { 13 12 private: 14 bool output_gui_;15 int argc_;16 wchar_t** argv_;17 13 std::wstring service_name_; 18 po::options_description desc_;19 po::variables_map vm_;20 14 21 15 public: 22 service_manager(int argc, wchar_t* argv[]) : argc_(argc), argv_(argv), output_gui_(false), desc_("Allowed options") {} 23 private: 16 service_manager(std::wstring service_name) : service_name_(service_name) {} 24 17 25 void add_global_options() { 26 desc_.add_options() 27 ("help", "produce help message") 28 ("gui", po::value<bool>(&output_gui_)->zero_tokens()->default_value(false), "show message in the GUI") 29 ("name", po::value<std::wstring>(&service_name_)->default_value(SZSERVICENAME), "service name to use") 30 ; 18 static std::wstring get_default_service_name() { 19 return DEFAULT_SERVICE_NAME; 31 20 } 32 33 void print_error(std::wstring message) { 34 if (output_gui_) 35 ::MessageBox(NULL, message.c_str(), _T("NSClient++ Error:"), MB_OK|MB_ICONERROR); 36 else 37 std::wcerr << message << std::endl; 21 static std::wstring get_default_service_desc() { 22 return DEFAULT_SERVICE_DESC; 38 23 } 39 void print_msg(std::wstring title, std::wstring message) { 40 if (output_gui_) 41 ::MessageBox(NULL, message.c_str(), title.c_str(), MB_OK|MB_ICONINFORMATION); 42 else 43 std::wcerr << title << _T(": ") << message << std::endl; 24 static std::wstring get_default_service_deps() { 25 return DEFAULT_SERVICE_DEPS; 44 26 } 45 46 boolean parse() { 27 static std::wstring get_default_arguments() { 28 return _T("--service --run"); 29 } 30 inline void print_msg(std::wstring str) { 31 std::wcout << str << std::endl; 32 } 33 inline void print_error(std::wstring str) { 34 std::wcerr << _T("ERROR: ") << str << std::endl; 35 } 36 public: 37 int install(std::wstring service_description) { 47 38 try { 48 po::store(po::parse_command_line(argc_, argv_, desc_), vm_); 49 po::notify(vm_); 50 return true; 51 } catch (po::unknown_option &e) { 52 print_error(_T("Failed to parse: ") + to_wstring(e.what())); 53 return false; 54 } catch (std::exception &e) { 55 print_error(_T("Failed to parse: ") + to_wstring(e.what())); 56 return false; 57 } catch (...) { 58 print_error(_T("Failed to parse: <UNKNOWN EXCEPTION>")); 59 return false; 60 } 61 } 62 63 public: 64 int install() { 65 bool bStart = false; 66 std::wstring service_description; 67 68 add_global_options(); 69 desc_.add_options() 70 ("start", po::value<bool>(&bStart)->zero_tokens()->default_value(false), "start the service") 71 ("description", po::value<std::wstring>(&service_description)->default_value(SZSERVICEDISPLAYNAME), "service description to use") 72 ; 73 if (!parse()) 74 return -1; 75 76 try { 77 serviceControll::Install(service_name_, service_description, SZDEPENDENCIES); 78 if (bStart) 79 serviceControll::Start(service_name_); 39 std::wstring args = get_default_arguments(); 40 if (service_name_ != get_default_service_name()) 41 args += _T(" --name ") + service_name_; 42 serviceControll::Install(service_name_, service_description, get_default_service_deps(), SERVICE_WIN32_OWN_PROCESS, args); 80 43 } catch (const serviceControll::SCException& e) { 81 44 print_error(_T("Service installation failed: ") + e.error_); … … 87 50 print_error(_T("Couldn't set service description: ") + e.error_); 88 51 } 89 print_msg(_T("Service installed "), _T("Service installedsuccessfully!"));52 print_msg(_T("Service installed successfully!")); 90 53 return 0; 91 54 } 92 55 int uninstall() { 93 bool bStop = false;94 add_global_options();95 desc_.add_options() ("stop", po::value<bool>(&bStop)->zero_tokens()->default_value(false), "stop the service") ;96 if (!parse())97 return -1;98 99 try {100 if (bStop)101 serviceControll::Stop(service_name_);102 } catch (const serviceControll::SCException& e) {103 print_error(_T("Failed to stop service (") + service_name_ + _T(") failed; ") + e.error_);104 }105 56 try { 106 57 serviceControll::Uninstall(service_name_); … … 109 60 return 0; 110 61 } 111 print_msg(_T("Service uninstalled "), _T("Service uninstalledsuccessfully!"));62 print_msg(_T("Service uninstalled successfully!")); 112 63 return 0; 113 64 } 114 65 int start() { 115 add_global_options();116 if (!parse())117 return -1;118 119 66 try { 120 67 serviceControll::Start(service_name_); … … 126 73 } 127 74 int stop() { 128 add_global_options();129 if (!parse())130 return -1;131 132 75 try { 133 76 serviceControll::Stop(service_name_); … … 138 81 return 0; 139 82 } 140 int print_command() { 141 add_global_options(); 142 if (!parse()) 143 return -1; 144 83 int info() { 145 84 try { 146 85 std::wstring exe = serviceControll::get_exe_path(service_name_); … … 173 112 return unsupported(); 174 113 } 175 int print_command() {114 int info() { 176 115 return unsupported(); 177 116 } -
service/simple_client.hpp
rc760fc9 r2d69ab6 14 14 return; 15 15 } 16 17 if (core_->get_service_control().is_started()) 18 std::wcerr << "Service seems to be started (Sockets and such will probably not work)..." << std::endl; 19 16 20 //std::wcout << _T("Using settings from: ") << settings_manager::get_core()->get_settings_type_desc() << std::endl; 17 21 std::wcout << _T("Enter command to inject or exit to terminate...") << std::endl;
Note: See TracChangeset
for help on using the changeset viewer.








