Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #78

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/include/winpty.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ WINPTY_API int winpty_get_process_id(winpty_t *pc);
*/
WINPTY_API HANDLE winpty_get_data_pipe(winpty_t *pc);

/*
* Returns an overlapped-mode pipe handle that can be read and written
* like a Unix terminal.
*/
WINPTY_API HANDLE winpty_get_control_pipe(winpty_t *pc);

/*
* Change the size of the Windows console.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/libwinpty/winpty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ WINPTY_API HANDLE winpty_get_data_pipe(winpty_t *pc)
return pc->dataPipe;
}

WINPTY_API HANDLE winpty_get_control_pipe(winpty_t *pc)
{
return pc->controlPipe;
}

WINPTY_API int winpty_set_size(winpty_t *pc, int cols, int rows)
{
auto packet = newPacket();
Expand Down
2 changes: 1 addition & 1 deletion src/shared/Buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void WriteBuffer::putRawData(const void *data, size_t len) {
void WriteBuffer::replaceRawData(size_t pos, const void *data, size_t len) {
ASSERT(pos <= m_buf.size() && len <= m_buf.size() - pos);
const auto p = reinterpret_cast<const char*>(data);
std::copy(p, p + len, m_buf.begin());
std::copy(p, p + len, m_buf.begin() + pos);
}

void WriteBuffer::putInt32(int32_t i) {
Expand Down
145 changes: 145 additions & 0 deletions src/unix-adapter/ControlHandler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright (c) 2011-2015 Ryan Prichard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

#include "ControlHandler.h"

#include <assert.h>
#include <errno.h>
#include <sys/select.h>
#include <unistd.h>

#include <algorithm>
#include <vector>

#include "../shared/DebugClient.h"
#include "Event.h"
#include "Util.h"
#include "WakeupFd.h"

ControlHandler::ControlHandler(HANDLE r, HANDLE w, WakeupFd &completionWakeup) :
m_read_pipe(r),
m_write_pipe(w),
m_completionWakeup(completionWakeup),
m_threadHasBeenJoined(false),
m_shouldShutdown(0),
m_threadCompleted(0)
{
pthread_create(&m_thread, NULL, ControlHandler::threadProcS, this);
}

void ControlHandler::shutdown() {
startShutdown();
if (!m_threadHasBeenJoined) {
int ret = pthread_join(m_thread, NULL);
assert(ret == 0 && "pthread_join failed");
m_threadHasBeenJoined = true;
}
}

static BOOL read_pipe(HANDLE p, char * buf, int read_size) {
char * tmp = buf;

while (read_size > 0) {
DWORD numRead = 0;
BOOL ret = ReadFile(p,
tmp,
read_size,
&numRead,
NULL);

if (!ret || numRead == 0) {
return FALSE;
}

read_size -= numRead;
tmp += numRead;
}

return TRUE;
}

static BOOL read_packet(HANDLE p, std::vector<char> & buf) {
typedef unsigned __int64 uint64_t;

uint64_t size = 0;

char * tmp = (char *)&size;
if (!read_pipe(p, tmp, sizeof(uint64_t)))
return FALSE;

buf.insert(buf.end(), tmp, tmp + sizeof(uint64_t));
buf.resize(size);

return read_pipe(p, &buf[sizeof(uint64_t)], size - sizeof(uint64_t));
}

void ControlHandler::threadProc() {
while (true) {
// Handle shutdown
m_wakeup.reset();
if (m_shouldShutdown) {
trace("ControlHandler: shutting down");
break;
}

// Read from the pipe.
std::vector<char> data;

ConnectNamedPipe(m_read_pipe, NULL);

BOOL ret = read_packet(m_read_pipe,
data);

if (!ret) {
trace("ControlHandler: read failed: "
"ret=%d lastError=0x%x",
ret,
static_cast<unsigned int>(GetLastError()));
break;
}

//Write to pipe
DWORD written;
ret = WriteFile(m_write_pipe,
&data[0], data.size(),
&written,
NULL);
if (!ret || written != data.size()) {
if (!ret && GetLastError() == ERROR_BROKEN_PIPE) {
trace("ControlHandler: pipe closed: written=%u",
static_cast<unsigned int>(written));
} else {
trace("ControlHandler: write failed: "
"ret=%d lastError=0x%x numRead=%ld written=%u",
ret,
static_cast<unsigned int>(GetLastError()),
static_cast<DWORD>(data.size()),
static_cast<unsigned int>(written));
}
break;
}

DWORD numRead = 0;
ReadFile(m_write_pipe, &data[0], 4, &numRead, NULL);
WriteFile(m_read_pipe, &data[0], numRead, &written, NULL);
}
m_threadCompleted = 1;
m_completionWakeup.set();
}
56 changes: 56 additions & 0 deletions src/unix-adapter/ControlHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2011-2015 Ryan Prichard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

#ifndef UNIX_ADAPTER_CONTROL_HANDLER_H
#define UNIX_ADAPTER_CONTROL_HANDLER_H

#include <windows.h>
#include <pthread.h>
#include <signal.h>

#include "Event.h"
#include "WakeupFd.h"

class ControlHandler {
public:
ControlHandler(HANDLE read_pipe, HANDLE write_pipe, WakeupFd &completionWakeup);
~ControlHandler() { shutdown(); }
bool isComplete() { return m_threadCompleted; }
void startShutdown() { m_shouldShutdown = 1; m_wakeup.set(); }
void shutdown();

private:
static void *threadProcS(void *pvthis) {
reinterpret_cast<ControlHandler*>(pvthis)->threadProc();
return NULL;
}
void threadProc();

HANDLE m_read_pipe;
HANDLE m_write_pipe;
pthread_t m_thread;
WakeupFd &m_completionWakeup;
Event m_wakeup;
bool m_threadHasBeenJoined;
volatile sig_atomic_t m_shouldShutdown;
volatile sig_atomic_t m_threadCompleted;
};

#endif // UNIX_ADAPTER_CONTROL_HANDLER_H
5 changes: 4 additions & 1 deletion src/unix-adapter/InputHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@
#include "Util.h"
#include "WakeupFd.h"

extern bool g_pipe_mode;

InputHandler::InputHandler(HANDLE winpty, WakeupFd &completionWakeup) :
m_winpty(winpty),
m_completionWakeup(completionWakeup),
m_threadHasBeenJoined(false),
m_shouldShutdown(0),
m_threadCompleted(0)
{
assert(isatty(STDIN_FILENO));
if (!g_pipe_mode)
assert(isatty(STDIN_FILENO));
pthread_create(&m_thread, NULL, InputHandler::threadProcS, this);
}

Expand Down
5 changes: 4 additions & 1 deletion src/unix-adapter/OutputHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@
#include "Util.h"
#include "WakeupFd.h"

extern bool g_pipe_mode;

OutputHandler::OutputHandler(HANDLE winpty, WakeupFd &completionWakeup) :
m_winpty(winpty),
m_completionWakeup(completionWakeup),
m_threadHasBeenJoined(false),
m_shouldShutdown(0),
m_threadCompleted(0)
{
assert(isatty(STDOUT_FILENO));
if (!g_pipe_mode)
assert(isatty(STDOUT_FILENO));
pthread_create(&m_thread, NULL, OutputHandler::threadProcS, this);
}

Expand Down
Loading