AttachConsole() isn't available in Windows 2000.
[nssm.git] / nssm.cpp
index 7a73114..0ce8664 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
@@ -14,17 +15,7 @@ int str_equiv(const char *a, const char *b) {
 \r
 /* How to use me correctly */\r
 int usage(int ret) {\r
-  fprintf(stderr, "NSSM: The non-sucking service manager\n");\r
-  fprintf(stderr, "Version %s, %s\n", NSSM_VERSION, NSSM_DATE);\r
-  fprintf(stderr, "Usage: nssm <option> [args]\n\n");\r
-  fprintf(stderr, "To show service installation GUI:\n\n");\r
-  fprintf(stderr, "        nssm install [<servicename>]\n\n");\r
-  fprintf(stderr, "To install a service without confirmation:\n\n");\r
-  fprintf(stderr, "        nssm install <servicename> <app> [<args>]\n\n");\r
-  fprintf(stderr, "To show service removal GUI:\n\n");\r
-  fprintf(stderr, "        nssm remove [<servicename>]\n\n");\r
-  fprintf(stderr, "To remove a service without confirmation:\n\n");\r
-  fprintf(stderr, "        nssm remove <servicename> confirm\n");\r
+  print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_DATE);\r
   return(ret);\r
 }\r
 \r
@@ -45,18 +36,19 @@ int main(int argc, char **argv) {
 \r
   /* Elevate */\r
   if (argc > 1) {\r
-    if (str_equiv(argv[1], "install") || str_equiv(argv[1], "remove")) {\r
+    /* Valid commands are install or remove */\r
+    if (str_equiv(argv[1], "install")) {\r
       if (! is_admin) {\r
-        fprintf(stderr, "Administrator access is needed to %s a service.\n", argv[1]);\r
+        print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);\r
         exit(100);\r
       }\r
-    }\r
-\r
-    /* Valid commands are install or remove */\r
-    if (str_equiv(argv[1], "install")) {\r
       exit(pre_install_service(argc - 2, argv + 2));\r
     }\r
     if (str_equiv(argv[1], "remove")) {\r
+      if (! is_admin) {\r
+        print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);\r
+        exit(100);\r
+      }\r
       exit(pre_remove_service(argc - 2, argv + 2));\r
     }\r
   }\r
@@ -67,15 +59,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