From: Iain Patterson Date: Wed, 26 Apr 2017 12:55:18 +0000 (+0100) Subject: Support starting processes with a console on Windows 10. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=897c7ad10ba9542b650f256445d953eff908b985;p=nssm.git Support starting processes with a console on Windows 10. By default we let the application inherit NSSM's console. On Windows 10 Creators Update an application inheriting a console would fail to start with error STATUS_DLL_INIT_FAILED. Now we close our own console and call CreateProcess() with the CREATE_NEW_CONSOLE flag if the application needs a console. Unfortunately the ASCII art logo has to be sacrificed in the name of progress... --- diff --git a/console.cpp b/console.cpp index b886021..2ec0008 100644 --- a/console.cpp +++ b/console.cpp @@ -21,156 +21,8 @@ bool check_console() { return false; } -/* 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(); - - /* Disable accidental closure. */ - HWND window = GetConsoleWindow(); - HMENU menu = GetSystemMenu(window, false); - EnableMenuItem(menu, SC_CLOSE, MF_GRAYED); - - /* Set a title like "[NSSM] Jenkins" */ - TCHAR displayname[SERVICE_NAME_LENGTH]; - unsigned long len = _countof(displayname); - SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT); - 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++; } diff --git a/io.cpp b/io.cpp index dd2641c..0ba7e18 100644 --- a/io.cpp +++ b/io.cpp @@ -305,6 +305,7 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { if (! si) return 1; + bool inherit_handles = false; /* Allocate a new console so we get a fresh stdin, stdout and stderr. */ alloc_console(service); @@ -316,6 +317,8 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0); return 2; } + + inherit_handles = true; } /* stdout */ @@ -341,6 +344,8 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { } if (dup_handle(service->stdout_si, &si->hStdOutput, _T("stdout_si"), _T("stdout"))) close_handle(&service->stdout_thread); + + inherit_handles = true; } /* stderr */ @@ -379,26 +384,15 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) { } if (dup_handle(service->stderr_si, &si->hStdError, _T("stderr_si"), _T("stderr"))) close_handle(&service->stderr_thread); + + inherit_handles = true; } /* We need to set the startup_info flags to make the new handles inheritable by the new process. */ - si->dwFlags |= STARTF_USESTDHANDLES; - - if (service->no_console) return 0; - - /* Redirect other handles. */ - if (! si->hStdInput) { - if (dup_handle(GetStdHandle(STD_INPUT_HANDLE), &si->hStdInput, _T("STD_INPUT_HANDLE"), _T("stdin"))) return 8; - } - if (! si->hStdOutput) { - if (dup_handle(GetStdHandle(STD_OUTPUT_HANDLE), &si->hStdOutput, _T("STD_OUTPUT_HANDLE"), _T("stdout"))) return 9; - } - if (! si->hStdError) { - if (dup_handle(GetStdHandle(STD_ERROR_HANDLE), &si->hStdError, _T("STD_ERROR_HANDLE"), _T("stderr"))) return 10; - } + if (inherit_handles) si->dwFlags |= STARTF_USESTDHANDLES; return 0; } diff --git a/service.cpp b/service.cpp index a18684e..c95da92 100644 --- a/service.cpp +++ b/service.cpp @@ -1875,11 +1875,12 @@ int start_service(nssm_service_t *service) { /* Set up I/O redirection. */ if (get_output_handles(service, &si)) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_GET_OUTPUT_HANDLES_FAILED, service->name, 0); - if (! service->no_console) FreeConsole(); + FreeConsole(); close_output_handles(&si); unset_service_environment(service); return stop_service(service, 4, true, true); } + FreeConsole(); /* Pre-start hook. May need I/O to have been redirected already. */ if (nssm_hook(&hook_threads, service, NSSM_HOOK_EVENT_START, NSSM_HOOK_ACTION_PRE, &control, NSSM_SERVICE_STATUS_DEADLINE, false) == NSSM_HOOK_STATUS_ABORT) { @@ -1897,6 +1898,7 @@ int start_service(nssm_service_t *service) { if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true; unsigned long flags = service->priority & priority_mask(); if (service->affinity) flags |= CREATE_SUSPENDED; + if (! service->no_console) flags |= CREATE_NEW_CONSOLE; if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, 0, service->dir, &si, &pi)) { unsigned long exitcode = 3; unsigned long error = GetLastError(); @@ -1913,8 +1915,6 @@ int start_service(nssm_service_t *service) { close_output_handles(&si); - if (! service->no_console) FreeConsole(); - if (service->affinity) { /* We are explicitly storing service->affinity as a 64-bit unsigned integer