From: Iain Patterson Date: Tue, 26 Nov 2013 17:23:11 +0000 (+0000) Subject: Use test_environment(). X-Git-Tag: v2.22~132 X-Git-Url: http://git.iain.cx/?p=nssm.git;a=commitdiff_plain;h=f113d025a47ea5a3dd0cb962b5913045ac811b71 Use test_environment(). Use the new test_environment() function when validating the environment in the GUI and when launching the monitored application fails with ERROR_INVALID_PARAMETER. In the latter case we set service-specific error code 4 to help identify the reason for the failure. --- diff --git a/gui.cpp b/gui.cpp index 1c77bfc..67458af 100644 --- a/gui.cpp +++ b/gui.cpp @@ -185,29 +185,12 @@ int install(HWND window) { envlen = newlen; /* Test the environment is valid. */ - TCHAR path[MAX_PATH]; - GetModuleFileName(0, path, _countof(path)); - STARTUPINFO si; - ZeroMemory(&si, sizeof(si)); - si.cb = sizeof(si); - PROCESS_INFORMATION pi; - ZeroMemory(&pi, sizeof(pi)); - unsigned long flags = CREATE_SUSPENDED; -#ifdef UNICODE - flags |= CREATE_UNICODE_ENVIRONMENT; -#endif - - if (! CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) { - unsigned long error = GetLastError(); - if (error == ERROR_INVALID_PARAMETER) { - popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT); - HeapFree(GetProcessHeap(), 0, env); - envlen = 0; - } + if (test_environment(env)) { + popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT); + HeapFree(GetProcessHeap(), 0, env); cleanup_nssm_service(service); return 5; } - TerminateProcess(pi.hProcess, 0); if (SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_GETCHECK, 0, 0) & BST_CHECKED) { service->env = env; diff --git a/service.cpp b/service.cpp index ea4b633..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;