const TCHAR *startup_strings[] = { _T("SERVICE_AUTO_START"), _T("SERVICE_DELAYED_AUTO_START"), _T("SERVICE_DEMAND_START"), _T("SERVICE_DISABLED"), 0 };\r
const TCHAR *priority_strings[] = { _T("REALTIME_PRIORITY_CLASS"), _T("HIGH_PRIORITY_CLASS"), _T("ABOVE_NORMAL_PRIORITY_CLASS"), _T("NORMAL_PRIORITY_CLASS"), _T("BELOW_NORMAL_PRIORITY_CLASS"), _T("IDLE_PRIORITY_CLASS"), 0 };\r
\r
+typedef struct {\r
+ int first;\r
+ int last;\r
+} list_t;\r
+\r
+int affinity_mask_to_string(__int64 mask, TCHAR **string) {\r
+ if (! string) return 1;\r
+ if (! mask) {\r
+ *string = 0;\r
+ return 0;\r
+ }\r
+\r
+ __int64 i, n;\r
+\r
+ /* SetProcessAffinityMask() accepts a mask of up to 64 processors. */\r
+ list_t set[64];\r
+ for (n = 0; n < _countof(set); n++) set[n].first = set[n].last = -1;\r
+\r
+ for (i = 0, n = 0; i < _countof(set); i++) {\r
+ if (mask & (1LL << i)) {\r
+ if (set[n].first == -1) set[n].first = set[n].last = (int) i;\r
+ else if (set[n].last == (int) i - 1) set[n].last = (int) i;\r
+ else {\r
+ n++;\r
+ set[n].first = set[n].last = (int) i;\r
+ }\r
+ }\r
+ }\r
+\r
+ /* Worst case is 2x2 characters for first and last CPU plus - and/or , */\r
+ size_t len = (size_t) (n + 1) * 6;\r
+ *string = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(TCHAR));\r
+ if (! string) return 2;\r
+\r
+ size_t s = 0;\r
+ int ret;\r
+ for (i = 0; i <= n; i++) {\r
+ if (i) (*string)[s++] = _T(',');\r
+ ret = _sntprintf_s(*string + s, 3, _TRUNCATE, _T("%u"), set[i].first);\r
+ if (ret < 0) {\r
+ HeapFree(GetProcessHeap(), 0, *string);\r
+ *string = 0;\r
+ return 3;\r
+ }\r
+ else s += ret;\r
+ if (set[i].last != set[i].first) {\r
+ ret =_sntprintf_s(*string + s, 4, _TRUNCATE, _T("%c%u"), (set[i].last == set[i].first + 1) ? _T(',') : _T('-'), set[i].last);\r
+ if (ret < 0) {\r
+ HeapFree(GetProcessHeap(), 0, *string);\r
+ *string = 0;\r
+ return 4;\r
+ }\r
+ else s += ret;\r
+ }\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+int affinity_string_to_mask(TCHAR *string, __int64 *mask) {\r
+ if (! mask) return 1;\r
+\r
+ *mask = 0LL;\r
+ if (! string) return 0;\r
+\r
+ list_t set[64];\r
+\r
+ TCHAR *s = string;\r
+ TCHAR *end;\r
+ int ret;\r
+ int i;\r
+ int n = 0;\r
+ unsigned long number;\r
+\r
+ for (n = 0; n < _countof(set); n++) set[n].first = set[n].last = -1;\r
+ n = 0;\r
+\r
+ while (*s) {\r
+ ret = str_number(s, &number, &end);\r
+ s = end;\r
+ if (ret == 0 || ret == 2) {\r
+ if (number >= _countof(set)) return 2;\r
+ set[n].first = set[n].last = (int) number;\r
+\r
+ switch (*s) {\r
+ case 0:\r
+ break;\r
+\r
+ case _T(','):\r
+ n++;\r
+ s++;\r
+ break;\r
+\r
+ case _T('-'):\r
+ if (! *(++s)) return 3;\r
+ ret = str_number(s, &number, &end);\r
+ if (ret == 0 || ret == 2) {\r
+ s = end;\r
+ if (! *s || *s == _T(',')) {\r
+ set[n].last = (int) number;\r
+ if (! *s) break;\r
+ n++;\r
+ s++;\r
+ }\r
+ else return 3;\r
+ }\r
+ else return 3;\r
+ break;\r
+\r
+ default:\r
+ return 3;\r
+ }\r
+ }\r
+ else return 4;\r
+ }\r
+\r
+ for (i = 0; i <= n; i++) {\r
+ for (int j = set[i].first; j <= set[i].last; j++) (__int64) *mask |= (1LL << (__int64) j);\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
inline 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
return NORMAL_PRIORITY_CLASS;\r
}\r
\r
-static inline int throttle_milliseconds(unsigned long throttle) {\r
+static inline unsigned long throttle_milliseconds(unsigned long throttle) {\r
/* pow() operates on doubles. */\r
- int ret = 1; for (unsigned long i = 1; i < throttle; i++) ret *= 2;\r
+ unsigned long ret = 1; for (unsigned long i = 1; i < throttle; i++) ret *= 2;\r
return ret * 1000;\r
}\r
\r
for (i = 0; settings[i].name; i++) _ftprintf(stderr, _T("%s\n"), settings[i].name);\r
return 1;\r
}\r
- if (argc < mandatory) return usage(1);\r
\r
additional = 0;\r
if (additional_mandatory) {\r
+ if (argc < mandatory) {\r
+ print_message(stderr, NSSM_MESSAGE_MISSING_SUBPARAMETER, parameter);\r
+ return 1;\r
+ }\r
additional = argv[3];\r
remainder = 4;\r
}\r
- else additional = argv[remainder];\r
+ else {\r
+ additional = argv[remainder];\r
+ if (argc < mandatory) return usage(1);\r
+ }\r
}\r
\r
nssm_service_t *service = alloc_nssm_service();\r
int ret;\r
unsigned long error;\r
SERVICE_STATUS service_status;\r
- if (control == 0) {\r
+ if (control == NSSM_SERVICE_CONTROL_START) {\r
ret = StartService(service_handle, (unsigned long) argc, (const TCHAR **) argv);\r
error = GetLastError();\r
CloseHandle(service_handle);\r
TCHAR *service_control_text(unsigned long control) {\r
switch (control) {\r
/* HACK: there is no SERVICE_CONTROL_START constant */\r
- case 0: return _T("START");\r
+ case NSSM_SERVICE_CONTROL_START: return _T("START");\r
case SERVICE_CONTROL_STOP: return _T("STOP");\r
case SERVICE_CONTROL_SHUTDOWN: return _T("SHUTDOWN");\r
case SERVICE_CONTROL_PAUSE: return _T("PAUSE");\r
case SERVICE_CONTROL_CONTINUE: return _T("CONTINUE");\r
case SERVICE_CONTROL_INTERROGATE: return _T("INTERROGATE");\r
+ case NSSM_SERVICE_CONTROL_ROTATE: return _T("ROTATE");\r
default: return 0;\r
}\r
}\r
*/\r
log_service_control(service->name, control, false);\r
return ERROR_CALL_NOT_IMPLEMENTED;\r
+\r
+ case NSSM_SERVICE_CONTROL_ROTATE:\r
+ log_service_control(service->name, control, true);\r
+ if (service->rotate_stdout_online) service->rotate_stdout_online = NSSM_ROTATE_ONLINE_ASAP;\r
+ if (service->rotate_stdout_online) service->rotate_stderr_online = NSSM_ROTATE_ONLINE_ASAP;\r
+ return NO_ERROR;\r
}\r
\r
/* Unknown control */\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->affinity) flags |= CREATE_SUSPENDED;\r
#ifdef UNICODE\r
flags |= CREATE_UNICODE_ENVIRONMENT;\r
#endif\r
\r
if (get_process_creation_time(service->process_handle, &service->creation_time)) ZeroMemory(&service->creation_time, sizeof(service->creation_time));\r
\r
- close_output_handles(&si);\r
+ close_output_handles(&si, ! service->rotate_stdout_online, ! service->rotate_stderr_online);\r
+\r
+ if (service->affinity) {\r
+ /*\r
+ We are explicitly storing service->affinity as a 64-bit unsigned integer\r
+ so that we can parse it regardless of whether we're running in 32-bit\r
+ or 64-bit mode. The arguments to SetProcessAffinityMask(), however, are\r
+ defined as type DWORD_PTR and hence limited to 32 bits on a 32-bit system\r
+ (or when running the 32-bit NSSM).\r
+\r
+ The result is a lot of seemingly-unnecessary casting throughout the code\r
+ and potentially confusion when we actually try to start the service.\r
+ Having said that, however, it's unlikely that we're actually going to\r
+ run in 32-bit mode on a system which has more than 32 CPUs so the\r
+ likelihood of seeing a confusing situation is somewhat diminished.\r
+ */\r
+ DWORD_PTR affinity, system_affinity;\r
+\r
+ if (GetProcessAffinityMask(service->process_handle, &affinity, &system_affinity)) affinity = service->affinity & system_affinity;\r
+ else {\r
+ affinity = (DWORD_PTR) service->affinity;\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GETPROCESSAFFINITYMASK_FAILED, service->name, error_string(GetLastError()), 0);\r
+ }\r
+\r
+ if (! SetProcessAffinityMask(service->process_handle, affinity)) {\r
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_SETPROCESSAFFINITYMASK_FAILED, service->name, error_string(GetLastError()), 0);\r
+ }\r
+\r
+ ResumeThread(pi.hThread);\r
+ }\r
\r
/*\r
Wait for a clean startup before changing the service status to RUNNING\r
else service->throttle = 0;\r
}\r
\r
+ /* Ensure the restart delay is always applied. */\r
+ if (service->restart_delay && ! service->throttle) service->throttle++;\r
+\r
return 0;\r
}\r
\r
/* This can't be a restart if the service is already running. */\r
if (! service->throttle++) return;\r
\r
- int ms = throttle_milliseconds(service->throttle);\r
+ unsigned long ms;\r
+ unsigned long throttle_ms = throttle_milliseconds(service->throttle);\r
+ TCHAR threshold[8], milliseconds[8];\r
+\r
+ if (service->restart_delay > throttle_ms) ms = service->restart_delay;\r
+ else ms = throttle_ms;\r
\r
if (service->throttle > 7) service->throttle = 8;\r
\r
- TCHAR threshold[8], milliseconds[8];\r
- _sntprintf_s(threshold, _countof(threshold), _TRUNCATE, _T("%lu"), service->throttle_delay);\r
_sntprintf_s(milliseconds, _countof(milliseconds), _TRUNCATE, _T("%lu"), ms);\r
- log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service->name, threshold, milliseconds, 0);\r
+\r
+ if (service->throttle == 1 && service->restart_delay > throttle_ms) log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESTART_DELAY, service->name, milliseconds, 0);\r
+ else {\r
+ _sntprintf_s(threshold, _countof(threshold), _TRUNCATE, _T("%lu"), service->throttle_delay);\r
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service->name, threshold, milliseconds, 0);\r
+ }\r
\r
if (use_critical_section) EnterCriticalSection(&service->throttle_section);\r
else if (service->throttle_timer) {\r