2019-01-15 08:30:19 +03:00
|
|
|
#include "MasterPTY.h"
|
2019-01-30 20:26:19 +03:00
|
|
|
#include "PTYMultiplexer.h"
|
2019-06-07 12:43:58 +03:00
|
|
|
#include "SlavePTY.h"
|
2019-01-31 07:55:30 +03:00
|
|
|
#include <Kernel/Process.h>
|
2019-01-30 20:26:19 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
2019-02-05 14:55:19 +03:00
|
|
|
#include <LibC/signal_numbers.h>
|
2019-02-21 01:32:33 +03:00
|
|
|
#include <LibC/sys/ioctl_numbers.h>
|
2019-01-15 08:30:19 +03:00
|
|
|
|
2019-03-27 17:07:12 +03:00
|
|
|
//#define MASTERPTY_DEBUG
|
|
|
|
|
2019-01-15 08:30:19 +03:00
|
|
|
MasterPTY::MasterPTY(unsigned index)
|
|
|
|
: CharacterDevice(10, index)
|
2019-01-30 20:26:19 +03:00
|
|
|
, m_slave(adopt(*new SlavePTY(*this, index)))
|
2019-01-15 08:30:19 +03:00
|
|
|
, m_index(index)
|
|
|
|
{
|
2019-04-16 01:35:02 +03:00
|
|
|
m_pts_name = String::format("/dev/pts/%u", m_index);
|
2019-03-24 00:03:17 +03:00
|
|
|
set_uid(current->process().uid());
|
|
|
|
set_gid(current->process().gid());
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MasterPTY::~MasterPTY()
|
|
|
|
{
|
2019-03-27 17:07:12 +03:00
|
|
|
#ifdef MASTERPTY_DEBUG
|
2019-01-30 21:05:59 +03:00
|
|
|
dbgprintf("~MasterPTY(%u)\n", m_index);
|
2019-03-27 17:07:12 +03:00
|
|
|
#endif
|
2019-05-31 16:44:04 +03:00
|
|
|
PTYMultiplexer::the().notify_master_destroyed({}, m_index);
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
String MasterPTY::pts_name() const
|
|
|
|
{
|
2019-04-16 01:35:02 +03:00
|
|
|
return m_pts_name;
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
ssize_t MasterPTY::read(FileDescription&, byte* buffer, ssize_t size)
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-01-30 20:26:19 +03:00
|
|
|
if (!m_slave && m_buffer.is_empty())
|
|
|
|
return 0;
|
2019-01-15 08:30:19 +03:00
|
|
|
return m_buffer.read(buffer, size);
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
ssize_t MasterPTY::write(FileDescription&, const byte* buffer, ssize_t size)
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-01-30 20:26:19 +03:00
|
|
|
if (!m_slave)
|
|
|
|
return -EIO;
|
|
|
|
m_slave->on_master_write(buffer, size);
|
2019-01-15 08:30:19 +03:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
bool MasterPTY::can_read(FileDescription&) const
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-01-30 20:26:19 +03:00
|
|
|
if (!m_slave)
|
|
|
|
return true;
|
2019-01-15 08:30:19 +03:00
|
|
|
return !m_buffer.is_empty();
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
bool MasterPTY::can_write(FileDescription&) const
|
2019-01-15 11:17:22 +03:00
|
|
|
{
|
2019-01-24 23:23:46 +03:00
|
|
|
return true;
|
2019-01-15 11:17:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:26:19 +03:00
|
|
|
void MasterPTY::notify_slave_closed(Badge<SlavePTY>)
|
|
|
|
{
|
2019-03-27 17:07:12 +03:00
|
|
|
#ifdef MASTERPTY_DEBUG
|
2019-01-30 20:26:19 +03:00
|
|
|
dbgprintf("MasterPTY(%u): slave closed, my retains: %u, slave retains: %u\n", m_index, retain_count(), m_slave->retain_count());
|
2019-03-27 17:07:12 +03:00
|
|
|
#endif
|
2019-01-30 20:26:19 +03:00
|
|
|
// +1 retain for my MasterPTY::m_slave
|
2019-06-07 10:36:51 +03:00
|
|
|
// +1 retain for FileDescription::m_device
|
2019-01-30 20:26:19 +03:00
|
|
|
if (m_slave->retain_count() == 2)
|
|
|
|
m_slave = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t MasterPTY::on_slave_write(const byte* data, ssize_t size)
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-02-05 15:09:01 +03:00
|
|
|
if (m_closed)
|
|
|
|
return -EIO;
|
2019-01-15 08:30:19 +03:00
|
|
|
m_buffer.write(data, size);
|
2019-02-05 15:09:01 +03:00
|
|
|
return size;
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
2019-01-25 02:13:54 +03:00
|
|
|
|
|
|
|
bool MasterPTY::can_write_from_slave() const
|
|
|
|
{
|
2019-02-05 15:09:01 +03:00
|
|
|
if (m_closed)
|
|
|
|
return true;
|
2019-01-25 02:13:54 +03:00
|
|
|
return m_buffer.bytes_in_write_buffer() < 4096;
|
|
|
|
}
|
2019-02-05 14:27:32 +03:00
|
|
|
|
|
|
|
void MasterPTY::close()
|
|
|
|
{
|
|
|
|
if (retain_count() == 2) {
|
2019-02-05 14:55:19 +03:00
|
|
|
InterruptDisabler disabler;
|
2019-06-07 10:36:51 +03:00
|
|
|
// After the closing FileDescription dies, slave is the only thing keeping me alive.
|
2019-02-05 14:27:32 +03:00
|
|
|
// From this point, let's consider ourselves closed.
|
|
|
|
m_closed = true;
|
2019-02-05 14:55:19 +03:00
|
|
|
|
|
|
|
m_slave->hang_up();
|
2019-02-05 14:27:32 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 01:32:33 +03:00
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
int MasterPTY::ioctl(FileDescription& description, unsigned request, unsigned arg)
|
2019-02-21 01:32:33 +03:00
|
|
|
{
|
|
|
|
if (request == TIOCSWINSZ)
|
2019-06-13 23:03:04 +03:00
|
|
|
return m_slave->ioctl(description, request, arg);
|
2019-02-21 01:32:33 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|