DevPtsFS.cpp 5.3 KB

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