From e11eb013766d246071ebfbd9bc893b9c6bbb1e5b Mon Sep 17 00:00:00 2001 From: Chiyuan Ma Date: Wed, 14 Mar 2018 18:28:36 -0700 Subject: [PATCH 1/2] properly handler service state for pending and pause --- src/ServiceMonitor/ServiceMonitor.cpp | 43 ++++++++------------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/src/ServiceMonitor/ServiceMonitor.cpp b/src/ServiceMonitor/ServiceMonitor.cpp index 4a83c62..1f6b8ae 100644 --- a/src/ServiceMonitor/ServiceMonitor.cpp +++ b/src/ServiceMonitor/ServiceMonitor.cpp @@ -135,38 +135,10 @@ HRESULT Service_Monitor::StopServiceByName(LPCTSTR pServiceName, DWORD dwTimeOut hr = GetServiceHandle(pServiceName, &hService); if (SUCCEEDED(hr)) { - if (!QueryServiceStatusEx(hService, - SC_STATUS_PROCESS_INFO, - (LPBYTE)&sStatus, - sizeof(SERVICE_STATUS_PROCESS), - &dwBytes)) - { - hr = HRESULT_FROM_WIN32(GetLastError()); - goto Finished; - } - - if (sStatus.dwCurrentState == SERVICE_STOPPED) - { - // - // Service has already been stopped, no action needed - // - goto Finished; - } - else if (sStatus.dwCurrentState != SERVICE_STOP_PENDING) - { - // - // Sending stop signal - // - if (!ControlService(hService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&sStatus)) - { - hr = HRESULT_FROM_WIN32(GetLastError()); - goto Finished; - } - } - while (dwRemainTime >0) { DWORD dwBytes = 0; + _tprintf(L"\n FUnction Begin'%s' error [%x]\n", pServiceName, hr); if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, @@ -174,6 +146,8 @@ HRESULT Service_Monitor::StopServiceByName(LPCTSTR pServiceName, DWORD dwTimeOut sizeof(SERVICE_STATUS_PROCESS), &dwBytes)) { + _tprintf(L"\n The Second Query Failed'%s' error [%x]\n", pServiceName, hr); + hr = HRESULT_FROM_WIN32(GetLastError()); goto Finished; } @@ -181,18 +155,27 @@ HRESULT Service_Monitor::StopServiceByName(LPCTSTR pServiceName, DWORD dwTimeOut { goto Finished; } - else if (sStatus.dwCurrentState == SERVICE_STOP_PENDING) + else if (sStatus.dwCurrentState == SERVICE_STOP_PENDING || sStatus.dwCurrentState == SERVICE_START_PENDING || sStatus.dwCurrentState == SERVICE_PAUSE_PENDING || sStatus.dwCurrentState == SERVICE_CONTINUE_PENDING) { dwSleepTime = rand() % 10 + 1; dwSleepTime = dwSleepTime < dwRemainTime ? dwSleepTime : dwRemainTime; dwRemainTime -= dwSleepTime; Sleep(dwSleepTime * 1000); } + else if (sStatus.dwCurrentState == SERVICE_RUNNING || sStatus.dwCurrentState == SERVICE_PAUSED) + { + if (!ControlService(hService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&sStatus)) + { + hr = HRESULT_FROM_WIN32(GetLastError()); + goto Finished; + } + } else { // // Service fails to stop // + _tprintf(L"\n ACTUAL STOP REQUEST '%s' error [%x] [%x]\n", pServiceName, hr, sStatus.dwCurrentState); hr = E_FAIL; goto Finished; } From 0f49dd8422de21fec09a4c3e42d98af74e357242 Mon Sep 17 00:00:00 2001 From: Chiyuan Ma Date: Mon, 9 Apr 2018 13:31:52 -0700 Subject: [PATCH 2/2] Remove Debug Print --- src/ServiceMonitor/ServiceMonitor.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/ServiceMonitor/ServiceMonitor.cpp b/src/ServiceMonitor/ServiceMonitor.cpp index 1f6b8ae..33502ca 100644 --- a/src/ServiceMonitor/ServiceMonitor.cpp +++ b/src/ServiceMonitor/ServiceMonitor.cpp @@ -138,16 +138,12 @@ HRESULT Service_Monitor::StopServiceByName(LPCTSTR pServiceName, DWORD dwTimeOut while (dwRemainTime >0) { DWORD dwBytes = 0; - _tprintf(L"\n FUnction Begin'%s' error [%x]\n", pServiceName, hr); - if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&sStatus, sizeof(SERVICE_STATUS_PROCESS), &dwBytes)) { - _tprintf(L"\n The Second Query Failed'%s' error [%x]\n", pServiceName, hr); - hr = HRESULT_FROM_WIN32(GetLastError()); goto Finished; } @@ -175,7 +171,6 @@ HRESULT Service_Monitor::StopServiceByName(LPCTSTR pServiceName, DWORD dwTimeOut // // Service fails to stop // - _tprintf(L"\n ACTUAL STOP REQUEST '%s' error [%x] [%x]\n", pServiceName, hr, sStatus.dwCurrentState); hr = E_FAIL; goto Finished; }