Serenity Operating System
at hosted 142 lines 4.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "MasterPTY.h" 28#include "PTYMultiplexer.h" 29#include "SlavePTY.h" 30#include <Kernel/Process.h> 31#include <LibC/errno_numbers.h> 32#include <LibC/signal_numbers.h> 33#include <LibC/sys/ioctl_numbers.h> 34 35//#define MASTERPTY_DEBUG 36 37namespace Kernel { 38 39MasterPTY::MasterPTY(unsigned index) 40 : CharacterDevice(200, index) 41 , m_slave(adopt(*new SlavePTY(*this, index))) 42 , m_index(index) 43{ 44 m_pts_name = String::format("/dev/pts/%u", m_index); 45 set_uid(Process::current->uid()); 46 set_gid(Process::current->gid()); 47} 48 49MasterPTY::~MasterPTY() 50{ 51#ifdef MASTERPTY_DEBUG 52 dbg() << "~MasterPTY(" << m_index << ")"; 53#endif 54 PTYMultiplexer::the().notify_master_destroyed({}, m_index); 55} 56 57String MasterPTY::pts_name() const 58{ 59 return m_pts_name; 60} 61 62ssize_t MasterPTY::read(FileDescription&, u8* buffer, ssize_t size) 63{ 64 if (!m_slave && m_buffer.is_empty()) 65 return 0; 66 return m_buffer.read(buffer, size); 67} 68 69ssize_t MasterPTY::write(FileDescription&, const u8* buffer, ssize_t size) 70{ 71 if (!m_slave) 72 return -EIO; 73 m_slave->on_master_write(buffer, size); 74 return size; 75} 76 77bool MasterPTY::can_read(const FileDescription&) const 78{ 79 if (!m_slave) 80 return true; 81 return !m_buffer.is_empty(); 82} 83 84bool MasterPTY::can_write(const FileDescription&) const 85{ 86 return true; 87} 88 89void MasterPTY::notify_slave_closed(Badge<SlavePTY>) 90{ 91#ifdef MASTERPTY_DEBUG 92 dbg() << "MasterPTY(" << m_index << "): slave closed, my retains: " << ref_count() << ", slave retains: " << m_slave->ref_count(); 93#endif 94 // +1 ref for my MasterPTY::m_slave 95 // +1 ref for FileDescription::m_device 96 if (m_slave->ref_count() == 2) 97 m_slave = nullptr; 98} 99 100ssize_t MasterPTY::on_slave_write(const u8* data, ssize_t size) 101{ 102 if (m_closed) 103 return -EIO; 104 m_buffer.write(data, size); 105 return size; 106} 107 108bool MasterPTY::can_write_from_slave() const 109{ 110 if (m_closed) 111 return true; 112 return m_buffer.space_for_writing(); 113} 114 115void MasterPTY::close() 116{ 117 if (ref_count() == 2) { 118 InterruptDisabler disabler; 119 // After the closing FileDescription dies, slave is the only thing keeping me alive. 120 // From this point, let's consider ourselves closed. 121 m_closed = true; 122 123 m_slave->hang_up(); 124 } 125} 126 127int MasterPTY::ioctl(FileDescription& description, unsigned request, unsigned arg) 128{ 129 REQUIRE_PROMISE(tty); 130 if (!m_slave) 131 return -EIO; 132 if (request == TIOCSWINSZ || request == TIOCGPGRP) 133 return m_slave->ioctl(description, request, arg); 134 return -EINVAL; 135} 136 137String MasterPTY::absolute_path(const FileDescription&) const 138{ 139 return String::format("ptm:%s", m_pts_name.characters()); 140} 141 142}