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"
|
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
|
|
|
|
2019-03-27 15:07:12 +01:00
|
|
|
//#define MASTERPTY_DEBUG
|
|
|
|
|
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)
|
|
|
|
{
|
2019-04-16 00:35:02 +02:00
|
|
|
m_pts_name = String::format("/dev/pts/%u", m_index);
|
2019-03-23 22:03:17 +01:00
|
|
|
set_uid(current->process().uid());
|
|
|
|
set_gid(current->process().gid());
|
2019-01-15 06:30:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MasterPTY::~MasterPTY()
|
|
|
|
{
|
2019-03-27 15:07:12 +01:00
|
|
|
#ifdef MASTERPTY_DEBUG
|
2019-01-30 19:05:59 +01:00
|
|
|
dbgprintf("~MasterPTY(%u)\n", m_index);
|
2019-03-27 15:07:12 +01:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t MasterPTY::read(FileDescription&, u8* buffer, ssize_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);
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t MasterPTY::write(FileDescription&, const u8* buffer, ssize_t size)
|
2019-01-15 06:30:19 +01:00
|
|
|
{
|
2019-01-30 18:26:19 +01:00
|
|
|
if (!m_slave)
|
|
|
|
return -EIO;
|
|
|
|
m_slave->on_master_write(buffer, size);
|
2019-01-15 06:30:19 +01:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-11-04 14:03:14 +01:00
|
|
|
bool MasterPTY::can_read(const FileDescription&) 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();
|
|
|
|
}
|
|
|
|
|
2019-11-04 14:03:14 +01:00
|
|
|
bool MasterPTY::can_write(const FileDescription&) 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>)
|
|
|
|
{
|
2019-03-27 15:07:12 +01:00
|
|
|
#ifdef MASTERPTY_DEBUG
|
2019-06-21 15:29:31 +02:00
|
|
|
dbgprintf("MasterPTY(%u): slave closed, my retains: %u, slave retains: %u\n", m_index, ref_count(), m_slave->ref_count());
|
2019-03-27 15:07:12 +01:00
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t MasterPTY::on_slave_write(const u8* 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;
|
2019-01-15 06:30:19 +01:00
|
|
|
m_buffer.write(data, size);
|
2019-02-05 13:09:01 +01:00
|
|
|
return 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;
|
2019-10-18 15:58:06 +02:00
|
|
|
return m_buffer.bytes_in_write_buffer() < 4096;
|
2019-01-25 00:13:54 +01:00
|
|
|
}
|
2019-02-05 12:27:32 +01:00
|
|
|
|
|
|
|
void MasterPTY::close()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
2019-02-20 23:32:33 +01:00
|
|
|
|
2019-06-13 22:03:04 +02:00
|
|
|
int MasterPTY::ioctl(FileDescription& description, unsigned request, unsigned 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
|
|
|
|
{
|
|
|
|
return String::format("ptm:%s", m_pts_name.characters());
|
|
|
|
}
|