ProcessExposed.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 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::OpenFileDescriptions, 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. auto name_or_error = KString::try_create(name);
  74. if (name_or_error.is_error())
  75. TODO();
  76. m_name = name_or_error.release_value();
  77. }
  78. ProcFSExposedDirectory::ProcFSExposedDirectory(StringView name)
  79. : ProcFSExposedComponent(name)
  80. {
  81. }
  82. ProcFSExposedDirectory::ProcFSExposedDirectory(StringView name, const ProcFSExposedDirectory& parent_directory)
  83. : ProcFSExposedComponent(name)
  84. , m_parent_directory(parent_directory)
  85. {
  86. }
  87. ProcFSExposedLink::ProcFSExposedLink(StringView name)
  88. : ProcFSExposedComponent(name)
  89. {
  90. }
  91. ErrorOr<size_t> ProcFSGlobalInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
  92. {
  93. dbgln_if(PROCFS_DEBUG, "ProcFSGlobalInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count);
  94. VERIFY(offset >= 0);
  95. VERIFY(buffer.user_or_kernel_ptr());
  96. if (!description)
  97. return Error::from_errno(EIO);
  98. MutexLocker locker(m_refresh_lock);
  99. if (!description->data()) {
  100. dbgln("ProcFSGlobalInformation: Do not have cached data!");
  101. return Error::from_errno(EIO);
  102. }
  103. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*description->data());
  104. auto& data_buffer = typed_cached_data.buffer;
  105. if (!data_buffer || (size_t)offset >= data_buffer->size())
  106. return 0;
  107. ssize_t nread = min(static_cast<off_t>(data_buffer->size() - offset), static_cast<off_t>(count));
  108. TRY(buffer.write(data_buffer->data() + offset, nread));
  109. return nread;
  110. }
  111. ErrorOr<void> ProcFSGlobalInformation::refresh_data(OpenFileDescription& description) const
  112. {
  113. MutexLocker lock(m_refresh_lock);
  114. auto& cached_data = description.data();
  115. if (!cached_data) {
  116. cached_data = adopt_own_if_nonnull(new (nothrow) ProcFSInodeData);
  117. if (!cached_data)
  118. return ENOMEM;
  119. }
  120. auto builder = TRY(KBufferBuilder::try_create());
  121. TRY(const_cast<ProcFSGlobalInformation&>(*this).try_generate(builder));
  122. auto& typed_cached_data = static_cast<ProcFSInodeData&>(*cached_data);
  123. typed_cached_data.buffer = builder.build();
  124. if (!typed_cached_data.buffer)
  125. return ENOMEM;
  126. return {};
  127. }
  128. ErrorOr<void> ProcFSSystemBoolean::try_generate(KBufferBuilder& builder)
  129. {
  130. return builder.appendff("{}\n", static_cast<int>(value()));
  131. }
  132. ErrorOr<size_t> ProcFSSystemBoolean::write_bytes(off_t, size_t count, const UserOrKernelBuffer& buffer, OpenFileDescription*)
  133. {
  134. if (count != 1)
  135. return EINVAL;
  136. MutexLocker locker(m_refresh_lock);
  137. char value = 0;
  138. TRY(buffer.read(&value, 1));
  139. if (value == '0')
  140. set_value(false);
  141. else if (value == '1')
  142. set_value(true);
  143. else
  144. return EINVAL;
  145. return 1;
  146. }
  147. ErrorOr<void> ProcFSSystemBoolean::truncate(u64 size)
  148. {
  149. if (size != 0)
  150. return EPERM;
  151. return {};
  152. }
  153. ErrorOr<void> ProcFSSystemBoolean::set_mtime(time_t)
  154. {
  155. return {};
  156. }
  157. ErrorOr<size_t> ProcFSExposedLink::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
  158. {
  159. VERIFY(offset == 0);
  160. MutexLocker locker(m_lock);
  161. auto builder = TRY(KBufferBuilder::try_create());
  162. if (!const_cast<ProcFSExposedLink&>(*this).acquire_link(builder))
  163. return Error::from_errno(EFAULT);
  164. auto blob = builder.build();
  165. if (!blob)
  166. return Error::from_errno(EFAULT);
  167. ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
  168. TRY(buffer.write(blob->data() + offset, nread));
  169. return nread;
  170. }
  171. ErrorOr<NonnullRefPtr<Inode>> ProcFSExposedLink::to_inode(const ProcFS& procfs_instance) const
  172. {
  173. return TRY(ProcFSLinkInode::try_create(procfs_instance, *this));
  174. }
  175. ErrorOr<NonnullRefPtr<Inode>> ProcFSExposedComponent::to_inode(const ProcFS& procfs_instance) const
  176. {
  177. return TRY(ProcFSGlobalInode::try_create(procfs_instance, *this));
  178. }
  179. ErrorOr<NonnullRefPtr<Inode>> ProcFSExposedDirectory::to_inode(const ProcFS& procfs_instance) const
  180. {
  181. return TRY(ProcFSDirectoryInode::try_create(procfs_instance, *this));
  182. }
  183. void ProcFSExposedDirectory::add_component(const ProcFSExposedComponent&)
  184. {
  185. TODO();
  186. }
  187. ErrorOr<NonnullRefPtr<ProcFSExposedComponent>> ProcFSExposedDirectory::lookup(StringView name)
  188. {
  189. for (auto& component : m_components) {
  190. if (component.name() == name) {
  191. return component;
  192. }
  193. }
  194. return ENOENT;
  195. }
  196. ErrorOr<void> ProcFSExposedDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  197. {
  198. MutexLocker locker(ProcFSComponentRegistry::the().get_lock());
  199. auto parent_directory = m_parent_directory.strong_ref();
  200. if (parent_directory.is_null())
  201. return Error::from_errno(EINVAL);
  202. TRY(callback({ ".", { fsid, component_index() }, DT_DIR }));
  203. TRY(callback({ "..", { fsid, parent_directory->component_index() }, DT_DIR }));
  204. for (auto const& component : m_components) {
  205. InodeIdentifier identifier = { fsid, component.component_index() };
  206. TRY(callback({ component.name(), identifier, 0 }));
  207. }
  208. return {};
  209. }
  210. }