2019-05-02 15:29:04 -07:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
|
// Licensed under the MIT license.
|
|
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
|
#include "ConhostConnection.h"
|
|
|
|
|
#include "windows.h"
|
|
|
|
|
#include <sstream>
|
|
|
|
|
// STARTF_USESTDHANDLES is only defined in WINAPI_PARTITION_DESKTOP
|
|
|
|
|
// We're just gonna manually define it for this prototyping code
|
|
|
|
|
#ifndef STARTF_USESTDHANDLES
|
2019-06-11 13:27:09 -07:00
|
|
|
#define STARTF_USESTDHANDLES 0x00000100
|
2019-05-02 15:29:04 -07:00
|
|
|
#endif
|
|
|
|
|
|
2019-05-23 10:36:29 -07:00
|
|
|
#include "ConhostConnection.g.cpp"
|
|
|
|
|
|
2019-05-02 15:29:04 -07:00
|
|
|
#include <conpty-universal.h>
|
2019-05-22 13:24:22 -07:00
|
|
|
#include "../../types/inc/Utils.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace ::Microsoft::Console;
|
2019-05-02 15:29:04 -07:00
|
|
|
|
|
|
|
|
namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
|
|
|
|
|
{
|
2019-05-22 13:24:22 -07:00
|
|
|
ConhostConnection::ConhostConnection(const hstring& commandline,
|
|
|
|
|
const hstring& startingDirectory,
|
|
|
|
|
const uint32_t initialRows,
|
|
|
|
|
const uint32_t initialCols,
|
|
|
|
|
const guid& initialGuid) :
|
|
|
|
|
_initialRows{ initialRows },
|
|
|
|
|
_initialCols{ initialCols },
|
|
|
|
|
_commandline{ commandline },
|
|
|
|
|
_startingDirectory{ startingDirectory },
|
|
|
|
|
_guid{ initialGuid }
|
2019-05-02 15:29:04 -07:00
|
|
|
{
|
2019-06-17 17:32:31 -07:00
|
|
|
if (_guid == guid{})
|
2019-05-22 13:24:22 -07:00
|
|
|
{
|
|
|
|
|
_guid = Utils::CreateGuid();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-02 15:29:04 -07:00
|
|
|
|
2019-05-22 13:24:22 -07:00
|
|
|
winrt::guid ConhostConnection::Guid() const noexcept
|
|
|
|
|
{
|
|
|
|
|
return _guid;
|
2019-05-02 15:29:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
winrt::event_token ConhostConnection::TerminalOutput(Microsoft::Terminal::TerminalConnection::TerminalOutputEventArgs const& handler)
|
|
|
|
|
{
|
|
|
|
|
return _outputHandlers.add(handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConhostConnection::TerminalOutput(winrt::event_token const& token) noexcept
|
|
|
|
|
{
|
|
|
|
|
_outputHandlers.remove(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
winrt::event_token ConhostConnection::TerminalDisconnected(Microsoft::Terminal::TerminalConnection::TerminalDisconnectedEventArgs const& handler)
|
|
|
|
|
{
|
|
|
|
|
return _disconnectHandlers.add(handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConhostConnection::TerminalDisconnected(winrt::event_token const& token) noexcept
|
|
|
|
|
{
|
|
|
|
|
_disconnectHandlers.remove(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConhostConnection::Start()
|
|
|
|
|
{
|
2019-05-22 13:24:22 -07:00
|
|
|
std::wstring cmdline{ _commandline.c_str() };
|
2019-05-02 15:29:04 -07:00
|
|
|
std::optional<std::wstring> startingDirectory;
|
|
|
|
|
if (!_startingDirectory.empty())
|
|
|
|
|
{
|
|
|
|
|
startingDirectory = _startingDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 13:24:22 -07:00
|
|
|
EnvironmentVariableMapW extraEnvVars;
|
|
|
|
|
{
|
|
|
|
|
// Convert connection Guid to string and ignore the enclosing '{}'.
|
|
|
|
|
std::wstring wsGuid{ Utils::GuidToString(_guid) };
|
|
|
|
|
wsGuid.pop_back();
|
|
|
|
|
|
|
|
|
|
const wchar_t* const pwszGuid{ wsGuid.data() + 1 };
|
|
|
|
|
|
|
|
|
|
// Ensure every connection has the unique identifier in the environment.
|
|
|
|
|
extraEnvVars.emplace(L"WT_SESSION", pwszGuid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
THROW_IF_FAILED(
|
|
|
|
|
CreateConPty(cmdline,
|
|
|
|
|
startingDirectory,
|
|
|
|
|
static_cast<short>(_initialCols),
|
|
|
|
|
static_cast<short>(_initialRows),
|
|
|
|
|
&_inPipe,
|
|
|
|
|
&_outPipe,
|
|
|
|
|
&_signalPipe,
|
|
|
|
|
&_piConhost,
|
2019-06-17 17:32:31 -07:00
|
|
|
CREATE_SUSPENDED,
|
2019-05-22 13:24:22 -07:00
|
|
|
extraEnvVars));
|
2019-05-02 15:29:04 -07:00
|
|
|
|
2019-06-17 17:32:31 -07:00
|
|
|
_hJob.reset(CreateJobObjectW(nullptr, nullptr));
|
|
|
|
|
THROW_LAST_ERROR_IF_NULL(_hJob);
|
|
|
|
|
|
|
|
|
|
// We want the conhost and all associated descendant processes
|
|
|
|
|
// to be terminated when the tab is closed. GUI applications
|
|
|
|
|
// spawned from the shell tend to end up in their own jobs.
|
|
|
|
|
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedInformation{};
|
|
|
|
|
jobExtendedInformation.BasicLimitInformation.LimitFlags =
|
|
|
|
|
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
|
|
|
|
|
|
|
|
|
|
THROW_IF_WIN32_BOOL_FALSE(SetInformationJobObject(_hJob.get(),
|
|
|
|
|
JobObjectExtendedLimitInformation,
|
|
|
|
|
&jobExtendedInformation,
|
|
|
|
|
sizeof(jobExtendedInformation)));
|
|
|
|
|
|
|
|
|
|
THROW_IF_WIN32_BOOL_FALSE(AssignProcessToJobObject(_hJob.get(), _piConhost.hProcess));
|
2019-05-02 15:29:04 -07:00
|
|
|
|
|
|
|
|
// Create our own output handling thread
|
2019-06-17 17:32:31 -07:00
|
|
|
// Each connection needs to make sure to drain the output from its backing host.
|
|
|
|
|
_hOutputThread.reset(CreateThread(nullptr,
|
|
|
|
|
0,
|
|
|
|
|
StaticOutputThreadProc,
|
|
|
|
|
this,
|
|
|
|
|
0,
|
|
|
|
|
nullptr));
|
|
|
|
|
|
|
|
|
|
// Wind up the conhost! We only do this after we've got everything in place.
|
|
|
|
|
THROW_LAST_ERROR_IF(-1 == ResumeThread(_piConhost.hThread));
|
|
|
|
|
|
|
|
|
|
_connected = true;
|
2019-05-02 15:29:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConhostConnection::WriteInput(hstring const& data)
|
|
|
|
|
{
|
2019-06-17 17:32:31 -07:00
|
|
|
if (!_connected || _closing.load())
|
2019-05-02 15:29:04 -07:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert from UTF-16LE to UTF-8 as ConPty expects UTF-8
|
|
|
|
|
std::string str = winrt::to_string(data);
|
2019-06-17 17:32:31 -07:00
|
|
|
bool fSuccess = !!WriteFile(_inPipe.get(), str.c_str(), (DWORD)str.length(), nullptr, nullptr);
|
2019-05-02 15:29:04 -07:00
|
|
|
fSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConhostConnection::Resize(uint32_t rows, uint32_t columns)
|
|
|
|
|
{
|
|
|
|
|
if (!_connected)
|
|
|
|
|
{
|
|
|
|
|
_initialRows = rows;
|
|
|
|
|
_initialCols = columns;
|
|
|
|
|
}
|
2019-06-17 17:32:31 -07:00
|
|
|
else if (!_closing.load())
|
2019-05-02 15:29:04 -07:00
|
|
|
{
|
2019-06-17 17:32:31 -07:00
|
|
|
SignalResizeWindow(_signalPipe.get(), Utils::ClampToShortMax(columns, 1), Utils::ClampToShortMax(rows, 1));
|
2019-05-02 15:29:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConhostConnection::Close()
|
|
|
|
|
{
|
2019-06-11 13:27:09 -07:00
|
|
|
if (!_connected)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-06-13 02:10:13 +02:00
|
|
|
|
2019-06-17 17:32:31 -07:00
|
|
|
if (!_closing.exchange(true))
|
|
|
|
|
{
|
|
|
|
|
_hJob.reset(); // This will terminate the process _piConhost is holding.
|
|
|
|
|
_piConhost.reset();
|
2019-06-13 02:10:13 +02:00
|
|
|
|
2019-06-17 17:32:31 -07:00
|
|
|
_inPipe.reset();
|
|
|
|
|
_outPipe.reset();
|
|
|
|
|
_signalPipe.reset();
|
2019-06-13 02:10:13 +02:00
|
|
|
|
2019-06-17 17:32:31 -07:00
|
|
|
WaitForSingleObject(_hOutputThread.get(), INFINITE);
|
|
|
|
|
_hOutputThread.reset();
|
|
|
|
|
}
|
2019-05-02 15:29:04 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 22:32:51 +02:00
|
|
|
DWORD WINAPI ConhostConnection::StaticOutputThreadProc(LPVOID lpParameter)
|
2019-05-02 15:29:04 -07:00
|
|
|
{
|
|
|
|
|
ConhostConnection* const pInstance = (ConhostConnection*)lpParameter;
|
|
|
|
|
return pInstance->_OutputThread();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DWORD ConhostConnection::_OutputThread()
|
|
|
|
|
{
|
|
|
|
|
const size_t bufferSize = 4096;
|
|
|
|
|
BYTE buffer[bufferSize];
|
|
|
|
|
DWORD dwRead;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
dwRead = 0;
|
|
|
|
|
bool fSuccess = false;
|
|
|
|
|
|
2019-06-17 17:32:31 -07:00
|
|
|
fSuccess = !!ReadFile(_outPipe.get(), buffer, bufferSize, &dwRead, nullptr);
|
2019-05-02 15:29:04 -07:00
|
|
|
if (!fSuccess)
|
|
|
|
|
{
|
2019-06-17 17:32:31 -07:00
|
|
|
if (_closing.load())
|
2019-05-02 15:29:04 -07:00
|
|
|
{
|
|
|
|
|
// This is okay, break out to kill the thread
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_disconnectHandlers();
|
|
|
|
|
return (DWORD)-1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-11 13:27:09 -07:00
|
|
|
if (dwRead == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-05-02 15:29:04 -07:00
|
|
|
// Convert buffer to hstring
|
|
|
|
|
char* pchStr = (char*)(buffer);
|
2019-06-11 13:27:09 -07:00
|
|
|
std::string str{ pchStr, dwRead };
|
2019-05-02 15:29:04 -07:00
|
|
|
auto hstr = winrt::to_hstring(str);
|
|
|
|
|
|
|
|
|
|
// Pass the output to our registered event handlers
|
|
|
|
|
_outputHandlers(hstr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|