Use CloseServiceHandle() on service handles.
authorIain Patterson <me@iain.cx>
Sun, 29 Jun 2014 10:24:36 +0000 (11:24 +0100)
committerIain Patterson <me@iain.cx>
Sun, 29 Jun 2014 10:28:35 +0000 (11:28 +0100)
We were incorrectly calling CloseHandle() to close service handles.
CloseServiceHandle() is not only for service manager handles and should
be used on service handles as well.

service.cpp

index 4327a01..61d4b84 100644 (file)
@@ -709,7 +709,7 @@ void cleanup_nssm_service(nssm_service_t *service) {
   if (service->dependencies) HeapFree(GetProcessHeap(), 0, service->dependencies);\r
   if (service->env) HeapFree(GetProcessHeap(), 0, service->env);\r
   if (service->env_extra) HeapFree(GetProcessHeap(), 0, service->env_extra);\r
-  if (service->handle) CloseHandle(service->handle);\r
+  if (service->handle) CloseServiceHandle(service->handle);\r
   if (service->process_handle) CloseHandle(service->process_handle);\r
   if (service->wait_handle) UnregisterWait(service->wait_handle);\r
   if (service->throttle_section_initialised) DeleteCriticalSection(&service->throttle_section);\r
@@ -856,7 +856,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   /* Get system details. */\r
   QUERY_SERVICE_CONFIG *qsc = query_service_config(service->name, service->handle);\r
   if (! qsc) {\r
-    CloseHandle(service->handle);\r
+    CloseServiceHandle(service->handle);\r
     CloseServiceHandle(services);\r
     return 4;\r
   }\r
@@ -865,7 +865,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   if (! (service->type & SERVICE_WIN32_OWN_PROCESS)) {\r
     if (mode != MODE_GETTING) {\r
       HeapFree(GetProcessHeap(), 0, qsc);\r
-      CloseHandle(service->handle);\r
+      CloseServiceHandle(service->handle);\r
       CloseServiceHandle(services);\r
       print_message(stderr, NSSM_MESSAGE_CANNOT_EDIT, service->name, NSSM_WIN32_OWN_PROCESS, 0);\r
       return 3;\r
@@ -875,7 +875,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   if (get_service_startup(service->name, service->handle, qsc, &service->startup)) {\r
     if (mode != MODE_GETTING) {\r
       HeapFree(GetProcessHeap(), 0, qsc);\r
-      CloseHandle(service->handle);\r
+      CloseServiceHandle(service->handle);\r
       CloseServiceHandle(services);\r
       return 4;\r
     }\r
@@ -884,7 +884,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) {\r
     if (mode != MODE_GETTING) {\r
       HeapFree(GetProcessHeap(), 0, qsc);\r
-      CloseHandle(service->handle);\r
+      CloseServiceHandle(service->handle);\r
       CloseServiceHandle(services);\r
       return 5;\r
     }\r
@@ -903,7 +903,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
   /* Get extended system details. */\r
   if (get_service_description(service->name, service->handle, _countof(service->description), service->description)) {\r
     if (mode != MODE_GETTING) {\r
-      CloseHandle(service->handle);\r
+      CloseServiceHandle(service->handle);\r
       CloseServiceHandle(services);\r
       return 6;\r
     }\r
@@ -911,7 +911,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
 \r
   if (get_service_dependencies(service->name, service->handle, &service->dependencies, &service->dependencieslen)) {\r
     if (mode != MODE_GETTING) {\r
-      CloseHandle(service->handle);\r
+      CloseServiceHandle(service->handle);\r
       CloseServiceHandle(services);\r
       return 7;\r
     }\r
@@ -935,7 +935,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
 \r
   /* Trying to manage App* parameters for a non-NSSM service. */\r
   if (! setting->native && service->native) {\r
-    CloseHandle(service->handle);\r
+    CloseServiceHandle(service->handle);\r
     print_message(stderr, NSSM_MESSAGE_NATIVE_PARAMETER, setting->name, NSSM);\r
     return 1;\r
   }\r
@@ -953,7 +953,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
     if (setting->native) ret = get_setting(service->name, service->handle, setting, &value, additional);\r
     else ret = get_setting(service->name, key, setting, &value, additional);\r
     if (ret < 0) {\r
-      CloseHandle(service->handle);\r
+      CloseServiceHandle(service->handle);\r
       return 5;\r
     }\r
 \r
@@ -971,7 +971,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
     }\r
 \r
     if (! service->native) RegCloseKey(key);\r
-    CloseHandle(service->handle);\r
+    CloseServiceHandle(service->handle);\r
     return 0;\r
   }\r
 \r
@@ -993,7 +993,7 @@ int pre_edit_service(int argc, TCHAR **argv) {
     value.string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));\r
     if (! value.string) {\r
       print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("value"), _T("edit_service()"));\r
-      CloseHandle(service->handle);\r
+      CloseServiceHandle(service->handle);\r
       return 2;\r
     }\r
 \r
@@ -1026,12 +1026,12 @@ int pre_edit_service(int argc, TCHAR **argv) {
   if (value.string) HeapFree(GetProcessHeap(), 0, value.string);\r
   if (ret < 0) {\r
     if (! service->native) RegCloseKey(key);\r
-    CloseHandle(service->handle);\r
+    CloseServiceHandle(service->handle);\r
     return 6;\r
   }\r
 \r
   if (! service->native) RegCloseKey(key);\r
-  CloseHandle(service->handle);\r
+  CloseServiceHandle(service->handle);\r
 \r
   return 0;\r
 }\r
@@ -1241,7 +1241,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
 \r
     if (ret) {\r
       int response = await_service_control_response(control, service_handle, &service_status, initial_status);\r
-      CloseHandle(service_handle);\r
+      CloseServiceHandle(service_handle);\r
 \r
       if (response) {\r
         print_message(stderr, NSSM_MESSAGE_BAD_CONTROL_RESPONSE, canonical_name, service_status_text(service_status.dwCurrentState), service_control_text(control));\r
@@ -1251,7 +1251,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
       return 0;\r
     }\r
     else {\r
-      CloseHandle(service_handle);\r
+      CloseServiceHandle(service_handle);\r
       _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));\r
       return 1;\r
     }\r
@@ -1287,7 +1287,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
 \r
     if (ret) {\r
       int response = await_service_control_response(control, service_handle, &service_status, initial_status);\r
-      CloseHandle(service_handle);\r
+      CloseServiceHandle(service_handle);\r
 \r
       if (response) {\r
         print_message(stderr, NSSM_MESSAGE_BAD_CONTROL_RESPONSE, canonical_name, service_status_text(service_status.dwCurrentState), service_control_text(control));\r
@@ -1297,7 +1297,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
       return 0;\r
     }\r
     else {\r
-      CloseHandle(service_handle);\r
+      CloseServiceHandle(service_handle);\r
       _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));\r
       if (error == ERROR_SERVICE_NOT_ACTIVE) {\r
         if (control == SERVICE_CONTROL_SHUTDOWN || control == SERVICE_CONTROL_STOP) return 0;\r