Added error message for missing subparameter.
[nssm.git] / service.cpp
index 93b7d4a..8074227 100644 (file)
@@ -162,9 +162,9 @@ unsigned long priority_index_to_constant(int index) {
   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
@@ -701,14 +701,20 @@ int pre_edit_service(int argc, TCHAR **argv) {
       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
@@ -1281,6 +1287,7 @@ TCHAR *service_control_text(unsigned long control) {
     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
@@ -1368,6 +1375,12 @@ unsigned long WINAPI service_control_handler(unsigned long control, unsigned lon
       */\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
@@ -1431,7 +1444,7 @@ int start_service(nssm_service_t *service) {
 \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
@@ -1490,6 +1503,9 @@ int start_service(nssm_service_t *service) {
     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
@@ -1632,14 +1648,22 @@ void throttle_restart(nssm_service_t *service) {
   /* 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