diff options
author | Jose <jose@zeroc.com> | 2012-08-15 15:53:28 +0200 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2012-08-15 15:53:28 +0200 |
commit | 8602ff6e888c4349f1c2f84c4b1d6936bc14f9fc (patch) | |
tree | 22ce0a5cf2cd8bc4d2e90885bad52b3cc9151739 | |
parent | Minor fix for WinRT builds (diff) | |
download | ice-8602ff6e888c4349f1c2f84c4b1d6936bc14f9fc.tar.bz2 ice-8602ff6e888c4349f1c2f84c4b1d6936bc14f9fc.tar.xz ice-8602ff6e888c4349f1c2f84c4b1d6936bc14f9fc.zip |
ICE-4820 - Service class public API
-rw-r--r-- | cpp/include/Ice/Service.h | 26 | ||||
-rw-r--r-- | cpp/src/Ice/Service.cpp | 442 |
2 files changed, 0 insertions, 468 deletions
diff --git a/cpp/include/Ice/Service.h b/cpp/include/Ice/Service.h index ead0f5e4e7d..c124d371e1e 100644 --- a/cpp/include/Ice/Service.h +++ b/cpp/include/Ice/Service.h @@ -45,10 +45,6 @@ public: // // Win32: // - // --install NAME [--display DISP] [--executable EXEC] - // --uninstall NAME - // --start NAME [args ...] - // --stop NAME // --service NAME // // Unix: @@ -130,28 +126,6 @@ public: // void configureService(const std::string&); - // - // Installs a Win32 service. - // - int installService(bool, const Ice::StringConverterPtr&, const std::string&, const std::string&, const std::string&, - const std::vector<std::string>&); - - // - // Uninstalls a Win32 service. - // - int uninstallService(bool, const std::string&); - - // - // Starts a Win32 service. The argument vector is passed to the - // service at startup. - // - int startService(const std::string&, const std::vector<std::string>&); - - // - // Stops a running Win32 service. - // - int stopService(const std::string&); - static void setModuleHandle(HMODULE); #else diff --git a/cpp/src/Ice/Service.cpp b/cpp/src/Ice/Service.cpp index f541593cb61..722a729229d 100644 --- a/cpp/src/Ice/Service.cpp +++ b/cpp/src/Ice/Service.cpp @@ -579,160 +579,6 @@ Ice::Service::main(int& argc, char* argv[], const InitializationData& initializa } } - // - // Next check for service control options. - // - string op; - idx = 1; - while(idx < argc) - { - if(strcmp(argv[idx], "--install") == 0 || - strcmp(argv[idx], "--uninstall") == 0 || - strcmp(argv[idx], "--start") == 0 || - strcmp(argv[idx], "--stop") == 0) - { - if(!op.empty()) - { - error("cannot specify `" + op + "' and `" + string(argv[idx]) + "'"); - return EXIT_FAILURE; - } - - if(!name.empty()) - { - error("cannot specify `--service' and `" + string(argv[idx]) + "'"); - return EXIT_FAILURE; - } - - if(idx + 1 >= argc) - { - error("service name argument expected for `" + string(argv[idx]) + "'"); - return EXIT_FAILURE; - } - - op = argv[idx]; - name = argv[idx + 1]; - - warning("the " + op + " option is deprecated"); - - for(int i = idx ; i + 2 < argc ; ++i) - { - argv[i] = argv[i + 2]; - } - argc -= 2; - } - else - { - ++idx; - } - } - - if(!op.empty()) - { - if(op == "--install") - { - // - // Check for --display, --executable. - // - string display, executable; - idx = 1; - while(idx < argc) - { - if(strcmp(argv[idx], "--display") == 0) - { - if(idx + 1 >= argc) - { - error("argument expected for `" + string(argv[idx]) + "'"); - return EXIT_FAILURE; - } - - display = argv[idx + 1]; - - for(int i = idx ; i + 2 < argc ; ++i) - { - argv[i] = argv[i + 2]; - } - argc -= 2; - } - else if(strcmp(argv[idx], "--executable") == 0) - { - if(idx + 1 >= argc) - { - error("argument expected for `" + string(argv[idx]) + "'"); - return EXIT_FAILURE; - } - - executable = argv[idx + 1]; - - for(int i = idx ; i + 2 < argc ; ++i) - { - argv[i] = argv[i + 2]; - } - argc -= 2; - } - else - { - ++idx; - } - } - - vector<string> args; - // - // Prepend the arguments "--service NAME" so that the service - // starts properly. - // - args.push_back("--service"); - args.push_back(name); - for(idx = 1; idx < argc; ++idx) - { - args.push_back(argv[idx]); - } - try - { - // - // Add the registry keys for the event logger if - // initData.logger is empty (which is the case if the - // user wants to use the service default logger). - // - return installService(!initData.logger, initData.stringConverter, name, display, executable, args); - } - catch(const Ice::Exception& ex) - { - ostringstream ostr; - ostr << ex; - error(ostr.str()); - return EXIT_FAILURE; - } - } - else if(op == "--uninstall") - { - try - { - return uninstallService(!initData.logger, name); - } - catch(const Ice::Exception& ex) - { - ostringstream ostr; - ostr << ex; - error(ostr.str()); - return EXIT_FAILURE; - } - } - else if(op == "--start") - { - vector<string> args; - for(idx = 1; idx < argc; ++idx) - { - args.push_back(argv[idx]); - } - return startService(name, args); - } - else - { - assert(op == "--stop"); - return stopService(name); - } - } - if(!name.empty()) { configureService(name); @@ -1040,294 +886,6 @@ Ice::Service::configureService(const string& name) _name = name; } -int -Ice::Service::installService(bool useEventLogger, const StringConverterPtr& stringConverter, const string& name, - const string& display, const string& executable, const vector<string>& args) -{ - wstring disp, exec; - - disp = IceUtil::stringToWstring(nativeToUTF8(stringConverter, display)); - if(disp.empty()) - { - disp = IceUtil::stringToWstring(nativeToUTF8(stringConverter, name)); - } - - exec = IceUtil::stringToWstring(nativeToUTF8(stringConverter, executable)); - if(exec.empty()) - { - // - // Use this executable if none is specified. - // - wchar_t buf[_MAX_PATH]; - if(GetModuleFileNameW(0, buf, _MAX_PATH) == 0) - { - error("unable to obtain file name of executable"); - return EXIT_FAILURE; - } - exec = buf; - } - - // - // Compose service command line. The executable and any arguments must - // be enclosed in quotes if they contain whitespace. - // - wstring command; - if(executable.find(' ') != string::npos) - { - command.push_back('"'); - command.append(exec); - command.push_back('"'); - } - else - { - command = exec; - } - for(vector<string>::const_iterator p = args.begin(); p != args.end(); ++p) - { - command.push_back(' '); - - if(p->find_first_of(" \t\n\r") != string::npos) - { - command.push_back('"'); - command.append(IceUtil::stringToWstring(nativeToUTF8(stringConverter, *p))); - command.push_back('"'); - } - else - { - command.append(IceUtil::stringToWstring(nativeToUTF8(stringConverter, *p))); - } - } - - SC_HANDLE hSCM = OpenSCManagerW(0, 0, SC_MANAGER_ALL_ACCESS); - if(hSCM == 0) - { - syserror("failure in OpenSCManagerW"); - return EXIT_FAILURE; - } - SC_HANDLE hService = CreateServiceW( - hSCM, - IceUtil::stringToWstring(nativeToUTF8(stringConverter, name)).c_str(), - disp.c_str(), - SERVICE_ALL_ACCESS, - SERVICE_WIN32_OWN_PROCESS, - SERVICE_AUTO_START, - SERVICE_ERROR_NORMAL, - command.c_str(), - 0, - 0, - 0, - 0, - 0); - - if(hService == 0) - { - syserror("unable to install service `" + name + "'"); - CloseServiceHandle(hSCM); - return EXIT_FAILURE; - } - - CloseServiceHandle(hSCM); - CloseServiceHandle(hService); - - // - // Add the registry keys for the event logger if _logger is - // empty (which is the case if the user wants to use - // the service default logger). - // - if(useEventLogger) - { - SMEventLoggerI::addKeys(name, stringConverter); - } - - return EXIT_SUCCESS; -} - -int -Ice::Service::uninstallService(bool useEventLogger, const string& name) -{ - SC_HANDLE hSCM = OpenSCManagerW(0, 0, SC_MANAGER_ALL_ACCESS); - if(hSCM == 0) - { - syserror("failure in OpenSCManagerW"); - return EXIT_FAILURE; - } - - SC_HANDLE hService = OpenServiceW(hSCM, - IceUtil::stringToWstring(nativeToUTF8(_initData.stringConverter, name)).c_str(), SERVICE_ALL_ACCESS); - if(hService == 0) - { - syserror("unable to open service `" + name + "'"); - CloseServiceHandle(hSCM); - return EXIT_FAILURE; - } - - BOOL b = DeleteService(hService); - - if(!b) - { - syserror("unable to uninstall service `" + name + "'"); - CloseServiceHandle(hSCM); - CloseServiceHandle(hService); - return EXIT_FAILURE; - } - - CloseServiceHandle(hSCM); - CloseServiceHandle(hService); - - // - // Remove the registry keys for the event logger if necessary. - // - if(useEventLogger) - { - SMEventLoggerI::removeKeys(name, _initData.stringConverter); - } - - return EXIT_SUCCESS; -} - -int -Ice::Service::startService(const string& name, const vector<string>& args) -{ - SC_HANDLE hSCM = OpenSCManagerW(0, 0, SC_MANAGER_ALL_ACCESS); - if(hSCM == 0) - { - syserror("failure in OpenSCManagerW"); - return EXIT_FAILURE; - } - - SC_HANDLE hService = OpenServiceW(hSCM, - IceUtil::stringToWstring(nativeToUTF8(_initData.stringConverter, name)).c_str(), SERVICE_ALL_ACCESS); - if(hService == 0) - { - syserror("unable to open service `" + name + "'"); - CloseServiceHandle(hSCM); - return EXIT_FAILURE; - } - - // - // Create argument vector. Note that StartServiceW() automatically - // adds the service name in argv[0], so the argv that is passed to - // StartService() must *not* include the the service name in - // argv[0]. - // - const int argc = static_cast<int>(args.size()); - LPCWSTR* argv = new LPCWSTR[argc]; - int i = 0; - for(vector<string>::const_iterator p = args.begin(); p != args.end(); ++p) - { - argv[i++] = _wcsdup(IceUtil::stringToWstring(nativeToUTF8(_initData.stringConverter, *p)).c_str()); - } - - // - // Start service. - // - BOOL b = StartServiceW(hService, argc, argv); - - // - // Clean up argument vector. - // - for(i = 0; i < argc; ++i) - { - free(const_cast<wchar_t*>(argv[i])); - } - delete[] argv; - - if(!b) - { - syserror("unable to start service `" + name + "'"); - CloseServiceHandle(hService); - CloseServiceHandle(hSCM); - return EXIT_FAILURE; - } - - trace("Service start pending."); - - // - // Wait until the service is started or an error is detected. - // - SERVICE_STATUS status; - if(!waitForServiceState(hService, SERVICE_START_PENDING, status)) - { - syserror("unable to query status of service `" + name + "'"); - CloseServiceHandle(hService); - CloseServiceHandle(hSCM); - return EXIT_FAILURE; - } - - CloseServiceHandle(hService); - CloseServiceHandle(hSCM); - - if(status.dwCurrentState == SERVICE_RUNNING) - { - trace("Service is running."); - } - else - { - showServiceStatus("Service failed to start.", status); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; -} - -int -Ice::Service::stopService(const string& name) -{ - SC_HANDLE hSCM = OpenSCManagerW(0, 0, SC_MANAGER_ALL_ACCESS); - if(hSCM == 0) - { - syserror("failure in OpenSCManagerW"); - return EXIT_FAILURE; - } - - SC_HANDLE hService = OpenServiceW(hSCM, IceUtil::stringToWstring(nativeToUTF8(_initData.stringConverter, name)).c_str(), SERVICE_ALL_ACCESS); - if(hService == 0) - { - syserror("unable to open service `" + name + "'"); - CloseServiceHandle(hSCM); - return EXIT_FAILURE; - } - - SERVICE_STATUS status; - BOOL b = ControlService(hService, SERVICE_CONTROL_STOP, &status); - - if(!b) - { - syserror("unable to stop service `" + name + "'"); - CloseServiceHandle(hSCM); - CloseServiceHandle(hService); - return EXIT_FAILURE; - } - - trace("Service stop pending."); - - // - // Wait until the service is stopped or an error is detected. - // - if(!waitForServiceState(hService, SERVICE_STOP_PENDING, status)) - { - syserror("unable to query status of service `" + name + "'"); - CloseServiceHandle(hService); - CloseServiceHandle(hSCM); - return EXIT_FAILURE; - } - - CloseServiceHandle(hService); - CloseServiceHandle(hSCM); - - if(status.dwCurrentState == SERVICE_STOPPED) - { - trace("Service is stopped."); - } - else - { - showServiceStatus("Service failed to stop.", status); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; -} - void Ice::Service::setModuleHandle(HMODULE module) { |