Allow setting processor affinity.
[nssm.git] / service.cpp
index 72e0192..93b7d4a 100644 (file)
@@ -13,6 +13,129 @@ const TCHAR *exit_action_strings[] = { _T("Restart"), _T("Ignore"), _T("Exit"),
 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
@@ -1288,6 +1411,7 @@ int start_service(nssm_service_t *service) {
   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
@@ -1309,6 +1433,35 @@ int start_service(nssm_service_t *service) {
 \r
   close_output_handles(&si);\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
     but be mindful of the fact that we are blocking the service control manager\r