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