ProcessExposed.cpp 8.4 KB

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