DevPtsFS.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@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 <AK/Singleton.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/StringView.h>
  29. #include <Kernel/FileSystem/DevPtsFS.h>
  30. #include <Kernel/FileSystem/VirtualFileSystem.h>
  31. #include <Kernel/TTY/SlavePTY.h>
  32. namespace Kernel {
  33. NonnullRefPtr<DevPtsFS> DevPtsFS::create()
  34. {
  35. return adopt(*new DevPtsFS);
  36. }
  37. DevPtsFS::DevPtsFS()
  38. {
  39. }
  40. DevPtsFS::~DevPtsFS()
  41. {
  42. }
  43. static AK::Singleton<HashTable<unsigned>> s_ptys;
  44. bool DevPtsFS::initialize()
  45. {
  46. m_root_inode = adopt(*new DevPtsFSInode(*this, 1, nullptr));
  47. m_root_inode->m_metadata.inode = { fsid(), 1 };
  48. m_root_inode->m_metadata.mode = 0040555;
  49. m_root_inode->m_metadata.uid = 0;
  50. m_root_inode->m_metadata.gid = 0;
  51. m_root_inode->m_metadata.size = 0;
  52. m_root_inode->m_metadata.mtime = mepoch;
  53. return true;
  54. }
  55. static unsigned inode_index_to_pty_index(unsigned inode_index)
  56. {
  57. ASSERT(inode_index > 1);
  58. return inode_index - 2;
  59. }
  60. static unsigned pty_index_to_inode_index(unsigned pty_index)
  61. {
  62. return pty_index + 2;
  63. }
  64. NonnullRefPtr<Inode> DevPtsFS::root_inode() const
  65. {
  66. return *m_root_inode;
  67. }
  68. RefPtr<Inode> DevPtsFS::get_inode(InodeIdentifier inode_id) const
  69. {
  70. if (inode_id.index() == 1)
  71. return m_root_inode;
  72. unsigned pty_index = inode_index_to_pty_index(inode_id.index());
  73. auto* device = Device::get_device(201, pty_index);
  74. ASSERT(device);
  75. auto inode = adopt(*new DevPtsFSInode(const_cast<DevPtsFS&>(*this), inode_id.index(), static_cast<SlavePTY*>(device)));
  76. inode->m_metadata.inode = inode_id;
  77. inode->m_metadata.size = 0;
  78. inode->m_metadata.uid = device->uid();
  79. inode->m_metadata.gid = device->gid();
  80. inode->m_metadata.mode = 0020600;
  81. inode->m_metadata.major_device = device->major();
  82. inode->m_metadata.minor_device = device->minor();
  83. inode->m_metadata.mtime = mepoch;
  84. return inode;
  85. }
  86. void DevPtsFS::register_slave_pty(SlavePTY& slave_pty)
  87. {
  88. s_ptys->set(slave_pty.index());
  89. }
  90. void DevPtsFS::unregister_slave_pty(SlavePTY& slave_pty)
  91. {
  92. s_ptys->remove(slave_pty.index());
  93. }
  94. DevPtsFSInode::DevPtsFSInode(DevPtsFS& fs, unsigned index, SlavePTY* pty)
  95. : Inode(fs, index)
  96. {
  97. if (pty)
  98. m_pty = pty->make_weak_ptr();
  99. }
  100. DevPtsFSInode::~DevPtsFSInode()
  101. {
  102. }
  103. ssize_t DevPtsFSInode::read_bytes(off_t, ssize_t, UserOrKernelBuffer&, FileDescription*) const
  104. {
  105. ASSERT_NOT_REACHED();
  106. }
  107. ssize_t DevPtsFSInode::write_bytes(off_t, ssize_t, const UserOrKernelBuffer&, FileDescription*)
  108. {
  109. ASSERT_NOT_REACHED();
  110. }
  111. InodeMetadata DevPtsFSInode::metadata() const
  112. {
  113. if (m_pty) {
  114. auto metadata = m_metadata;
  115. metadata.mtime = m_pty->time_of_last_write();
  116. return metadata;
  117. }
  118. return m_metadata;
  119. }
  120. KResult DevPtsFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  121. {
  122. if (identifier().index() > 1)
  123. return KResult(-ENOTDIR);
  124. callback({ ".", identifier(), 0 });
  125. callback({ "..", identifier(), 0 });
  126. for (unsigned pty_index : *s_ptys) {
  127. String name = String::number(pty_index);
  128. InodeIdentifier identifier = { fsid(), pty_index_to_inode_index(pty_index) };
  129. callback({ name, identifier, 0 });
  130. }
  131. return KSuccess;
  132. }
  133. KResultOr<size_t> DevPtsFSInode::directory_entry_count() const
  134. {
  135. ASSERT(identifier().index() == 1);
  136. return 2 + s_ptys->size();
  137. }
  138. RefPtr<Inode> DevPtsFSInode::lookup(StringView name)
  139. {
  140. ASSERT(identifier().index() == 1);
  141. if (name == "." || name == "..")
  142. return this;
  143. auto& fs = static_cast<DevPtsFS&>(this->fs());
  144. auto pty_index = name.to_uint();
  145. if (pty_index.has_value() && s_ptys->contains(pty_index.value())) {
  146. return fs.get_inode({ fsid(), pty_index_to_inode_index(pty_index.value()) });
  147. }
  148. return {};
  149. }
  150. void DevPtsFSInode::flush_metadata()
  151. {
  152. }
  153. KResult DevPtsFSInode::add_child(Inode&, const StringView&, mode_t)
  154. {
  155. return KResult(-EROFS);
  156. }
  157. KResultOr<NonnullRefPtr<Inode>> DevPtsFSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
  158. {
  159. return KResult(-EROFS);
  160. }
  161. KResult DevPtsFSInode::remove_child(const StringView&)
  162. {
  163. return KResult(-EROFS);
  164. }
  165. KResult DevPtsFSInode::chmod(mode_t)
  166. {
  167. return KResult(-EPERM);
  168. }
  169. KResult DevPtsFSInode::chown(uid_t, gid_t)
  170. {
  171. return KResult(-EPERM);
  172. }
  173. }