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
}\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
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
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
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
}\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