X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;ds=sidebyside;f=service.cpp;h=30a75d3d4cccf06003cf175b23f01e561133a336;hb=2f219930f488b2551326900df5c201de2e9304f2;hp=294497a7e42d3694a83a3ed49592100333bf3518;hpb=ef26a193bb671cd22d498839f205a0ec4523518e;p=nssm.git diff --git a/service.cpp b/service.cpp index 294497a..30a75d3 100644 --- a/service.cpp +++ b/service.cpp @@ -449,11 +449,15 @@ int start_service(nssm_service_t *service) { flags |= CREATE_UNICODE_ENVIRONMENT; #endif if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, service->env, 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 (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); close_output_handles(&si); - return stop_service(service, 3, true, true); + return stop_service(service, exitcode, true, true); } service->process_handle = pi.hProcess; service->pid = pi.dwProcessId; @@ -551,12 +555,14 @@ void CALLBACK end_service(void *arg, unsigned char why) { /* Check exit code */ unsigned long exitcode = 0; TCHAR code[16]; - GetExitCodeProcess(service->process_handle, &exitcode); - if (exitcode == STILL_ACTIVE || get_process_exit_time(service->process_handle, &service->exit_time)) GetSystemTimeAsFileTime(&service->exit_time); - CloseHandle(service->process_handle); + if (service->process_handle) { + GetExitCodeProcess(service->process_handle, &exitcode); + if (exitcode == STILL_ACTIVE || get_process_exit_time(service->process_handle, &service->exit_time)) GetSystemTimeAsFileTime(&service->exit_time); + CloseHandle(service->process_handle); + } + else GetSystemTimeAsFileTime(&service->exit_time); service->process_handle = 0; - service->pid = 0; /* Log that the service ended BEFORE logging about killing the process @@ -569,7 +575,8 @@ void CALLBACK end_service(void *arg, unsigned char why) { /* Clean up. */ if (exitcode == STILL_ACTIVE) exitcode = 0; - kill_process_tree(service, service->pid, exitcode, service->pid); + if (service->pid) kill_process_tree(service, service->pid, exitcode, service->pid); + service->pid = 0; /* The why argument is true if our wait timed out or false otherwise.