2019-01-15 03:30:19 -02:00
|
|
|
#include "SlavePTY.h"
|
|
|
|
|
#include "MasterPTY.h"
|
2019-04-03 07:25:24 -03:00
|
|
|
#include <Kernel/FileSystem/DevPtsFS.h>
|
2019-01-31 02:55:30 -02:00
|
|
|
#include <Kernel/Process.h>
|
2019-01-15 03:30:19 -02:00
|
|
|
|
2019-03-27 11:07:12 -03:00
|
|
|
//#define SLAVEPTY_DEBUG
|
|
|
|
|
|
2019-01-15 23:11:50 -02:00
|
|
|
SlavePTY::SlavePTY(MasterPTY& master, unsigned index)
|
2019-01-15 03:30:19 -02:00
|
|
|
: TTY(11, index)
|
2019-01-15 23:11:50 -02:00
|
|
|
, m_master(master)
|
2019-01-15 03:30:19 -02:00
|
|
|
, m_index(index)
|
|
|
|
|
{
|
2019-11-27 10:06:24 -03:00
|
|
|
sprintf(m_tty_name, "/dev/pts/%u", m_index);
|
2019-03-23 18:03:17 -03:00
|
|
|
set_uid(current->process().uid());
|
|
|
|
|
set_gid(current->process().gid());
|
2019-08-16 12:46:18 -03:00
|
|
|
DevPtsFS::register_slave_pty(*this);
|
2019-01-15 05:49:24 -02:00
|
|
|
set_size(80, 25);
|
2019-01-15 03:30:19 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SlavePTY::~SlavePTY()
|
|
|
|
|
{
|
2019-03-27 11:07:12 -03:00
|
|
|
#ifdef SLAVEPTY_DEBUG
|
2019-01-30 16:05:59 -02:00
|
|
|
dbgprintf("~SlavePTY(%u)\n", m_index);
|
2019-03-27 11:07:12 -03:00
|
|
|
#endif
|
2019-08-16 12:46:18 -03:00
|
|
|
DevPtsFS::unregister_slave_pty(*this);
|
2019-01-15 03:30:19 -02:00
|
|
|
}
|
|
|
|
|
|
2019-10-18 09:13:43 -03:00
|
|
|
StringView SlavePTY::tty_name() const
|
2019-01-15 03:30:19 -02:00
|
|
|
{
|
2019-04-15 19:35:02 -03:00
|
|
|
return m_tty_name;
|
2019-01-15 03:30:19 -02:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 03:12:00 -03:00
|
|
|
void SlavePTY::echo(u8 ch)
|
|
|
|
|
{
|
|
|
|
|
if (should_echo_input()) {
|
|
|
|
|
m_master->on_slave_write(&ch, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
void SlavePTY::on_master_write(const u8* buffer, ssize_t size)
|
2019-01-15 03:30:19 -02:00
|
|
|
{
|
2019-02-25 17:19:57 -03:00
|
|
|
for (ssize_t i = 0; i < size; ++i)
|
2019-01-15 03:30:19 -02:00
|
|
|
emit(buffer[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
ssize_t SlavePTY::on_tty_write(const u8* data, ssize_t size)
|
2019-01-15 03:30:19 -02:00
|
|
|
{
|
2019-02-05 10:09:01 -02:00
|
|
|
return m_master->on_slave_write(data, size);
|
2019-01-15 03:30:19 -02:00
|
|
|
}
|
2019-01-15 06:17:22 -02:00
|
|
|
|
2019-11-04 10:03:14 -03:00
|
|
|
bool SlavePTY::can_write(const FileDescription&) const
|
2019-01-15 06:17:22 -02:00
|
|
|
{
|
2019-01-30 16:05:59 -02:00
|
|
|
return m_master->can_write_from_slave();
|
2019-01-15 06:17:22 -02:00
|
|
|
}
|
2019-01-30 15:26:19 -02:00
|
|
|
|
2019-11-04 10:03:14 -03:00
|
|
|
bool SlavePTY::can_read(const FileDescription& description) const
|
2019-02-05 09:27:32 -02:00
|
|
|
{
|
|
|
|
|
if (m_master->is_closed())
|
|
|
|
|
return true;
|
2019-06-13 17:03:04 -03:00
|
|
|
return TTY::can_read(description);
|
2019-02-05 09:27:32 -02:00
|
|
|
}
|
|
|
|
|
|
2019-07-03 16:17:35 -03:00
|
|
|
ssize_t SlavePTY::read(FileDescription& description, u8* buffer, ssize_t size)
|
2019-02-05 09:27:32 -02:00
|
|
|
{
|
|
|
|
|
if (m_master->is_closed())
|
|
|
|
|
return 0;
|
2019-06-13 17:03:04 -03:00
|
|
|
return TTY::read(description, buffer, size);
|
2019-02-05 09:27:32 -02:00
|
|
|
}
|
|
|
|
|
|
2019-01-30 15:26:19 -02:00
|
|
|
void SlavePTY::close()
|
|
|
|
|
{
|
2019-05-31 10:44:04 -03:00
|
|
|
m_master->notify_slave_closed({});
|
2019-01-30 15:26:19 -02:00
|
|
|
}
|