Rotate files while the service is running.
[nssm.git] / process.cpp
index 34b07d3..69aea03 100644 (file)
@@ -40,8 +40,8 @@ int check_parent(nssm_service_t *service, PROCESSENTRY32 *pe, unsigned long ppid
   */
   HANDLE process_handle = OpenProcess(PROCESS_QUERY_INFORMATION, false, pe->th32ProcessID);
   if (! process_handle) {
-    char pid_string[16];
-    _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%lu", pe->th32ProcessID);
+    TCHAR pid_string[16];
+    _sntprintf_s(pid_string, _countof(pid_string), _TRUNCATE, _T("%lu"), pe->th32ProcessID);
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENPROCESS_FAILED, pid_string, service->name, error_string(GetLastError()), 0);
     return 2;
   }
@@ -90,7 +90,7 @@ int CALLBACK kill_window(HWND window, LPARAM arg) {
   processes so this function returns only true if at least one thread was
   successfully prodded.
 */
-int kill_threads(char *service_name, kill_t *k) {
+int kill_threads(TCHAR *service_name, kill_t *k) {
   int ret = 0;
 
   /* Get a snapshot of all threads in the system. */
@@ -162,7 +162,7 @@ int kill_process(nssm_service_t *service, HANDLE process_handle, unsigned long p
   if (service->stop_method & NSSM_STOP_METHOD_WINDOW) {
     EnumWindows((WNDENUMPROC) kill_window, (LPARAM) &k);
     if (k.signalled) {
-      if (! await_shutdown(service, __FUNCTION__, service->kill_window_delay)) return 1;
+      if (! await_shutdown(service, _T(__FUNCTION__), service->kill_window_delay)) return 1;
     }
   }
 
@@ -173,7 +173,7 @@ int kill_process(nssm_service_t *service, HANDLE process_handle, unsigned long p
   */
   if (service->stop_method & NSSM_STOP_METHOD_THREADS) {
     if (kill_threads(service->name, &k)) {
-      if (! await_shutdown(service, __FUNCTION__, service->kill_threads_delay)) return 1;
+      if (! await_shutdown(service, _T(__FUNCTION__), service->kill_threads_delay)) return 1;
     }
   }
 
@@ -236,7 +236,7 @@ int kill_console(nssm_service_t *service) {
   }
 
   /* Wait for process to exit. */
-  if (await_shutdown(service, __FUNCTION__, service->kill_console_delay)) ret = 6;
+  if (await_shutdown(service, _T(__FUNCTION__), service->kill_console_delay)) ret = 6;
 
   return ret;
 }
@@ -245,9 +245,9 @@ void kill_process_tree(nssm_service_t *service, unsigned long pid, unsigned long
   /* Shouldn't happen unless the service failed to start. */
   if (! pid) return;
 
-  char pid_string[16], code[16];
-  _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%lu", pid);
-  _snprintf_s(code, sizeof(code), _TRUNCATE, "%lu", exitcode);
+  TCHAR pid_string[16], code[16];
+  _sntprintf_s(pid_string, _countof(pid_string), _TRUNCATE, _T("%lu"), pid);
+  _sntprintf_s(code, _countof(code), _TRUNCATE, _T("%lu"), exitcode);
   log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILLING, service->name, pid_string, code, 0);
 
   /* Get a snapshot of all processes in the system. */
@@ -292,8 +292,8 @@ void kill_process_tree(nssm_service_t *service, unsigned long pid, unsigned long
     return;
   }
 
-  char ppid_string[16];
-  _snprintf_s(ppid_string, sizeof(ppid_string), _TRUNCATE, "%lu", ppid);
+  TCHAR ppid_string[16];
+  _sntprintf_s(ppid_string, _countof(ppid_string), _TRUNCATE, _T("%lu"), ppid);
   log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILL_PROCESS_TREE, pid_string, ppid_string, service->name, 0);
   if (! kill_process(service, process_handle, pid, exitcode)) {
     /* Maybe it already died. */
@@ -306,3 +306,39 @@ 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;
+}