Compiler food.
[nssm.git] / nssm.cpp
index dec4745..a60c378 100644 (file)
--- a/nssm.cpp
+++ b/nssm.cpp
@@ -2,6 +2,7 @@
 \r
 extern unsigned long tls_index;\r
 extern bool is_admin;\r
+extern imports_t imports;\r
 \r
 /* String function */\r
 int str_equiv(const char *a, const char *b) {\r
@@ -12,6 +13,16 @@ int str_equiv(const char *a, const char *b) {
   }\r
 }\r
 \r
+/* Remove basename of a path. */\r
+void strip_basename(char *buffer) {\r
+  size_t len = strlen(buffer);\r
+  size_t i;\r
+  for (i = len; i && buffer[i] != '\\' && buffer[i] != '/'; i--);\r
+  /* X:\ is OK. */\r
+  if (i && buffer[i-1] == ':') i++;\r
+  buffer[i] = '\0';\r
+}\r
+\r
 /* How to use me correctly */\r
 int usage(int ret) {\r
   print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
@@ -58,15 +69,32 @@ 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
+    /* Set up function pointers. */\r
+    if (get_imports()) exit(111);\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
+      free_imports();\r
+      exit(100);\r
+    }\r
   }\r
+  else exit(usage(1));\r
 \r
   /* And nothing more to do */\r
   exit(0);\r