ProcessSpecificExposed.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/JsonArraySerializer.h>
  7. #include <AK/JsonObjectSerializer.h>
  8. #include <AK/JsonValue.h>
  9. #include <Kernel/Arch/x86/InterruptDisabler.h>
  10. #include <Kernel/FileSystem/Custody.h>
  11. #include <Kernel/FileSystem/ProcFS.h>
  12. #include <Kernel/KBufferBuilder.h>
  13. #include <Kernel/Memory/AnonymousVMObject.h>
  14. #include <Kernel/Memory/MemoryManager.h>
  15. #include <Kernel/Process.h>
  16. #include <Kernel/ProcessExposed.h>
  17. #include <Kernel/TTY/TTY.h>
  18. namespace Kernel {
  19. ErrorOr<size_t> Process::procfs_get_thread_stack(ThreadID thread_id, KBufferBuilder& builder) const
  20. {
  21. JsonArraySerializer array { builder };
  22. auto thread = Thread::from_tid(thread_id);
  23. if (!thread)
  24. return Error::from_errno(ESRCH);
  25. bool show_kernel_addresses = Process::current().is_superuser();
  26. bool kernel_address_added = false;
  27. for (auto address : Processor::capture_stack_trace(*thread, 1024)) {
  28. if (!show_kernel_addresses && !Memory::is_user_address(VirtualAddress { address })) {
  29. if (kernel_address_added)
  30. continue;
  31. address = 0xdeadc0de;
  32. kernel_address_added = true;
  33. }
  34. array.add(address);
  35. }
  36. array.finish();
  37. // FIXME: This return value seems useless.
  38. return 0;
  39. }
  40. ErrorOr<void> Process::traverse_stacks_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  41. {
  42. TRY(callback({ ".", { fsid, SegmentedProcFSIndex::build_segmented_index_for_main_property(pid(), SegmentedProcFSIndex::ProcessSubDirectory::Stacks, SegmentedProcFSIndex::MainProcessProperty::Reserved) }, 0 }));
  43. TRY(callback({ "..", { fsid, m_procfs_traits->component_index() }, 0 }));
  44. return thread_list().with([&](auto& list) -> ErrorOr<void> {
  45. for (auto const& thread : list) {
  46. int tid = thread.tid().value();
  47. InodeIdentifier identifier = { fsid, SegmentedProcFSIndex::build_segmented_index_for_thread_stack(pid(), thread.tid()) };
  48. auto name = TRY(KString::number(tid));
  49. TRY(callback({ name->view(), identifier, 0 }));
  50. }
  51. return {};
  52. });
  53. }
  54. ErrorOr<NonnullRefPtr<Inode>> Process::lookup_stacks_directory(const ProcFS& procfs, StringView name) const
  55. {
  56. ErrorOr<NonnullRefPtr<ProcFSProcessPropertyInode>> thread_stack_inode { ENOENT };
  57. // FIXME: Try to exit the loop earlier
  58. for_each_thread([&](const Thread& thread) {
  59. int tid = thread.tid().value();
  60. VERIFY(!(tid < 0));
  61. if (name.to_int() == tid) {
  62. auto maybe_inode = ProcFSProcessPropertyInode::try_create_for_thread_stack(procfs, thread.tid(), pid());
  63. if (maybe_inode.is_error()) {
  64. thread_stack_inode = maybe_inode.release_error();
  65. return;
  66. }
  67. thread_stack_inode = maybe_inode.release_value();
  68. }
  69. });
  70. if (thread_stack_inode.is_error())
  71. return thread_stack_inode.release_error();
  72. return thread_stack_inode.release_value();
  73. }
  74. ErrorOr<size_t> Process::procfs_get_file_description_link(unsigned fd, KBufferBuilder& builder) const
  75. {
  76. auto file_description = TRY(m_fds.open_file_description(fd));
  77. // Note: These links are not guaranteed to point to actual VFS paths, just like in other kernels.
  78. auto data = TRY(file_description->pseudo_path());
  79. TRY(builder.append(data->view()));
  80. return data->length();
  81. }
  82. ErrorOr<void> Process::traverse_file_descriptions_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  83. {
  84. TRY(callback({ ".", { fsid, m_procfs_traits->component_index() }, 0 }));
  85. TRY(callback({ "..", { fsid, m_procfs_traits->component_index() }, 0 }));
  86. size_t count = 0;
  87. fds().enumerate([&](auto& file_description_metadata) {
  88. if (!file_description_metadata.is_valid()) {
  89. count++;
  90. return;
  91. }
  92. StringBuilder builder;
  93. builder.appendff("{}", count);
  94. // FIXME: Propagate errors from callback.
  95. (void)callback({ builder.string_view(), { fsid, SegmentedProcFSIndex::build_segmented_index_for_file_description(pid(), count) }, DT_LNK });
  96. count++;
  97. });
  98. return {};
  99. }
  100. ErrorOr<NonnullRefPtr<Inode>> Process::lookup_file_descriptions_directory(const ProcFS& procfs, StringView name) const
  101. {
  102. auto maybe_index = name.to_uint();
  103. if (!maybe_index.has_value())
  104. return ENOENT;
  105. if (!fds().get_if_valid(*maybe_index))
  106. return ENOENT;
  107. return TRY(ProcFSProcessPropertyInode::try_create_for_file_description_link(procfs, *maybe_index, pid()));
  108. }
  109. ErrorOr<void> Process::procfs_get_pledge_stats(KBufferBuilder& builder) const
  110. {
  111. JsonObjectSerializer obj { builder };
  112. #define __ENUMERATE_PLEDGE_PROMISE(x) \
  113. if (has_promised(Pledge::x)) { \
  114. if (!builder.is_empty()) \
  115. builder.append(' '); \
  116. builder.append(#x); \
  117. }
  118. if (has_promises()) {
  119. StringBuilder builder;
  120. ENUMERATE_PLEDGE_PROMISES
  121. obj.add("promises", builder.build());
  122. }
  123. #undef __ENUMERATE_PLEDGE_PROMISE
  124. obj.finish();
  125. return {};
  126. }
  127. ErrorOr<void> Process::procfs_get_unveil_stats(KBufferBuilder& builder) const
  128. {
  129. JsonArraySerializer array { builder };
  130. for (auto const& unveiled_path : unveiled_paths()) {
  131. if (!unveiled_path.was_explicitly_unveiled())
  132. continue;
  133. auto obj = array.add_object();
  134. obj.add("path", unveiled_path.path());
  135. StringBuilder permissions_builder;
  136. if (unveiled_path.permissions() & UnveilAccess::Read)
  137. permissions_builder.append('r');
  138. if (unveiled_path.permissions() & UnveilAccess::Write)
  139. permissions_builder.append('w');
  140. if (unveiled_path.permissions() & UnveilAccess::Execute)
  141. permissions_builder.append('x');
  142. if (unveiled_path.permissions() & UnveilAccess::CreateOrRemove)
  143. permissions_builder.append('c');
  144. if (unveiled_path.permissions() & UnveilAccess::Browse)
  145. permissions_builder.append('b');
  146. obj.add("permissions", permissions_builder.string_view());
  147. }
  148. array.finish();
  149. return {};
  150. }
  151. ErrorOr<void> Process::procfs_get_perf_events(KBufferBuilder& builder) const
  152. {
  153. InterruptDisabler disabler;
  154. if (!perf_events()) {
  155. dbgln("ProcFS: No perf events for {}", pid());
  156. return Error::from_errno(ENOBUFS);
  157. }
  158. return perf_events()->to_json(builder);
  159. }
  160. ErrorOr<void> Process::procfs_get_fds_stats(KBufferBuilder& builder) const
  161. {
  162. JsonArraySerializer array { builder };
  163. if (fds().open_count() == 0) {
  164. array.finish();
  165. return {};
  166. }
  167. size_t count = 0;
  168. fds().enumerate([&](auto& file_description_metadata) {
  169. if (!file_description_metadata.is_valid()) {
  170. count++;
  171. return;
  172. }
  173. bool cloexec = file_description_metadata.flags() & FD_CLOEXEC;
  174. RefPtr<OpenFileDescription> description = file_description_metadata.description();
  175. auto description_object = array.add_object();
  176. description_object.add("fd", count);
  177. // TODO: Better OOM handling.
  178. auto pseudo_path_or_error = description->pseudo_path();
  179. description_object.add("absolute_path", pseudo_path_or_error.is_error() ? "???"sv : pseudo_path_or_error.value()->view());
  180. description_object.add("seekable", description->file().is_seekable());
  181. description_object.add("class", description->file().class_name());
  182. description_object.add("offset", description->offset());
  183. description_object.add("cloexec", cloexec);
  184. description_object.add("blocking", description->is_blocking());
  185. description_object.add("can_read", description->can_read());
  186. description_object.add("can_write", description->can_write());
  187. Inode* inode = description->inode();
  188. if (inode != nullptr) {
  189. auto inode_object = description_object.add_object("inode");
  190. inode_object.add("fsid", inode->fsid().value());
  191. inode_object.add("index", inode->index().value());
  192. inode_object.finish();
  193. }
  194. count++;
  195. });
  196. array.finish();
  197. return {};
  198. }
  199. ErrorOr<void> Process::procfs_get_virtual_memory_stats(KBufferBuilder& builder) const
  200. {
  201. JsonArraySerializer array { builder };
  202. {
  203. SpinlockLocker lock(address_space().get_lock());
  204. for (auto const& region : address_space().regions()) {
  205. if (!region->is_user() && !Process::current().is_superuser())
  206. continue;
  207. auto region_object = array.add_object();
  208. region_object.add("readable", region->is_readable());
  209. region_object.add("writable", region->is_writable());
  210. region_object.add("executable", region->is_executable());
  211. region_object.add("stack", region->is_stack());
  212. region_object.add("shared", region->is_shared());
  213. region_object.add("syscall", region->is_syscall_region());
  214. region_object.add("purgeable", region->vmobject().is_anonymous());
  215. if (region->vmobject().is_anonymous()) {
  216. region_object.add("volatile", static_cast<Memory::AnonymousVMObject const&>(region->vmobject()).is_volatile());
  217. }
  218. region_object.add("cacheable", region->is_cacheable());
  219. region_object.add("address", region->vaddr().get());
  220. region_object.add("size", region->size());
  221. region_object.add("amount_resident", region->amount_resident());
  222. region_object.add("amount_dirty", region->amount_dirty());
  223. region_object.add("cow_pages", region->cow_pages());
  224. region_object.add("name", region->name());
  225. region_object.add("vmobject", region->vmobject().class_name());
  226. StringBuilder pagemap_builder;
  227. for (size_t i = 0; i < region->page_count(); ++i) {
  228. auto const* page = region->physical_page(i);
  229. if (!page)
  230. pagemap_builder.append('N');
  231. else if (page->is_shared_zero_page() || page->is_lazy_committed_page())
  232. pagemap_builder.append('Z');
  233. else
  234. pagemap_builder.append('P');
  235. }
  236. region_object.add("pagemap", pagemap_builder.string_view());
  237. }
  238. }
  239. array.finish();
  240. return {};
  241. }
  242. ErrorOr<void> Process::procfs_get_current_work_directory_link(KBufferBuilder& builder) const
  243. {
  244. return builder.append_bytes(const_cast<Process&>(*this).current_directory().absolute_path().bytes());
  245. }
  246. mode_t Process::binary_link_required_mode() const
  247. {
  248. if (!executable())
  249. return 0;
  250. return m_procfs_traits->required_mode();
  251. }
  252. ErrorOr<void> Process::procfs_get_binary_link(KBufferBuilder& builder) const
  253. {
  254. auto const* custody = executable();
  255. if (!custody)
  256. return Error::from_errno(ENOEXEC);
  257. return builder.append(custody->absolute_path().bytes());
  258. }
  259. ErrorOr<void> Process::procfs_get_tty_link(KBufferBuilder& builder) const
  260. {
  261. if (m_tty.is_null())
  262. return Error::from_errno(ENOENT);
  263. return builder.append(m_tty->tty_name().view());
  264. }
  265. }