DevPtsFS.cpp 5.4 KB

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