Ensure we close correct handle after checking if service exists (#793)

Previously, this code would close the Service Control Manager (SCM)
handle instead of the service handle, meaning:

- any future calls to the SCM would fail due to invalid handle
- a handle to the service was left open in error

Also, reduce the access rights given to the handle, as we don't need
ALL rights just to check if the service exists.
This commit is contained in:
Richard Fawcett
2021-02-17 11:38:14 +00:00
committed by GitHub
parent 8b8d983b51
commit 80ec5356cc

View File

@@ -164,13 +164,13 @@ namespace WinSW.Native
internal bool ServiceExists(string serviceName)
{
var serviceHandle = ServiceApis.OpenService(this.handle, serviceName, ServiceAccess.All);
var serviceHandle = ServiceApis.OpenService(this.handle, serviceName, ServiceAccess.QueryStatus);
if (serviceHandle == IntPtr.Zero)
{
return false;
}
_ = CloseServiceHandle(this.handle);
_ = CloseServiceHandle(serviceHandle);
return true;
}