Переглянути джерело

Moved console functions to a new file.

Functions for allocating and setting up a console are moved to
console.cpp.
Iain Patterson 11 роки тому
батько
коміт
b0a6672810
7 змінених файлів з 189 додано та 171 видалено
  1. 170 0
      console.cpp
  2. 7 0
      console.h
  3. 1 19
      io.cpp
  4. 0 149
      nssm.cpp
  5. 1 1
      nssm.h
  6. 8 0
      nssm.vcproj
  7. 2 2
      service.cpp

+ 170 - 0
console.cpp

@@ -0,0 +1,170 @@
+#include "nssm.h"
+
+/* See if we were launched from a console window. */
+void check_console() {
+  /* If we're running in a service context there will be no console window. */
+  HWND console = GetConsoleWindow();
+  if (! console) return;
+
+  unsigned long pid;
+  if (! GetWindowThreadProcessId(console, &pid)) return;
+
+  /*
+    If the process associated with the console window handle is the same as
+    this process, we were not launched from an existing console.  The user
+    probably double-clicked our executable.
+  */
+  if (GetCurrentProcessId() != pid) return;
+
+  /* We close our new console so that subsequent messages appear in a popup. */
+  FreeConsole();
+}
+
+/* Helpers for drawing the banner. */
+static inline void block(unsigned int a, short x, short y, unsigned long n) {
+  HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
+  TCHAR s = _T(' ');
+
+  unsigned long out;
+  COORD c = { x, y };
+  FillConsoleOutputAttribute(h, a, n, c, &out);
+  FillConsoleOutputCharacter(h, s, n, c, &out);
+}
+
+static inline void R(short x, short y, unsigned long n) {
+  block(BACKGROUND_RED | BACKGROUND_INTENSITY, x, y, n);
+}
+
+static inline void r(short x, short y, unsigned long n) {
+  block(BACKGROUND_RED, x, y, n);
+}
+
+static inline void b(short x, short y, unsigned long n) {
+  block(0, x, y, n);
+}
+
+void alloc_console(nssm_service_t *service) {
+  if (service->no_console) return;
+
+  AllocConsole();
+
+  /* Set a title like "[NSSM] Jenkins" */
+  TCHAR displayname[SERVICE_NAME_LENGTH];
+  unsigned long len = _countof(displayname);
+  SC_HANDLE services = open_service_manager();
+  if (services) {
+    if (! GetServiceDisplayName(services, service->name, displayname, &len)) ZeroMemory(displayname, sizeof(displayname));
+    CloseServiceHandle(services);
+  }
+  if (! displayname[0]) _sntprintf_s(displayname, _countof(displayname), _TRUNCATE, _T("%s"), service->name);
+
+  TCHAR title[65535];
+  _sntprintf_s(title, _countof(title), _TRUNCATE, _T("[%s] %s"), NSSM, displayname);
+  SetConsoleTitle(title);
+
+  /* Draw the NSSM logo on the console window. */
+  short y = 0;
+
+  b(0, y, 80);
+  y++;
+
+  b(0, y, 80);
+  y++;
+
+  b(0, y, 80);
+  y++;
+
+  b(0, y, 80);
+  y++;
+
+  b(0, y, 80);
+  r(18, y, 5); r(28, y, 4); r(41, y, 4); r(68, y, 1);
+  R(6, y, 5); R(19, y, 4); R(29, y, 1); R(32, y, 3); R(42, y, 1); R(45, y, 3); R(52, y, 5); R(69, y, 4);
+  y++;
+
+  b(0, y, 80);
+  r(8, y, 4); r(20, y, 1); r(28, y, 1); r(33, y, 3); r(41, y, 1); r(46, y, 3); r (57, y, 1);
+  R(9, y, 2); R(21, y, 1); R(27, y, 1); R(34, y, 1); R(40, y, 1); R(47, y, 1); R(54, y, 3); R(68, y, 3);
+  y++;
+
+  b(0, y, 80);
+  r(12, y, 1); r(20, y, 1); r(26, y, 1); r(34, y, 2); r(39, y, 1); r(47, y, 2); r(67, y, 2);
+  R(9, y, 3); R(21, y, 1); R(27, y, 1); R(40, y, 1); R(54, y, 1); R(56, y, 2); R(67, y, 1); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(20, y, 1); r(26, y, 1); r (35, y, 1); r(39, y, 1); r(48, y, 1); r(58, y, 1);
+  R(10, y, 3); R(21, y, 1); R(27, y, 1); R(40, y, 1); R(54, y, 1); R(56, y, 2); R(67, y, 1); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(56, y, 1); r(66, y, 2);
+  R(11, y, 3); R(21, y, 1); R(26, y, 2); R(39, y, 2); R(54, y, 1); R(57, y, 2); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(26, y, 1); r(39, y, 1); r(59, y, 1);
+  R(12, y, 3); R(21, y, 1); R(27, y, 2); R(40, y, 2); R(54, y, 1); R(57, y, 2); R(66, y, 1); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(12, y, 4); r(30, y, 1); r(43, y, 1); r(57, y, 1); r(65, y, 2);
+  R(13, y, 2); R(21, y, 1); R(27, y, 3); R(40, y, 3); R(54, y, 1); R(58, y, 2); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(13, y, 4); r(27, y, 7); r(40, y, 7);
+  R(14, y, 2); R(21, y, 1); R(28, y, 5); R(41, y, 5); R(54, y, 1); R(58, y, 2); R(65, y, 1); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(60, y, 1); r(65, y, 1);
+  R(14, y, 3); R(21, y, 1); R(29, y, 6); R(42, y, 6); R(54, y, 1); R(58, y, 2); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(31, y, 1); r(44, y, 1); r(58, y, 1); r(64, y, 1);
+  R(15, y, 3); R(21, y, 1); R(32, y, 4); R(45, y, 4); R(54, y, 1); R(59, y, 2); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(33, y, 1); r(46, y, 1); r(61, y, 1); r(64, y, 1);
+  R(16, y, 3); R(21, y, 1); R(34, y, 2); R(47, y, 2); R(54, y, 1); R(59, y, 2); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(16, y, 4); r(36, y, 1); r(49, y, 1); r(59, y, 1); r(63, y, 1);
+  R(17, y, 2); R(21, y, 1); R(34, y, 2); R(47, y, 2); R(54, y, 1); R(60, y, 2); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(9, y, 1); r(17, y, 4); r(26, y, 1); r(36, y, 1); r(39, y, 1); r(49, y, 1);
+  R(18, y, 2); R(21, y, 1); R(35, y, 1); R(48, y, 1); R(54, y, 1); R(60, y, 2); R(63, y, 1); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(26, y, 2); r(39, y, 2); r(63, y, 1);
+  R(9, y, 1); R(18, y, 4); R(35, y, 1); R(48, y, 1); R(54, y, 1); R(60, y, 3); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(34, y, 1); r(47, y, 1); r(60, y, 1);
+  R(9, y, 1); R(19, y, 3); R(26, y, 2); R(35, y, 1); R(39, y, 2); R(48, y, 1); R(54, y, 1); R(61, y, 2); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(8, y, 1); r(35, y, 1); r(48, y, 1); r(62, y, 1); r(71, y, 1);
+  R(9, y, 1); R(20, y, 2); R(26, y, 3); R(34, y, 1); R(39, y, 3); R(47, y, 1); R(54, y, 1); R(61, y, 1); R(69, y, 2);
+  y++;
+
+  b(0, y, 80);
+  r(11, y, 1); r(26, y, 1); r(28, y, 5); r(39, y, 1); r(41, y, 5); r(51, y, 7); r(61, y, 1); r(66, y, 8);
+  R(7, y, 4); R(21, y, 1); R(29, y, 1); R(33, y, 1); R(42, y, 1); R(46, y, 1); R(52, y, 5); R(67, y, 7);
+  y++;
+
+  b(0, y, 80);
+  y++;
+
+  b(0, y, 80);
+  y++;
+}

+ 7 - 0
console.h

@@ -0,0 +1,7 @@
+#ifndef CONSOLE_H
+#define CONSOLE_H
+
+void check_console();
+void alloc_console(nssm_service_t *);
+
+#endif

+ 1 - 19
io.cpp

@@ -232,25 +232,7 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign
 
 
 int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
 int get_output_handles(nssm_service_t *service, HKEY key, STARTUPINFO *si) {
   /* Allocate a new console so we get a fresh stdin, stdout and stderr. */
   /* Allocate a new console so we get a fresh stdin, stdout and stderr. */
-  if (si && ! service->no_console) {
-    FreeConsole();
-    AllocConsole();
-    banner();
-
-    /* Set a title like "[NSSM] Jenkins" */
-    TCHAR displayname[SERVICE_NAME_LENGTH];
-    unsigned long len = _countof(displayname);
-    SC_HANDLE services = open_service_manager();
-    if (services) {
-      if (! GetServiceDisplayName(services, service->name, displayname, &len)) ZeroMemory(displayname, sizeof(displayname));
-      CloseServiceHandle(services);
-    }
-    if (! displayname[0]) _sntprintf_s(displayname, _countof(displayname), _TRUNCATE, _T("%s"), service->name);
-
-    TCHAR title[65535];
-    _sntprintf_s(title, _countof(title), _TRUNCATE, _T("[%s] %s"), NSSM, displayname);
-    SetConsoleTitle(title);
-  }
+  if (si) alloc_console(service);
 
 
   /* stdin */
   /* stdin */
   if (get_createfile_parameters(key, NSSM_REG_STDIN, service->stdin_path, &service->stdin_sharing, NSSM_STDIN_SHARING, &service->stdin_disposition, NSSM_STDIN_DISPOSITION, &service->stdin_flags, NSSM_STDIN_FLAGS)) {
   if (get_createfile_parameters(key, NSSM_REG_STDIN, service->stdin_path, &service->stdin_sharing, NSSM_STDIN_SHARING, &service->stdin_disposition, NSSM_STDIN_DISPOSITION, &service->stdin_flags, NSSM_STDIN_FLAGS)) {

+ 0 - 149
nssm.cpp

@@ -55,26 +55,6 @@ void check_admin() {
   FreeSid(AdministratorsGroup);
   FreeSid(AdministratorsGroup);
 }
 }
 
 
-/* See if we were launched from a console window. */
-static void check_console() {
-  /* If we're running in a service context there will be no console window. */
-  HWND console = GetConsoleWindow();
-  if (! console) return;
-
-  unsigned long pid;
-  if (! GetWindowThreadProcessId(console, &pid)) return;
-
-  /*
-    If the process associated with the console window handle is the same as
-    this process, we were not launched from an existing console.  The user
-    probably double-clicked our executable.
-  */
-  if (GetCurrentProcessId() != pid) return;
-
-  /* We close our new console so that subsequent messages appear in a popup. */
-  FreeConsole();
-}
-
 int num_cpus() {
 int num_cpus() {
   DWORD_PTR i, affinity, system_affinity;
   DWORD_PTR i, affinity, system_affinity;
   if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) return 64;
   if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, &system_affinity)) return 64;
@@ -82,135 +62,6 @@ int num_cpus() {
   return (int) i;
   return (int) i;
 }
 }
 
 
-static inline void block(unsigned int a, short x, short y, unsigned long n) {
-  HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
-  TCHAR s = _T(' ');
-
-  unsigned long out;
-  COORD c = { x, y };
-  FillConsoleOutputAttribute(h, a, n, c, &out);
-  FillConsoleOutputCharacter(h, s, n, c, &out);
-}
-
-static inline void R(short x, short y, unsigned long n) {
-  block(BACKGROUND_RED | BACKGROUND_INTENSITY, x, y, n);
-}
-
-static inline void r(short x, short y, unsigned long n) {
-  block(BACKGROUND_RED, x, y, n);
-}
-
-static inline void b(short x, short y, unsigned long n) {
-  block(0, x, y, n);
-}
-
-void banner() {
-  short y = 0;
-
-  b(0, y, 80);
-  y++;
-
-  b(0, y, 80);
-  y++;
-
-  b(0, y, 80);
-  y++;
-
-  b(0, y, 80);
-  y++;
-
-  b(0, y, 80);
-  r(18, y, 5); r(28, y, 4); r(41, y, 4); r(68, y, 1);
-  R(6, y, 5); R(19, y, 4); R(29, y, 1); R(32, y, 3); R(42, y, 1); R(45, y, 3); R(52, y, 5); R(69, y, 4);
-  y++;
-
-  b(0, y, 80);
-  r(8, y, 4); r(20, y, 1); r(28, y, 1); r(33, y, 3); r(41, y, 1); r(46, y, 3); r (57, y, 1);
-  R(9, y, 2); R(21, y, 1); R(27, y, 1); R(34, y, 1); R(40, y, 1); R(47, y, 1); R(54, y, 3); R(68, y, 3);
-  y++;
-
-  b(0, y, 80);
-  r(12, y, 1); r(20, y, 1); r(26, y, 1); r(34, y, 2); r(39, y, 1); r(47, y, 2); r(67, y, 2);
-  R(9, y, 3); R(21, y, 1); R(27, y, 1); R(40, y, 1); R(54, y, 1); R(56, y, 2); R(67, y, 1); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(20, y, 1); r(26, y, 1); r (35, y, 1); r(39, y, 1); r(48, y, 1); r(58, y, 1);
-  R(10, y, 3); R(21, y, 1); R(27, y, 1); R(40, y, 1); R(54, y, 1); R(56, y, 2); R(67, y, 1); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(56, y, 1); r(66, y, 2);
-  R(11, y, 3); R(21, y, 1); R(26, y, 2); R(39, y, 2); R(54, y, 1); R(57, y, 2); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(26, y, 1); r(39, y, 1); r(59, y, 1);
-  R(12, y, 3); R(21, y, 1); R(27, y, 2); R(40, y, 2); R(54, y, 1); R(57, y, 2); R(66, y, 1); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(12, y, 4); r(30, y, 1); r(43, y, 1); r(57, y, 1); r(65, y, 2);
-  R(13, y, 2); R(21, y, 1); R(27, y, 3); R(40, y, 3); R(54, y, 1); R(58, y, 2); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(13, y, 4); r(27, y, 7); r(40, y, 7);
-  R(14, y, 2); R(21, y, 1); R(28, y, 5); R(41, y, 5); R(54, y, 1); R(58, y, 2); R(65, y, 1); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(60, y, 1); r(65, y, 1);
-  R(14, y, 3); R(21, y, 1); R(29, y, 6); R(42, y, 6); R(54, y, 1); R(58, y, 2); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(31, y, 1); r(44, y, 1); r(58, y, 1); r(64, y, 1);
-  R(15, y, 3); R(21, y, 1); R(32, y, 4); R(45, y, 4); R(54, y, 1); R(59, y, 2); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(33, y, 1); r(46, y, 1); r(61, y, 1); r(64, y, 1);
-  R(16, y, 3); R(21, y, 1); R(34, y, 2); R(47, y, 2); R(54, y, 1); R(59, y, 2); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(16, y, 4); r(36, y, 1); r(49, y, 1); r(59, y, 1); r(63, y, 1);
-  R(17, y, 2); R(21, y, 1); R(34, y, 2); R(47, y, 2); R(54, y, 1); R(60, y, 2); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(9, y, 1); r(17, y, 4); r(26, y, 1); r(36, y, 1); r(39, y, 1); r(49, y, 1);
-  R(18, y, 2); R(21, y, 1); R(35, y, 1); R(48, y, 1); R(54, y, 1); R(60, y, 2); R(63, y, 1); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(26, y, 2); r(39, y, 2); r(63, y, 1);
-  R(9, y, 1); R(18, y, 4); R(35, y, 1); R(48, y, 1); R(54, y, 1); R(60, y, 3); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(34, y, 1); r(47, y, 1); r(60, y, 1);
-  R(9, y, 1); R(19, y, 3); R(26, y, 2); R(35, y, 1); R(39, y, 2); R(48, y, 1); R(54, y, 1); R(61, y, 2); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(8, y, 1); r(35, y, 1); r(48, y, 1); r(62, y, 1); r(71, y, 1);
-  R(9, y, 1); R(20, y, 2); R(26, y, 3); R(34, y, 1); R(39, y, 3); R(47, y, 1); R(54, y, 1); R(61, y, 1); R(69, y, 2);
-  y++;
-
-  b(0, y, 80);
-  r(11, y, 1); r(26, y, 1); r(28, y, 5); r(39, y, 1); r(41, y, 5); r(51, y, 7); r(61, y, 1); r(66, y, 8);
-  R(7, y, 4); R(21, y, 1); R(29, y, 1); R(33, y, 1); R(42, y, 1); R(46, y, 1); R(52, y, 5); R(67, y, 7);
-  y++;
-
-  b(0, y, 80);
-  y++;
-
-  b(0, y, 80);
-  y++;
-}
-
 int _tmain(int argc, TCHAR **argv) {
 int _tmain(int argc, TCHAR **argv) {
   check_console();
   check_console();
 
 

+ 1 - 1
nssm.h

@@ -41,6 +41,7 @@
 #include <tchar.h>
 #include <tchar.h>
 #include <windows.h>
 #include <windows.h>
 #include "service.h"
 #include "service.h"
+#include "console.h"
 #include "env.h"
 #include "env.h"
 #include "event.h"
 #include "event.h"
 #include "imports.h"
 #include "imports.h"
@@ -57,7 +58,6 @@ int str_number(const TCHAR *, unsigned long *, TCHAR **);
 int str_number(const TCHAR *, unsigned long *);
 int str_number(const TCHAR *, unsigned long *);
 int num_cpus();
 int num_cpus();
 int usage(int);
 int usage(int);
-void banner();
 
 
 #define NSSM _T("NSSM")
 #define NSSM _T("NSSM")
 #ifdef _WIN64
 #ifdef _WIN64

+ 8 - 0
nssm.vcproj

@@ -402,6 +402,10 @@
 			Name="Source Files"
 			Name="Source Files"
 			Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
 			Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
 			>
 			>
+			<File
+				RelativePath="console.cpp"
+				>
+			</File>
 			<File
 			<File
 				RelativePath="env.cpp"
 				RelativePath="env.cpp"
 				>
 				>
@@ -639,6 +643,10 @@
 			Name="Header Files"
 			Name="Header Files"
 			Filter="h;hpp;hxx;hm;inl"
 			Filter="h;hpp;hxx;hm;inl"
 			>
 			>
+			<File
+				RelativePath="console.h"
+				>
+			</File>
 			<File
 			<File
 				RelativePath="env.h"
 				RelativePath="env.h"
 				>
 				>

+ 2 - 2
service.cpp

@@ -1546,6 +1546,8 @@ int start_service(nssm_service_t *service) {
 
 
   close_output_handles(&si);
   close_output_handles(&si);
 
 
+  if (! service->no_console) FreeConsole();
+
   /* Restore our environment. */
   /* Restore our environment. */
   duplicate_environment(service->initial_env);
   duplicate_environment(service->initial_env);
 
 
@@ -1697,8 +1699,6 @@ void CALLBACK end_service(void *arg, unsigned char why) {
   if (service->pid) kill_process_tree(service, service->pid, exitcode, service->pid);
   if (service->pid) kill_process_tree(service, service->pid, exitcode, service->pid);
   service->pid = 0;
   service->pid = 0;
 
 
-  if (! service->no_console) FreeConsole();
-
   /*
   /*
     The why argument is true if our wait timed out or false otherwise.
     The why argument is true if our wait timed out or false otherwise.
     Our wait is infinite so why will never be true when called by the system.
     Our wait is infinite so why will never be true when called by the system.