ProcessSpecificExposed.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. namespace Kernel {
  18. KResultOr<size_t> Process::procfs_get_thread_stack(ThreadID thread_id, KBufferBuilder& builder) const
  19. {
  20. JsonArraySerializer array { builder };
  21. auto thread = Thread::from_tid(thread_id);
  22. if (!thread)
  23. return KResult(ESRCH);
  24. bool show_kernel_addresses = Process::current().is_superuser();
  25. bool kernel_address_added = false;
  26. for (auto address : Processor::capture_stack_trace(*thread, 1024)) {
  27. if (!show_kernel_addresses && !Memory::is_user_address(VirtualAddress { address })) {
  28. if (kernel_address_added)
  29. continue;
  30. address = 0xdeadc0de;
  31. kernel_address_added = true;
  32. }
  33. array.add(address);
  34. }
  35. array.finish();
  36. return KSuccess;
  37. }
  38. KResult Process::traverse_stacks_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  39. {
  40. callback({ ".", { fsid, SegmentedProcFSIndex::build_segmented_index_for_main_property(pid(), SegmentedProcFSIndex::ProcessSubDirectory::Stacks, SegmentedProcFSIndex::MainProcessProperty::Reserved) }, 0 });
  41. callback({ "..", { fsid, m_procfs_traits->component_index() }, 0 });
  42. for_each_thread([&](const Thread& thread) {
  43. int tid = thread.tid().value();
  44. InodeIdentifier identifier = { fsid, SegmentedProcFSIndex::build_segmented_index_for_thread_stack(pid(), thread.tid()) };
  45. callback({ String::number(tid), identifier, 0 });
  46. });
  47. return KSuccess;
  48. }
  49. KResultOr<NonnullRefPtr<Inode>> Process::lookup_stacks_directory(const ProcFS& procfs, StringView name) const
  50. {
  51. KResultOr<NonnullRefPtr<ProcFSProcessPropertyInode>> thread_stack_inode { ENOENT };
  52. // FIXME: Try to exit the loop earlier
  53. for_each_thread([&](const Thread& thread) {
  54. int tid = thread.tid().value();
  55. VERIFY(!(tid < 0));
  56. if (name.to_int() == tid) {
  57. auto maybe_inode = ProcFSProcessPropertyInode::try_create_for_thread_stack(procfs, thread.tid(), pid());
  58. if (maybe_inode.is_error()) {
  59. thread_stack_inode = maybe_inode.error();
  60. return;
  61. }
  62. thread_stack_inode = maybe_inode.release_value();
  63. }
  64. });
  65. if (thread_stack_inode.is_error())
  66. return thread_stack_inode.error();
  67. return thread_stack_inode.release_value();
  68. }
  69. KResultOr<size_t> Process::procfs_get_file_description_link(unsigned fd, KBufferBuilder& builder) const
  70. {
  71. auto file_description = TRY(m_fds.file_description(fd));
  72. auto data = file_description->absolute_path();
  73. builder.append(data);
  74. return data.length();
  75. }
  76. KResult Process::traverse_file_descriptions_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  77. {
  78. callback({ ".", { fsid, m_procfs_traits->component_index() }, 0 });
  79. callback({ "..", { fsid, m_procfs_traits->component_index() }, 0 });
  80. size_t count = 0;
  81. fds().enumerate([&](auto& file_description_metadata) {
  82. if (!file_description_metadata.is_valid()) {
  83. count++;
  84. return;
  85. }
  86. StringBuilder builder;
  87. builder.appendff("{}", count);
  88. callback({ builder.string_view(), { fsid, SegmentedProcFSIndex::build_segmented_index_for_file_description(pid(), count) }, DT_LNK });
  89. count++;
  90. });
  91. return KSuccess;
  92. }
  93. KResultOr<NonnullRefPtr<Inode>> Process::lookup_file_descriptions_directory(const ProcFS& procfs, StringView name) const
  94. {
  95. auto maybe_index = name.to_uint();
  96. if (!maybe_index.has_value())
  97. return ENOENT;
  98. if (!fds().get_if_valid(*maybe_index))
  99. return ENOENT;
  100. return TRY(ProcFSProcessPropertyInode::try_create_for_file_description_link(procfs, *maybe_index, pid()));
  101. }
  102. KResult Process::procfs_get_pledge_stats(KBufferBuilder& builder) const
  103. {
  104. JsonObjectSerializer obj { builder };
  105. #define __ENUMERATE_PLEDGE_PROMISE(x) \
  106. if (has_promised(Pledge::x)) { \
  107. if (!builder.is_empty()) \
  108. builder.append(' '); \
  109. builder.append(#x); \
  110. }
  111. if (has_promises()) {
  112. StringBuilder builder;
  113. ENUMERATE_PLEDGE_PROMISES
  114. obj.add("promises", builder.build());
  115. }
  116. #undef __ENUMERATE_PLEDGE_PROMISE
  117. obj.finish();
  118. return KSuccess;
  119. }
  120. KResult Process::procfs_get_unveil_stats(KBufferBuilder& builder) const
  121. {
  122. JsonArraySerializer array { builder };
  123. for (auto& unveiled_path : unveiled_paths()) {
  124. if (!unveiled_path.was_explicitly_unveiled())
  125. continue;
  126. auto obj = array.add_object();
  127. obj.add("path", unveiled_path.path());
  128. StringBuilder permissions_builder;
  129. if (unveiled_path.permissions() & UnveilAccess::Read)
  130. permissions_builder.append('r');
  131. if (unveiled_path.permissions() & UnveilAccess::Write)
  132. permissions_builder.append('w');
  133. if (unveiled_path.permissions() & UnveilAccess::Execute)
  134. permissions_builder.append('x');
  135. if (unveiled_path.permissions() & UnveilAccess::CreateOrRemove)
  136. permissions_builder.append('c');
  137. if (unveiled_path.permissions() & UnveilAccess::Browse)
  138. permissions_builder.append('b');
  139. obj.add("permissions", permissions_builder.to_string());
  140. }
  141. array.finish();
  142. return KSuccess;
  143. }
  144. KResult Process::procfs_get_perf_events(KBufferBuilder& builder) const
  145. {
  146. InterruptDisabler disabler;
  147. if (!const_cast<Process&>(*this).perf_events()) {
  148. dbgln("ProcFS: No perf events for {}", pid());
  149. return KResult(ENOBUFS);
  150. }
  151. return const_cast<Process&>(*this).perf_events()->to_json(builder) ? KSuccess : KResult(EINVAL);
  152. }
  153. KResult Process::procfs_get_fds_stats(KBufferBuilder& builder) const
  154. {
  155. JsonArraySerializer array { builder };
  156. if (fds().open_count() == 0) {
  157. array.finish();
  158. return KSuccess;
  159. }
  160. size_t count = 0;
  161. fds().enumerate([&](auto& file_description_metadata) {
  162. if (!file_description_metadata.is_valid()) {
  163. count++;
  164. return;
  165. }
  166. bool cloexec = file_description_metadata.flags() & FD_CLOEXEC;
  167. RefPtr<FileDescription> description = file_description_metadata.description();
  168. auto description_object = array.add_object();
  169. description_object.add("fd", count);
  170. description_object.add("absolute_path", description->absolute_path());
  171. description_object.add("seekable", description->file().is_seekable());
  172. description_object.add("class", description->file().class_name());
  173. description_object.add("offset", description->offset());
  174. description_object.add("cloexec", cloexec);
  175. description_object.add("blocking", description->is_blocking());
  176. description_object.add("can_read", description->can_read());
  177. description_object.add("can_write", description->can_write());
  178. count++;
  179. });
  180. array.finish();
  181. return KSuccess;
  182. }
  183. KResult Process::procfs_get_virtual_memory_stats(KBufferBuilder& builder) const
  184. {
  185. JsonArraySerializer array { builder };
  186. {
  187. SpinlockLocker lock(address_space().get_lock());
  188. for (auto& region : address_space().regions()) {
  189. if (!region->is_user() && !Process::current().is_superuser())
  190. continue;
  191. auto region_object = array.add_object();
  192. region_object.add("readable", region->is_readable());
  193. region_object.add("writable", region->is_writable());
  194. region_object.add("executable", region->is_executable());
  195. region_object.add("stack", region->is_stack());
  196. region_object.add("shared", region->is_shared());
  197. region_object.add("syscall", region->is_syscall_region());
  198. region_object.add("purgeable", region->vmobject().is_anonymous());
  199. if (region->vmobject().is_anonymous()) {
  200. region_object.add("volatile", static_cast<Memory::AnonymousVMObject const&>(region->vmobject()).is_volatile());
  201. }
  202. region_object.add("cacheable", region->is_cacheable());
  203. region_object.add("address", region->vaddr().get());
  204. region_object.add("size", region->size());
  205. region_object.add("amount_resident", region->amount_resident());
  206. region_object.add("amount_dirty", region->amount_dirty());
  207. region_object.add("cow_pages", region->cow_pages());
  208. region_object.add("name", region->name());
  209. region_object.add("vmobject", region->vmobject().class_name());
  210. StringBuilder pagemap_builder;
  211. for (size_t i = 0; i < region->page_count(); ++i) {
  212. auto* page = region->physical_page(i);
  213. if (!page)
  214. pagemap_builder.append('N');
  215. else if (page->is_shared_zero_page() || page->is_lazy_committed_page())
  216. pagemap_builder.append('Z');
  217. else
  218. pagemap_builder.append('P');
  219. }
  220. region_object.add("pagemap", pagemap_builder.to_string());
  221. }
  222. }
  223. array.finish();
  224. return KSuccess;
  225. }
  226. KResult Process::procfs_get_current_work_directory_link(KBufferBuilder& builder) const
  227. {
  228. builder.append_bytes(const_cast<Process&>(*this).current_directory().absolute_path().bytes());
  229. return KSuccess;
  230. }
  231. mode_t Process::binary_link_required_mode() const
  232. {
  233. if (!executable())
  234. return 0;
  235. return m_procfs_traits->required_mode();
  236. }
  237. KResult Process::procfs_get_binary_link(KBufferBuilder& builder) const
  238. {
  239. auto* custody = executable();
  240. if (!custody)
  241. return KResult(ENOEXEC);
  242. builder.append(custody->absolute_path().bytes());
  243. return KSuccess;
  244. }
  245. }