Alternative open_registry_key() signature.
[nssm.git] / service.cpp
index 6cdad35..d864fa2 100644 (file)
@@ -240,7 +240,7 @@ int affinity_string_to_mask(TCHAR *string, __int64 *mask) {
   return 0;\r
 }\r
 \r
-inline unsigned long priority_mask() {\r
+unsigned long priority_mask() {\r
  return REALTIME_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | ABOVE_NORMAL_PRIORITY_CLASS | NORMAL_PRIORITY_CLASS | BELOW_NORMAL_PRIORITY_CLASS | IDLE_PRIORITY_CLASS;\r
 }\r
 \r
@@ -273,6 +273,27 @@ static inline unsigned long throttle_milliseconds(unsigned long throttle) {
   return ret * 1000;\r
 }\r
 \r
+void set_service_environment(nssm_service_t *service) {\r
+  if (! service) return;\r
+\r
+  /*\r
+    We have to duplicate the block because this function will be called\r
+    multiple times between registry reads.\r
+  */\r
+  if (service->env) duplicate_environment_strings(service->env);\r
+  if (! service->env_extra) return;\r
+  TCHAR *env_extra = copy_environment_block(service->env_extra);\r
+  if (! env_extra) return;\r
+\r
+  set_environment_block(env_extra);\r
+  HeapFree(GetProcessHeap(), 0, env_extra);\r
+}\r
+\r
+void unset_service_environment(nssm_service_t *service) {\r
+  if (! service) return;\r
+  duplicate_environment_strings(service->initial_env);\r
+}\r
+\r
 /*\r
   Wrapper to be called in a new thread so that we can acknowledge a STOP\r
   control immediately.\r
@@ -753,7 +774,7 @@ void cleanup_nssm_service(nssm_service_t *service) {
   if (service->throttle_section_initialised) DeleteCriticalSection(&service->throttle_section);\r
   if (service->throttle_timer) CloseHandle(service->throttle_timer);\r
   if (service->hook_section_initialised) DeleteCriticalSection(&service->hook_section);\r
-  if (service->initial_env) FreeEnvironmentStrings(service->initial_env);\r
+  if (service->initial_env) HeapFree(GetProcessHeap(), 0, service->initial_env);\r
   HeapFree(GetProcessHeap(), 0, service);\r
 }\r
 \r
@@ -1105,7 +1126,7 @@ int install_service(nssm_service_t *service) {
   }\r
 \r
   /* Get path of this program */\r
-  GetModuleFileName(0, service->image, _countof(service->image));\r
+  _sntprintf_s(service->image, _countof(service->image), _TRUNCATE, _T("%s"), nssm_imagepath());\r
 \r
   /* Create the service - settings will be changed in edit_service() */\r
   service->handle = CreateService(services, service->name, service->name, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, service->image, 0, 0, 0, 0, 0);\r
@@ -1458,7 +1479,7 @@ void WINAPI service_main(unsigned long argc, TCHAR **argv) {
   service->hook_section_initialised = true;\r
 \r
   /* Remember our initial environment. */\r
-  service->initial_env = GetEnvironmentStrings();\r
+  service->initial_env = copy_environment();\r
 \r
   /* Remember our creation time. */\r
   if (get_process_creation_time(GetCurrentProcess(), &service->nssm_creation_time)) ZeroMemory(&service->nssm_creation_time, sizeof(service->nssm_creation_time));\r
@@ -1684,6 +1705,7 @@ int start_service(nssm_service_t *service) {
   int ret = get_parameters(service, &si);\r
   if (ret) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, service->name, 0);\r
+    unset_service_environment(service);\r
     return stop_service(service, 2, true, true);\r
   }\r
 \r
@@ -1691,15 +1713,12 @@ int start_service(nssm_service_t *service) {
   TCHAR cmd[CMD_LENGTH];\r
   if (_sntprintf_s(cmd, _countof(cmd), _TRUNCATE, _T("\"%s\" %s"), service->exe, service->flags) < 0) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("command line"), _T("start_service"), 0);\r
+    unset_service_environment(service);\r
     return stop_service(service, 2, true, true);\r
   }\r
 \r
   throttle_restart(service);\r
 \r
-  /* Set the environment. */\r
-  if (service->env) duplicate_environment(service->env);\r
-  if (service->env_extra) set_environment_block(service->env_extra);\r
-\r
   service->status.dwCurrentState = SERVICE_START_PENDING;\r
   service->status.dwControlsAccepted = SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;\r
   SetServiceStatus(service->status_handle, &service->status);\r
@@ -1710,7 +1729,7 @@ int start_service(nssm_service_t *service) {
     TCHAR code[16];\r
     _sntprintf_s(code, _countof(code), _TRUNCATE, _T("%lu"), NSSM_HOOK_STATUS_ABORT);\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_PRESTART_HOOK_ABORT, NSSM_HOOK_EVENT_START, NSSM_HOOK_ACTION_PRE, service->name, code, 0);\r
-    duplicate_environment_strings(service->initial_env);\r
+    unset_service_environment(service);\r
     return stop_service(service, 5, true, true);\r
   }\r
 \r
@@ -1721,10 +1740,13 @@ int start_service(nssm_service_t *service) {
       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0);\r
       if (! service->no_console) FreeConsole();\r
       close_output_handles(&si);\r
-      duplicate_environment_strings(service->initial_env);\r
+      unset_service_environment(service);\r
       return stop_service(service, 4, true, true);\r
     }\r
 \r
+    /* The pre-start hook will have cleaned the environment. */\r
+    set_service_environment(service);\r
+\r
     bool inherit_handles = false;\r
     if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;\r
     unsigned long flags = service->priority & priority_mask();\r
@@ -1734,7 +1756,7 @@ int start_service(nssm_service_t *service) {
       unsigned long error = GetLastError();\r
       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name, service->exe, error_string(error), 0);\r
       close_output_handles(&si);\r
-      duplicate_environment_strings(service->initial_env);\r
+      unset_service_environment(service);\r
       return stop_service(service, exitcode, true, true);\r
     }\r
     service->start_count++;\r
@@ -1778,7 +1800,7 @@ int start_service(nssm_service_t *service) {
   }\r
 \r
   /* Restore our environment. */\r
-  duplicate_environment_strings(service->initial_env);\r
+  unset_service_environment(service);\r
 \r
   /*\r
     Wait for a clean startup before changing the service status to RUNNING\r