| 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or |
| 3 | * modify it under the terms of the GNU General Public License |
| 4 | * as published by the Free Software Foundation; either version 2 |
| 5 | * of the License, or (at your option) any later version. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 15 | */ |
| 16 | |
| 17 | #ifdef WIN32 |
| 18 | |
| 19 | #include <cstring> |
| 20 | #include <cwchar> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <windows.h> |
| 25 | |
| 26 | #ifndef WINADVAPI |
| 27 | #if !defined(_ADVAPI32_) |
| 28 | #define WINADVAPI DECLSPEC_IMPORT |
| 29 | #else |
| 30 | #define WINADVAPI |
| 31 | #endif |
| 32 | #endif |
| 33 | |
| 34 | extern char serviceLongName[]; |
| 35 | extern char serviceName[]; |
| 36 | extern char serviceDescription[]; |
| 37 | |
| 38 | extern int g_ServiceStatus; |
| 39 | |
| 40 | #ifdef WIN32_GUI |
| 41 | extern int app_main(int argc, char ** argv); |
| 42 | #else |
| 43 | extern int main(int argc, char ** argv); |
| 44 | #endif |
| 45 | |
| 46 | SERVICE_STATUS serviceStatus; |
| 47 | SERVICE_STATUS_HANDLE serviceStatusHandle = 0; |
| 48 | |
| 49 | typedef WINADVAPI BOOL(WINAPI *CSD_T)(SC_HANDLE hService, DWORD dwInfoLevel, LPVOID lpInfo); |
| 50 | |
| 51 | void Win32_ServiceInstall() |
| 52 | { |
| 53 | SC_HANDLE serviceControlManager = OpenSCManagerW(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE); |
| 54 | if (!serviceControlManager) |
| 55 | { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | wchar_t path[MAX_PATH + 10] = {}; |
| 60 | if (GetModuleFileNameW(nullptr, path, sizeof path / sizeof *path) != 0) |
| 61 | { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | std::wcscat(path, L"--service"); |
| 66 | |
| 67 | auto utf8_decode = [](const std::string& str) |
| 68 | { |
| 69 | if (str.empty()) |
| 70 | return std::wstring(); |
| 71 | int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); |
| 72 | std::wstring wstrTo(size_needed, 0); |
| 73 | MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); |
| 74 | return wstrTo; |
| 75 | }; |
| 76 | |
| 77 | SC_HANDLE service = CreateServiceW(serviceControlManager, |
| 78 | utf8_decode(serviceName).c_str(), utf8_decode(serviceLongName).c_str(), |
| 79 | SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, |
| 80 | SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, path, |
| 81 | 0, 0, 0, 0, 0); |
| 82 | if (service == NULL) |
| 83 | { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | SERVICE_DESCRIPTIONW sdBuf = {}; |
| 88 | auto temp = utf8_decode(serviceDescription); |
| 89 | std::vector<wchar_t> buffer(temp.begin(), temp.end()); |
| 90 | sdBuf.lpDescription = buffer.data(); |
| 91 | |
| 92 | HMODULE advapi32 = GetModuleHandleW(L"advapi32.dll"); |
| 93 | if (advapi32 == NULL) |
| 94 | { |
| 95 | CloseServiceHandle(service); |
| 96 | CloseServiceHandle(serviceControlManager); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | auto ChangeServiceDescription = reinterpret_cast<CSD_T>(GetProcAddress(advapi32, "ChangeServiceConfig2W")); |
| 101 | if (ChangeServiceDescription == NULL) |
| 102 | { |
| 103 | CloseServiceHandle(service); |
| 104 | CloseServiceHandle(serviceControlManager); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | ChangeServiceDescription( |
| 109 | service, // handle to service |
| 110 | SERVICE_CONFIG_DESCRIPTION, // change: description |
| 111 | &sdBuf); |
| 112 | CloseServiceHandle(service); |
| 113 | |
| 114 | CloseServiceHandle(serviceControlManager); |
| 115 | } |
| 116 | |
| 117 | void Win32_ServiceUninstall() |
| 118 | { |
| 119 | SC_HANDLE serviceControlManager = OpenSCManagerW(nullptr, nullptr, SC_MANAGER_CONNECT); |
| 120 | |
| 121 | if (serviceControlManager) |
| 122 | { |
| 123 | auto utf8_decode = [](const std::string& str) |
| 124 | { |
| 125 | if (str.empty()) |
| 126 | return std::wstring(); |
| 127 | int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); |
| 128 | std::wstring wstrTo(size_needed, 0); |
| 129 | MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); |
| 130 | return wstrTo; |
| 131 | }; |
| 132 | |
| 133 | SC_HANDLE service = OpenServiceW(serviceControlManager, |
| 134 | utf8_decode(serviceName).c_str(), SERVICE_QUERY_STATUS | DELETE); |
| 135 | if (service) |
| 136 | { |
| 137 | SERVICE_STATUS serviceStatus; |
| 138 | if (QueryServiceStatus(service, &serviceStatus)) |
| 139 | { |
| 140 | if (serviceStatus.dwCurrentState == SERVICE_STOPPED) |
| 141 | DeleteService(service); |
| 142 | } |
| 143 | //DeleteService(service); |
| 144 | |
| 145 | CloseServiceHandle(service); |
| 146 | } |
| 147 | |
| 148 | CloseServiceHandle(serviceControlManager); |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | |
| 153 | void WINAPI ServiceControlHandler(DWORD controlCode) |
| 154 | { |
| 155 | switch (controlCode) |
| 156 | { |
| 157 | case SERVICE_CONTROL_INTERROGATE: |
| 158 | break; |
| 159 | |
| 160 | case SERVICE_CONTROL_SHUTDOWN: |
| 161 | case SERVICE_CONTROL_STOP: |
| 162 | serviceStatus.dwCurrentState = SERVICE_STOP_PENDING; |
| 163 | SetServiceStatus(serviceStatusHandle, &serviceStatus); |
| 164 | |
| 165 | g_ServiceStatus = 0; |
| 166 | return; |
| 167 | |
| 168 | case SERVICE_CONTROL_PAUSE: |
| 169 | g_ServiceStatus = 2; |
| 170 | serviceStatus.dwCurrentState = SERVICE_PAUSED; |
| 171 | SetServiceStatus(serviceStatusHandle, &serviceStatus); |
| 172 | break; |
| 173 | |
| 174 | case SERVICE_CONTROL_CONTINUE: |
| 175 | serviceStatus.dwCurrentState = SERVICE_RUNNING; |
| 176 | SetServiceStatus(serviceStatusHandle, &serviceStatus); |
| 177 | g_ServiceStatus = 1; |
| 178 | break; |
| 179 | |
| 180 | default: |
| 181 | if (controlCode >= 128 && controlCode <= 255) |
| 182 | // user defined control code |
| 183 | break; |
| 184 | else |
| 185 | // unrecognized control code |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | SetServiceStatus(serviceStatusHandle, &serviceStatus); |
| 190 | } |
| 191 | |
| 192 | void WINAPI ServiceMain(DWORD argc, char* argv[]) |
| 193 | { |
| 194 | // initialise service status |
| 195 | serviceStatus.dwServiceType = SERVICE_WIN32; |
| 196 | serviceStatus.dwCurrentState = SERVICE_START_PENDING; |
| 197 | serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | |
| 198 | SERVICE_ACCEPT_PAUSE_CONTINUE; |
| 199 | serviceStatus.dwWin32ExitCode = NO_ERROR; |
| 200 | serviceStatus.dwServiceSpecificExitCode = NO_ERROR; |
| 201 | serviceStatus.dwCheckPoint = 0; |
| 202 | serviceStatus.dwWaitHint = 0; |
| 203 | |
| 204 | auto utf8_decode = [](const std::string& str) |
| 205 | { |
| 206 | if (str.empty()) |
| 207 | return std::wstring(); |
| 208 | int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); |
| 209 | std::wstring wstrTo(size_needed, 0); |
| 210 | MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); |
| 211 | return wstrTo; |
| 212 | }; |
| 213 | |
| 214 | serviceStatusHandle = RegisterServiceCtrlHandlerW(utf8_decode(serviceName).c_str(), ServiceControlHandler); |
| 215 | |
| 216 | if (serviceStatusHandle) |
| 217 | { |
| 218 | wchar_t path[MAX_PATH + 1] = {}; |
| 219 | unsigned last_slash = 0; |
| 220 | |
| 221 | GetModuleFileNameW(0, path, sizeof path / sizeof *path); |
| 222 | |
| 223 | for (std::size_t i = 0; i < std::wcslen(path); i++) |
| 224 | { |
| 225 | if (path[i] == '\\') |
| 226 | last_slash = i; |
| 227 | } |
| 228 | |
| 229 | path[last_slash] = 0; |
| 230 | |
| 231 | // service is starting |
| 232 | serviceStatus.dwCurrentState = SERVICE_START_PENDING; |
| 233 | SetServiceStatus(serviceStatusHandle, &serviceStatus); |
| 234 | |
| 235 | // do initialisation here |
| 236 | SetCurrentDirectoryW(path); |
| 237 | |
| 238 | // running |
| 239 | serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN); |
| 240 | serviceStatus.dwCurrentState = SERVICE_RUNNING; |
| 241 | SetServiceStatus(serviceStatusHandle, &serviceStatus); |
| 242 | |
| 243 | //////////////////////// |
| 244 | // service main cycle // |
| 245 | //////////////////////// |
| 246 | |
| 247 | g_ServiceStatus = 1; |
| 248 | argc = 1; |
| 249 | |
| 250 | |
| 251 | #ifdef WIN32_GUI |
| 252 | app_main(argc, argv); |
| 253 | #else |
| 254 | main(argc, argv); |
| 255 | #endif |
| 256 | |
| 257 | |
| 258 | |
| 259 | // service was stopped |
| 260 | serviceStatus.dwCurrentState = SERVICE_STOP_PENDING; |
| 261 | SetServiceStatus(serviceStatusHandle, &serviceStatus); |
| 262 | |
| 263 | // do cleanup here |
| 264 | |
| 265 | // service is now stopped |
| 266 | serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN); |
| 267 | serviceStatus.dwCurrentState = SERVICE_STOPPED; |
| 268 | SetServiceStatus(serviceStatusHandle, &serviceStatus); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void Win32_ServiceRun() |
| 273 | { |
| 274 | SERVICE_TABLE_ENTRYA serviceTable[] = |
| 275 | { |
| 276 | { serviceName, ServiceMain }, |
| 277 | { 0, 0 } |
| 278 | }; |
| 279 | |
| 280 | StartServiceCtrlDispatcherA(serviceTable); |
| 281 | } |
| 282 | |
| 283 | #endif |