X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=e8a1569c7d75bc7514fb35269954f25fd0488500;hb=c03d51cbbfe9f4f8a28dbabbfc068573a1842b74;hp=53ee349a3fe3c02dcb62f7b7e08d8e62252e22b5;hpb=e0f182e26d12875ad2e03f634f5d8eb46f75038b;p=nssm.git diff --git a/service.cpp b/service.cpp index 53ee349..e8a1569 100644 --- a/service.cpp +++ b/service.cpp @@ -681,6 +681,7 @@ void cleanup_nssm_service(nssm_service_t *service) { if (service->wait_handle) UnregisterWait(service->process_handle); if (service->throttle_section_initialised) DeleteCriticalSection(&service->throttle_section); if (service->throttle_timer) CloseHandle(service->throttle_timer); + if (service->initial_env) FreeEnvironmentStrings(service->initial_env); HeapFree(GetProcessHeap(), 0, service); } @@ -1214,6 +1215,9 @@ int control_service(unsigned long control, int argc, TCHAR **argv) { else { CloseHandle(service_handle); _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error)); + if (error == ERROR_SERVICE_NOT_ACTIVE) { + if (control == SERVICE_CONTROL_SHUTDOWN || control == SERVICE_CONTROL_STOP) return 0; + } return 1; } } @@ -1321,6 +1325,9 @@ void WINAPI service_main(unsigned long argc, TCHAR **argv) { } } + /* Remember our initial environment. */ + service->initial_env = GetEnvironmentStrings(); + monitor_service(service); } @@ -1516,22 +1523,20 @@ int start_service(nssm_service_t *service) { throttle_restart(service); + /* Set the environment. */ + if (service->env) duplicate_environment(service->env); + if (service->env_extra) set_environment_block(service->env_extra); + bool inherit_handles = false; if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true; unsigned long flags = service->priority & priority_mask(); if (service->affinity) flags |= CREATE_SUSPENDED; -#ifdef UNICODE - flags |= CREATE_UNICODE_ENVIRONMENT; -#endif - if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, service->env, service->dir, &si, &pi)) { + if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, 0, service->dir, &si, &pi)) { unsigned long exitcode = 3; unsigned long error = GetLastError(); - if (error == ERROR_INVALID_PARAMETER && service->env) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service->name, service->exe, NSSM_REG_ENV, 0); - if (test_environment(service->env)) exitcode = 4; - } - else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name, service->exe, error_string(error), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name, service->exe, error_string(error), 0); close_output_handles(&si); + duplicate_environment(service->initial_env); return stop_service(service, exitcode, true, true); } service->process_handle = pi.hProcess; @@ -1541,6 +1546,9 @@ int start_service(nssm_service_t *service) { close_output_handles(&si); + /* Restore our environment. */ + duplicate_environment(service->initial_env); + if (service->affinity) { /* We are explicitly storing service->affinity as a 64-bit unsigned integer @@ -1689,6 +1697,8 @@ void CALLBACK end_service(void *arg, unsigned char why) { if (service->pid) kill_process_tree(service, service->pid, exitcode, service->pid); service->pid = 0; + if (! service->no_console) FreeConsole(); + /* The why argument is true if our wait timed out or false otherwise. Our wait is infinite so why will never be true when called by the system.