Serenity Operating System
at hosted 103 lines 3.0 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 "SlavePTY.h" 28#include "MasterPTY.h" 29#include <Kernel/FileSystem/DevPtsFS.h> 30#include <Kernel/Process.h> 31 32//#define SLAVEPTY_DEBUG 33 34namespace Kernel { 35 36SlavePTY::SlavePTY(MasterPTY& master, unsigned index) 37 : TTY(201, index) 38 , m_master(master) 39 , m_index(index) 40{ 41 sprintf(m_tty_name, "/dev/pts/%u", m_index); 42 set_uid(Process::current->uid()); 43 set_gid(Process::current->gid()); 44 DevPtsFS::register_slave_pty(*this); 45 set_size(80, 25); 46} 47 48SlavePTY::~SlavePTY() 49{ 50#ifdef SLAVEPTY_DEBUG 51 dbg() << "~SlavePTY(" << m_index << ")"; 52#endif 53 DevPtsFS::unregister_slave_pty(*this); 54} 55 56StringView SlavePTY::tty_name() const 57{ 58 return m_tty_name; 59} 60 61void SlavePTY::echo(u8 ch) 62{ 63 if (should_echo_input()) { 64 m_master->on_slave_write(&ch, 1); 65 } 66} 67 68void SlavePTY::on_master_write(const u8* buffer, ssize_t size) 69{ 70 for (ssize_t i = 0; i < size; ++i) 71 emit(buffer[i]); 72} 73 74ssize_t SlavePTY::on_tty_write(const u8* data, ssize_t size) 75{ 76 return m_master->on_slave_write(data, size); 77} 78 79bool SlavePTY::can_write(const FileDescription&) const 80{ 81 return m_master->can_write_from_slave(); 82} 83 84bool SlavePTY::can_read(const FileDescription& description) const 85{ 86 if (m_master->is_closed()) 87 return true; 88 return TTY::can_read(description); 89} 90 91ssize_t SlavePTY::read(FileDescription& description, u8* buffer, ssize_t size) 92{ 93 if (m_master->is_closed()) 94 return 0; 95 return TTY::read(description, buffer, size); 96} 97 98void SlavePTY::close() 99{ 100 m_master->notify_slave_closed({}); 101} 102 103}