SlavePTY.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "SlavePTY.h"
  27. #include "MasterPTY.h"
  28. #include <Kernel/Debug.h>
  29. #include <Kernel/FileSystem/DevPtsFS.h>
  30. #include <Kernel/Process.h>
  31. namespace Kernel {
  32. SlavePTY::SlavePTY(MasterPTY& master, unsigned index)
  33. : TTY(201, index)
  34. , m_master(master)
  35. , m_index(index)
  36. {
  37. snprintf(m_tty_name, sizeof(m_tty_name), "/dev/pts/%u", m_index);
  38. auto process = Process::current();
  39. set_uid(process->uid());
  40. set_gid(process->gid());
  41. DevPtsFS::register_slave_pty(*this);
  42. set_size(80, 25);
  43. }
  44. SlavePTY::~SlavePTY()
  45. {
  46. dbgln<SLAVEPTY_DEBUG>("~SlavePTY({})", m_index);
  47. DevPtsFS::unregister_slave_pty(*this);
  48. }
  49. String SlavePTY::tty_name() const
  50. {
  51. return m_tty_name;
  52. }
  53. void SlavePTY::echo(u8 ch)
  54. {
  55. if (should_echo_input()) {
  56. auto buffer = UserOrKernelBuffer::for_kernel_buffer(&ch);
  57. m_master->on_slave_write(buffer, 1);
  58. }
  59. }
  60. void SlavePTY::on_master_write(const UserOrKernelBuffer& buffer, ssize_t size)
  61. {
  62. ssize_t nread = buffer.read_buffered<128>(size, [&](const u8* data, size_t data_size) {
  63. for (size_t i = 0; i < data_size; ++i)
  64. emit(data[i], false);
  65. return (ssize_t)data_size;
  66. });
  67. if (nread > 0)
  68. evaluate_block_conditions();
  69. }
  70. ssize_t SlavePTY::on_tty_write(const UserOrKernelBuffer& data, ssize_t size)
  71. {
  72. m_time_of_last_write = kgettimeofday().tv_sec;
  73. return m_master->on_slave_write(data, size);
  74. }
  75. bool SlavePTY::can_write(const FileDescription&, size_t) const
  76. {
  77. return m_master->can_write_from_slave();
  78. }
  79. bool SlavePTY::can_read(const FileDescription& description, size_t offset) const
  80. {
  81. if (m_master->is_closed())
  82. return true;
  83. return TTY::can_read(description, offset);
  84. }
  85. KResultOr<size_t> SlavePTY::read(FileDescription& description, size_t offset, UserOrKernelBuffer& buffer, size_t size)
  86. {
  87. if (m_master->is_closed())
  88. return 0;
  89. return TTY::read(description, offset, buffer, size);
  90. }
  91. KResult SlavePTY::close()
  92. {
  93. m_master->notify_slave_closed({});
  94. return KSuccess;
  95. }
  96. String SlavePTY::device_name() const
  97. {
  98. return String::formatted("{}", minor());
  99. }
  100. FileBlockCondition& SlavePTY::block_condition()
  101. {
  102. return m_master->block_condition();
  103. }
  104. }