MasterPTY.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/Debug.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. 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::formatted("/dev/pts/{}", m_index);
  41. auto process = Process::current();
  42. set_uid(process->uid());
  43. set_gid(process->gid());
  44. m_buffer.set_unblock_callback([this]() {
  45. if (m_slave)
  46. evaluate_block_conditions();
  47. });
  48. }
  49. MasterPTY::~MasterPTY()
  50. {
  51. dbgln<MASTERPTY_DEBUG>("~MasterPTY({})", m_index);
  52. PTYMultiplexer::the().notify_master_destroyed({}, m_index);
  53. }
  54. String MasterPTY::pts_name() const
  55. {
  56. return m_pts_name;
  57. }
  58. KResultOr<size_t> MasterPTY::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
  59. {
  60. if (!m_slave && m_buffer.is_empty())
  61. return 0;
  62. return m_buffer.read(buffer, size);
  63. }
  64. KResultOr<size_t> MasterPTY::write(FileDescription&, size_t, const UserOrKernelBuffer& buffer, size_t size)
  65. {
  66. if (!m_slave)
  67. return EIO;
  68. m_slave->on_master_write(buffer, size);
  69. return size;
  70. }
  71. bool MasterPTY::can_read(const FileDescription&, size_t) const
  72. {
  73. if (!m_slave)
  74. return true;
  75. return !m_buffer.is_empty();
  76. }
  77. bool MasterPTY::can_write(const FileDescription&, size_t) const
  78. {
  79. return true;
  80. }
  81. void MasterPTY::notify_slave_closed(Badge<SlavePTY>)
  82. {
  83. dbgln<MASTERPTY_DEBUG>("MasterPTY({}): slave closed, my retains: {}, slave retains: {}", m_index, ref_count(), m_slave->ref_count());
  84. // +1 ref for my MasterPTY::m_slave
  85. // +1 ref for FileDescription::m_device
  86. if (m_slave->ref_count() == 2)
  87. m_slave = nullptr;
  88. }
  89. ssize_t MasterPTY::on_slave_write(const UserOrKernelBuffer& data, ssize_t size)
  90. {
  91. if (m_closed)
  92. return -EIO;
  93. return m_buffer.write(data, 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. KResult 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. return KSuccess;
  111. }
  112. int MasterPTY::ioctl(FileDescription& description, unsigned request, FlatPtr arg)
  113. {
  114. REQUIRE_PROMISE(tty);
  115. if (!m_slave)
  116. return -EIO;
  117. if (request == TIOCSWINSZ || request == TIOCGPGRP)
  118. return m_slave->ioctl(description, request, arg);
  119. return -EINVAL;
  120. }
  121. String MasterPTY::absolute_path(const FileDescription&) const
  122. {
  123. return String::formatted("ptm:{}", m_pts_name);
  124. }
  125. String MasterPTY::device_name() const
  126. {
  127. return String::formatted("{}", minor());
  128. }
  129. }