ProcessExposed.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Debug.h>
  7. #include <Kernel/Devices/BlockDevice.h>
  8. #include <Kernel/FileSystem/ProcFS.h>
  9. #include <Kernel/FileSystem/VirtualFileSystem.h>
  10. #include <Kernel/KBufferBuilder.h>
  11. #include <Kernel/PerformanceEventBuffer.h>
  12. #include <Kernel/Process.h>
  13. #include <Kernel/ProcessExposed.h>
  14. namespace Kernel {
  15. static Spinlock<u8> s_index_lock;
  16. static InodeIndex s_next_inode_index = 0;
  17. namespace SegmentedProcFSIndex {
  18. static InodeIndex __build_raw_segmented_index(u32 primary, u16 sub_directory, u32 property)
  19. {
  20. VERIFY(primary < 0x10000000);
  21. VERIFY(property < 0x100000);
  22. // Note: The sub-directory part is already limited to 0xFFFF, so no need to VERIFY it.
  23. return static_cast<u64>((static_cast<u64>(primary) << 36) | (static_cast<u64>(sub_directory) << 20) | property);
  24. }
  25. static InodeIndex build_segmented_index_with_known_pid(ProcessID pid, u16 sub_directory, u32 property)
  26. {
  27. return __build_raw_segmented_index(pid.value() + 1, sub_directory, property);
  28. }
  29. static InodeIndex build_segmented_index_with_unknown_property(ProcessID pid, ProcessSubDirectory sub_directory, unsigned property)
  30. {
  31. return build_segmented_index_with_known_pid(pid, to_underlying(sub_directory), static_cast<u32>(property));
  32. }
  33. InodeIndex build_segmented_index_for_pid_directory(ProcessID pid)
  34. {
  35. return build_segmented_index_with_unknown_property(pid, ProcessSubDirectory::Reserved, to_underlying(MainProcessProperty::Reserved));
  36. }
  37. InodeIndex build_segmented_index_for_sub_directory(ProcessID pid, ProcessSubDirectory sub_directory)
  38. {
  39. return build_segmented_index_with_unknown_property(pid, sub_directory, to_underlying(MainProcessProperty::Reserved));
  40. }
  41. InodeIndex build_segmented_index_for_main_property(ProcessID pid, ProcessSubDirectory sub_directory, MainProcessProperty property)
  42. {
  43. return build_segmented_index_with_known_pid(pid, to_underlying(sub_directory), to_underlying(property));
  44. }
  45. InodeIndex build_segmented_index_for_main_property_in_pid_directory(ProcessID pid, MainProcessProperty property)
  46. {
  47. return build_segmented_index_with_known_pid(pid, to_underlying(ProcessSubDirectory::Reserved), to_underlying(property));
  48. }
  49. InodeIndex build_segmented_index_for_thread_stack(ProcessID pid, ThreadID thread_id)
  50. {
  51. return build_segmented_index_with_unknown_property(pid, ProcessSubDirectory::Stacks, thread_id.value());
  52. }
  53. InodeIndex build_segmented_index_for_file_description(ProcessID pid, unsigned fd)
  54. {
  55. return build_segmented_index_with_unknown_property(pid, ProcessSubDirectory::FileDescriptions, fd);
  56. }
  57. }
  58. static size_t s_allocate_global_inode_index()
  59. {
  60. SpinlockLocker lock(s_index_lock);
  61. s_next_inode_index = s_next_inode_index.value() + 1;
  62. // Note: Global ProcFS indices must be above 0 and up to maximum of what 36 bit (2 ^ 36 - 1) can represent.
  63. VERIFY(s_next_inode_index > 0);
  64. VERIFY(s_next_inode_index < 0x100000000);
  65. return s_next_inode_index.value();
  66. }
  67. ProcFSExposedComponent::ProcFSExposedComponent()
  68. {
  69. }
  70. ProcFSExposedComponent::ProcFSExposedComponent(StringView name)
  71. : m_component_index(s_allocate_global_inode_index())
  72. {
  73. m_name = KString::try_create(name);
  74. }
  75. ProcFSExposedDirectory::ProcFSExposedDirectory(StringView name)
  76. : ProcFSExposedComponent(name)
  77. {
  78. }
  79. ProcFSExposedDirectory::ProcFSExposedDirectory(StringView name, const ProcFSExposedDirectory& parent_directory)
  80. : ProcFSExposedComponent(name)
  81. , m_parent_directory(parent_directory)
  82. {
  83. }
  84. ProcFSExposedLink::ProcFSExposedLink(StringView name)
  85. : ProcFSExposedComponent(name)
  86. {
  87. }
  88. KResultOr<size_t> ProcFSGlobalInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  89. {
  90. dbgln_if(PROCFS_DEBUG, "ProcFSGlobalInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count);
  91. VERIFY(offset >= 0);
  92. VERIFY(buffer.user_or_kernel_ptr());
  93. if (!description)
  94. return KResult(EIO);
  95. MutexLocker locker(m_refresh_lock);
  96. if (!description->data()) {
  97. dbgln("ProcFSGlobalInformation: Do not have cached data!");
  98. return KResult(EIO);
  99. }
  100. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*description->data());
  101. auto& data_buffer = typed_cached_data.buffer;
  102. if (!data_buffer || (size_t)offset >= data_buffer->size())
  103. return 0;
  104. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  105. if (!buffer.write(data_buffer->data() + offset, nread))
  106. return KResult(EFAULT);
  107. return nread;
  108. }
  109. KResult ProcFSGlobalInformation::refresh_data(FileDescription& description) const
  110. {
  111. MutexLocker lock(m_refresh_lock);
  112. auto& cached_data = description.data();
  113. if (!cached_data) {
  114. cached_data = adopt_own_if_nonnull(new (nothrow) ProcFSInodeData);
  115. if (!cached_data)
  116. return ENOMEM;
  117. }
  118. KBufferBuilder builder;
  119. if (!const_cast<ProcFSGlobalInformation&>(*this).output(builder))
  120. return ENOENT;
  121. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*cached_data);
  122. typed_cached_data.buffer = builder.build();
  123. if (!typed_cached_data.buffer)
  124. return ENOMEM;
  125. return KSuccess;
  126. }
  127. KResultOr<size_t> ProcFSExposedLink::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription*) const
  128. {
  129. VERIFY(offset == 0);
  130. MutexLocker locker(m_lock);
  131. KBufferBuilder builder;
  132. if (!const_cast<ProcFSExposedLink&>(*this).acquire_link(builder))
  133. return KResult(EFAULT);
  134. auto blob = builder.build();
  135. if (!blob)
  136. return KResult(EFAULT);
  137. ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
  138. if (!buffer.write(blob->data() + offset, nread))
  139. return KResult(EFAULT);
  140. return nread;
  141. }
  142. KResultOr<NonnullRefPtr<Inode>> ProcFSExposedLink::to_inode(const ProcFS& procfs_instance) const
  143. {
  144. auto maybe_inode = ProcFSLinkInode::try_create(procfs_instance, *this);
  145. if (maybe_inode.is_error())
  146. return maybe_inode.error();
  147. return maybe_inode.release_value();
  148. }
  149. KResultOr<NonnullRefPtr<Inode>> ProcFSExposedComponent::to_inode(const ProcFS& procfs_instance) const
  150. {
  151. auto maybe_inode = ProcFSGlobalInode::try_create(procfs_instance, *this);
  152. if (maybe_inode.is_error())
  153. return maybe_inode.error();
  154. return maybe_inode.release_value();
  155. }
  156. KResultOr<NonnullRefPtr<Inode>> ProcFSExposedDirectory::to_inode(const ProcFS& procfs_instance) const
  157. {
  158. auto maybe_inode = ProcFSDirectoryInode::try_create(procfs_instance, *this);
  159. if (maybe_inode.is_error())
  160. return maybe_inode.error();
  161. return maybe_inode.release_value();
  162. }
  163. void ProcFSExposedDirectory::add_component(const ProcFSExposedComponent&)
  164. {
  165. TODO();
  166. }
  167. KResultOr<NonnullRefPtr<ProcFSExposedComponent>> ProcFSExposedDirectory::lookup(StringView name)
  168. {
  169. for (auto& component : m_components) {
  170. if (component.name() == name) {
  171. return component;
  172. }
  173. }
  174. return ENOENT;
  175. }
  176. KResult ProcFSExposedDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  177. {
  178. MutexLocker locker(ProcFSComponentRegistry::the().get_lock());
  179. auto parent_directory = m_parent_directory.strong_ref();
  180. if (parent_directory.is_null())
  181. return KResult(EINVAL);
  182. callback({ ".", { fsid, component_index() }, DT_DIR });
  183. callback({ "..", { fsid, parent_directory->component_index() }, DT_DIR });
  184. for (auto& component : m_components) {
  185. InodeIdentifier identifier = { fsid, component.component_index() };
  186. callback({ component.name(), identifier, 0 });
  187. }
  188. return KSuccess;
  189. }
  190. }