Browse Source

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.
Iain Patterson 9 years ago
parent
commit
f113d025a4
2 changed files with 9 additions and 22 deletions
  1. 3 20
      gui.cpp
  2. 6 2
      service.cpp

+ 3 - 20
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;

+ 6 - 2
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;