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