Added open_service().
authorIain Patterson <me@iain.cx>
Wed, 1 Jan 2014 16:00:17 +0000 (16:00 +0000)
committerIain Patterson <me@iain.cx>
Wed, 1 Jan 2014 21:28:00 +0000 (21:28 +0000)
New function to open a service either by name or display name.  Since no
service's display name may conflict with any other service's display
name or key name, we can safely use a display name as a key when opening
a service.

OpenService() expects a service name so if it fails with status
ERROR_SERVICE_DOES_NOT_EXIST we can enumerate all services and look for
one whose display name matches the argument passed to open_service(),
and open it.  As a side effect we can optionally store the canonical
name in a separate (or not) buffer.

README.txt
messages.mc
service.cpp
service.h

index 9a9a11e..6955ba4 100644 (file)
@@ -63,11 +63,14 @@ Since version 2.22, NSSM can manage existing services.
 \r
 Usage\r
 -----\r
-In the usage notes below, arguments to the program may be written in angle \r
-brackets and/or square brackets.  <string> means you must insert the \r
-appropriate string and [<string>] means the string is optional.  See the \r
+In the usage notes below, arguments to the program may be written in angle\r
+brackets and/or square brackets.  <string> means you must insert the\r
+appropriate string and [<string>] means the string is optional.  See the\r
 examples below...\r
 \r
+Note that everywhere <servicename> appears you may substitute the\r
+service's display name.\r
+\r
 \r
 Installation using the GUI\r
 --------------------------\r
index 1be1d0a..a53eee1 100644 (file)
Binary files a/messages.mc and b/messages.mc differ
index 8440038..621277d 100644 (file)
@@ -37,6 +37,85 @@ SC_HANDLE open_service_manager() {
   return ret;\r
 }\r
 \r
+/* Open a service by name or display name. */\r
+SC_HANDLE open_service(SC_HANDLE services, TCHAR *service_name, TCHAR *canonical_name, unsigned long canonical_namelen) {\r
+  SC_HANDLE service_handle = OpenService(services, service_name, SERVICE_ALL_ACCESS);\r
+  if (service_handle) {\r
+    if (canonical_name && canonical_name != service_name) {\r
+      if (_sntprintf_s(canonical_name, canonical_namelen, _TRUNCATE, _T("%s"), service_name) < 0) {\r
+        print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canonical_name"), _T("open_service()"));\r
+        return 0;\r
+      }\r
+    }\r
+    return service_handle;\r
+  }\r
+\r
+  unsigned long error = GetLastError();\r
+  if (error != ERROR_SERVICE_DOES_NOT_EXIST) {\r
+    print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED, error_string(GetLastError()));\r
+    return 0;\r
+  }\r
+\r
+  /* We can't look for a display name because there's no buffer to store it. */\r
+  if (! canonical_name) {\r
+    print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED, error_string(GetLastError()));\r
+    return 0;\r
+  }\r
+\r
+  unsigned long bufsize, required, count, i;\r
+  unsigned long resume = 0;\r
+  EnumServicesStatus(services, SERVICE_DRIVER | SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL, 0, 0, &required, &count, &resume);\r
+  error = GetLastError();\r
+  if (error != ERROR_MORE_DATA) {\r
+    print_message(stderr, NSSM_MESSAGE_ENUMSERVICESSTATUS_FAILED, error_string(GetLastError()));\r
+    return 0;\r
+  }\r
+\r
+  ENUM_SERVICE_STATUS *status = (ENUM_SERVICE_STATUS *) HeapAlloc(GetProcessHeap(), 0, required);\r
+  if (! status) {\r
+    print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("ENUM_SERVICE_STATUS"), _T("open_service()"));\r
+    return 0;\r
+  }\r
+\r
+  bufsize = required;\r
+  while (true) {\r
+    /*\r
+      EnumServicesStatus() returns:\r
+      1 when it retrieved data and there's no more data to come.\r
+      0 and sets last error to ERROR_MORE_DATA when it retrieved data and\r
+        there's more data to come.\r
+      0 and sets last error to something else on error.\r
+    */\r
+    int ret = EnumServicesStatus(services, SERVICE_DRIVER | SERVICE_FILE_SYSTEM_DRIVER | SERVICE_KERNEL_DRIVER | SERVICE_WIN32, SERVICE_STATE_ALL, status, bufsize, &required, &count, &resume);\r
+    if (! ret) {\r
+      error = GetLastError();\r
+      if (error != ERROR_MORE_DATA) {\r
+        HeapFree(GetProcessHeap(), 0, status);\r
+        print_message(stderr, NSSM_MESSAGE_ENUMSERVICESSTATUS_FAILED, error_string(GetLastError()));\r
+        return 0;\r
+      }\r
+    }\r
+\r
+    for (i = 0; i < count; i++) {\r
+      if (str_equiv(status[i].lpDisplayName, service_name)) {\r
+        if (_sntprintf_s(canonical_name, canonical_namelen, _TRUNCATE, _T("%s"), status[i].lpServiceName) < 0) {\r
+          HeapFree(GetProcessHeap(), 0, status);\r
+          print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canonical_name"), _T("open_service()"));\r
+          return 0;\r
+        }\r
+\r
+        HeapFree(GetProcessHeap(), 0, status);\r
+        return open_service(services, canonical_name, 0, 0);\r
+      }\r
+    }\r
+\r
+    if (ret) break;\r
+  }\r
+\r
+  /* Recurse so we can get an error message. */\r
+  return open_service(services, service_name, 0, 0);\r
+}\r
+\r
 QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *service_name, SC_HANDLE service_handle) {\r
   QUERY_SERVICE_CONFIG *qsc;\r
   unsigned long bufsize;\r
@@ -491,10 +570,9 @@ int pre_edit_service(int argc, TCHAR **argv) {
   }\r
 \r
   /* Try to open the service */\r
-  service->handle = OpenService(services, service->name, SC_MANAGER_ALL_ACCESS);\r
+  service->handle = open_service(services, service->name, service->name, _countof(service->name));\r
   if (! service->handle) {\r
     CloseServiceHandle(services);\r
-    print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED);\r
     return 3;\r
   }\r
 \r
@@ -806,6 +884,7 @@ int edit_service(nssm_service_t *service, bool editing) {
 int control_service(unsigned long control, int argc, TCHAR **argv) {\r
   if (argc < 1) return usage(1);\r
   TCHAR *service_name = argv[0];\r
+  TCHAR canonical_name[SERVICE_NAME_LENGTH];\r
 \r
   SC_HANDLE services = open_service_manager();\r
   if (! services) {\r
@@ -813,9 +892,8 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
     return 2;\r
   }\r
 \r
-  SC_HANDLE service_handle = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS);\r
+  SC_HANDLE service_handle = open_service(services, service_name, canonical_name, _countof(canonical_name));\r
   if (! service_handle) {\r
-    print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED);\r
     CloseServiceHandle(services);\r
     return 3;\r
   }\r
@@ -861,7 +939,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) {
       return 0;\r
     }\r
     else {\r
-      _ftprintf(stderr, _T("%s: %s\n"), service_name, error_string(error));\r
+      _ftprintf(stderr, _T("%s: %s\n"), canonical_name, error_string(error));\r
       return 1;\r
     }\r
   }\r
@@ -894,9 +972,8 @@ int remove_service(nssm_service_t *service) {
   }\r
 \r
   /* Try to open the service */\r
-  service->handle = OpenService(services, service->name, SC_MANAGER_ALL_ACCESS);\r
+  service->handle = open_service(services, service->name, service->name, _countof(service->name));\r
   if (! service->handle) {\r
-    print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED);\r
     CloseServiceHandle(services);\r
     return 3;\r
   }\r
index 29c237e..e4049db 100644 (file)
--- a/service.h
+++ b/service.h
@@ -96,6 +96,7 @@ nssm_service_t *alloc_nssm_service();
 void set_nssm_service_defaults(nssm_service_t *);\r
 void cleanup_nssm_service(nssm_service_t *);\r
 SC_HANDLE open_service_manager();\r
+SC_HANDLE open_service(SC_HANDLE, TCHAR *, TCHAR *, unsigned long);\r
 QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);\r
 int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);\r
 int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);\r