NSSM 2.15.
[nssm.git] / nssm.cpp
index dec4745..1ee38bd 100644 (file)
--- a/nssm.cpp
+++ b/nssm.cpp
@@ -58,15 +58,28 @@ int main(int argc, char **argv) {
   /* Register messages */\r
   if (is_admin) create_messages();\r
 \r
-  /* Start service magic */\r
-  SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
-  if (! StartServiceCtrlDispatcher(table)) {\r
-    unsigned long error = GetLastError();\r
-    /* User probably ran nssm with no argument */\r
-    if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));\r
-    log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);\r
-    exit(100);\r
+  /*\r
+    Optimisation for Windows 2000:\r
+    When we're run from the command line the StartServiceCtrlDispatcher() call\r
+    will time out after a few seconds on Windows 2000.  On newer versions the\r
+    call returns instantly.  Check for stdin first and only try to call the\r
+    function if there's no input stream found.  Although it's possible that\r
+    we're running with input redirected it's much more likely that we're\r
+    actually running as a service.\r
+    This will save time when running with no arguments from a command prompt.\r
+  */\r
+  if (_fileno(stdin) < 0) {\r
+    /* Start service magic */\r
+    SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
+    if (! StartServiceCtrlDispatcher(table)) {\r
+      unsigned long error = GetLastError();\r
+      /* User probably ran nssm with no argument */\r
+      if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));\r
+      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);\r
+      exit(100);\r
+    }\r
   }\r
+  else exit(usage(1));\r
 \r
   /* And nothing more to do */\r
   exit(0);\r