Use test_environment().
authorIain Patterson <me@iain.cx>
Tue, 26 Nov 2013 17:23:11 +0000 (17:23 +0000)
committerIain Patterson <me@iain.cx>
Tue, 26 Nov 2013 20:38:08 +0000 (20:38 +0000)
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.

gui.cpp
service.cpp

diff --git a/gui.cpp b/gui.cpp
index 1c77bfc..67458af 100644 (file)
--- a/gui.cpp
+++ b/gui.cpp
@@ -185,29 +185,12 @@ int install(HWND window) {
       envlen = newlen;\r
 \r
       /* Test the environment is valid. */\r
-      TCHAR path[MAX_PATH];\r
-      GetModuleFileName(0, path, _countof(path));\r
-      STARTUPINFO si;\r
-      ZeroMemory(&si, sizeof(si));\r
-      si.cb = sizeof(si);\r
-      PROCESS_INFORMATION pi;\r
-      ZeroMemory(&pi, sizeof(pi));\r
-      unsigned long flags = CREATE_SUSPENDED;\r
-#ifdef UNICODE\r
-      flags |= CREATE_UNICODE_ENVIRONMENT;\r
-#endif\r
-\r
-      if (! CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) {\r
-        unsigned long error = GetLastError();\r
-        if (error == ERROR_INVALID_PARAMETER) {\r
-          popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);\r
-          HeapFree(GetProcessHeap(), 0, env);\r
-          envlen = 0;\r
-        }\r
+      if (test_environment(env)) {\r
+        popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_ENVIRONMENT);\r
+        HeapFree(GetProcessHeap(), 0, env);\r
         cleanup_nssm_service(service);\r
         return 5;\r
       }\r
-      TerminateProcess(pi.hProcess, 0);\r
 \r
       if (SendDlgItemMessage(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT_REPLACE, BM_GETCHECK, 0, 0) & BST_CHECKED) {\r
         service->env = env;\r
index ea4b633..30a75d3 100644 (file)
@@ -449,11 +449,15 @@ int start_service(nssm_service_t *service) {
   flags |= CREATE_UNICODE_ENVIRONMENT;\r
 #endif\r
   if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, service->env, service->dir, &si, &pi)) {\r
+    unsigned long exitcode = 3;\r
     unsigned long error = GetLastError();\r
-    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);\r
+    if (error == ERROR_INVALID_PARAMETER && service->env) {\r
+      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service->name, service->exe, NSSM_REG_ENV, 0);\r
+      if (test_environment(service->env)) exitcode = 4;\r
+    }\r
     else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name, service->exe, error_string(error), 0);\r
     close_output_handles(&si);\r
-    return stop_service(service, 3, true, true);\r
+    return stop_service(service, exitcode, true, true);\r
   }\r
   service->process_handle = pi.hProcess;\r
   service->pid = pi.dwProcessId;\r