ProcessSpecificExposed.cpp 11 KB

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