DevPtsFS.cpp 5.4 KB

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