X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=b3b0c628559eb17907539aac57a5e9292f0a6dd5;hb=1f0b03b38f7d76814d1c7c627f64462362100223;hp=f575a37284941eb98f5389c5f82d3b86e47668d0;hpb=32a1d0fab47a050086106aac346342c70f4d985b;p=nssm.git diff --git a/service.cpp b/service.cpp index f575a37..b3b0c62 100644 --- a/service.cpp +++ b/service.cpp @@ -240,7 +240,7 @@ int affinity_string_to_mask(TCHAR *string, __int64 *mask) { return 0; } -inline unsigned long priority_mask() { +unsigned long priority_mask() { return REALTIME_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | ABOVE_NORMAL_PRIORITY_CLASS | NORMAL_PRIORITY_CLASS | BELOW_NORMAL_PRIORITY_CLASS | IDLE_PRIORITY_CLASS; } @@ -275,8 +275,18 @@ static inline unsigned long throttle_milliseconds(unsigned long throttle) { void set_service_environment(nssm_service_t *service) { if (! service) return; - if (service->env) duplicate_environment(service->env); - if (service->env_extra) set_environment_block(service->env_extra); + + /* + We have to duplicate the block because this function will be called + multiple times between registry reads. + */ + if (service->env) duplicate_environment_strings(service->env); + if (! service->env_extra) return; + TCHAR *env_extra = copy_environment_block(service->env_extra); + if (! env_extra) return; + + set_environment_block(env_extra); + HeapFree(GetProcessHeap(), 0, env_extra); } void unset_service_environment(nssm_service_t *service) { @@ -764,7 +774,7 @@ void cleanup_nssm_service(nssm_service_t *service) { if (service->throttle_section_initialised) DeleteCriticalSection(&service->throttle_section); if (service->throttle_timer) CloseHandle(service->throttle_timer); if (service->hook_section_initialised) DeleteCriticalSection(&service->hook_section); - if (service->initial_env) FreeEnvironmentStrings(service->initial_env); + if (service->initial_env) HeapFree(GetProcessHeap(), 0, service->initial_env); HeapFree(GetProcessHeap(), 0, service); } @@ -1116,7 +1126,7 @@ int install_service(nssm_service_t *service) { } /* Get path of this program */ - GetModuleFileName(0, service->image, _countof(service->image)); + _sntprintf_s(service->image, _countof(service->image), _TRUNCATE, _T("%s"), nssm_imagepath()); /* Create the service - settings will be changed in edit_service() */ service->handle = CreateService(services, service->name, service->name, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, service->image, 0, 0, 0, 0, 0); @@ -1469,7 +1479,7 @@ void WINAPI service_main(unsigned long argc, TCHAR **argv) { service->hook_section_initialised = true; /* Remember our initial environment. */ - service->initial_env = GetEnvironmentStrings(); + service->initial_env = copy_environment(); /* Remember our creation time. */ if (get_process_creation_time(GetCurrentProcess(), &service->nssm_creation_time)) ZeroMemory(&service->nssm_creation_time, sizeof(service->nssm_creation_time)); @@ -1734,6 +1744,9 @@ int start_service(nssm_service_t *service) { return stop_service(service, 4, true, true); } + /* The pre-start hook will have cleaned the environment. */ + set_service_environment(service); + bool inherit_handles = false; if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true; unsigned long flags = service->priority & priority_mask(); @@ -2102,3 +2115,62 @@ awaited: return ret; } + +int list_nssm_services() { + /* Open service manager. */ + SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE); + if (! services) { + print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED); + return 1; + } + + unsigned long bufsize, required, count, i; + unsigned long resume = 0; + EnumServicesStatus(services, SERVICE_WIN32, SERVICE_STATE_ALL, 0, 0, &required, &count, &resume); + unsigned long error = GetLastError(); + if (error != ERROR_MORE_DATA) { + print_message(stderr, NSSM_MESSAGE_ENUMSERVICESSTATUS_FAILED, error_string(GetLastError())); + return 2; + } + + ENUM_SERVICE_STATUS *status = (ENUM_SERVICE_STATUS *) HeapAlloc(GetProcessHeap(), 0, required); + if (! status) { + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("ENUM_SERVICE_STATUS"), _T("list_nssm_services()")); + return 3; + } + + bufsize = required; + while (true) { + int ret = EnumServicesStatus(services, SERVICE_WIN32, SERVICE_STATE_ALL, status, bufsize, &required, &count, &resume); + if (! ret) { + error = GetLastError(); + if (error != ERROR_MORE_DATA) { + HeapFree(GetProcessHeap(), 0, status); + print_message(stderr, NSSM_MESSAGE_ENUMSERVICESSTATUS_FAILED, error_string(GetLastError())); + return 4; + } + } + + for (i = 0; i < count; i++) { + /* Try to get the service parameters. */ + nssm_service_t *service = alloc_nssm_service(); + if (! service) { + HeapFree(GetProcessHeap(), 0, status); + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("nssm_service_t"), _T("list_nssm_services()")); + return 5; + } + _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), status[i].lpServiceName); + + get_parameters(service, 0); + /* We manage the service if we have an Application. */ + if (service->exe[0]) _tprintf(_T("%s\n"), service->name); + + cleanup_nssm_service(service); + } + + if (ret) break; + } + + HeapFree(GetProcessHeap(), 0, status); + return 0; +}