Skip to content

Commit

Permalink
Kernel: Properly destroy PTYs after master is closed. Ensure signal s…
Browse files Browse the repository at this point in the history
…tack alignment.
  • Loading branch information
fido2020 committed Oct 21, 2021
1 parent 5147925 commit 0f03716
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
4 changes: 4 additions & 0 deletions Kernel/include/TTY/PTY.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PTMultiplexor final : public Device {
static inline PTMultiplexor& Instance() { return *m_instance; }

UNIXFileDescriptor* Open(size_t flags) override;
void DestroyPTY(PTY* pt);

private:
static PTMultiplexor* m_instance;
Expand All @@ -58,6 +59,8 @@ class PTYDevice : public FsNode {

PTYDevice();

void Close() override;

ssize_t Read(size_t, size_t, uint8_t *) override;
ssize_t Write(size_t, size_t, uint8_t *) override;
int Ioctl(uint64_t cmd, uint64_t arg) override;
Expand All @@ -83,6 +86,7 @@ class PTY{
termios tios;

PTY(int id);
~PTY();

inline int GetID() const { return m_id; }

Expand Down
3 changes: 2 additions & 1 deletion Kernel/src/Arch/x86_64/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ void Thread::HandlePendingSignal(RegisterContext* regs) {
//uint64_t* stack = reinterpret_cast<uint64_t*>(regs->rsp - sizeof(ucontext_t));
//ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(stack);

uint64_t* stack = reinterpret_cast<uint64_t*>(regs->rsp - sizeof(RegisterContext));
// Ensure stack alignment
uint64_t* stack = reinterpret_cast<uint64_t*>((regs->rsp & (~0xfULL)) - sizeof(RegisterContext));
*reinterpret_cast<RegisterContext*>(stack) = *regs;

*(--stack) = oldSignalMask;
Expand Down
6 changes: 6 additions & 0 deletions Kernel/src/CharacterBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ CharacterBuffer::CharacterBuffer() {
lines = 0;
}

CharacterBuffer::~CharacterBuffer() {
if(buffer){
delete buffer;
}
}

ssize_t CharacterBuffer::Write(char* _buffer, size_t size) {
if (bufferPos + size > maxBufferSize) {
size = maxBufferSize - bufferPos;
Expand Down
20 changes: 5 additions & 15 deletions Kernel/src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class TTY : public Device {
SetDeviceName("Process Terminal");
}

ssize_t Read(size_t, size_t, uint8_t*);
ssize_t Write(size_t, size_t, uint8_t*);
UNIXFileDescriptor* Open(size_t flags) override;
};

int Device::nextUnnamedDeviceNumber = 0;
Expand Down Expand Up @@ -131,23 +130,14 @@ ssize_t URandom::Read(size_t offset, size_t size, uint8_t* buffer) {

ssize_t URandom::Write(size_t offset, size_t size, uint8_t* buffer) { return size; }


ssize_t TTY::Read(size_t offset, size_t size, uint8_t* buffer) {
auto stdin = Scheduler::GetCurrentProcess()->GetFileDescriptor(0);

if(stdin && stdin->node){
return fs::Read(stdin->node, offset, size, buffer);
}
return -EBADF;
}

ssize_t TTY::Write(size_t offset, size_t size, uint8_t* buffer) {
UNIXFileDescriptor* TTY::Open(size_t flags){
auto stdout = Scheduler::GetCurrentProcess()->GetFileDescriptor(1);

if(stdout && stdout->node){
return fs::Write(stdout->node, offset, size, buffer);
return stdout->node->Open(flags);
}
return -EBADF;

return nullptr;
}

Null null = Null("null");
Expand Down
12 changes: 12 additions & 0 deletions Kernel/src/TTY/PTMX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,15 @@ UNIXFileDescriptor* PTMultiplexor::Open(size_t flags){

return pty->masterFile.Open(flags);
}

void PTMultiplexor::DestroyPTY(PTY* pt){
ScopedSpinLock lock(m_ptmxLock);
for(auto it = m_ptList.begin(); it != m_ptList.end(); it++) {
if(*it == pt){
m_ptList.remove(pt);
delete pt;

return;
}
}
}
25 changes: 25 additions & 0 deletions Kernel/src/TTY/PTY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ PTYDevice::PTYDevice() {

}

void PTYDevice::Close() {
handleCount--;

if(handleCount == 0 && device == PTYMasterDevice){
PTMultiplexor::Instance().DestroyPTY(pty);
}
}

ssize_t PTYDevice::Read(size_t offset, size_t size, uint8_t* buffer) {
assert(pty);
assert(device == PTYSlaveDevice || device == PTYMasterDevice);
Expand Down Expand Up @@ -99,6 +107,13 @@ int PTYDevice::Ioctl(uint64_t cmd, uint64_t arg) {
assert(pty);

switch (cmd) {
case TCGETS:
*((termios*)arg) = pty->tios;
break;
case TCSETS:
pty->tios = *((termios*)arg);
pty->slave.ignoreBackspace = !pty->IsCanonical();
break;
case TIOCGWINSZ:
*((winsz*)arg) = pty->wSz;
break;
Expand Down Expand Up @@ -187,6 +202,16 @@ PTY::PTY(int id) : m_id(id) {
tios.c_cc[i] = c_cc_default[i];
}

PTY::~PTY(){
while (m_watchingSlave.get_length()) {
m_watchingSlave.remove_at(0)->Signal(); // Signal all watching
}

while (m_watchingMaster.get_length()) {
m_watchingMaster.remove_at(0)->Signal(); // Signal all watching
}
}

ssize_t PTY::MasterRead(char* buffer, size_t count) { return master.Read(buffer, count); }

ssize_t PTY::SlaveRead(char* buffer, size_t count) {
Expand Down

0 comments on commit 0f03716

Please sign in to comment.