2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-01-15 08:30:19 +03:00
|
|
|
#include "SlavePTY.h"
|
|
|
|
#include "MasterPTY.h"
|
2019-04-03 13:25:24 +03:00
|
|
|
#include <Kernel/FileSystem/DevPtsFS.h>
|
2019-01-31 07:55:30 +03:00
|
|
|
#include <Kernel/Process.h>
|
2019-01-15 08:30:19 +03:00
|
|
|
|
2019-03-27 17:07:12 +03:00
|
|
|
//#define SLAVEPTY_DEBUG
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-01-16 04:11:50 +03:00
|
|
|
SlavePTY::SlavePTY(MasterPTY& master, unsigned index)
|
2019-12-09 23:03:39 +03:00
|
|
|
: TTY(201, index)
|
2019-01-16 04:11:50 +03:00
|
|
|
, m_master(master)
|
2019-01-15 08:30:19 +03:00
|
|
|
, m_index(index)
|
|
|
|
{
|
2020-08-16 08:51:56 +03:00
|
|
|
snprintf(m_tty_name, sizeof(m_tty_name), "/dev/pts/%u", m_index);
|
2020-06-29 00:34:31 +03:00
|
|
|
auto process = Process::current();
|
|
|
|
set_uid(process->uid());
|
|
|
|
set_gid(process->gid());
|
2019-08-16 18:46:18 +03:00
|
|
|
DevPtsFS::register_slave_pty(*this);
|
2019-01-15 10:49:24 +03:00
|
|
|
set_size(80, 25);
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SlavePTY::~SlavePTY()
|
|
|
|
{
|
2019-03-27 17:07:12 +03:00
|
|
|
#ifdef SLAVEPTY_DEBUG
|
2020-02-25 02:10:07 +03:00
|
|
|
dbg() << "~SlavePTY(" << m_index << ")";
|
2019-03-27 17:07:12 +03:00
|
|
|
#endif
|
2019-08-16 18:46:18 +03:00
|
|
|
DevPtsFS::unregister_slave_pty(*this);
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
2020-08-04 17:27:52 +03:00
|
|
|
String SlavePTY::tty_name() const
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-04-16 01:35:02 +03:00
|
|
|
return m_tty_name;
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
2019-10-20 09:12:00 +03:00
|
|
|
void SlavePTY::echo(u8 ch)
|
|
|
|
{
|
|
|
|
if (should_echo_input()) {
|
|
|
|
m_master->on_slave_write(&ch, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void SlavePTY::on_master_write(const u8* buffer, ssize_t size)
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-02-25 23:19:57 +03:00
|
|
|
for (ssize_t i = 0; i < size; ++i)
|
2019-01-15 08:30:19 +03:00
|
|
|
emit(buffer[i]);
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
ssize_t SlavePTY::on_tty_write(const u8* data, ssize_t size)
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2020-09-06 19:48:24 +03:00
|
|
|
m_time_of_last_write = kgettimeofday().tv_sec;
|
2019-02-05 15:09:01 +03:00
|
|
|
return m_master->on_slave_write(data, size);
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
2019-01-15 11:17:22 +03:00
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
bool SlavePTY::can_write(const FileDescription&, size_t) const
|
2019-01-15 11:17:22 +03:00
|
|
|
{
|
2019-01-30 21:05:59 +03:00
|
|
|
return m_master->can_write_from_slave();
|
2019-01-15 11:17:22 +03:00
|
|
|
}
|
2019-01-30 20:26:19 +03:00
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
bool SlavePTY::can_read(const FileDescription& description, size_t offset) const
|
2019-02-05 14:27:32 +03:00
|
|
|
{
|
|
|
|
if (m_master->is_closed())
|
|
|
|
return true;
|
2020-04-10 12:44:42 +03:00
|
|
|
return TTY::can_read(description, offset);
|
2019-02-05 14:27:32 +03:00
|
|
|
}
|
|
|
|
|
2020-08-04 19:02:23 +03:00
|
|
|
KResultOr<size_t> SlavePTY::read(FileDescription& description, size_t offset, u8* buffer, size_t size)
|
2019-02-05 14:27:32 +03:00
|
|
|
{
|
|
|
|
if (m_master->is_closed())
|
|
|
|
return 0;
|
2020-04-10 12:44:42 +03:00
|
|
|
return TTY::read(description, offset, buffer, size);
|
2019-02-05 14:27:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 19:20:05 +03:00
|
|
|
KResult SlavePTY::close()
|
2019-01-30 20:26:19 +03:00
|
|
|
{
|
2019-05-31 16:44:04 +03:00
|
|
|
m_master->notify_slave_closed({});
|
2020-06-02 19:20:05 +03:00
|
|
|
return KSuccess;
|
2019-01-30 20:26:19 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|