Bug 1528963 - Attach console before launching child. r=aklotz

Differential Revision: https://phabricator.services.mozilla.com/D23807

--HG--
rename : toolkit/xre/nsNativeAppSupportWin.cpp => widget/windows/WindowsConsole.cpp
extra : moz-landing-system : lando
This commit is contained in:
Masatoshi Kimura 2019-03-28 21:37:52 +00:00
Родитель 75e424b241
Коммит 0cf76eebc1
8 изменённых файлов: 59 добавлений и 35 удалений

Просмотреть файл

@ -17,6 +17,7 @@
#include "mozilla/Maybe.h"
#include "mozilla/SafeMode.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/WindowsConsole.h"
#include "mozilla/WindowsVersion.h"
#include "mozilla/WinHeaderOnlyUtils.h"
#include "nsWindowsHelpers.h"
@ -219,6 +220,8 @@ Maybe<int> LauncherMain(int& argc, wchar_t* argv[],
}
}
mozilla::UseParentConsole();
if (!SetArgv0ToFullBinaryPath(argv)) {
HandleLauncherError(LAUNCHER_ERROR_GENERIC());
return Nothing();

Просмотреть файл

@ -31,6 +31,7 @@ TEST_DIRS += [
if CONFIG['MOZ_LAUNCHER_PROCESS']:
UNIFIED_SOURCES += [
'/toolkit/xre/LauncherRegistryInfo.cpp',
'/widget/windows/WindowsConsole.cpp',
]
for var in ('MOZ_APP_BASENAME', 'MOZ_APP_VENDOR'):
DEFINES[var] = '"%s"' % CONFIG[var]

Просмотреть файл

@ -8,6 +8,7 @@
#ifdef XP_WIN
# include <windows.h>
# include "mozilla/WindowsConsole.h"
#else
# include <limits.h>
#endif
@ -100,7 +101,6 @@ void OverrideDefaultLocaleIfNeeded();
void MozExpectedExit();
#ifdef XP_WIN
void UseParentConsole();
BOOL WinLaunchChild(const wchar_t* exePath, int argc, char** argv,
HANDLE userToken = nullptr, HANDLE* hProcess = nullptr);

Просмотреть файл

@ -80,6 +80,7 @@
#include "mozilla/ipc/TestShellParent.h"
#include "mozilla/ipc/XPCShellEnvironment.h"
#include "mozilla/WindowsConsole.h"
#include "mozilla/WindowsDllBlocklist.h"
#include "GMPProcessChild.h"
@ -381,19 +382,7 @@ nsresult XRE_InitChildProcess(int aArgc, char* aArgv[],
// Try to attach console to the parent process.
// It will succeed when the parent process is a command line,
// so that stdio will be displayed in it.
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
// Change std handles to refer to new console handles.
// Before doing so, ensure that stdout/stderr haven't been
// redirected to a valid file
if (_fileno(stdout) == -1 || _get_osfhandle(fileno(stdout)) == -1)
freopen("CONOUT$", "w", stdout);
// Merge stderr into CONOUT$ since there isn't any `CONERR$`.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
if (_fileno(stderr) == -1 || _get_osfhandle(fileno(stderr)) == -1)
freopen("CONOUT$", "w", stderr);
if (_fileno(stdin) == -1 || _get_osfhandle(fileno(stdin)) == -1)
freopen("CONIN$", "r", stdin);
}
UseParentConsole();
# if defined(MOZ_SANDBOX)
if (aChildData->sandboxTargetServices) {

Просмотреть файл

@ -6,8 +6,7 @@
#include "nsNativeAppSupportBase.h"
#include "nsNativeAppSupportWin.h"
#include <windows.h>
#include <fcntl.h>
#include "mozilla/WindowsConsole.h"
using namespace mozilla;
@ -26,25 +25,6 @@ class nsNativeAppSupportWin : public nsNativeAppSupportBase {
~nsNativeAppSupportWin() {}
}; // nsNativeAppSupportWin
void UseParentConsole() {
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
// Redirect the standard streams to the existing console, but
// only if they haven't been redirected to a valid file.
// Visual Studio's _fileno() returns -2 for the standard
// streams if they aren't associated with an output stream.
if (_fileno(stdout) == -2) {
freopen("CONOUT$", "w", stdout);
}
// There is no CONERR$, so use CONOUT$ for stderr as well.
if (_fileno(stderr) == -2) {
freopen("CONOUT$", "w", stderr);
}
if (_fileno(stdin) == -2) {
freopen("CONIN$", "r", stdin);
}
}
}
void nsNativeAppSupportWin::CheckConsole() {
for (int i = 1; i < gArgc; ++i) {
if (strcmp("-console", gArgv[i]) == 0 ||

Просмотреть файл

@ -0,0 +1,33 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "WindowsConsole.h"
#include <windows.h>
#include <fcntl.h>
namespace mozilla {
// This code attaches the process to the appropriate console.
void UseParentConsole() {
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
// Redirect the standard streams to the existing console, but
// only if they haven't been redirected to a valid file.
// Visual Studio's _fileno() returns -2 for the standard
// streams if they aren't associated with an output stream.
if (_fileno(stdout) == -2) {
freopen("CONOUT$", "w", stdout);
}
// There is no CONERR$, so use CONOUT$ for stderr as well.
if (_fileno(stderr) == -2) {
freopen("CONOUT$", "w", stderr);
}
if (_fileno(stdin) == -2) {
freopen("CONIN$", "r", stdin);
}
}
}
} // namespace mozilla

Просмотреть файл

@ -0,0 +1,16 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_WindowsConsole_h
#define mozilla_WindowsConsole_h
namespace mozilla {
// This code attaches the process to the appropriate console.
void UseParentConsole();
} // namespace mozilla
#endif // mozilla_WindowsConsole_h

Просмотреть файл

@ -19,6 +19,7 @@ EXPORTS += [
]
EXPORTS.mozilla += [
'WindowsConsole.h',
'WinHeaderOnlyUtils.h',
]
@ -75,6 +76,7 @@ UNIFIED_SOURCES += [
'WidgetTraceEvent.cpp',
'WinCompositorWindowThread.cpp',
'WindowHook.cpp',
'WindowsConsole.cpp',
'WinIMEHandler.cpp',
'WinPointerEvents.cpp',
'WinTaskbar.cpp',