Removed redundant NSSM_EVENT_STDIN_CREATEPIPE_FAILED.
[nssm.git] / nssm.cpp
index 8dd264a..e29ead6 100644 (file)
--- a/nssm.cpp
+++ b/nssm.cpp
@@ -55,26 +55,6 @@ void check_admin() {
   FreeSid(AdministratorsGroup);\r
 }\r
 \r
-/* See if we were launched from a console window. */\r
-static void check_console() {\r
-  /* If we're running in a service context there will be no console window. */\r
-  HWND console = GetConsoleWindow();\r
-  if (! console) return;\r
-\r
-  unsigned long pid;\r
-  if (! GetWindowThreadProcessId(console, &pid)) return;\r
-\r
-  /*\r
-    If the process associated with the console window handle is the same as\r
-    this process, we were not launched from an existing console.  The user\r
-    probably double-clicked our executable.\r
-  */\r
-  if (GetCurrentProcessId() != pid) return;\r
-\r
-  /* We close our new console so that subsequent messages appear in a popup. */\r
-  FreeConsole();\r
-}\r
-\r
 int num_cpus() {\r
   DWORD_PTR i, affinity, system_affinity;\r
   if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) return 64;\r
@@ -97,17 +77,26 @@ int _tmain(int argc, TCHAR **argv) {
   /* Remember if we are admin */\r
   check_admin();\r
 \r
+  /* Set up function pointers. */\r
+  if (get_imports()) exit(111);\r
+\r
   /* Elevate */\r
   if (argc > 1) {\r
     /*\r
       Valid commands are:\r
       start, stop, pause, continue, install, edit, get, set, reset, unset, remove\r
     */\r
-    if (str_equiv(argv[1], _T("start"))) exit(control_service(0, argc - 2, argv + 2));\r
+    if (str_equiv(argv[1], _T("start"))) exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));\r
     if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2));\r
+    if (str_equiv(argv[1], _T("restart"))) {\r
+      int ret = control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2);\r
+      if (ret) exit(ret);\r
+      exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2));\r
+    }\r
     if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2));\r
     if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2));\r
     if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2));\r
+    if (str_equiv(argv[1], _T("rotate"))) exit(control_service(NSSM_SERVICE_CONTROL_ROTATE, argc - 2, argv + 2));\r
     if (str_equiv(argv[1], _T("install"))) {\r
       if (! is_admin) {\r
         print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);\r
@@ -150,10 +139,7 @@ int _tmain(int argc, TCHAR **argv) {
     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
+  if (! GetStdHandle(STD_INPUT_HANDLE)) {\r
     /* Start service magic */\r
     SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
     if (! StartServiceCtrlDispatcher(table)) {\r