| 1 | #pragma once
|
|---|
| 2 |
|
|---|
| 3 | #ifdef _WIN32
|
|---|
| 4 | #include <ServiceCmd.h>
|
|---|
| 5 | #endif
|
|---|
| 6 |
|
|---|
| 7 | namespace nsclient {
|
|---|
| 8 | namespace client {
|
|---|
| 9 | #ifdef _WIN32
|
|---|
| 10 | class service_manager {
|
|---|
| 11 | private:
|
|---|
| 12 | std::wstring service_name_;
|
|---|
| 13 |
|
|---|
| 14 | public:
|
|---|
| 15 | service_manager(std::wstring service_name) : service_name_(service_name) {}
|
|---|
| 16 |
|
|---|
| 17 | static std::wstring get_default_service_name() {
|
|---|
| 18 | return DEFAULT_SERVICE_NAME;
|
|---|
| 19 | }
|
|---|
| 20 | static std::wstring get_default_service_desc() {
|
|---|
| 21 | return DEFAULT_SERVICE_DESC;
|
|---|
| 22 | }
|
|---|
| 23 | static std::wstring get_default_service_deps() {
|
|---|
| 24 | return DEFAULT_SERVICE_DEPS;
|
|---|
| 25 | }
|
|---|
| 26 | static std::wstring get_default_arguments() {
|
|---|
| 27 | return _T("service --run");
|
|---|
| 28 | }
|
|---|
| 29 | inline void print_msg(std::wstring str) {
|
|---|
| 30 | std::wcout << str << std::endl;
|
|---|
| 31 | }
|
|---|
| 32 | inline void print_error(std::wstring str) {
|
|---|
| 33 | std::wcerr << _T("ERROR: ") << str << std::endl;
|
|---|
| 34 | }
|
|---|
| 35 | public:
|
|---|
| 36 | int install(std::wstring service_description) {
|
|---|
| 37 | try {
|
|---|
| 38 | std::wstring args = get_default_arguments();
|
|---|
| 39 | if (service_name_ != get_default_service_name())
|
|---|
| 40 | args += _T(" --name ") + service_name_;
|
|---|
| 41 | serviceControll::Install(service_name_, service_description, get_default_service_deps(), SERVICE_WIN32_OWN_PROCESS, args);
|
|---|
| 42 | } catch (const serviceControll::SCException& e) {
|
|---|
| 43 | print_error(_T("Service installation failed of '") + service_name_ + _T("' failed: ") + e.error_);
|
|---|
| 44 | return -1;
|
|---|
| 45 | }
|
|---|
| 46 | try {
|
|---|
| 47 | serviceControll::SetDescription(service_name_, service_description);
|
|---|
| 48 | } catch (const serviceControll::SCException& e) {
|
|---|
| 49 | print_error(_T("Couldn't set service description: ") + e.error_);
|
|---|
| 50 | }
|
|---|
| 51 | print_msg(_T("Service installed successfully!"));
|
|---|
| 52 | return 0;
|
|---|
| 53 | }
|
|---|
| 54 | int uninstall() {
|
|---|
| 55 | try {
|
|---|
| 56 | serviceControll::Uninstall(service_name_);
|
|---|
| 57 | } catch (const serviceControll::SCException& e) {
|
|---|
| 58 | print_error(_T("Service de-installation (") + service_name_ + _T(") failed; ") + e.error_ + _T("\nMaybe the service was not previously installed properly?"));
|
|---|
| 59 | return 0;
|
|---|
| 60 | }
|
|---|
| 61 | print_msg(_T("Service uninstalled successfully!"));
|
|---|
| 62 | return 0;
|
|---|
| 63 | }
|
|---|
| 64 | int start() {
|
|---|
| 65 | try {
|
|---|
| 66 | serviceControll::Start(service_name_);
|
|---|
| 67 | } catch (const serviceControll::SCException& e) {
|
|---|
| 68 | print_error(_T("Service failed to start: ") + e.error_);
|
|---|
| 69 | return -1;
|
|---|
| 70 | }
|
|---|
| 71 | return 0;
|
|---|
| 72 | }
|
|---|
| 73 | int stop() {
|
|---|
| 74 | try {
|
|---|
| 75 | serviceControll::Stop(service_name_);
|
|---|
| 76 | } catch (const serviceControll::SCException& e) {
|
|---|
| 77 | print_error(_T("Service failed to stop: ") + e.error_);
|
|---|
| 78 | return -1;
|
|---|
| 79 | }
|
|---|
| 80 | return 0;
|
|---|
| 81 | }
|
|---|
| 82 | int info() {
|
|---|
| 83 | try {
|
|---|
| 84 | std::wstring exe = serviceControll::get_exe_path(service_name_);
|
|---|
| 85 | print_error(_T("The Service uses: ") + exe);
|
|---|
| 86 | } catch (const serviceControll::SCException& e) {
|
|---|
| 87 | print_error(_T("Failed to find service: ") + e.error_);
|
|---|
| 88 | return -1;
|
|---|
| 89 | }
|
|---|
| 90 | return 0;
|
|---|
| 91 | }
|
|---|
| 92 | };
|
|---|
| 93 | #else
|
|---|
| 94 | class service_manager {
|
|---|
| 95 | public:
|
|---|
| 96 | service_manager(std::wstring service_name) {}
|
|---|
| 97 | int unsupported() {
|
|---|
| 98 | std::wcout << _T("Service management is not supported on non Windows operating systems...") << std::endl;
|
|---|
| 99 | return -1;
|
|---|
| 100 | }
|
|---|
| 101 | int install(std::wstring service_description) {
|
|---|
| 102 | return unsupported();
|
|---|
| 103 | }
|
|---|
| 104 | int uninstall() {
|
|---|
| 105 | return unsupported();
|
|---|
| 106 | }
|
|---|
| 107 | int start() {
|
|---|
| 108 | return unsupported();
|
|---|
| 109 | }
|
|---|
| 110 | int stop() {
|
|---|
| 111 | return unsupported();
|
|---|
| 112 | }
|
|---|
| 113 | int info() {
|
|---|
| 114 | return unsupported();
|
|---|
| 115 | }
|
|---|
| 116 | };
|
|---|
| 117 | #endif
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|