2020-01-18 05: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 03:30:19 -02:00
|
|
|
#include "MasterPTY.h"
|
2019-01-30 15:26:19 -02:00
|
|
|
#include "PTYMultiplexer.h"
|
2019-06-07 06:43:58 -03:00
|
|
|
#include "SlavePTY.h"
|
2021-01-25 12:07:10 -03:00
|
|
|
#include <Kernel/Debug.h>
|
2019-01-31 02:55:30 -02:00
|
|
|
#include <Kernel/Process.h>
|
2019-01-30 15:26:19 -02:00
|
|
|
#include <LibC/errno_numbers.h>
|
2019-02-05 09:55:19 -02:00
|
|
|
#include <LibC/signal_numbers.h>
|
2019-02-20 19:32:33 -03:00
|
|
|
#include <LibC/sys/ioctl_numbers.h>
|
2019-01-15 03:30:19 -02:00
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2019-01-15 03:30:19 -02:00
|
|
|
MasterPTY::MasterPTY(unsigned index)
|
2019-12-09 17:03:39 -03:00
|
|
|
: CharacterDevice(200, index)
|
2019-01-30 15:26:19 -02:00
|
|
|
, m_slave(adopt(*new SlavePTY(*this, index)))
|
2019-01-15 03:30:19 -02:00
|
|
|
, m_index(index)
|
|
|
|
|
{
|
2021-01-11 18:07:01 -03:00
|
|
|
m_pts_name = String::formatted("/dev/pts/{}", m_index);
|
2020-06-28 18:34:31 -03:00
|
|
|
auto process = Process::current();
|
|
|
|
|
set_uid(process->uid());
|
|
|
|
|
set_gid(process->gid());
|
2020-11-29 20:05:27 -03:00
|
|
|
|
|
|
|
|
m_buffer.set_unblock_callback([this]() {
|
|
|
|
|
if (m_slave)
|
|
|
|
|
evaluate_block_conditions();
|
|
|
|
|
});
|
2019-01-15 03:30:19 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MasterPTY::~MasterPTY()
|
|
|
|
|
{
|
2021-02-07 09:03:24 -03:00
|
|
|
dbgln_if(MASTERPTY_DEBUG, "~MasterPTY({})", m_index);
|
2019-05-31 10:44:04 -03:00
|
|
|
PTYMultiplexer::the().notify_master_destroyed({}, m_index);
|
2019-01-15 03:30:19 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String MasterPTY::pts_name() const
|
|
|
|
|
{
|
2019-04-15 19:35:02 -03:00
|
|
|
return m_pts_name;
|
2019-01-15 03:30:19 -02:00
|
|
|
}
|
|
|
|
|
|
2020-09-12 00:11:07 -03:00
|
|
|
KResultOr<size_t> MasterPTY::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
|
2019-01-15 03:30:19 -02:00
|
|
|
{
|
2019-01-30 15:26:19 -02:00
|
|
|
if (!m_slave && m_buffer.is_empty())
|
|
|
|
|
return 0;
|
2019-01-15 03:30:19 -02:00
|
|
|
return m_buffer.read(buffer, size);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 00:11:07 -03:00
|
|
|
KResultOr<size_t> MasterPTY::write(FileDescription&, size_t, const UserOrKernelBuffer& buffer, size_t size)
|
2019-01-15 03:30:19 -02:00
|
|
|
{
|
2019-01-30 15:26:19 -02:00
|
|
|
if (!m_slave)
|
2021-01-20 19:11:17 -03:00
|
|
|
return EIO;
|
2019-01-30 15:26:19 -02:00
|
|
|
m_slave->on_master_write(buffer, size);
|
2019-01-15 03:30:19 -02:00
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 06:44:42 -03:00
|
|
|
bool MasterPTY::can_read(const FileDescription&, size_t) const
|
2019-01-15 03:30:19 -02:00
|
|
|
{
|
2019-01-30 15:26:19 -02:00
|
|
|
if (!m_slave)
|
|
|
|
|
return true;
|
2019-01-15 03:30:19 -02:00
|
|
|
return !m_buffer.is_empty();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 06:44:42 -03:00
|
|
|
bool MasterPTY::can_write(const FileDescription&, size_t) const
|
2019-01-15 06:17:22 -02:00
|
|
|
{
|
2019-01-24 18:23:46 -02:00
|
|
|
return true;
|
2019-01-15 06:17:22 -02:00
|
|
|
}
|
|
|
|
|
|
2019-01-30 15:26:19 -02:00
|
|
|
void MasterPTY::notify_slave_closed(Badge<SlavePTY>)
|
|
|
|
|
{
|
2021-02-07 09:03:24 -03:00
|
|
|
dbgln_if(MASTERPTY_DEBUG, "MasterPTY({}): slave closed, my retains: {}, slave retains: {}", m_index, ref_count(), m_slave->ref_count());
|
2019-06-21 13:40:24 -03:00
|
|
|
// +1 ref for my MasterPTY::m_slave
|
|
|
|
|
// +1 ref for FileDescription::m_device
|
2019-06-21 10:29:31 -03:00
|
|
|
if (m_slave->ref_count() == 2)
|
2019-01-30 15:26:19 -02:00
|
|
|
m_slave = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 00:11:07 -03:00
|
|
|
ssize_t MasterPTY::on_slave_write(const UserOrKernelBuffer& data, ssize_t size)
|
2019-01-15 03:30:19 -02:00
|
|
|
{
|
2019-02-05 10:09:01 -02:00
|
|
|
if (m_closed)
|
|
|
|
|
return -EIO;
|
2020-09-12 00:11:07 -03:00
|
|
|
return m_buffer.write(data, size);
|
2019-01-15 03:30:19 -02:00
|
|
|
}
|
2019-01-24 21:13:54 -02:00
|
|
|
|
|
|
|
|
bool MasterPTY::can_write_from_slave() const
|
|
|
|
|
{
|
2019-02-05 10:09:01 -02:00
|
|
|
if (m_closed)
|
|
|
|
|
return true;
|
2020-01-20 12:08:49 -03:00
|
|
|
return m_buffer.space_for_writing();
|
2019-01-24 21:13:54 -02:00
|
|
|
}
|
2019-02-05 09:27:32 -02:00
|
|
|
|
2020-06-02 13:20:05 -03:00
|
|
|
KResult MasterPTY::close()
|
2019-02-05 09:27:32 -02:00
|
|
|
{
|
2019-06-21 10:29:31 -03:00
|
|
|
if (ref_count() == 2) {
|
2019-02-05 09:55:19 -02:00
|
|
|
InterruptDisabler disabler;
|
2019-06-07 04:36:51 -03:00
|
|
|
// After the closing FileDescription dies, slave is the only thing keeping me alive.
|
2019-02-05 09:27:32 -02:00
|
|
|
// From this point, let's consider ourselves closed.
|
|
|
|
|
m_closed = true;
|
2019-02-05 09:55:19 -02:00
|
|
|
|
|
|
|
|
m_slave->hang_up();
|
2019-02-05 09:27:32 -02:00
|
|
|
}
|
2020-06-02 13:20:05 -03:00
|
|
|
|
|
|
|
|
return KSuccess;
|
2019-02-05 09:27:32 -02:00
|
|
|
}
|
2019-02-20 19:32:33 -03:00
|
|
|
|
2020-05-23 08:17:58 -03:00
|
|
|
int MasterPTY::ioctl(FileDescription& description, unsigned request, FlatPtr arg)
|
2019-02-20 19:32:33 -03:00
|
|
|
{
|
2020-01-12 09:28:45 -03:00
|
|
|
REQUIRE_PROMISE(tty);
|
2019-10-24 16:03:49 -03:00
|
|
|
if (!m_slave)
|
|
|
|
|
return -EIO;
|
2019-10-24 15:54:35 -03:00
|
|
|
if (request == TIOCSWINSZ || request == TIOCGPGRP)
|
2019-06-13 17:03:04 -03:00
|
|
|
return m_slave->ioctl(description, request, arg);
|
2019-02-20 19:32:33 -03:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2019-08-10 13:10:36 -03:00
|
|
|
|
|
|
|
|
String MasterPTY::absolute_path(const FileDescription&) const
|
|
|
|
|
{
|
2021-01-11 18:07:01 -03:00
|
|
|
return String::formatted("ptm:{}", m_pts_name);
|
2019-08-10 13:10:36 -03:00
|
|
|
}
|
2020-02-15 21:27:42 -03:00
|
|
|
|
2021-01-21 14:49:56 -03:00
|
|
|
String MasterPTY::device_name() const
|
|
|
|
|
{
|
|
|
|
|
return String::formatted("{}", minor());
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
}
|