if (service->wait_handle) UnregisterWait(service->process_handle);\r
if (service->throttle_section_initialised) DeleteCriticalSection(&service->throttle_section);\r
if (service->throttle_timer) CloseHandle(service->throttle_timer);\r
+ if (service->initial_env) FreeEnvironmentStrings(service->initial_env);\r
HeapFree(GetProcessHeap(), 0, service);\r
}\r
\r
}\r
else {\r
CloseHandle(service_handle);\r
- _ftprintf(stderr, _T("%s: %s %s"), canonical_name, service_control_text(control), error_string(error));\r
+ _ftprintf(stderr, _T("%s: %s: %s"), canonical_name, service_control_text(control), error_string(error));\r
+ if (error == ERROR_SERVICE_NOT_ACTIVE) {\r
+ if (control == SERVICE_CONTROL_SHUTDOWN || control == SERVICE_CONTROL_STOP) return 0;\r
+ }\r
return 1;\r
}\r
}\r
}\r
}\r
\r
+ /* Remember our initial environment. */\r
+ service->initial_env = GetEnvironmentStrings();\r
+\r
monitor_service(service);\r
}\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
bool inherit_handles = false;\r
if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;\r
unsigned long flags = service->priority & priority_mask();\r
+ if (service->stdin_pipe) flags |= DETACHED_PROCESS;\r
if (service->affinity) flags |= CREATE_SUSPENDED;\r
-#ifdef UNICODE\r
- flags |= CREATE_UNICODE_ENVIRONMENT;\r
-#endif\r
- if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, service->env, service->dir, &si, &pi)) {\r
+ if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, 0, service->dir, &si, &pi)) {\r
unsigned long exitcode = 3;\r
unsigned long error = GetLastError();\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
+ 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(service->initial_env);\r
return stop_service(service, exitcode, true, true);\r
}\r
service->process_handle = pi.hProcess;\r
\r
close_output_handles(&si);\r
\r
+ /* Restore our environment. */\r
+ duplicate_environment(service->initial_env);\r
+\r
if (service->affinity) {\r
/*\r
We are explicitly storing service->affinity as a 64-bit unsigned integer\r
UnregisterWait(service->wait_handle);\r
service->wait_handle = 0;\r
}\r
+ if (service->stdin_pipe) {\r
+ CloseHandle(service->stdin_pipe);\r
+ service->stdin_pipe = 0;\r
+ }\r
+\r
+ service->rotate_stdout_online = service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;\r
\r
if (default_action && ! exitcode && ! graceful) {\r
log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_GRACEFUL_SUICIDE, service->name, service->exe, exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_REALLY], 0);\r
\r
service->stopping = true;\r
\r
+ service->rotate_stdout_online = service->rotate_stderr_online = NSSM_ROTATE_OFFLINE;\r
+\r
/* Check exit code */\r
unsigned long exitcode = 0;\r
TCHAR code[16];\r