Ensure we have a valid service exit time.
[nssm.git] / process.cpp
index 69aea03..c0b65df 100644 (file)
@@ -23,6 +23,7 @@ int get_process_exit_time(HANDLE process_handle, FILETIME *ft) {
     return 1;
   }
 
+  if (! (exit_time.dwLowDateTime || exit_time.dwHighDateTime)) return 2;
   memmove(ft, &exit_time, sizeof(exit_time));
 
   return 0;
@@ -151,7 +152,7 @@ int kill_process(nssm_service_t *service, HANDLE process_handle, unsigned long p
 
   /* Try to send a Control-C event to the console. */
   if (service->stop_method & NSSM_STOP_METHOD_CONSOLE) {
-    if (! kill_console(service)) return 1;
+    if (! kill_console(service, &k)) return 1;
   }
 
   /*
@@ -186,7 +187,7 @@ int kill_process(nssm_service_t *service, HANDLE process_handle, unsigned long p
 }
 
 /* Simulate a Control-C event to our console (shared with the app). */
-int kill_console(nssm_service_t *service) {
+int kill_console(nssm_service_t *service, kill_t *k) {
   unsigned long ret;
 
   if (! service) return 1;
@@ -195,7 +196,7 @@ int kill_console(nssm_service_t *service) {
   if (! imports.AttachConsole) return 4;
 
   /* Try to attach to the process's console. */
-  if (! imports.AttachConsole(service->pid)) {
+  if (! imports.AttachConsole(k->pid)) {
     ret = GetLastError();
 
     switch (ret) {
@@ -306,39 +307,3 @@ void kill_process_tree(nssm_service_t *service, unsigned long pid, unsigned long
 
   CloseHandle(process_handle);
 }
-
-/*
-  Verify an environment block.
-  Returns:  1 if environment is invalid.
-            0 if environment is OK.
-           -1 on error.
-*/
-int test_environment(TCHAR *env) {
-  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
-
-  /*
-    Try to relaunch ourselves but with the candidate environment set.
-    Assuming no solar flare activity, the only reason this would fail is if
-    the environment were invalid.
-  */
-  if (CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) {
-    TerminateProcess(pi.hProcess, 0);
-  }
-  else {
-    unsigned long error = GetLastError();
-    if (error == ERROR_INVALID_PARAMETER) return 1;
-    else return -1;
-  }
-
-  return 0;
-}