MasterPTY.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "MasterPTY.h"
  27. #include "PTYMultiplexer.h"
  28. #include "SlavePTY.h"
  29. #include <Kernel/Process.h>
  30. #include <LibC/errno_numbers.h>
  31. #include <LibC/signal_numbers.h>
  32. #include <LibC/sys/ioctl_numbers.h>
  33. //#define MASTERPTY_DEBUG
  34. namespace Kernel {
  35. MasterPTY::MasterPTY(unsigned index)
  36. : CharacterDevice(200, index)
  37. , m_slave(adopt(*new SlavePTY(*this, index)))
  38. , m_index(index)
  39. {
  40. m_pts_name = String::format("/dev/pts/%u", m_index);
  41. set_uid(Process::current->uid());
  42. set_gid(Process::current->gid());
  43. }
  44. MasterPTY::~MasterPTY()
  45. {
  46. #ifdef MASTERPTY_DEBUG
  47. dbg() << "~MasterPTY(" << m_index << ")";
  48. #endif
  49. PTYMultiplexer::the().notify_master_destroyed({}, m_index);
  50. }
  51. String MasterPTY::pts_name() const
  52. {
  53. return m_pts_name;
  54. }
  55. ssize_t MasterPTY::read(FileDescription&, u8* buffer, ssize_t size)
  56. {
  57. if (!m_slave && m_buffer.is_empty())
  58. return 0;
  59. return m_buffer.read(buffer, size);
  60. }
  61. ssize_t MasterPTY::write(FileDescription&, const u8* buffer, ssize_t size)
  62. {
  63. if (!m_slave)
  64. return -EIO;
  65. m_slave->on_master_write(buffer, size);
  66. return size;
  67. }
  68. bool MasterPTY::can_read(const FileDescription&) const
  69. {
  70. if (!m_slave)
  71. return true;
  72. return !m_buffer.is_empty();
  73. }
  74. bool MasterPTY::can_write(const FileDescription&) const
  75. {
  76. return true;
  77. }
  78. void MasterPTY::notify_slave_closed(Badge<SlavePTY>)
  79. {
  80. #ifdef MASTERPTY_DEBUG
  81. dbg() << "MasterPTY(" << m_index << "): slave closed, my retains: " << ref_count() << ", slave retains: " << m_slave->ref_count();
  82. #endif
  83. // +1 ref for my MasterPTY::m_slave
  84. // +1 ref for FileDescription::m_device
  85. if (m_slave->ref_count() == 2)
  86. m_slave = nullptr;
  87. }
  88. ssize_t MasterPTY::on_slave_write(const u8* data, ssize_t size)
  89. {
  90. if (m_closed)
  91. return -EIO;
  92. m_buffer.write(data, size);
  93. return size;
  94. }
  95. bool MasterPTY::can_write_from_slave() const
  96. {
  97. if (m_closed)
  98. return true;
  99. return m_buffer.space_for_writing();
  100. }
  101. void MasterPTY::close()
  102. {
  103. if (ref_count() == 2) {
  104. InterruptDisabler disabler;
  105. // After the closing FileDescription dies, slave is the only thing keeping me alive.
  106. // From this point, let's consider ourselves closed.
  107. m_closed = true;
  108. m_slave->hang_up();
  109. }
  110. }
  111. int MasterPTY::ioctl(FileDescription& description, unsigned request, unsigned arg)
  112. {
  113. REQUIRE_PROMISE(tty);
  114. if (!m_slave)
  115. return -EIO;
  116. if (request == TIOCSWINSZ || request == TIOCGPGRP)
  117. return m_slave->ioctl(description, request, arg);
  118. return -EINVAL;
  119. }
  120. String MasterPTY::absolute_path(const FileDescription&) const
  121. {
  122. return String::format("ptm:%s", m_pts_name.characters());
  123. }
  124. }