@@ -273,6 +273,17 @@ static inline unsigned long throttle_milliseconds(unsigned long throttle) {
273
273
return ret * 1000 ;
274
274
}
275
275
276
+ void set_service_environment (nssm_service_t *service) {
277
+ if (! service) return ;
278
+ if (service->env ) duplicate_environment (service->env );
279
+ if (service->env_extra ) set_environment_block (service->env_extra );
280
+ }
281
+
282
+ void unset_service_environment (nssm_service_t *service) {
283
+ if (! service) return ;
284
+ duplicate_environment_strings (service->initial_env );
285
+ }
286
+
276
287
/*
277
288
Wrapper to be called in a new thread so that we can acknowledge a STOP
278
289
control immediately.
@@ -1684,22 +1695,20 @@ int start_service(nssm_service_t *service) {
1684
1695
int ret = get_parameters (service, &si);
1685
1696
if (ret) {
1686
1697
log_event (EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_PARAMETERS_FAILED, service->name , 0 );
1698
+ unset_service_environment (service);
1687
1699
return stop_service (service, 2 , true , true );
1688
1700
}
1689
1701
1690
1702
/* Launch executable with arguments */
1691
1703
TCHAR cmd[CMD_LENGTH];
1692
1704
if (_sntprintf_s (cmd, _countof (cmd), _TRUNCATE, _T (" \" %s\" %s" ), service->exe , service->flags ) < 0 ) {
1693
1705
log_event (EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T (" command line" ), _T (" start_service" ), 0 );
1706
+ unset_service_environment (service);
1694
1707
return stop_service (service, 2 , true , true );
1695
1708
}
1696
1709
1697
1710
throttle_restart (service);
1698
1711
1699
- /* Set the environment. */
1700
- if (service->env ) duplicate_environment (service->env );
1701
- if (service->env_extra ) set_environment_block (service->env_extra );
1702
-
1703
1712
service->status .dwCurrentState = SERVICE_START_PENDING;
1704
1713
service->status .dwControlsAccepted = SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
1705
1714
SetServiceStatus (service->status_handle , &service->status );
@@ -1710,7 +1719,7 @@ int start_service(nssm_service_t *service) {
1710
1719
TCHAR code[16 ];
1711
1720
_sntprintf_s (code, _countof (code), _TRUNCATE, _T (" %lu" ), NSSM_HOOK_STATUS_ABORT);
1712
1721
log_event (EVENTLOG_ERROR_TYPE, NSSM_EVENT_PRESTART_HOOK_ABORT, NSSM_HOOK_EVENT_START, NSSM_HOOK_ACTION_PRE, service->name , code, 0 );
1713
- duplicate_environment_strings (service-> initial_env );
1722
+ unset_service_environment (service);
1714
1723
return stop_service (service, 5 , true , true );
1715
1724
}
1716
1725
@@ -1721,7 +1730,7 @@ int start_service(nssm_service_t *service) {
1721
1730
log_event (EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name , 0 );
1722
1731
if (! service->no_console ) FreeConsole ();
1723
1732
close_output_handles (&si);
1724
- duplicate_environment_strings (service-> initial_env );
1733
+ unset_service_environment (service);
1725
1734
return stop_service (service, 4 , true , true );
1726
1735
}
1727
1736
@@ -1734,7 +1743,7 @@ int start_service(nssm_service_t *service) {
1734
1743
unsigned long error = GetLastError ();
1735
1744
log_event (EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name , service->exe , error_string (error), 0 );
1736
1745
close_output_handles (&si);
1737
- duplicate_environment_strings (service-> initial_env );
1746
+ unset_service_environment (service);
1738
1747
return stop_service (service, exitcode, true , true );
1739
1748
}
1740
1749
service->start_count ++;
@@ -1778,7 +1787,7 @@ int start_service(nssm_service_t *service) {
1778
1787
}
1779
1788
1780
1789
/* Restore our environment. */
1781
- duplicate_environment_strings (service-> initial_env );
1790
+ unset_service_environment (service);
1782
1791
1783
1792
/*
1784
1793
Wait for a clean startup before changing the service status to RUNNING
0 commit comments