ProcessSpecificExposed.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. auto array = TRY(JsonArraySerializer<>::try_create(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. TRY(array.add(address));
  35. }
  36. TRY(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. auto maybe_needle = name.to_uint();
  56. if (!maybe_needle.has_value())
  57. return ENOENT;
  58. auto needle = maybe_needle.release_value();
  59. ErrorOr<NonnullRefPtr<ProcFSProcessPropertyInode>> thread_stack_inode { ENOENT };
  60. for_each_thread([&](const Thread& thread) {
  61. int tid = thread.tid().value();
  62. VERIFY(!(tid < 0));
  63. if (needle == (unsigned)tid) {
  64. thread_stack_inode = ProcFSProcessPropertyInode::try_create_for_thread_stack(procfs, thread.tid(), pid());
  65. return IterationDecision::Break;
  66. }
  67. return IterationDecision::Continue;
  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. auto obj = TRY(JsonObjectSerializer<>::try_create(builder));
  113. #define __ENUMERATE_PLEDGE_PROMISE(x) \
  114. if (has_promised(Pledge::x)) { \
  115. if (!promises_builder.is_empty()) \
  116. TRY(promises_builder.try_append(' ')); \
  117. TRY(promises_builder.try_append(#x)); \
  118. }
  119. if (has_promises()) {
  120. StringBuilder promises_builder;
  121. ENUMERATE_PLEDGE_PROMISES
  122. TRY(obj.add("promises", promises_builder.string_view()));
  123. }
  124. #undef __ENUMERATE_PLEDGE_PROMISE
  125. TRY(obj.finish());
  126. return {};
  127. }
  128. ErrorOr<void> Process::procfs_get_unveil_stats(KBufferBuilder& builder) const
  129. {
  130. auto array = TRY(JsonArraySerializer<>::try_create(builder));
  131. TRY(m_unveil_data.with([&](auto& unveil_data) -> ErrorOr<void> {
  132. TRY(unveil_data.paths.for_each_node_in_tree_order([&](auto const& unveiled_path) -> ErrorOr<IterationDecision> {
  133. if (!unveiled_path.was_explicitly_unveiled())
  134. return IterationDecision::Continue;
  135. auto obj = TRY(array.add_object());
  136. TRY(obj.add("path", unveiled_path.path()));
  137. StringBuilder permissions_builder;
  138. if (unveiled_path.permissions() & UnveilAccess::Read)
  139. permissions_builder.append('r');
  140. if (unveiled_path.permissions() & UnveilAccess::Write)
  141. permissions_builder.append('w');
  142. if (unveiled_path.permissions() & UnveilAccess::Execute)
  143. permissions_builder.append('x');
  144. if (unveiled_path.permissions() & UnveilAccess::CreateOrRemove)
  145. permissions_builder.append('c');
  146. if (unveiled_path.permissions() & UnveilAccess::Browse)
  147. permissions_builder.append('b');
  148. TRY(obj.add("permissions", permissions_builder.string_view()));
  149. TRY(obj.finish());
  150. return IterationDecision::Continue;
  151. }));
  152. return {};
  153. }));
  154. TRY(array.finish());
  155. return {};
  156. }
  157. ErrorOr<void> Process::procfs_get_perf_events(KBufferBuilder& builder) const
  158. {
  159. InterruptDisabler disabler;
  160. if (!perf_events()) {
  161. dbgln("ProcFS: No perf events for {}", pid());
  162. return Error::from_errno(ENOBUFS);
  163. }
  164. return perf_events()->to_json(builder);
  165. }
  166. ErrorOr<void> Process::procfs_get_fds_stats(KBufferBuilder& builder) const
  167. {
  168. auto array = TRY(JsonArraySerializer<>::try_create(builder));
  169. return fds().with_shared([&](auto& fds) -> ErrorOr<void> {
  170. if (fds.open_count() == 0) {
  171. TRY(array.finish());
  172. return {};
  173. }
  174. size_t count = 0;
  175. TRY(fds.try_enumerate([&](auto& file_description_metadata) -> ErrorOr<void> {
  176. if (!file_description_metadata.is_valid()) {
  177. count++;
  178. return {};
  179. }
  180. bool cloexec = file_description_metadata.flags() & FD_CLOEXEC;
  181. RefPtr<OpenFileDescription> description = file_description_metadata.description();
  182. auto description_object = TRY(array.add_object());
  183. TRY(description_object.add("fd", count));
  184. // TODO: Better OOM handling.
  185. auto pseudo_path_or_error = description->pseudo_path();
  186. TRY(description_object.add("absolute_path", pseudo_path_or_error.is_error() ? "???"sv : pseudo_path_or_error.value()->view()));
  187. TRY(description_object.add("seekable", description->file().is_seekable()));
  188. TRY(description_object.add("class", description->file().class_name()));
  189. TRY(description_object.add("offset", description->offset()));
  190. TRY(description_object.add("cloexec", cloexec));
  191. TRY(description_object.add("blocking", description->is_blocking()));
  192. TRY(description_object.add("can_read", description->can_read()));
  193. TRY(description_object.add("can_write", description->can_write()));
  194. Inode* inode = description->inode();
  195. if (inode != nullptr) {
  196. auto inode_object = TRY(description_object.add_object("inode"));
  197. TRY(inode_object.add("fsid", inode->fsid().value()));
  198. TRY(inode_object.add("index", inode->index().value()));
  199. TRY(inode_object.finish());
  200. }
  201. TRY(description_object.finish());
  202. count++;
  203. return {};
  204. }));
  205. TRY(array.finish());
  206. return {};
  207. });
  208. }
  209. ErrorOr<void> Process::procfs_get_virtual_memory_stats(KBufferBuilder& builder) const
  210. {
  211. auto array = TRY(JsonArraySerializer<>::try_create(builder));
  212. {
  213. SpinlockLocker lock(address_space().get_lock());
  214. for (auto const& region : address_space().regions()) {
  215. if (!region->is_user() && !Process::current().is_superuser())
  216. continue;
  217. auto region_object = TRY(array.add_object());
  218. TRY(region_object.add("readable", region->is_readable()));
  219. TRY(region_object.add("writable", region->is_writable()));
  220. TRY(region_object.add("executable", region->is_executable()));
  221. TRY(region_object.add("stack", region->is_stack()));
  222. TRY(region_object.add("shared", region->is_shared()));
  223. TRY(region_object.add("syscall", region->is_syscall_region()));
  224. TRY(region_object.add("purgeable", region->vmobject().is_anonymous()));
  225. if (region->vmobject().is_anonymous()) {
  226. TRY(region_object.add("volatile", static_cast<Memory::AnonymousVMObject const&>(region->vmobject()).is_volatile()));
  227. }
  228. TRY(region_object.add("cacheable", region->is_cacheable()));
  229. TRY(region_object.add("address", region->vaddr().get()));
  230. TRY(region_object.add("size", region->size()));
  231. TRY(region_object.add("amount_resident", region->amount_resident()));
  232. TRY(region_object.add("amount_dirty", region->amount_dirty()));
  233. TRY(region_object.add("cow_pages", region->cow_pages()));
  234. TRY(region_object.add("name", region->name()));
  235. TRY(region_object.add("vmobject", region->vmobject().class_name()));
  236. StringBuilder pagemap_builder;
  237. for (size_t i = 0; i < region->page_count(); ++i) {
  238. auto const* page = region->physical_page(i);
  239. if (!page)
  240. pagemap_builder.append('N');
  241. else if (page->is_shared_zero_page() || page->is_lazy_committed_page())
  242. pagemap_builder.append('Z');
  243. else
  244. pagemap_builder.append('P');
  245. }
  246. TRY(region_object.add("pagemap", pagemap_builder.string_view()));
  247. TRY(region_object.finish());
  248. }
  249. }
  250. TRY(array.finish());
  251. return {};
  252. }
  253. ErrorOr<void> Process::procfs_get_current_work_directory_link(KBufferBuilder& builder) const
  254. {
  255. return builder.append(TRY(const_cast<Process&>(*this).current_directory()->try_serialize_absolute_path())->view());
  256. }
  257. mode_t Process::binary_link_required_mode() const
  258. {
  259. if (!executable())
  260. return 0;
  261. return m_procfs_traits->required_mode();
  262. }
  263. ErrorOr<void> Process::procfs_get_binary_link(KBufferBuilder& builder) const
  264. {
  265. auto const* custody = executable();
  266. if (!custody)
  267. return Error::from_errno(ENOEXEC);
  268. return builder.append(TRY(custody->try_serialize_absolute_path())->view());
  269. }
  270. ErrorOr<void> Process::procfs_get_tty_link(KBufferBuilder& builder) const
  271. {
  272. if (m_tty.is_null())
  273. return Error::from_errno(ENOENT);
  274. return builder.append(m_tty->tty_name().view());
  275. }
  276. }