2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2023-01-07 23:52:06 +03:00
|
|
|
#include <Kernel/API/Ioctl.h>
|
2021-09-12 14:29:28 +03:00
|
|
|
#include <Kernel/API/POSIX/errno.h>
|
2023-01-07 20:54:01 +03:00
|
|
|
#include <Kernel/API/POSIX/signal_numbers.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2022-10-05 20:27:36 +03:00
|
|
|
#include <Kernel/InterruptDisabler.h>
|
2019-01-31 07:55:30 +03:00
|
|
|
#include <Kernel/Process.h>
|
2021-07-11 22:02:15 +03:00
|
|
|
#include <Kernel/TTY/MasterPTY.h>
|
|
|
|
#include <Kernel/TTY/PTYMultiplexer.h>
|
|
|
|
#include <Kernel/TTY/SlavePTY.h>
|
2019-01-15 08:30:19 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<MasterPTY>> MasterPTY::try_create(unsigned int index)
|
2021-08-01 11:54:09 +03:00
|
|
|
{
|
2022-04-11 01:08:07 +03:00
|
|
|
auto buffer = TRY(DoubleBuffer::try_create("MasterPTY: Buffer"sv));
|
2022-08-19 21:53:40 +03:00
|
|
|
auto master_pty = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) MasterPTY(index, move(buffer))));
|
|
|
|
auto slave_pty = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) SlavePTY(*master_pty, index)));
|
2021-08-01 11:54:09 +03:00
|
|
|
master_pty->m_slave = slave_pty;
|
2022-12-21 23:33:56 +03:00
|
|
|
TRY(master_pty->after_inserting());
|
|
|
|
TRY(slave_pty->after_inserting());
|
2021-08-01 11:54:09 +03:00
|
|
|
return master_pty;
|
|
|
|
}
|
|
|
|
|
2022-02-15 22:41:41 +03:00
|
|
|
MasterPTY::MasterPTY(unsigned index, NonnullOwnPtr<DoubleBuffer> buffer)
|
2019-12-09 23:03:39 +03:00
|
|
|
: CharacterDevice(200, index)
|
2019-01-15 08:30:19 +03:00
|
|
|
, m_index(index)
|
2021-08-01 11:54:09 +03:00
|
|
|
, m_buffer(move(buffer))
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2021-08-19 22:45:07 +03:00
|
|
|
auto& process = Process::current();
|
2022-08-21 01:21:01 +03:00
|
|
|
auto credentials = process.credentials();
|
|
|
|
set_uid(credentials->uid());
|
|
|
|
set_gid(credentials->gid());
|
2020-11-30 02:05:27 +03:00
|
|
|
|
2021-08-01 11:54:09 +03:00
|
|
|
m_buffer->set_unblock_callback([this]() {
|
2020-11-30 02:05:27 +03:00
|
|
|
if (m_slave)
|
|
|
|
evaluate_block_conditions();
|
|
|
|
});
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MasterPTY::~MasterPTY()
|
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(MASTERPTY_DEBUG, "~MasterPTY({})", m_index);
|
2019-05-31 16:44:04 +03:00
|
|
|
PTYMultiplexer::the().notify_master_destroyed({}, m_index);
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<size_t> MasterPTY::read(OpenFileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2021-08-01 11:54:09 +03:00
|
|
|
if (!m_slave && m_buffer->is_empty())
|
2019-01-30 20:26:19 +03:00
|
|
|
return 0;
|
2021-08-01 11:54:09 +03:00
|
|
|
return m_buffer->read(buffer, size);
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<size_t> MasterPTY::write(OpenFileDescription&, u64, UserOrKernelBuffer const& buffer, size_t size)
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-01-30 20:26:19 +03:00
|
|
|
if (!m_slave)
|
2021-01-21 01:11:17 +03:00
|
|
|
return EIO;
|
2019-01-30 20:26:19 +03:00
|
|
|
m_slave->on_master_write(buffer, size);
|
2019-01-15 08:30:19 +03:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool MasterPTY::can_read(OpenFileDescription const&, u64) const
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-01-30 20:26:19 +03:00
|
|
|
if (!m_slave)
|
|
|
|
return true;
|
2021-08-01 11:54:09 +03:00
|
|
|
return !m_buffer->is_empty();
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool MasterPTY::can_write(OpenFileDescription const&, u64) 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>)
|
|
|
|
{
|
2021-02-07 15: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 19:40:24 +03:00
|
|
|
// +1 ref for my MasterPTY::m_slave
|
2021-09-07 14:39:11 +03:00
|
|
|
// +1 ref for OpenFileDescription::m_device
|
2019-06-21 16:29:31 +03:00
|
|
|
if (m_slave->ref_count() == 2)
|
2019-01-30 20:26:19 +03:00
|
|
|
m_slave = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<size_t> MasterPTY::on_slave_write(UserOrKernelBuffer const& data, size_t size)
|
2019-01-15 08:30:19 +03:00
|
|
|
{
|
2019-02-05 15:09:01 +03:00
|
|
|
if (m_closed)
|
2021-06-16 16:20:35 +03:00
|
|
|
return EIO;
|
2021-08-01 11:54:09 +03:00
|
|
|
return m_buffer->write(data, 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;
|
2021-08-01 11:54:09 +03:00
|
|
|
return m_buffer->space_for_writing();
|
2019-01-25 02:13:54 +03:00
|
|
|
}
|
2019-02-05 14:27:32 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> MasterPTY::close()
|
2019-02-05 14:27:32 +03:00
|
|
|
{
|
2021-05-01 23:53:58 +03:00
|
|
|
InterruptDisabler disabler;
|
2021-09-07 14:39:11 +03:00
|
|
|
// After the closing OpenFileDescription dies, slave is the only thing keeping me alive.
|
2021-05-01 23:53:58 +03:00
|
|
|
// From this point, let's consider ourselves closed.
|
|
|
|
m_closed = true;
|
2019-02-05 14:55:19 +03:00
|
|
|
|
2021-05-01 23:53:58 +03:00
|
|
|
if (m_slave)
|
2019-02-05 14:55:19 +03:00
|
|
|
m_slave->hang_up();
|
2020-06-02 19:20:05 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
return {};
|
2019-02-05 14:27:32 +03:00
|
|
|
}
|
2019-02-21 01:32:33 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> MasterPTY::ioctl(OpenFileDescription& description, unsigned request, Userspace<void*> arg)
|
2019-02-21 01:32:33 +03:00
|
|
|
{
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(Process::current().require_promise(Pledge::tty));
|
2019-10-24 22:03:49 +03:00
|
|
|
if (!m_slave)
|
2021-07-26 13:47:25 +03:00
|
|
|
return EIO;
|
2022-02-15 22:31:17 +03:00
|
|
|
switch (request) {
|
|
|
|
case TIOCGPTN: {
|
|
|
|
int master_pty_index = index();
|
|
|
|
return copy_to_user(static_ptr_cast<int*>(arg), &master_pty_index);
|
|
|
|
}
|
|
|
|
case TIOCSWINSZ:
|
|
|
|
case TIOCGPGRP:
|
2019-06-13 23:03:04 +03:00
|
|
|
return m_slave->ioctl(description, request, arg);
|
2022-02-15 22:31:17 +03:00
|
|
|
default:
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2019-02-21 01:32:33 +03:00
|
|
|
}
|
2019-08-10 19:10:36 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> MasterPTY::pseudo_path(OpenFileDescription const&) const
|
2019-08-10 19:10:36 +03:00
|
|
|
{
|
2022-02-15 22:41:41 +03:00
|
|
|
return KString::formatted("ptm:{}", m_index);
|
2019-08-10 19:10:36 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|